mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Fix groups index page
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
16
app/serializers/api/group_list_serializer.rb
Normal file
16
app/serializers/api/group_list_serializer.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
25
spec/serializers/api/group_list_serializer_spec.rb
Normal file
25
spec/serializers/api/group_list_serializer_spec.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user