From 4f3d468db6adeb1bbab5008711c6efc85ff46064 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Tue, 24 Jun 2014 16:34:14 +1000 Subject: [PATCH] Dereferencing groups and adding modals to page --- .../active_table_hub_link.js.coffee | 2 +- .../directives/empties_cart.js.coffee | 2 +- .../darkswarm/directives/hub_modal.js.coffee | 7 ++++ .../darkswarm/services/dereferencer.js.coffee | 6 ++++ .../darkswarm/services/enterprises.js.coffee | 12 ++----- .../darkswarm/services/groups.js.coffee | 14 ++++++-- .../darkswarm/services/map_modal.js.coffee | 2 +- ...odal_hub.html.haml => hub_modal.html.haml} | 0 .../partials/enterprise_header.html.haml | 3 +- app/views/groups/index.html.haml | 5 +-- app/views/json/_groups.rabl | 2 +- spec/controllers/groups_controller_spec.rb | 8 +++++ spec/features/consumer/groups_spec.rb | 21 ++++++++++++ spec/features/consumer/home_spec.rb | 6 ++-- .../darkswarm/services/groups_spec.js.coffee | 33 +++++++++++++++++++ spec/support/request/ui_component_helper.rb | 10 ++++++ 16 files changed, 111 insertions(+), 22 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/directives/hub_modal.js.coffee create mode 100644 app/assets/javascripts/darkswarm/services/dereferencer.js.coffee rename app/assets/javascripts/templates/{map_modal_hub.html.haml => hub_modal.html.haml} (100%) create mode 100644 spec/features/consumer/groups_spec.rb create mode 100644 spec/javascripts/unit/darkswarm/services/groups_spec.js.coffee diff --git a/app/assets/javascripts/darkswarm/directives/active_table_hub_link.js.coffee b/app/assets/javascripts/darkswarm/directives/active_table_hub_link.js.coffee index ff220d0d5e..4369fced34 100644 --- a/app/assets/javascripts/darkswarm/directives/active_table_hub_link.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/active_table_hub_link.js.coffee @@ -6,7 +6,7 @@ Darkswarm.directive "activeTableHubLink", (CurrentHub, CurrentOrder) -> link: (scope, elm, attr)-> # Swap out the text of the hub link depending on whether it'll change current hub # To be used with ofnEmptiesCart - if CurrentHub.hub.id and CurrentHub.hub.id isnt scope.hub.id + if CurrentHub.hub?.id and CurrentHub.hub.id isnt scope.hub.id scope.action = attr.change else scope.action = attr.shop diff --git a/app/assets/javascripts/darkswarm/directives/empties_cart.js.coffee b/app/assets/javascripts/darkswarm/directives/empties_cart.js.coffee index b71196c40c..75e88f3682 100644 --- a/app/assets/javascripts/darkswarm/directives/empties_cart.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/empties_cart.js.coffee @@ -3,7 +3,7 @@ Darkswarm.directive "ofnEmptiesCart", (CurrentHub, CurrentOrder, Navigation, sto link: (scope, elm, attr)-> hub = scope.$eval(attr.ofnEmptiesCart) # A hub is selected, we're changing to a different hub, and the cart isn't empty - if CurrentHub.hub.id and CurrentHub.hub.id isnt hub.id + if CurrentHub.hub?.id and CurrentHub.hub.id isnt hub.id unless CurrentOrder.empty() elm.bind 'click', (ev)-> ev.preventDefault() diff --git a/app/assets/javascripts/darkswarm/directives/hub_modal.js.coffee b/app/assets/javascripts/darkswarm/directives/hub_modal.js.coffee new file mode 100644 index 0000000000..4810cdbcf0 --- /dev/null +++ b/app/assets/javascripts/darkswarm/directives/hub_modal.js.coffee @@ -0,0 +1,7 @@ +Darkswarm.directive "hubModal", ($modal)-> + restrict: 'E' + replace: true + template: "{{enterprise.name}}" + link: (scope, elem, attrs, ctrl)-> + elem.on "click", => + scope.modalInstance = $modal.open(controller: ctrl, templateUrl: 'hub_modal.html', scope: scope) diff --git a/app/assets/javascripts/darkswarm/services/dereferencer.js.coffee b/app/assets/javascripts/darkswarm/services/dereferencer.js.coffee new file mode 100644 index 0000000000..2061f95ea1 --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/dereferencer.js.coffee @@ -0,0 +1,6 @@ +Darkswarm.factory 'Dereferencer', -> + new class Dereferencer + dereference: (array, data)-> + if array + for object, i in array + array[i] = data[object.id] diff --git a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee index c3721f4896..b442773d31 100644 --- a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee +++ b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.factory 'Enterprises', (enterprises, CurrentHub)-> +Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Dereferencer)-> new class Enterprises enterprises_by_id: {} # id/object pairs for lookup constructor: -> @@ -10,12 +10,6 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub)-> dereference: -> if CurrentHub.hub?.id CurrentHub.hub = @enterprises_by_id[CurrentHub.hub.id] - for enterprise in @enterprises - if enterprise.hubs - for hub, i in enterprise.hubs - enterprise.hubs[i] = @enterprises_by_id[hub.id] - - if enterprise.producers - for producer, i in enterprise.producers - enterprise.producers[i] = @enterprises_by_id[producer.id] + Dereferencer.dereference enterprise.hubs, @enterprises_by_id + Dereferencer.dereference enterprise.producers, @enterprises_by_id diff --git a/app/assets/javascripts/darkswarm/services/groups.js.coffee b/app/assets/javascripts/darkswarm/services/groups.js.coffee index e5e50615e8..e07d6c2055 100644 --- a/app/assets/javascripts/darkswarm/services/groups.js.coffee +++ b/app/assets/javascripts/darkswarm/services/groups.js.coffee @@ -1,4 +1,14 @@ -Darkswarm.factory 'Groups', (groups) -> +Darkswarm.factory 'Groups', (groups, Enterprises, Dereferencer) -> new class Groups + groups: groups + groups_by_id: {} constructor: -> - @groups = groups + for group in @groups + @groups_by_id[group.id] = group + @dereference() + dereference: -> + for group in @groups + Dereferencer.dereference group.enterprises, Enterprises.enterprises_by_id + for enterprise in Enterprises.enterprises + Dereferencer.dereference enterprise.groups, @groups_by_id + diff --git a/app/assets/javascripts/darkswarm/services/map_modal.js.coffee b/app/assets/javascripts/darkswarm/services/map_modal.js.coffee index 1c9bf13bb3..c9ed30f558 100644 --- a/app/assets/javascripts/darkswarm/services/map_modal.js.coffee +++ b/app/assets/javascripts/darkswarm/services/map_modal.js.coffee @@ -6,7 +6,7 @@ Darkswarm.factory "MapModal", ($modal, $rootScope)-> scope.enterprise = enterprise if enterprise.is_distributor scope.hub = enterprise - $modal.open(templateUrl: "map_modal_hub.html", scope: scope) + $modal.open(templateUrl: "hub_modal.html", scope: scope) else scope.producer = enterprise $modal.open(templateUrl: "map_modal_producer.html", scope: scope) diff --git a/app/assets/javascripts/templates/map_modal_hub.html.haml b/app/assets/javascripts/templates/hub_modal.html.haml similarity index 100% rename from app/assets/javascripts/templates/map_modal_hub.html.haml rename to app/assets/javascripts/templates/hub_modal.html.haml diff --git a/app/assets/javascripts/templates/partials/enterprise_header.html.haml b/app/assets/javascripts/templates/partials/enterprise_header.html.haml index 2e7da13794..698a5c6be6 100644 --- a/app/assets/javascripts/templates/partials/enterprise_header.html.haml +++ b/app/assets/javascripts/templates/partials/enterprise_header.html.haml @@ -3,6 +3,7 @@ %p.right {{ [enterprise.address.city, enterprise.address.state] | printArray}} %h3 - %i.ofn-i_036-producers + %i.ofn-i_036-producers{"ng-show" => "!enterprise.is_distributor"} + %i.ofn-i_040-hub{"ng-show" => "enterprise.is_distributor"} {{ enterprise.name }} %img.hero-img{"ng-src" => "{{enterprise.promo_image}}"} diff --git a/app/views/groups/index.html.haml b/app/views/groups/index.html.haml index 7872bef239..bf94e690ab 100644 --- a/app/views/groups/index.html.haml +++ b/app/views/groups/index.html.haml @@ -36,10 +36,11 @@ .small-6.columns %p {{ group.long_description }} .small-6.columns - %h5 Our hubs & producers + %h5 Our hubs & producers %ul.small-block-grid-2 %li{"ng-repeat" => "enterprise in group.enterprises"} - %a{"bo-href" => "enterprise.path"} {{ enterprise.name }} + %hub-modal{"ng-if" => "enterprise.is_distributor"} + %producer-modal{"ng-if" => "!enterprise.is_distributor"} .row.group_footer .small-12.columns diff --git a/app/views/json/_groups.rabl b/app/views/json/_groups.rabl index 27325bb850..691d36f42f 100644 --- a/app/views/json/_groups.rabl +++ b/app/views/json/_groups.rabl @@ -2,7 +2,7 @@ collection @groups attributes :id, :name, :position, :description, :long_description child enterprises: :enterprises do - extends 'json/enterprises' + attributes :id end node :logo do |group| diff --git a/spec/controllers/groups_controller_spec.rb b/spec/controllers/groups_controller_spec.rb index 4005d82a2a..67193cb9a0 100644 --- a/spec/controllers/groups_controller_spec.rb +++ b/spec/controllers/groups_controller_spec.rb @@ -1,9 +1,17 @@ require 'spec_helper' describe GroupsController do + render_views + let(:enterprise) { create(:distributor_enterprise) } + let!(:group) { create(:enterprise_group, enterprises: [enterprise], on_front_page: true) } it "gets all visible groups" do EnterpriseGroup.stub_chain :on_front_page, :by_position EnterpriseGroup.should_receive :on_front_page get :index end + + it "loads all enterprises for group" do + get :index + response.body.should have_text enterprise.id + end end diff --git a/spec/features/consumer/groups_spec.rb b/spec/features/consumer/groups_spec.rb new file mode 100644 index 0000000000..7baa5a2807 --- /dev/null +++ b/spec/features/consumer/groups_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +feature 'Groups', js: true do + include AuthenticationWorkflow + include UIComponentHelper + + let(:enterprise) { create(:distributor_enterprise) } + let!(:group) { create(:enterprise_group, enterprises: [enterprise], on_front_page: true) } + + it "renders groups" do + visit groups_path + page.should have_content group.name + end + + it "renders enterprise modals for groups" do + visit groups_path + page.should have_content enterprise.name + open_enterprise_modal enterprise + modal_should_be_open_for enterprise + end +end diff --git a/spec/features/consumer/home_spec.rb b/spec/features/consumer/home_spec.rb index d8d3d0a3e7..c6ac4af2ba 100644 --- a/spec/features/consumer/home_spec.rb +++ b/spec/features/consumer/home_spec.rb @@ -36,9 +36,7 @@ feature 'Home', js: true do it "should show hub producer modals" do expand_active_table_node distributor.name page.should have_content producer.name - find("a", text: producer.name).click - within ".reveal-modal" do - page.should have_content producer.name - end + open_enterprise_modal producer + modal_should_be_open_for producer end end diff --git a/spec/javascripts/unit/darkswarm/services/groups_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/groups_spec.js.coffee new file mode 100644 index 0000000000..abd9c5c617 --- /dev/null +++ b/spec/javascripts/unit/darkswarm/services/groups_spec.js.coffee @@ -0,0 +1,33 @@ +describe "Groups service", -> + Groups = null + Enterprises = null + CurrentHubMock = {} + groups = [{ + id: 1 + name: "Test Group" + enterprises: [ + {id: 1}, + {id: 2} + ] + }] + enterprises = [ + {id: 1, name: "Test 1", groups: [{id: 1}]}, + {id: 2, name: "Test 2", groups: [{id: 1}]} + ] + + beforeEach -> + module 'Darkswarm' + angular.module('Darkswarm').value('groups', groups) + angular.module('Darkswarm').value('enterprises', enterprises) + module ($provide)-> + $provide.value "CurrentHub", CurrentHubMock + null + inject (_Groups_, _Enterprises_)-> + Groups = _Groups_ + Enterprises = _Enterprises_ + + it "dereferences group enterprises", -> + expect(Groups.groups[0].enterprises[0]).toBe enterprises[0] + + it "dereferences enterprise groups", -> + expect(Enterprises.enterprises[0].groups[0]).toBe groups[0] diff --git a/spec/support/request/ui_component_helper.rb b/spec/support/request/ui_component_helper.rb index 269f28cca1..ebf95548d6 100644 --- a/spec/support/request/ui_component_helper.rb +++ b/spec/support/request/ui_component_helper.rb @@ -38,6 +38,16 @@ module UIComponentHelper have_selector ".login-modal" end + def open_enterprise_modal(enterprise) + find("a", text: enterprise.name).click + end + + def modal_should_be_open_for(enterprise) + within ".reveal-modal" do + page.should have_content enterprise.name + end + end + def have_reset_password have_content "An email with instructions on resetting your password has been sent!" end