From 9557d69f711e659aa1c1b023dc1f711ddc3f0796 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Tue, 20 May 2014 12:30:28 +1000 Subject: [PATCH 01/32] Showing admin for enterprise or admin user --- app/helpers/shared_helper.rb | 6 +++++- app/views/shared/menu/_large_menu.html.haml | 2 +- app/views/shared/menu/_mobile_menu.html.haml | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/helpers/shared_helper.rb b/app/helpers/shared_helper.rb index f255999009..be1e0ba0f4 100644 --- a/app/helpers/shared_helper.rb +++ b/app/helpers/shared_helper.rb @@ -20,6 +20,10 @@ module SharedHelper end def enterprise_user? - spree_current_user.andand.enterprises.count > 0 + spree_current_user.andand.enterprises.andand.count.to_i > 0 + end + + def admin_user? + spree_current_user.andand.has_spree_role? 'admin' end end diff --git a/app/views/shared/menu/_large_menu.html.haml b/app/views/shared/menu/_large_menu.html.haml index 328d46ca85..9443dfa979 100644 --- a/app/views/shared/menu/_large_menu.html.haml +++ b/app/views/shared/menu/_large_menu.html.haml @@ -24,7 +24,7 @@ %a{href: ""} %span.nav-primary Groups %li.divider - - if spree_current_user.andand.has_spree_role? 'admin' + - if admin_user? or enterprise_user? %li %a{href: spree.admin_path} %span.nav-primary Admin diff --git a/app/views/shared/menu/_mobile_menu.html.haml b/app/views/shared/menu/_mobile_menu.html.haml index a33cb85c11..7d37d9b2cb 100644 --- a/app/views/shared/menu/_mobile_menu.html.haml +++ b/app/views/shared/menu/_mobile_menu.html.haml @@ -15,7 +15,7 @@ %ul.off-canvas-list %li= link_to image_tag("ofn_logo_small.png"), root_path - - if spree_current_user.andand.has_spree_role? 'admin' + - if admin_user? or enterprise_user? %li %a{href: spree.admin_path} %span.nav-primary Admin From 3eb6f98c70b7dfa988f66bacf861a1114daccfab Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Tue, 20 May 2014 12:39:22 +1000 Subject: [PATCH 02/32] Putting error messages on checkout into error flash for easier debugging --- app/controllers/checkout_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 5cf940c223..2bedcf5b6b 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -26,7 +26,7 @@ class CheckoutController < Spree::CheckoutController if @order.next state_callback(:after) else - flash[:error] = t(:payment_processing_failed) + flash[:error] = @order.errors.full_messages.to_sentence update_failed return end From 4e463e70874ae8244e491392bba0c4491ef447aa Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Tue, 20 May 2014 12:30:28 +1000 Subject: [PATCH 03/32] Showing admin for enterprise or admin user --- app/helpers/shared_helper.rb | 6 +++++- app/views/shared/menu/_large_menu.html.haml | 2 +- app/views/shared/menu/_mobile_menu.html.haml | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/helpers/shared_helper.rb b/app/helpers/shared_helper.rb index f255999009..be1e0ba0f4 100644 --- a/app/helpers/shared_helper.rb +++ b/app/helpers/shared_helper.rb @@ -20,6 +20,10 @@ module SharedHelper end def enterprise_user? - spree_current_user.andand.enterprises.count > 0 + spree_current_user.andand.enterprises.andand.count.to_i > 0 + end + + def admin_user? + spree_current_user.andand.has_spree_role? 'admin' end end diff --git a/app/views/shared/menu/_large_menu.html.haml b/app/views/shared/menu/_large_menu.html.haml index 6f9bae4488..7a72b80f60 100644 --- a/app/views/shared/menu/_large_menu.html.haml +++ b/app/views/shared/menu/_large_menu.html.haml @@ -24,7 +24,7 @@ %a{href: main_app.groups_path} %span.nav-primary Groups %li.divider - - if spree_current_user.andand.has_spree_role? 'admin' + - if admin_user? or enterprise_user? %li %a{href: spree.admin_path} %span.nav-primary Admin diff --git a/app/views/shared/menu/_mobile_menu.html.haml b/app/views/shared/menu/_mobile_menu.html.haml index 5478436eb8..5e9f22c597 100644 --- a/app/views/shared/menu/_mobile_menu.html.haml +++ b/app/views/shared/menu/_mobile_menu.html.haml @@ -15,7 +15,7 @@ %ul.off-canvas-list %li= link_to image_tag("ofn_logo_small.png"), root_path - - if spree_current_user.andand.has_spree_role? 'admin' + - if admin_user? or enterprise_user? %li %a{href: spree.admin_path} %span.nav-primary Admin From 2fc45d635d4e9d0a616fd27f7c5954c365d69844 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Tue, 20 May 2014 12:39:22 +1000 Subject: [PATCH 04/32] Putting error messages on checkout into error flash for easier debugging --- app/controllers/checkout_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 5cf940c223..2bedcf5b6b 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -26,7 +26,7 @@ class CheckoutController < Spree::CheckoutController if @order.next state_callback(:after) else - flash[:error] = t(:payment_processing_failed) + flash[:error] = @order.errors.full_messages.to_sentence update_failed return end From df82627bd1058289dffc7b331e075f0f0e558b2d Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Tue, 20 May 2014 15:47:00 +1000 Subject: [PATCH 05/32] Starting to rework login behaviour when hitting /admin --- app/controllers/checkout_controller.rb | 6 +++++- .../spree/admin/base_controller_decorator.rb | 2 +- .../spree/admin/overview_controller_decorator.rb | 15 ++++++++++++++- config/routes.rb | 2 ++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 2bedcf5b6b..3879f16741 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -26,7 +26,11 @@ class CheckoutController < Spree::CheckoutController if @order.next state_callback(:after) else - flash[:error] = @order.errors.full_messages.to_sentence + unless @order.errors.empty? + flash[:error] = @order.errors.full_messages.to_sentence + else + flash[:error] = t(:payment_processing_failed) + end update_failed return end diff --git a/app/controllers/spree/admin/base_controller_decorator.rb b/app/controllers/spree/admin/base_controller_decorator.rb index 8e876513fa..df7076345f 100644 --- a/app/controllers/spree/admin/base_controller_decorator.rb +++ b/app/controllers/spree/admin/base_controller_decorator.rb @@ -11,4 +11,4 @@ Spree::Admin::BaseController.class_eval do authorize! :admin, record authorize! action, record end -end \ No newline at end of file +end diff --git a/app/controllers/spree/admin/overview_controller_decorator.rb b/app/controllers/spree/admin/overview_controller_decorator.rb index dd53294f6f..5c96901c1d 100644 --- a/app/controllers/spree/admin/overview_controller_decorator.rb +++ b/app/controllers/spree/admin/overview_controller_decorator.rb @@ -4,4 +4,17 @@ Spree::Admin::OverviewController.class_eval do @product_count = Spree::Product.active.managed_by(spree_current_user).count @order_cycle_count = OrderCycle.active.managed_by(spree_current_user).count end -end \ No newline at end of file + + # This is in Spree::Core::ControllerHelpers::Auth + # But you can't easily reopen modules in Ruby + def unauthorized + if try_spree_current_user + flash[:error] = t(:authorization_failure) + redirect_to '/unauthorized' + else + store_location + url = respond_to?(:spree_login_path) ? spree_login_path : root_path + redirect_to url + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 69a3c99d56..310d1d9e4d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,8 @@ Openfoodnetwork::Application.routes.draw do root :to => 'home#index' + get "/#/login", to: "home#index", as: :spree_login + resource :shop, controller: "shop" do get :products post :order_cycle From 391b1da04c09aca950196c6bed0c166411eb35aa Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 21 May 2014 10:46:03 +1000 Subject: [PATCH 06/32] adding session token to order test --- app/controllers/spree/orders_controller_decorator.rb | 1 + spec/controllers/spree/orders_controller_spec.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index ce20dfd58d..959e51204f 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -28,6 +28,7 @@ Spree::OrdersController.class_eval do end populator = Spree::OrderPopulator.new(current_order(true), current_currency) if populator.populate(params.slice(:products, :variants, :quantity)) + fire_event('spree.cart.add') fire_event('spree.order.contents_changed') respond_with(@order) do |format| diff --git a/spec/controllers/spree/orders_controller_spec.rb b/spec/controllers/spree/orders_controller_spec.rb index 53c7be68f0..e6c9b91894 100644 --- a/spec/controllers/spree/orders_controller_spec.rb +++ b/spec/controllers/spree/orders_controller_spec.rb @@ -15,6 +15,7 @@ describe Spree::OrdersController do controller.stub(:current_order_cycle).and_return(order_cycle) controller.stub(:current_order).and_return order order.stub_chain(:line_items, :empty?).and_return true + session[:access_token] = order.token spree_get :edit response.should redirect_to shop_path end From 4f1b8d06e71e1bcb654f5436a948750884a9efcd Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 12:19:07 +1000 Subject: [PATCH 07/32] Construct enterprise relationships with a factory --- spec/factories.rb | 3 +++ spec/models/enterprise_spec.rb | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 13c0bd0e8e..b2ed52b7a7 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -96,6 +96,9 @@ FactoryGirl.define do is_distributor true end + factory :enterprise_relationship do + end + factory :enterprise_group, :class => EnterpriseGroup do name 'Enterprise group' description 'this is a group' diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 06c4c0850e..f375b8890a 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -31,10 +31,10 @@ describe Enterprise do let(:e) { create(:distributor_enterprise) } let(:p) { create(:supplier_enterprise) } let(:c) { create(:distributor_enterprise) } - before do - EnterpriseRelationship.create! parent_id: p.id, child_id: e.id - EnterpriseRelationship.create! parent_id: e.id, child_id: c.id - end + + let!(:er1) { create(:enterprise_relationship, parent_id: p.id, child_id: e.id) } + let!(:er2) { create(:enterprise_relationship, parent_id: e.id, child_id: c.id) } + it "finds relatives" do e.relatives.sort.should == [p, c].sort end From aa53d8238655d544c6557fe12d28d23c05781d36 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 12:19:48 +1000 Subject: [PATCH 08/32] Add stub page for managing enterprise relationships --- .../admin/enterprise_relationships_controller.rb | 7 +++++++ app/views/admin/enterprise_relationships/index.html.haml | 8 ++++++++ app/views/admin/enterprises/index.html.erb | 2 ++ app/views/admin/shared/_enterprises_sub_menu.html.haml | 4 ++++ config/routes.rb | 2 ++ 5 files changed, 23 insertions(+) create mode 100644 app/controllers/admin/enterprise_relationships_controller.rb create mode 100644 app/views/admin/enterprise_relationships/index.html.haml create mode 100644 app/views/admin/shared/_enterprises_sub_menu.html.haml diff --git a/app/controllers/admin/enterprise_relationships_controller.rb b/app/controllers/admin/enterprise_relationships_controller.rb new file mode 100644 index 0000000000..7959b9d9b5 --- /dev/null +++ b/app/controllers/admin/enterprise_relationships_controller.rb @@ -0,0 +1,7 @@ +module Admin + class EnterpriseRelationshipsController < ResourceController + def index + + end + end +end diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml new file mode 100644 index 0000000000..7f2c236d82 --- /dev/null +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -0,0 +1,8 @@ +- content_for :page_title do + Enterprise Relationships + += render 'admin/shared/enterprises_sub_menu' + +%table#enterprise-relationships + %tr + %td Hello, world. diff --git a/app/views/admin/enterprises/index.html.erb b/app/views/admin/enterprises/index.html.erb index d2917bcc45..1795b17950 100644 --- a/app/views/admin/enterprises/index.html.erb +++ b/app/views/admin/enterprises/index.html.erb @@ -8,6 +8,8 @@ <% end %> +<%= render 'admin/shared/enterprises_sub_menu' %> + <%= form_for @enterprise_set, :url => main_app.bulk_update_admin_enterprises_path do |f| %> diff --git a/app/views/admin/shared/_enterprises_sub_menu.html.haml b/app/views/admin/shared/_enterprises_sub_menu.html.haml new file mode 100644 index 0000000000..57ce5e9194 --- /dev/null +++ b/app/views/admin/shared/_enterprises_sub_menu.html.haml @@ -0,0 +1,4 @@ += content_for :sub_menu do + %ul#sub_nav.inline-menu{"data-hook" => "admin_order_sub_tabs"} + = tab :enterprises, url: main_app.admin_enterprises_path + = tab :relationships, url: main_app.admin_enterprise_relationships_path diff --git a/config/routes.rb b/config/routes.rb index 310d1d9e4d..973cb288cc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,8 @@ Openfoodnetwork::Application.routes.draw do post :bulk_update, :on => :collection, :as => :bulk_update end + resources :enterprise_relationships + resources :enterprise_fees do post :bulk_update, :on => :collection, :as => :bulk_update end From f7de504d89bb461fe45d6b259070e728c93d4367 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 12:27:58 +1000 Subject: [PATCH 09/32] Add stub angular controller for managing enterprise relationships --- .../enterprise_relationships_controller.js.coffee | 2 ++ .../admin/enterprise_relationships/index.html.haml | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee diff --git a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee new file mode 100644 index 0000000000..39a4ad38c4 --- /dev/null +++ b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee @@ -0,0 +1,2 @@ +Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope) -> + $scope.hello = "Hello, angular." diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index 7f2c236d82..aff1fa2e6f 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -3,6 +3,13 @@ = render 'admin/shared/enterprises_sub_menu' -%table#enterprise-relationships - %tr - %td Hello, world. +%div{"ng-app" => "ofn.admin", "ng-controller" => "AdminEnterpriseRelationshipsCtrl"} + %table#enterprise-relationships + %thead + %tr + %th Parent + %th Child + %tbody + %tr + %td {{ hello }} + %td {{ hello }} From 1a854b89eca8b0dda3f6fc5632af68c99e77c4e3 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 14:24:35 +1000 Subject: [PATCH 10/32] Sort enterprise relationships by enterprise name --- app/models/enterprise_relationship.rb | 5 +++++ spec/models/enterprise_relationship_spec.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 spec/models/enterprise_relationship_spec.rb diff --git a/app/models/enterprise_relationship.rb b/app/models/enterprise_relationship.rb index 2cbda1511a..2afd8c8a3e 100644 --- a/app/models/enterprise_relationship.rb +++ b/app/models/enterprise_relationship.rb @@ -4,4 +4,9 @@ class EnterpriseRelationship < ActiveRecord::Base validates_presence_of :parent_id, :child_id validates_uniqueness_of :child_id, scope: :parent_id + + scope :with_enterprises, + joins('LEFT JOIN enterprises AS parent_enterprises ON parent_enterprises.id = enterprise_relationships.parent_id'). + joins('LEFT JOIN enterprises AS child_enterprises ON child_enterprises.id = enterprise_relationships.child_id') + scope :by_name, with_enterprises.order('parent_enterprises.name, child_enterprises.name') end diff --git a/spec/models/enterprise_relationship_spec.rb b/spec/models/enterprise_relationship_spec.rb new file mode 100644 index 0000000000..7cca07472e --- /dev/null +++ b/spec/models/enterprise_relationship_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe EnterpriseRelationship do + describe "scopes" do + it "sorts by parent, child enterprise name" do + e1 = create(:enterprise, name: 'A') + e2 = create(:enterprise, name: 'B') + e3 = create(:enterprise, name: 'C') + er1 = create(:enterprise_relationship, parent: e1, child: e3) + er2 = create(:enterprise_relationship, parent: e2, child: e1) + er3 = create(:enterprise_relationship, parent: e1, child: e2) + + EnterpriseRelationship.by_name.should == [er3, er1, er2] + end + end +end From 6414ae2972454cffd4171a00cdbd2d8b4a7f21d4 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 14:47:53 +1000 Subject: [PATCH 11/32] Output enterprise relationships as JSON --- .../admin/json/_enterprise_relationships.rabl | 11 +++++++++ .../enterprise_relationships_rabl_spec.rb | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 app/views/admin/json/_enterprise_relationships.rabl create mode 100644 spec/views/admin/json/enterprise_relationships_rabl_spec.rb diff --git a/app/views/admin/json/_enterprise_relationships.rabl b/app/views/admin/json/_enterprise_relationships.rabl new file mode 100644 index 0000000000..1867dd9c56 --- /dev/null +++ b/app/views/admin/json/_enterprise_relationships.rabl @@ -0,0 +1,11 @@ +collection @enterprise_relationships + +attributes :parent_id, :child_id + +node :parent_name do |enterprise_relationship| + enterprise_relationship.parent.name +end + +node :child_name do |enterprise_relationship| + enterprise_relationship.child.name +end diff --git a/spec/views/admin/json/enterprise_relationships_rabl_spec.rb b/spec/views/admin/json/enterprise_relationships_rabl_spec.rb new file mode 100644 index 0000000000..9b72a0d45b --- /dev/null +++ b/spec/views/admin/json/enterprise_relationships_rabl_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe "admin/json/_enterprise_relationships.json.rabl" do + let(:parent) { create(:enterprise) } + let(:child) { create(:enterprise) } + let(:enterprise_relationship) { create(:enterprise_relationship, parent: parent, child: child) } + let(:render) { Rabl.render([enterprise_relationship], 'admin/json/enterprise_relationships', view_path: 'app/views', scope: RablHelper::FakeContext.instance) } + + it "renders a list of enterprise relationships" do + render.should have_json_type(Array).at_path '' + render.should have_json_type(Object).at_path '0' + end + + it "renders enterprise ids" do + render.should be_json_eql(parent.id).at_path '0/parent_id' + render.should be_json_eql(child.id).at_path '0/child_id' + end + + it "renders enterprise names" do + render.should be_json_eql(parent.name.to_json).at_path '0/parent_name' + render.should be_json_eql(child.name.to_json).at_path '0/child_name' + end +end From 61af8204d9f078d69989aae3611683f2920cab87 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 14:49:25 +1000 Subject: [PATCH 12/32] Add spec matcher have_table_row --- spec/support/matchers/table_matchers.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 spec/support/matchers/table_matchers.rb diff --git a/spec/support/matchers/table_matchers.rb b/spec/support/matchers/table_matchers.rb new file mode 100644 index 0000000000..7a06965c67 --- /dev/null +++ b/spec/support/matchers/table_matchers.rb @@ -0,0 +1,16 @@ +RSpec::Matchers.define :have_table_row do |row| + + match do |node| + @row = row + node.all('tr').map { |tr| tr.all('th, td').map(&:text) }.include? row + end + + failure_message_for_should do |text| + "expected to find table row #{@row}" + end + + failure_message_for_should_not do |text| + "expected not to find table row #{@row}" + end + +end From 5f4e4ebeb025e26b2e48e17452c4eb71b3784d2b Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 14:49:39 +1000 Subject: [PATCH 13/32] Admin can list enterprise relationships --- ...erprise_relationships_controller.js.coffee | 4 +-- .../enterprise_relationships.js.coffee | 4 +++ .../enterprise_relationships_controller.rb | 2 +- .../enterprise_relationships/index.html.haml | 10 ++++-- .../admin/enterprise_relationships_spec.rb | 32 +++++++++++++++++++ 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 app/assets/javascripts/admin/services/enterprise_relationships.js.coffee create mode 100644 spec/features/admin/enterprise_relationships_spec.rb diff --git a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee index 39a4ad38c4..cd86cf20d6 100644 --- a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee +++ b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee @@ -1,2 +1,2 @@ -Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope) -> - $scope.hello = "Hello, angular." +Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope, EnterpriseRelationships) -> + $scope.EnterpriseRelationships = EnterpriseRelationships diff --git a/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee new file mode 100644 index 0000000000..0201a9793e --- /dev/null +++ b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee @@ -0,0 +1,4 @@ +Admin.factory 'EnterpriseRelationships', (enterprise_relationships) -> + new class EnterpriseRelationships + constructor: -> + @enterprise_relationships = enterprise_relationships diff --git a/app/controllers/admin/enterprise_relationships_controller.rb b/app/controllers/admin/enterprise_relationships_controller.rb index 7959b9d9b5..d8dcfdde9d 100644 --- a/app/controllers/admin/enterprise_relationships_controller.rb +++ b/app/controllers/admin/enterprise_relationships_controller.rb @@ -1,7 +1,7 @@ module Admin class EnterpriseRelationshipsController < ResourceController def index - + @enterprise_relationships = EnterpriseRelationship.by_name end end end diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index aff1fa2e6f..897593b22d 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -4,12 +4,16 @@ = render 'admin/shared/enterprises_sub_menu' %div{"ng-app" => "ofn.admin", "ng-controller" => "AdminEnterpriseRelationshipsCtrl"} + :javascript + angular.module('ofn.admin').value('enterprise_relationships', #{render partial: "admin/json/enterprise_relationships", object: @enterprise_relationships}) + -#%pre {{ EnterpriseRelationships.enterprise_relationships | json }} + %table#enterprise-relationships %thead %tr %th Parent %th Child %tbody - %tr - %td {{ hello }} - %td {{ hello }} + %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships"} + %td {{ enterprise_relationship.parent_name }} + %td {{ enterprise_relationship.child_name }} diff --git a/spec/features/admin/enterprise_relationships_spec.rb b/spec/features/admin/enterprise_relationships_spec.rb new file mode 100644 index 0000000000..ca0a065e5f --- /dev/null +++ b/spec/features/admin/enterprise_relationships_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +feature %q{ + As an Administrator + I want to manage relationships between enterprises +}, js: true do + include AuthenticationWorkflow + include WebHelper + + before { login_to_admin_section } + + scenario "listing relationships" do + # Given some enterprises with relationships + e1, e2, e3, e4 = create(:enterprise), create(:enterprise), create(:enterprise), create(:enterprise) + create(:enterprise_relationship, parent: e1, child: e2) + create(:enterprise_relationship, parent: e3, child: e4) + + # When I go to the relationships page + click_link 'Enterprises' + click_link 'Relationships' + + # Then I should see the relationships + within('table#enterprise-relationships') do + page.should have_table_row [e1.name, e2.name] + page.should have_table_row [e3.name, e4.name] + end + end + + + scenario "creating a relationship" + scenario "deleting a relationship" +end From ba7e29074f825f54150628dbf64be97b02942b71 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 16:20:02 +1000 Subject: [PATCH 14/32] have_table_row waits --- spec/support/matchers/table_matchers.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spec/support/matchers/table_matchers.rb b/spec/support/matchers/table_matchers.rb index 7a06965c67..71fb297e33 100644 --- a/spec/support/matchers/table_matchers.rb +++ b/spec/support/matchers/table_matchers.rb @@ -2,7 +2,14 @@ RSpec::Matchers.define :have_table_row do |row| match do |node| @row = row - node.all('tr').map { |tr| tr.all('th, td').map(&:text) }.include? row + + begin + wait_until { node.all('tr').map { |tr| tr.all('th, td').map(&:text) }.include? row } + rescue TimeoutError + false + else + true + end end failure_message_for_should do |text| From 3cb55062ccca817215a17bbe65358cd49eab7be2 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 16:20:25 +1000 Subject: [PATCH 15/32] Split enterprise_relationships rabl into show and index --- app/views/admin/json/_enterprise_relationship.rabl | 11 +++++++++++ app/views/admin/json/_enterprise_relationships.rabl | 11 +---------- 2 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 app/views/admin/json/_enterprise_relationship.rabl diff --git a/app/views/admin/json/_enterprise_relationship.rabl b/app/views/admin/json/_enterprise_relationship.rabl new file mode 100644 index 0000000000..23d08d4c87 --- /dev/null +++ b/app/views/admin/json/_enterprise_relationship.rabl @@ -0,0 +1,11 @@ +object @enterprise_relationship + +attributes :parent_id, :child_id + +node :parent_name do |enterprise_relationship| + enterprise_relationship.parent.name +end + +node :child_name do |enterprise_relationship| + enterprise_relationship.child.name +end diff --git a/app/views/admin/json/_enterprise_relationships.rabl b/app/views/admin/json/_enterprise_relationships.rabl index 1867dd9c56..aad55b9770 100644 --- a/app/views/admin/json/_enterprise_relationships.rabl +++ b/app/views/admin/json/_enterprise_relationships.rabl @@ -1,11 +1,2 @@ collection @enterprise_relationships - -attributes :parent_id, :child_id - -node :parent_name do |enterprise_relationship| - enterprise_relationship.parent.name -end - -node :child_name do |enterprise_relationship| - enterprise_relationship.child.name -end +extends "admin/json/enterprise_relationship" From 9b2a80c6b5320336dfe934071c9e1ba5da369037 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 15 May 2014 16:23:04 +1000 Subject: [PATCH 16/32] Admin can create enterprise relationships --- ...nterprise_relationships_controller.js.coffee | 12 +++++++++++- .../admin/services/enterprises.js.coffee | 4 ++++ .../enterprise_relationships_controller.rb | 11 +++++++++++ .../enterprise_relationships/index.html.haml | 13 +++++++++++-- app/views/admin/json/_enterprises.rabl | 3 +++ .../admin/enterprise_relationships_spec.rb | 17 ++++++++++++++++- 6 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/admin/services/enterprises.js.coffee create mode 100644 app/views/admin/json/_enterprises.rabl diff --git a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee index cd86cf20d6..e7934f86fc 100644 --- a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee +++ b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee @@ -1,2 +1,12 @@ -Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope, EnterpriseRelationships) -> +Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope, $http, EnterpriseRelationships, Enterprises) -> $scope.EnterpriseRelationships = EnterpriseRelationships + $scope.Enterprises = Enterprises + + $scope.create = -> + $http.post('/admin/enterprise_relationships', {enterprise_relationship: {parent_id: $scope.parent_id, child_id: $scope.child_id}}).success (data, status) => + $scope.EnterpriseRelationships.enterprise_relationships.unshift({parent_name: data.parent_name, child_name: data.child_name}) + + .error (response, status) => + #@errors = response.errors + #flash.error = response.flash?.error + #flash.success = response.flash?.notice diff --git a/app/assets/javascripts/admin/services/enterprises.js.coffee b/app/assets/javascripts/admin/services/enterprises.js.coffee new file mode 100644 index 0000000000..199b6294c3 --- /dev/null +++ b/app/assets/javascripts/admin/services/enterprises.js.coffee @@ -0,0 +1,4 @@ +Admin.factory 'Enterprises', (enterprises) -> + new class Enterprises + constructor: -> + @enterprises = enterprises diff --git a/app/controllers/admin/enterprise_relationships_controller.rb b/app/controllers/admin/enterprise_relationships_controller.rb index d8dcfdde9d..aecc788cc0 100644 --- a/app/controllers/admin/enterprise_relationships_controller.rb +++ b/app/controllers/admin/enterprise_relationships_controller.rb @@ -1,7 +1,18 @@ module Admin class EnterpriseRelationshipsController < ResourceController def index + @enterprises = Enterprise.managed_by(spree_current_user).by_name @enterprise_relationships = EnterpriseRelationship.by_name end + + def create + @enterprise_relationship = EnterpriseRelationship.new params[:enterprise_relationship] + + if @enterprise_relationship.save + render partial: "admin/json/enterprise_relationship", locals: {enterprise_relationship: @enterprise_relationship} + else + render status: 413, json: @enterprise_relationship.errors + end + end end end diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index 897593b22d..abe74c1738 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -5,15 +5,24 @@ %div{"ng-app" => "ofn.admin", "ng-controller" => "AdminEnterpriseRelationshipsCtrl"} :javascript - angular.module('ofn.admin').value('enterprise_relationships', #{render partial: "admin/json/enterprise_relationships", object: @enterprise_relationships}) - -#%pre {{ EnterpriseRelationships.enterprise_relationships | json }} + angular.module('ofn.admin').value('enterprise_relationships', #{render partial: "admin/json/enterprise_relationships", object: @enterprise_relationships}); + angular.module('ofn.admin').value('enterprises', #{render partial: "admin/json/enterprises", object: @enterprises}); %table#enterprise-relationships %thead %tr %th Parent %th Child + %th.actions %tbody + %tr + %td + %select{name: "enterprise_relationship_parent_name", "ng-model" => "parent_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} + %td + %select{name: "enterprise_relationship_child_name", "ng-model" => "child_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} + %td.actions + %input{type: "button", value: "Create", "ng-click" => "create()"} + %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships"} %td {{ enterprise_relationship.parent_name }} %td {{ enterprise_relationship.child_name }} diff --git a/app/views/admin/json/_enterprises.rabl b/app/views/admin/json/_enterprises.rabl new file mode 100644 index 0000000000..29e1dc7bf6 --- /dev/null +++ b/app/views/admin/json/_enterprises.rabl @@ -0,0 +1,3 @@ +collection @enterprises + +attributes :id, :name diff --git a/spec/features/admin/enterprise_relationships_spec.rb b/spec/features/admin/enterprise_relationships_spec.rb index ca0a065e5f..f2421320a6 100644 --- a/spec/features/admin/enterprise_relationships_spec.rb +++ b/spec/features/admin/enterprise_relationships_spec.rb @@ -27,6 +27,21 @@ feature %q{ end - scenario "creating a relationship" + scenario "creating a relationship" do + e1 = create(:enterprise, name: 'One') + e2 = create(:enterprise, name: 'Two') + + visit admin_enterprise_relationships_path + select 'One', from: 'enterprise_relationship_parent_name' + select 'Two', from: 'enterprise_relationship_child_name' + click_button 'Create' + + page.should have_table_row [e1.name, e2.name] + EnterpriseRelationship.where(parent_id: e1, child_id: e2).should be_present + end + + + scenario "error when creating relationship" + scenario "deleting a relationship" end From e40843bccb64ef8d04d8f478e894385f70413b2e Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 16 May 2014 11:25:51 +1000 Subject: [PATCH 17/32] Allow error messages without attribute name. Improve enterprise relationship error message. --- Gemfile | 1 + Gemfile.lock | 7 +++++++ app/models/enterprise_relationship.rb | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index af4108c9eb..87566eebfa 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,7 @@ gem 'geocoder' gem 'gmaps4rails' gem 'spinjs-rails' gem 'rack-ssl', :require => 'rack/ssl' +gem 'custom_error_message', :github => 'jeremydurham/custom-err-msg' gem 'foreigner' gem 'immigrant' diff --git a/Gemfile.lock b/Gemfile.lock index 87a0acf967..733ef43572 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,6 +6,12 @@ GIT actionpack (~> 3.0) activemodel (~> 3.0) +GIT + remote: git://github.com/jeremydurham/custom-err-msg.git + revision: 3a8ec9dddc7a5b0aab7c69a6060596de300c68f4 + specs: + custom_error_message (1.1.1) + GIT remote: git://github.com/openfoodfoundation/spree.git revision: da651b40f5c6cdd32e00b060729eb9aefd4f615f @@ -492,6 +498,7 @@ DEPENDENCIES coffee-rails (~> 3.2.1) comfortable_mexican_sofa compass-rails + custom_error_message! database_cleaner (= 0.7.1) db2fog debugger-linecache diff --git a/app/models/enterprise_relationship.rb b/app/models/enterprise_relationship.rb index 2afd8c8a3e..09fff8ba03 100644 --- a/app/models/enterprise_relationship.rb +++ b/app/models/enterprise_relationship.rb @@ -3,7 +3,7 @@ class EnterpriseRelationship < ActiveRecord::Base belongs_to :child, class_name: 'Enterprise' validates_presence_of :parent_id, :child_id - validates_uniqueness_of :child_id, scope: :parent_id + validates_uniqueness_of :child_id, scope: :parent_id, message: "^That relationship is already established." scope :with_enterprises, joins('LEFT JOIN enterprises AS parent_enterprises ON parent_enterprises.id = enterprise_relationships.parent_id'). From b9a9937541f21bb6347003a9b3a6ac914b510a46 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 16 May 2014 11:27:59 +1000 Subject: [PATCH 18/32] Attempting to create a relationship with invalid data shows an error message --- ...erprise_relationships_controller.js.coffee | 5 ++--- .../enterprise_relationships_controller.rb | 2 +- .../enterprise_relationships/index.html.haml | 1 + .../admin/enterprise_relationships_spec.rb | 19 ++++++++++++++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee index e7934f86fc..02001bc48f 100644 --- a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee +++ b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee @@ -5,8 +5,7 @@ Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope, $http, EnterpriseR $scope.create = -> $http.post('/admin/enterprise_relationships', {enterprise_relationship: {parent_id: $scope.parent_id, child_id: $scope.child_id}}).success (data, status) => $scope.EnterpriseRelationships.enterprise_relationships.unshift({parent_name: data.parent_name, child_name: data.child_name}) + $scope.errors = "" .error (response, status) => - #@errors = response.errors - #flash.error = response.flash?.error - #flash.success = response.flash?.notice + $scope.errors = response.errors diff --git a/app/controllers/admin/enterprise_relationships_controller.rb b/app/controllers/admin/enterprise_relationships_controller.rb index aecc788cc0..9661470574 100644 --- a/app/controllers/admin/enterprise_relationships_controller.rb +++ b/app/controllers/admin/enterprise_relationships_controller.rb @@ -11,7 +11,7 @@ module Admin if @enterprise_relationship.save render partial: "admin/json/enterprise_relationship", locals: {enterprise_relationship: @enterprise_relationship} else - render status: 413, json: @enterprise_relationship.errors + render status: 400, json: {errors: @enterprise_relationship.errors.full_messages.join(', ')} end end end diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index abe74c1738..050c57cc96 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -22,6 +22,7 @@ %select{name: "enterprise_relationship_child_name", "ng-model" => "child_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} %td.actions %input{type: "button", value: "Create", "ng-click" => "create()"} + .errors {{ errors }} %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships"} %td {{ enterprise_relationship.parent_name }} diff --git a/spec/features/admin/enterprise_relationships_spec.rb b/spec/features/admin/enterprise_relationships_spec.rb index f2421320a6..ae54f1112c 100644 --- a/spec/features/admin/enterprise_relationships_spec.rb +++ b/spec/features/admin/enterprise_relationships_spec.rb @@ -9,6 +9,7 @@ feature %q{ before { login_to_admin_section } + scenario "listing relationships" do # Given some enterprises with relationships e1, e2, e3, e4 = create(:enterprise), create(:enterprise), create(:enterprise), create(:enterprise) @@ -41,7 +42,23 @@ feature %q{ end - scenario "error when creating relationship" + scenario "attempting to create a relationship with invalid data" do + e1 = create(:enterprise, name: 'One') + e2 = create(:enterprise, name: 'Two') + create(:enterprise_relationship, parent: e1, child: e2) + + expect do + # When I attempt to create a duplicate relationship + visit admin_enterprise_relationships_path + select 'One', from: 'enterprise_relationship_parent_name' + select 'Two', from: 'enterprise_relationship_child_name' + click_button 'Create' + + # Then I should see an error message + page.should have_content "That relationship is already established." + end.to change(EnterpriseRelationship, :count).by(0) + end + scenario "deleting a relationship" end From f1df0210915933a2aa82c6c9ed9b899e93ca732d Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 16 May 2014 11:28:13 +1000 Subject: [PATCH 19/32] Show enterprise relationships tab as selected --- app/views/admin/shared/_enterprises_sub_menu.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/admin/shared/_enterprises_sub_menu.html.haml b/app/views/admin/shared/_enterprises_sub_menu.html.haml index 57ce5e9194..4a793689b7 100644 --- a/app/views/admin/shared/_enterprises_sub_menu.html.haml +++ b/app/views/admin/shared/_enterprises_sub_menu.html.haml @@ -1,4 +1,4 @@ = content_for :sub_menu do %ul#sub_nav.inline-menu{"data-hook" => "admin_order_sub_tabs"} = tab :enterprises, url: main_app.admin_enterprises_path - = tab :relationships, url: main_app.admin_enterprise_relationships_path + = tab :relationships, url: main_app.admin_enterprise_relationships_path, match_path: '/enterprise_relationships' From aa965e3752d7256686bbbe716b5926de32c94f87 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 16 May 2014 11:34:10 +1000 Subject: [PATCH 20/32] Show a CSS3 animation when a row is added --- app/assets/javascripts/admin/admin.js.coffee | 2 +- app/assets/javascripts/admin/all.js | 1 + .../admin/enterprise_relationships.css.sass | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/admin/enterprise_relationships.css.sass diff --git a/app/assets/javascripts/admin/admin.js.coffee b/app/assets/javascripts/admin/admin.js.coffee index ba07bb7933..76154e9e20 100644 --- a/app/assets/javascripts/admin/admin.js.coffee +++ b/app/assets/javascripts/admin/admin.js.coffee @@ -1,3 +1,3 @@ -window.Admin = angular.module("ofn.admin", ["ngResource","ofn.dropdown"]).config ($httpProvider) -> +window.Admin = angular.module("ofn.admin", ["ngResource", "ngAnimate", "ofn.dropdown"]).config ($httpProvider) -> $httpProvider.defaults.headers.common["X-CSRF-Token"] = $("meta[name=csrf-token]").attr("content") $httpProvider.defaults.headers.common["Accept"] = "application/json, text/javascript, */*" \ No newline at end of file diff --git a/app/assets/javascripts/admin/all.js b/app/assets/javascripts/admin/all.js index 3cf4fe97e7..99c936c4d7 100644 --- a/app/assets/javascripts/admin/all.js +++ b/app/assets/javascripts/admin/all.js @@ -11,6 +11,7 @@ //= require shared/jquery-ui-timepicker-addon //= require angular //= require angular-resource +//= require angular-animate //= require admin/spree_core //= require admin/spree_auth //= require admin/spree_promo diff --git a/app/assets/stylesheets/admin/enterprise_relationships.css.sass b/app/assets/stylesheets/admin/enterprise_relationships.css.sass new file mode 100644 index 0000000000..cffc0b0623 --- /dev/null +++ b/app/assets/stylesheets/admin/enterprise_relationships.css.sass @@ -0,0 +1,20 @@ +// TODO: Provide -moz- and -o- directives +@-webkit-keyframes alert-flash + 0% + background-color: #f9f1ae + + 100% + background-color: #fff + + +table#enterprise-relationships + th.actions, td.actions + width: 16% + .errors + color: #f00 + + tr.ng-enter + -webkit-animation-name: alert-flash + -webkit-animation-duration: 1200ms + -webkit-animation-iteration-count: 1 + -webkit-animation-timing-function: ease-in-out From 5d0680234d5ba003bc47bc0381ea846828cd7780 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 16 May 2014 14:44:01 +1000 Subject: [PATCH 21/32] Admin can delete enterprise relationships --- ...erprise_relationships_controller.js.coffee | 6 +++++- .../enterprise_relationships.js.coffee | 7 ++++++- .../enterprise_relationships_controller.rb | 6 ++++++ .../enterprise_relationships/index.html.haml | 2 ++ .../admin/json/_enterprise_relationship.rabl | 2 +- .../admin/enterprise_relationships_spec.rb | 20 +++++++++++++++---- 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee index 02001bc48f..51e2dd9ee0 100644 --- a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee +++ b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee @@ -4,8 +4,12 @@ Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope, $http, EnterpriseR $scope.create = -> $http.post('/admin/enterprise_relationships', {enterprise_relationship: {parent_id: $scope.parent_id, child_id: $scope.child_id}}).success (data, status) => - $scope.EnterpriseRelationships.enterprise_relationships.unshift({parent_name: data.parent_name, child_name: data.child_name}) + $scope.EnterpriseRelationships.enterprise_relationships.unshift(data) $scope.errors = "" .error (response, status) => $scope.errors = response.errors + + $scope.delete = (enterprise_relationship) -> + if confirm("Are you sure?") + $scope.EnterpriseRelationships.delete enterprise_relationship diff --git a/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee index 0201a9793e..8af5fef666 100644 --- a/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee +++ b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee @@ -1,4 +1,9 @@ -Admin.factory 'EnterpriseRelationships', (enterprise_relationships) -> +Admin.factory 'EnterpriseRelationships', ($http, enterprise_relationships) -> new class EnterpriseRelationships constructor: -> @enterprise_relationships = enterprise_relationships + + delete: (er) -> + ers = @enterprise_relationships + $http.delete('/admin/enterprise_relationships/' + er.id).success (data) -> + ers.splice ers.indexOf(er), 1 diff --git a/app/controllers/admin/enterprise_relationships_controller.rb b/app/controllers/admin/enterprise_relationships_controller.rb index 9661470574..46ac45a3e5 100644 --- a/app/controllers/admin/enterprise_relationships_controller.rb +++ b/app/controllers/admin/enterprise_relationships_controller.rb @@ -14,5 +14,11 @@ module Admin render status: 400, json: {errors: @enterprise_relationship.errors.full_messages.join(', ')} end end + + def destroy + @enterprise_relationship = EnterpriseRelationship.find params[:id] + @enterprise_relationship.destroy + render nothing: true + end end end diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index 050c57cc96..1992f883d4 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -27,3 +27,5 @@ %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships"} %td {{ enterprise_relationship.parent_name }} %td {{ enterprise_relationship.child_name }} + %td.actions + %a.delete-enterprise-relationship.icon-trash.no-text{'ng-click' => 'delete(enterprise_relationship)'} diff --git a/app/views/admin/json/_enterprise_relationship.rabl b/app/views/admin/json/_enterprise_relationship.rabl index 23d08d4c87..9be152ec5c 100644 --- a/app/views/admin/json/_enterprise_relationship.rabl +++ b/app/views/admin/json/_enterprise_relationship.rabl @@ -1,6 +1,6 @@ object @enterprise_relationship -attributes :parent_id, :child_id +attributes :id, :parent_id, :child_id node :parent_name do |enterprise_relationship| enterprise_relationship.parent.name diff --git a/spec/features/admin/enterprise_relationships_spec.rb b/spec/features/admin/enterprise_relationships_spec.rb index ae54f1112c..0161677afe 100644 --- a/spec/features/admin/enterprise_relationships_spec.rb +++ b/spec/features/admin/enterprise_relationships_spec.rb @@ -22,8 +22,8 @@ feature %q{ # Then I should see the relationships within('table#enterprise-relationships') do - page.should have_table_row [e1.name, e2.name] - page.should have_table_row [e3.name, e4.name] + page.should have_table_row [e1.name, e2.name, ''] + page.should have_table_row [e3.name, e4.name, ''] end end @@ -37,7 +37,7 @@ feature %q{ select 'Two', from: 'enterprise_relationship_child_name' click_button 'Create' - page.should have_table_row [e1.name, e2.name] + page.should have_table_row [e1.name, e2.name, ''] EnterpriseRelationship.where(parent_id: e1, child_id: e2).should be_present end @@ -60,5 +60,17 @@ feature %q{ end - scenario "deleting a relationship" + scenario "deleting a relationship" do + e1 = create(:enterprise, name: 'One') + e2 = create(:enterprise, name: 'Two') + er = create(:enterprise_relationship, parent: e1, child: e2) + + visit admin_enterprise_relationships_path + page.should have_table_row [e1.name, e2.name, ''] + + first("a.delete-enterprise-relationship").click + + page.should_not have_table_row [e1.name, e2.name, ''] + EnterpriseRelationship.where(id: er.id).should be_empty + end end From 18886c931f643267109eb4ca8f1e440b4251256b Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 16 May 2014 15:04:14 +1000 Subject: [PATCH 22/32] Extract create EnterpriseRelationship to service --- .../enterprise_relationships_controller.js.coffee | 9 ++------- .../services/enterprise_relationships.js.coffee | 15 ++++++++++++--- .../enterprise_relationships/index.html.haml | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee index 51e2dd9ee0..c66e79c046 100644 --- a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee +++ b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee @@ -1,14 +1,9 @@ -Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope, $http, EnterpriseRelationships, Enterprises) -> +Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope, EnterpriseRelationships, Enterprises) -> $scope.EnterpriseRelationships = EnterpriseRelationships $scope.Enterprises = Enterprises $scope.create = -> - $http.post('/admin/enterprise_relationships', {enterprise_relationship: {parent_id: $scope.parent_id, child_id: $scope.child_id}}).success (data, status) => - $scope.EnterpriseRelationships.enterprise_relationships.unshift(data) - $scope.errors = "" - - .error (response, status) => - $scope.errors = response.errors + $scope.EnterpriseRelationships.create($scope.parent_id, $scope.child_id) $scope.delete = (enterprise_relationship) -> if confirm("Are you sure?") diff --git a/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee index 8af5fef666..0469aa5637 100644 --- a/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee +++ b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee @@ -1,9 +1,18 @@ Admin.factory 'EnterpriseRelationships', ($http, enterprise_relationships) -> new class EnterpriseRelationships + create_errors: "" + constructor: -> @enterprise_relationships = enterprise_relationships + create: (parent_id, child_id) -> + $http.post('/admin/enterprise_relationships', {enterprise_relationship: {parent_id: parent_id, child_id: child_id}}).success (data, status) => + @enterprise_relationships.unshift(data) + @create_errors = "" + + .error (response, status) => + @create_errors = response.errors + delete: (er) -> - ers = @enterprise_relationships - $http.delete('/admin/enterprise_relationships/' + er.id).success (data) -> - ers.splice ers.indexOf(er), 1 + $http.delete('/admin/enterprise_relationships/' + er.id).success (data) => + @enterprise_relationships.splice @enterprise_relationships.indexOf(er), 1 diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index 1992f883d4..ec843edc9d 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -22,7 +22,7 @@ %select{name: "enterprise_relationship_child_name", "ng-model" => "child_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} %td.actions %input{type: "button", value: "Create", "ng-click" => "create()"} - .errors {{ errors }} + .errors {{ EnterpriseRelationships.create_errors }} %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships"} %td {{ enterprise_relationship.parent_name }} From 6026a74c7322b7b06594985d78c2d53e67777aeb Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 21 May 2014 14:59:24 +1000 Subject: [PATCH 23/32] Table matcher have_table_row does not wait for full duration on should_not --- spec/support/matchers/table_matchers.rb | 34 ++++++++++++++++++++----- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/spec/support/matchers/table_matchers.rb b/spec/support/matchers/table_matchers.rb index 71fb297e33..d0eb28d641 100644 --- a/spec/support/matchers/table_matchers.rb +++ b/spec/support/matchers/table_matchers.rb @@ -1,17 +1,37 @@ RSpec::Matchers.define :have_table_row do |row| - match do |node| + match_for_should do |node| @row = row - begin - wait_until { node.all('tr').map { |tr| tr.all('th, td').map(&:text) }.include? row } - rescue TimeoutError - false - else - true + false_on_timeout_error do + wait_until { rows_under(node).include? row } end end + match_for_should_not do |node| + @row = row + + false_on_timeout_error do + # Without this sleep, we trigger capybara's wait when looking up the table, for the full + # period of default_wait_time. + sleep 0.1 + wait_until { !rows_under(node).include? row } + end + end + + def rows_under(node) + node.all('tr').map { |tr| tr.all('th, td').map(&:text) } + end + + def false_on_timeout_error + yield + rescue TimeoutError + false + else + true + end + + failure_message_for_should do |text| "expected to find table row #{@row}" end From 92292c853ddbeb3e9833607081cfd91c84a9adb8 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 21 May 2014 15:03:29 +1000 Subject: [PATCH 24/32] Extract partials from enterprise relationships admin page --- .../enterprise_relationships/_data.html.haml | 3 +++ .../_enterprise_relationship.html.haml | 4 ++++ .../enterprise_relationships/_form.html.haml | 8 ++++++++ .../enterprise_relationships/index.html.haml | 19 +++---------------- 4 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 app/views/admin/enterprise_relationships/_data.html.haml create mode 100644 app/views/admin/enterprise_relationships/_enterprise_relationship.html.haml create mode 100644 app/views/admin/enterprise_relationships/_form.html.haml diff --git a/app/views/admin/enterprise_relationships/_data.html.haml b/app/views/admin/enterprise_relationships/_data.html.haml new file mode 100644 index 0000000000..2361755707 --- /dev/null +++ b/app/views/admin/enterprise_relationships/_data.html.haml @@ -0,0 +1,3 @@ +:javascript + angular.module('ofn.admin').value('enterprise_relationships', #{render partial: "admin/json/enterprise_relationships", object: @enterprise_relationships}); + angular.module('ofn.admin').value('enterprises', #{render partial: "admin/json/enterprises", object: @enterprises}); diff --git a/app/views/admin/enterprise_relationships/_enterprise_relationship.html.haml b/app/views/admin/enterprise_relationships/_enterprise_relationship.html.haml new file mode 100644 index 0000000000..237a0f5a38 --- /dev/null +++ b/app/views/admin/enterprise_relationships/_enterprise_relationship.html.haml @@ -0,0 +1,4 @@ +%td {{ enterprise_relationship.parent_name }} +%td {{ enterprise_relationship.child_name }} +%td.actions + %a.delete-enterprise-relationship.icon-trash.no-text{'ng-click' => 'delete(enterprise_relationship)'} diff --git a/app/views/admin/enterprise_relationships/_form.html.haml b/app/views/admin/enterprise_relationships/_form.html.haml new file mode 100644 index 0000000000..f0b5a9fe72 --- /dev/null +++ b/app/views/admin/enterprise_relationships/_form.html.haml @@ -0,0 +1,8 @@ +%tr + %td + %select{name: "enterprise_relationship_parent_name", "ng-model" => "parent_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} + %td + %select{name: "enterprise_relationship_child_name", "ng-model" => "child_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} + %td.actions + %input{type: "button", value: "Create", "ng-click" => "create()"} + .errors {{ EnterpriseRelationships.create_errors }} diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index ec843edc9d..4e5cedb944 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -4,9 +4,7 @@ = render 'admin/shared/enterprises_sub_menu' %div{"ng-app" => "ofn.admin", "ng-controller" => "AdminEnterpriseRelationshipsCtrl"} - :javascript - angular.module('ofn.admin').value('enterprise_relationships', #{render partial: "admin/json/enterprise_relationships", object: @enterprise_relationships}); - angular.module('ofn.admin').value('enterprises', #{render partial: "admin/json/enterprises", object: @enterprises}); + = render 'data' %table#enterprise-relationships %thead @@ -15,17 +13,6 @@ %th Child %th.actions %tbody - %tr - %td - %select{name: "enterprise_relationship_parent_name", "ng-model" => "parent_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} - %td - %select{name: "enterprise_relationship_child_name", "ng-model" => "child_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} - %td.actions - %input{type: "button", value: "Create", "ng-click" => "create()"} - .errors {{ EnterpriseRelationships.create_errors }} - + = render 'form' %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships"} - %td {{ enterprise_relationship.parent_name }} - %td {{ enterprise_relationship.child_name }} - %td.actions - %a.delete-enterprise-relationship.icon-trash.no-text{'ng-click' => 'delete(enterprise_relationship)'} + = render 'enterprise_relationship' From 76d1754c32509b9fc0290aec9ffcfe87df196c93 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 21 May 2014 15:11:14 +1000 Subject: [PATCH 25/32] Reorder helper method at bottom --- spec/support/matchers/table_matchers.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/spec/support/matchers/table_matchers.rb b/spec/support/matchers/table_matchers.rb index d0eb28d641..146002b751 100644 --- a/spec/support/matchers/table_matchers.rb +++ b/spec/support/matchers/table_matchers.rb @@ -19,6 +19,15 @@ RSpec::Matchers.define :have_table_row do |row| end end + failure_message_for_should do |text| + "expected to find table row #{@row}" + end + + failure_message_for_should_not do |text| + "expected not to find table row #{@row}" + end + + def rows_under(node) node.all('tr').map { |tr| tr.all('th, td').map(&:text) } end @@ -31,13 +40,4 @@ RSpec::Matchers.define :have_table_row do |row| true end - - failure_message_for_should do |text| - "expected to find table row #{@row}" - end - - failure_message_for_should_not do |text| - "expected not to find table row #{@row}" - end - end From 0c647841a6a157bcd134a6183a9781399d089088 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 21 May 2014 16:18:07 +1000 Subject: [PATCH 26/32] Add dynamic filter to enterprise relationships --- app/assets/stylesheets/admin/openfoodnetwork.css.scss | 4 +++- app/views/admin/enterprise_relationships/index.html.haml | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/assets/stylesheets/admin/openfoodnetwork.css.scss b/app/assets/stylesheets/admin/openfoodnetwork.css.scss index 92092bd136..87983a6bc7 100644 --- a/app/assets/stylesheets/admin/openfoodnetwork.css.scss +++ b/app/assets/stylesheets/admin/openfoodnetwork.css.scss @@ -19,9 +19,11 @@ table .blank-action { } +input.search { + margin-bottom: 1em; +} #new_enterprise_fee_set input.search { float: right; - margin-bottom: 1em; } .ng .ng-invalid.ng-dirty { diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index 4e5cedb944..a12eaaa2b8 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -6,6 +6,8 @@ %div{"ng-app" => "ofn.admin", "ng-controller" => "AdminEnterpriseRelationshipsCtrl"} = render 'data' + %input.search{"ng-model" => "query", "placeholder" => "Search"} + %table#enterprise-relationships %thead %tr @@ -14,5 +16,5 @@ %th.actions %tbody = render 'form' - %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships"} + %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships | filter:query"} = render 'enterprise_relationship' From a049266732561c21b53c33a076f7bd2a0464fe05 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 21 May 2014 16:28:27 +1000 Subject: [PATCH 27/32] Replace enterprise relationship parent/child concepts with 'X permits Y' --- .../_enterprise_relationship.html.haml | 1 + app/views/admin/enterprise_relationships/_form.html.haml | 1 + app/views/admin/enterprise_relationships/index.html.haml | 5 ----- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/views/admin/enterprise_relationships/_enterprise_relationship.html.haml b/app/views/admin/enterprise_relationships/_enterprise_relationship.html.haml index 237a0f5a38..3f93027ca1 100644 --- a/app/views/admin/enterprise_relationships/_enterprise_relationship.html.haml +++ b/app/views/admin/enterprise_relationships/_enterprise_relationship.html.haml @@ -1,4 +1,5 @@ %td {{ enterprise_relationship.parent_name }} +%td permits %td {{ enterprise_relationship.child_name }} %td.actions %a.delete-enterprise-relationship.icon-trash.no-text{'ng-click' => 'delete(enterprise_relationship)'} diff --git a/app/views/admin/enterprise_relationships/_form.html.haml b/app/views/admin/enterprise_relationships/_form.html.haml index f0b5a9fe72..5cc10b0fe1 100644 --- a/app/views/admin/enterprise_relationships/_form.html.haml +++ b/app/views/admin/enterprise_relationships/_form.html.haml @@ -1,6 +1,7 @@ %tr %td %select{name: "enterprise_relationship_parent_name", "ng-model" => "parent_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} + %td permits %td %select{name: "enterprise_relationship_child_name", "ng-model" => "child_id", "ng-options" => "e.id as e.name for e in Enterprises.enterprises"} %td.actions diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index a12eaaa2b8..e0e289efcf 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -9,11 +9,6 @@ %input.search{"ng-model" => "query", "placeholder" => "Search"} %table#enterprise-relationships - %thead - %tr - %th Parent - %th Child - %th.actions %tbody = render 'form' %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships | filter:query"} From d8fa30ea6fbe3213a7fa5310e349e7b237536889 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 21 May 2014 16:54:26 +1000 Subject: [PATCH 28/32] Add a sequence to option type position, prevents inconsistent ordering in specs --- spec/factories.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/factories.rb b/spec/factories.rb index b2ed52b7a7..6da7824d47 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -190,6 +190,10 @@ FactoryGirl.modify do distributors { [Enterprise.is_distributor.first || FactoryGirl.create(:distributor_enterprise)] } end + factory :option_type do + # Prevent inconsistent ordering in specs when all option types have the same (0) position + sequence(:position) + end end From 48a7b9c3f86e7b6099830ab78592d68dbe6c8468 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 22 May 2014 08:50:52 +1000 Subject: [PATCH 29/32] Fix specs from 'X permits Y' table format change --- spec/features/admin/enterprise_relationships_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/features/admin/enterprise_relationships_spec.rb b/spec/features/admin/enterprise_relationships_spec.rb index 0161677afe..ea95f86ba4 100644 --- a/spec/features/admin/enterprise_relationships_spec.rb +++ b/spec/features/admin/enterprise_relationships_spec.rb @@ -22,8 +22,8 @@ feature %q{ # Then I should see the relationships within('table#enterprise-relationships') do - page.should have_table_row [e1.name, e2.name, ''] - page.should have_table_row [e3.name, e4.name, ''] + page.should have_table_row [e1.name, 'permits', e2.name, ''] + page.should have_table_row [e3.name, 'permits', e4.name, ''] end end @@ -37,7 +37,7 @@ feature %q{ select 'Two', from: 'enterprise_relationship_child_name' click_button 'Create' - page.should have_table_row [e1.name, e2.name, ''] + page.should have_table_row [e1.name, 'permits', e2.name, ''] EnterpriseRelationship.where(parent_id: e1, child_id: e2).should be_present end @@ -66,11 +66,11 @@ feature %q{ er = create(:enterprise_relationship, parent: e1, child: e2) visit admin_enterprise_relationships_path - page.should have_table_row [e1.name, e2.name, ''] + page.should have_table_row [e1.name, 'permits', e2.name, ''] first("a.delete-enterprise-relationship").click - page.should_not have_table_row [e1.name, e2.name, ''] + page.should_not have_table_row [e1.name, 'permits', e2.name, ''] EnterpriseRelationship.where(id: er.id).should be_empty end end From 2d0a42b2c232592d297833250bae71574bf4f0e0 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 21 May 2014 14:09:48 +1000 Subject: [PATCH 30/32] Adding working modals --- .../controllers/products/product_node_controller.js.coffee | 2 ++ app/assets/javascripts/darkswarm/directives/modal.js.coffee | 5 ++++- app/views/modals/_producer.html.haml | 5 ++--- .../products/_modal.html.haml => modals/_product.html.haml} | 2 ++ app/views/shop/products/_summary.html.haml | 5 ++--- 5 files changed, 12 insertions(+), 7 deletions(-) rename app/views/{shop/products/_modal.html.haml => modals/_product.html.haml} (73%) diff --git a/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee index 4ec316e65f..3e24f006ae 100644 --- a/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee @@ -6,4 +6,6 @@ Darkswarm.controller "ProductNodeCtrl", ($scope) -> else $scope.product.price + $scope.producer = $scope.product.supplier + $scope.hasVariants = $scope.product.variants.length > 0 diff --git a/app/assets/javascripts/darkswarm/directives/modal.js.coffee b/app/assets/javascripts/darkswarm/directives/modal.js.coffee index 73217b3812..6f62c7cb45 100644 --- a/app/assets/javascripts/darkswarm/directives/modal.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/modal.js.coffee @@ -8,7 +8,10 @@ Darkswarm.directive "ofnModal", ($modal)-> link: (scope, elem, attrs, ctrl, transclude)-> scope.title = attrs.title contents = null - transclude scope, (clone)-> + + # We're using an isolate scope, which is a child of the original scope + # We have to compile the transclude against the original scope, not the isolate + transclude scope.$parent, (clone)-> contents = clone elem.on "click", => diff --git a/app/views/modals/_producer.html.haml b/app/views/modals/_producer.html.haml index 691e9378d3..056f9e488f 100644 --- a/app/views/modals/_producer.html.haml +++ b/app/views/modals/_producer.html.haml @@ -1,10 +1,9 @@ %ofn-modal{title: "{{ producer.name }}"} - #producer_modal{bindonce: true} + #producer_modal .row .small-12.columns - %img{"bo-src" => "producer.promo_image"} + %img{"ng-src" => "producer.promo_image"} %h3 {{ producer.name }} - .row .small-6.columns %p diff --git a/app/views/shop/products/_modal.html.haml b/app/views/modals/_product.html.haml similarity index 73% rename from app/views/shop/products/_modal.html.haml rename to app/views/modals/_product.html.haml index 29a023704f..acca8f919b 100644 --- a/app/views/shop/products/_modal.html.haml +++ b/app/views/modals/_product.html.haml @@ -1,2 +1,4 @@ %ofn-modal{title: "{{product.name}}"} + + {{ product | json }} {{ product.description }} diff --git a/app/views/shop/products/_summary.html.haml b/app/views/shop/products/_summary.html.haml index 5e777a5dbb..e41c6ef440 100644 --- a/app/views/shop/products/_summary.html.haml +++ b/app/views/shop/products/_summary.html.haml @@ -6,11 +6,10 @@ %img{"bo-src" => "product.primary_taxon.icon", "ng-click" => "ordering.order = 'primary_taxon.name'", name: "{{product.primary_taxon.name}}"} - {{ product.name}} - -#= render partial: "shop/products/modal" + = render partial: "modals/product" .small-5.columns.summary-header - {{ product.supplier.name }} + = render partial: "modals/producer" .small-2.columns.summary-price.text-right.price %span{"ng-if" => "hasVariants"} From 67e72f4504d04cb4a8894dcb4162f96c1e338014 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 21 May 2014 16:39:59 +1000 Subject: [PATCH 31/32] Setting up after_login redirects --- .../authentication/login_controller.js.coffee | 7 ++++-- .../services/authentication_service.js.coffee | 2 +- .../darkswarm/services/redirections.js.coffee | 3 +++ .../admin/overview_controller_decorator.rb | 3 +-- app/helpers/shared_helper.rb | 5 +++++ app/views/json/_injection.html.haml | 2 ++ app/views/layouts/darkswarm.html.haml | 8 +++---- app/views/shared/_current_hub.haml | 2 -- app/views/shared/_current_user.haml | 2 -- spec/features/consumer/authentication_spec.rb | 22 ++++++++++++++++++- 10 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/services/redirections.js.coffee create mode 100644 app/views/json/_injection.html.haml delete mode 100644 app/views/shared/_current_hub.haml delete mode 100644 app/views/shared/_current_user.haml diff --git a/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee index c2211bc033..da625e3d34 100644 --- a/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee @@ -1,8 +1,11 @@ -Darkswarm.controller "LoginCtrl", ($scope, $http, $location, AuthenticationService) -> +Darkswarm.controller "LoginCtrl", ($scope, $http, AuthenticationService, Redirections) -> $scope.path = "/login" $scope.submit = -> $http.post("/user/spree_user/sign_in", {spree_user: $scope.spree_user}).success (data)-> - location.href = location.origin + location.pathname # Strips out hash fragments + if Redirections.after_login + location.href = location.origin + Redirections.after_login + else + location.href = location.origin + location.pathname # Strips out hash fragments .error (data) -> $scope.errors = data.message diff --git a/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee b/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee index eada8bc44b..820d5bde9c 100644 --- a/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee +++ b/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.factory "AuthenticationService", (Navigation, $modal, $location)-> +Darkswarm.factory "AuthenticationService", (Navigation, $modal, $location, Redirections)-> new class AuthenticationService selectedPath: "/login" diff --git a/app/assets/javascripts/darkswarm/services/redirections.js.coffee b/app/assets/javascripts/darkswarm/services/redirections.js.coffee new file mode 100644 index 0000000000..a479964e9e --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/redirections.js.coffee @@ -0,0 +1,3 @@ +Darkswarm.factory "Redirections", ($location)-> + new class Redirections + after_login: $location.search().after_login diff --git a/app/controllers/spree/admin/overview_controller_decorator.rb b/app/controllers/spree/admin/overview_controller_decorator.rb index 5c96901c1d..a6142d26c0 100644 --- a/app/controllers/spree/admin/overview_controller_decorator.rb +++ b/app/controllers/spree/admin/overview_controller_decorator.rb @@ -13,8 +13,7 @@ Spree::Admin::OverviewController.class_eval do redirect_to '/unauthorized' else store_location - url = respond_to?(:spree_login_path) ? spree_login_path : root_path - redirect_to url + redirect_to root_path(anchor: "login?after_login=#{spree.admin_path}") end end end diff --git a/app/helpers/shared_helper.rb b/app/helpers/shared_helper.rb index be1e0ba0f4..828277cb57 100644 --- a/app/helpers/shared_helper.rb +++ b/app/helpers/shared_helper.rb @@ -1,4 +1,9 @@ module SharedHelper + + def inject_json(name, partial) + render "json/injection", name: name, partial: partial + end + def distributor_link_class(distributor) cart = current_order(true) @active_distributors ||= Enterprise.distributors_with_active_order_cycles diff --git a/app/views/json/_injection.html.haml b/app/views/json/_injection.html.haml new file mode 100644 index 0000000000..95b921406b --- /dev/null +++ b/app/views/json/_injection.html.haml @@ -0,0 +1,2 @@ +:javascript + angular.module('Darkswarm').value("#{name.to_s}", #{render "json/#{partial.to_s}"}) diff --git a/app/views/layouts/darkswarm.html.haml b/app/views/layouts/darkswarm.html.haml index 5c2dcaee66..edeffafeed 100644 --- a/app/views/layouts/darkswarm.html.haml +++ b/app/views/layouts/darkswarm.html.haml @@ -15,16 +15,16 @@ = csrf_meta_tags %body.off-canvas{"ng-app" => "Darkswarm"} + = inject_json "currentHub", "current_hub" + = inject_json "user", "current_user" + .off-canvas-wrap{offcanvas: true} .inner-wrap - = render partial: "shared/current_hub" - = render partial: "shared/current_user" = render partial: "shared/menu/menu" + = display_flash_messages %ofn-flash - -#= render "shared/sidebar" - %section{ role: "main" } = yield diff --git a/app/views/shared/_current_hub.haml b/app/views/shared/_current_hub.haml deleted file mode 100644 index 5a0ff7c564..0000000000 --- a/app/views/shared/_current_hub.haml +++ /dev/null @@ -1,2 +0,0 @@ -:javascript - angular.module('Darkswarm').value('currentHub', #{render "json/current_hub"}) diff --git a/app/views/shared/_current_user.haml b/app/views/shared/_current_user.haml deleted file mode 100644 index 9745a71313..0000000000 --- a/app/views/shared/_current_user.haml +++ /dev/null @@ -1,2 +0,0 @@ -:javascript - angular.module('Darkswarm').value('user', #{render "json/current_user"}) diff --git a/spec/features/consumer/authentication_spec.rb b/spec/features/consumer/authentication_spec.rb index 77b60290d1..76e4a20877 100644 --- a/spec/features/consumer/authentication_spec.rb +++ b/spec/features/consumer/authentication_spec.rb @@ -5,7 +5,27 @@ feature "Authentication", js: true do describe "login" do let(:user) { create(:user, password: "password", password_confirmation: "password") } - describe "newskool" do + describe "With redirects" do + scenario "logging in with a redirect set" do + visit groups_path(anchor: "login?after_login=#{producers_path}") + fill_in "Email", with: user.email + fill_in "Password", with: user.password + click_login_button + page.should have_content "Select a producer from the list below" + current_path.should == producers_path + end + + scenario "logging into admin redirects home, then back to admin" do + visit spree.admin_path + fill_in "Email", with: user.email + fill_in "Password", with: user.password + click_login_button + page.should have_content "Dashboard" + current_path.should == spree.admin_path + end + end + + describe "Loggin in from the home page" do before do visit root_path end From f997026796896aea652967cc249e86e56ccecd41 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 21 May 2014 16:54:48 +1000 Subject: [PATCH 32/32] Reworking the JS specs --- .../product_node_controller.js.coffee | 1 + .../controllers/products_controller.js.coffee | 2 - .../darkswarm/services/product.js.coffee | 3 + .../hub_node_controller_spec.js.coffee | 11 ---- .../product_node_controller_spec.js.coffee | 24 +++++++ .../products_controller_spec.js.coffee | 64 ++++++------------- .../darkswarm/services/order_spec.js.coffee | 2 + .../{ => services}/product_spec.js.coffee | 2 +- 8 files changed, 50 insertions(+), 59 deletions(-) create mode 100644 spec/javascripts/unit/darkswarm/controllers/products/product_node_controller_spec.js.coffee rename spec/javascripts/unit/darkswarm/{ => services}/product_spec.js.coffee (86%) diff --git a/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee index 3e24f006ae..959423e3ca 100644 --- a/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/products/product_node_controller.js.coffee @@ -1,4 +1,5 @@ Darkswarm.controller "ProductNodeCtrl", ($scope) -> + $scope.price = -> if $scope.product.variants.length > 0 prices = (v.price for v in $scope.product.variants) diff --git a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee index 09df18ca38..323df2fc4e 100644 --- a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee @@ -12,5 +12,3 @@ Darkswarm.controller "ProductsCtrl", ($scope, $rootScope, Product, OrderCycle) - code = e.keyCode || e.which if code == 13 e.preventDefault() - - $scope.productPrice = (product) -> diff --git a/app/assets/javascripts/darkswarm/services/product.js.coffee b/app/assets/javascripts/darkswarm/services/product.js.coffee index ca31d5e9c0..e58b83bacc 100644 --- a/app/assets/javascripts/darkswarm/services/product.js.coffee +++ b/app/assets/javascripts/darkswarm/services/product.js.coffee @@ -2,6 +2,9 @@ Darkswarm.factory 'Product', ($resource) -> new class Product constructor: -> @update() + + # TODO: don't need to scope this into object + # Already on object as far as controller scope is concerned data: products: null loading: true diff --git a/spec/javascripts/unit/darkswarm/controllers/hub_node_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/hub_node_controller_spec.js.coffee index a2d14f5811..cb77cb1703 100644 --- a/spec/javascripts/unit/darkswarm/controllers/hub_node_controller_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers/hub_node_controller_spec.js.coffee @@ -19,14 +19,3 @@ describe "HubNodeCtrl", -> expect(scope.current()).toEqual false scope.hub = {id: 99} expect(scope.current()).toEqual true - - it "knows whether selecting this hub will empty the cart", -> - CurrentHub.id = undefined - expect(scope.emptiesCart()).toEqual false - - CurrentHub.id = 99 - scope.hub.id = 99 - expect(scope.emptiesCart()).toEqual false - - scope.hub.id = 1 - expect(scope.emptiesCart()).toEqual true diff --git a/spec/javascripts/unit/darkswarm/controllers/products/product_node_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/products/product_node_controller_spec.js.coffee new file mode 100644 index 0000000000..4b9e404b9e --- /dev/null +++ b/spec/javascripts/unit/darkswarm/controllers/products/product_node_controller_spec.js.coffee @@ -0,0 +1,24 @@ +describe "ProductNodeCtrl", -> + ctrl = null + scope = null + product = + id: 99 + price: 10.00 + variants: [] + + beforeEach -> + module('Darkswarm') + inject ($controller) -> + scope = + product: product + ctrl = $controller 'ProductNodeCtrl', {$scope: scope} + + describe "determining the price to display for a product", -> + it "displays the product price when the product does not have variants", -> + expect(scope.price()).toEqual 10.00 + + it "displays the minimum variant price when the product has variants", -> + scope.product = + price: 11 + variants: [{price: 22}, {price: 33}] + expect(scope.price()).toEqual 22 diff --git a/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee index 269c8385b8..685d495039 100644 --- a/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers/products_controller_spec.js.coffee @@ -1,47 +1,21 @@ -describe 'All controllers', -> - describe 'ProductsCtrl', -> - ctrl = null - scope = null - event = null - Product = null +describe 'ProductsCtrl', -> + ctrl = null + scope = null + event = null + Product = null - beforeEach -> - module('Darkswarm') - Product = - all: -> - update: -> - data: "testy mctest" - OrderCycle = - order_cycle: {} - - inject ($controller) -> - scope = {} - ctrl = $controller 'ProductsCtrl', {$scope: scope, Product: Product, OrderCycle: OrderCycle} - - it 'fetches products from Product', -> - expect(scope.data).toEqual 'testy mctest' - - describe "determining the price to display for a product", -> - it "displays the product price when the product does not have variants", -> - product = {variants: [], price: 12.34} - expect(scope.productPrice(product)).toEqual 12.34 - - it "displays the minimum variant price when the product has variants", -> - product = - price: 11 - variants: [{price: 22}, {price: 33}] - expect(scope.productPrice(product)).toEqual 22 - - describe 'OrderCycleCtrl', -> - ctrl = null - scope = null - event = null - product_ctrl = null - OrderCycle = null - - beforeEach -> - module 'Darkswarm' + beforeEach -> + module('Darkswarm') + Product = + all: -> + update: -> + data: "testy mctest" + OrderCycle = + order_cycle: {} + + inject ($controller) -> scope = {} - inject ($controller) -> - scope = {} - ctrl = $controller 'OrderCycleCtrl', {$scope: scope} + ctrl = $controller 'ProductsCtrl', {$scope: scope, Product: Product, OrderCycle: OrderCycle} + + it 'fetches products from Product', -> + expect(scope.data).toEqual 'testy mctest' diff --git a/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee index 0462e92952..fedcf23629 100644 --- a/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee @@ -26,8 +26,10 @@ describe 'Order service', -> } angular.module('Darkswarm').value('order', orderData) module 'Darkswarm' + inject ($injector, _$httpBackend_)-> $httpBackend = _$httpBackend_ + $httpBackend.expectGET("/shop/products").respond 200, [] Order = $injector.get("Order") Navigation = $injector.get("Navigation") flash = $injector.get("flash") diff --git a/spec/javascripts/unit/darkswarm/product_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee similarity index 86% rename from spec/javascripts/unit/darkswarm/product_spec.js.coffee rename to spec/javascripts/unit/darkswarm/services/product_spec.js.coffee index 7108d819af..260074a981 100644 --- a/spec/javascripts/unit/darkswarm/product_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/product_spec.js.coffee @@ -10,5 +10,5 @@ describe 'Product service', -> it "Fetches products from the backend on init", -> $httpBackend.expectGET("/shop/products").respond([{test : "cats"}]) - products = Product.all() $httpBackend.flush() + expect(Product.data.products[0].test).toEqual "cats"