diff --git a/app/assets/javascripts/darkswarm/services/groups.js.coffee b/app/assets/javascripts/darkswarm/services/groups.js.coffee index e07d6c2055..69a7d2f564 100644 --- a/app/assets/javascripts/darkswarm/services/groups.js.coffee +++ b/app/assets/javascripts/darkswarm/services/groups.js.coffee @@ -1,14 +1,3 @@ -Darkswarm.factory 'Groups', (groups, Enterprises, Dereferencer) -> +Darkswarm.factory 'Groups', (groups) -> new class Groups groups: groups - groups_by_id: {} - constructor: -> - 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/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index ce72038e70..6dcc582f86 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -1,10 +1,6 @@ class GroupsController < BaseController layout 'darkswarm' - def index - @groups = EnterpriseGroup.on_front_page.by_position - end - def show enable_embedded_shopfront @hide_menu = true if @shopfront_layout == 'embedded' diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 87f3efe1d7..921648544c 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -12,6 +12,16 @@ module InjectionHelper ) end + def inject_groups + select_only = required_attributes EnterpriseGroup, Api::GroupListSerializer + + inject_json_ams( + 'groups', + EnterpriseGroup.on_front_page.by_position.select(select_only).includes(address: :state).all, + Api::GroupListSerializer + ) + end + def inject_enterprise_shopfront_list select_only = required_attributes Enterprise, Api::EnterpriseShopfrontListSerializer diff --git a/app/serializers/api/group_list_serializer.rb b/app/serializers/api/group_list_serializer.rb new file mode 100644 index 0000000000..46e851ab4f --- /dev/null +++ b/app/serializers/api/group_list_serializer.rb @@ -0,0 +1,16 @@ +module Api + class GroupListSerializer < ActiveModel::Serializer + attributes :id, :name, :permalink, :email, :website, :facebook, :instagram, + :linkedin, :twitter, :enterprises, :state, :address_id + + def state + object.address.state.abbr + end + + def enterprises + ActiveModel::ArraySerializer.new( + object.enterprises, each_serializer: Api::EnterpriseThinSerializer + ) + end + end +end diff --git a/app/views/groups/index.html.haml b/app/views/groups/index.html.haml index ad93d4f366..dabfd70b6c 100644 --- a/app/views/groups/index.html.haml +++ b/app/views/groups/index.html.haml @@ -1,10 +1,7 @@ - content_for(:title) do = t :groups_title -= inject_enterprises - -:javascript - angular.module('Darkswarm').value('groups', #{render partial: "json/groups", object: @groups}) += inject_groups #groups.pad-top.footer-pad{"ng-controller" => "GroupsCtrl"} .row diff --git a/spec/serializers/api/group_list_serializer_spec.rb b/spec/serializers/api/group_list_serializer_spec.rb new file mode 100644 index 0000000000..df69cf75c1 --- /dev/null +++ b/spec/serializers/api/group_list_serializer_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe Api::GroupListSerializer do + let!(:group) { create(:enterprise_group) } + let!(:producer) { create(:supplier_enterprise) } + + let(:serializer) { Api::GroupListSerializer.new group } + + before do + group.enterprises << producer + end + + it "serializes group attributes" do + expect(serializer.serializable_hash[:name]).to match group.name + end + + it "serializes abbreviated state" do + expect(serializer.serializable_hash[:state]).to eq group.address.state.abbr + end + + it "serialises an array of enterprises" do + expect(serializer.serializable_hash[:enterprises]).to be_a ActiveModel::ArraySerializer + expect(serializer.serializable_hash[:enterprises].to_json).to match producer.name + end +end