Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add navision and mounted_attrs to group resource #144

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/resources/sac_cas/group_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
#
# Copyright (c) 2023, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito

module SacCas::GroupResource
extend ActiveSupport::Concern

included do
with_options writable: false do
attribute :navision_id, :integer

Group.subclasses.flat_map(&:mounted_attr_names).each do |attr|
extra_attribute attr, :string do
next if @object.class.mounted_attr_names.exclude?(attr)

@object.send(attr)
end
end
end
end
end
9 changes: 7 additions & 2 deletions lib/hitobito_sac_cas/wagon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ class Wagon < Rails::Engine
Person.include SacCas::Person
Role.prepend SacCas::Role

## Abilities
PersonAbility.include SacCas::PersonAbility

## Decorators
GroupDecorator.prepend SacCas::GroupDecorator

## Abilities
PersonAbility.include SacCas::PersonAbility
## Resources
GroupResource.include SacCas::GroupResource



FilterNavigation::People.send :prepend, SacCas::FilterNavigation::People

Expand Down
39 changes: 39 additions & 0 deletions spec/resources/sac_cas/group_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true
#
# Copyright (c) 2023, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito

require 'spec_helper'

describe GroupResource, type: :resource do
include Rails.application.routes.url_helpers

let(:person) { people(:admin) }
let(:bluemlisalp) { groups(:bluemlisalp) }
let(:geschaeftsstelle) { groups(:geschaeftsstelle) }

it 'includes navision id' do
params[:filter] = { id: { eq: bluemlisalp.id } }
render
expect(jsonapi_data[0].attributes['navision_id']).to eq 1650
end

it 'includes foundation year and section canton as extra attribute' do
bluemlisalp.update!(created_at: 1.day.ago, foundation_year: 1900, section_canton: 'BE')
params[:filter] = { id: { eq: bluemlisalp.id } }
params[:extra_fields] = { groups: 'foundation_year,section_canton' }
render
expect(jsonapi_data[0].attributes['foundation_year']).to eq '1900'
expect(jsonapi_data[0].attributes['section_canton']).to eq 'BE'
end

it 'returns blank values if group does not have underlying mounted attributes' do
params[:filter] = { id: { eq: geschaeftsstelle.id } }
params[:extra_fields] = { groups: 'foundation_year,section_canton' }
render
expect(jsonapi_data[0].attributes['foundation_year']).to be_blank
expect(jsonapi_data[0].attributes['section_canton']).to be_blank
end
end