diff options
| author | Zog | 2018-04-24 09:04:13 +0200 | 
|---|---|---|
| committer | Zog | 2018-04-24 09:04:13 +0200 | 
| commit | d521797a4ca3e668934c6a767f0c2b357824107d (patch) | |
| tree | 6ff8551d916f9dabbf6c8661b0dcf5734e6f5bf5 | |
| parent | 38b338af7d3ec9416b797a12be938ed17bc20dd6 (diff) | |
| download | chouette-core-d521797a4ca3e668934c6a767f0c2b357824107d.tar.bz2 | |
Refs #6669; Fix CustomField initialization
Use the current referential to infer the workgroup
| -rw-r--r-- | app/models/application_model.rb | 16 | ||||
| -rw-r--r-- | app/models/concerns/custom_fields_support.rb | 16 | ||||
| -rw-r--r-- | app/models/custom_field.rb | 2 | ||||
| -rw-r--r-- | spec/models/custom_field_spec.rb | 36 | 
4 files changed, 55 insertions, 15 deletions
| diff --git a/app/models/application_model.rb b/app/models/application_model.rb index 1a2a5099d..1715e06c4 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -2,4 +2,20 @@ class ApplicationModel < ::ActiveRecord::Base    include MetadataSupport    self.abstract_class = true + +  def self.referential +    Referential.where(slug: Apartment::Tenant.current).last +  end + +  def referential +    self.class.referential +  end + +  def self.workgroup +    referential&.workgroup +  end + +  def workgroup +    self.class.workgroup +  end  end diff --git a/app/models/concerns/custom_fields_support.rb b/app/models/concerns/custom_fields_support.rb index c39dfd1fc..8d4b37657 100644 --- a/app/models/concerns/custom_fields_support.rb +++ b/app/models/concerns/custom_fields_support.rb @@ -5,13 +5,15 @@ module CustomFieldsSupport      validate :custom_fields_values_are_valid      after_initialize :initialize_custom_fields -    def self.custom_fields workgroup=:all +    def self.custom_fields _workgroup=nil +      _workgroup ||= self.workgroup +      return [] unless _workgroup        fields = CustomField.where(resource_type: self.name.split("::").last) -      fields = fields.where(workgroup_id: workgroup&.id) if workgroup != :all +      fields = fields.where(workgroup_id: _workgroup.id)        fields      end -    def self.custom_fields_definitions workgroup=:all +    def self.custom_fields_definitions workgroup=nil        Hash[*custom_fields(workgroup).map{|cf| [cf.code, cf]}.flatten]      end @@ -24,7 +26,7 @@ module CustomFieldsSupport        end      end -    def custom_fields workgroup=:all +    def custom_fields workgroup=nil        CustomField::Collection.new self, workgroup      end @@ -43,8 +45,8 @@ module CustomFieldsSupport      def initialize_custom_fields        return unless self.attributes.has_key?("custom_field_values")        self.custom_field_values ||= {} -      custom_fields(:all).values.each &:initialize_custom_field -      custom_fields(:all).each do |k, v| +      custom_fields.values.each &:initialize_custom_field +      custom_fields.each do |k, v|          custom_field_values[k] ||= v.default_value        end        @custom_fields_initialized = true @@ -56,7 +58,7 @@ module CustomFieldsSupport      private      def custom_fields_values_are_valid -      custom_fields(:all).values.all?{|cf| cf.valid?} +      custom_fields.values.all?{|cf| cf.valid?}      end    end  end diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 88783b5b4..b5c08465d 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -8,7 +8,7 @@ class CustomField < ApplicationModel    validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false}, presence: true    class Collection < HashWithIndifferentAccess -    def initialize object, workgroup=:all +    def initialize object, workgroup=nil        vals = object.class.custom_fields(workgroup).map do |v|          [v.code, CustomField::Instance.new(object, v, object.custom_field_value(v.code))]        end diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb index ce6ce9fa5..b0aa0ff1e 100644 --- a/spec/models/custom_field_spec.rb +++ b/spec/models/custom_field_spec.rb @@ -3,6 +3,11 @@ require 'rails_helper'  RSpec.describe CustomField, type: :model do    let( :vj ){ create :vehicle_journey, custom_field_values: {energy: 99} } +  let(:referential){ create :workbench_referential } +  let(:workgroup){ referential.workgroup } +  before do +    referential.switch +  end    context "validates" do      it { should validate_uniqueness_of(:name).scoped_to(:resource_type, :workgroup_id) } @@ -18,7 +23,7 @@ RSpec.describe CustomField, type: :model do    end    context "custom fields for a resource" do -    let!( :fields ){ [create(:custom_field), create(:custom_field, code: :energy)] } +    let!( :fields ){ [create(:custom_field, workgroup: workgroup), create(:custom_field, code: :energy, workgroup: workgroup)] }      let!( :instance_fields ){        {          fields[0].code => fields[0].slice(:code, :name, :field_type, :options).update(value: nil), @@ -37,14 +42,31 @@ RSpec.describe CustomField, type: :model do    context "custom field_values for a resource" do      before do -      create :custom_field, field_type: :integer, code: :energy, name: :energy +      create :custom_field, field_type: :integer, code: :energy, name: :energy, workgroup: workgroup      end      it { expect(vj.custom_field_value("energy")).to eq(99) } + +    context "given different workgroups" do +      let(:ref1){ create :workbench_referential } +      let(:ref2){ create :workbench_referential } +      before do +        create :custom_field, field_type: :integer, code: :ref1_energy, name: :energy, workgroup: ref1.workgroup, options: {default: 12} +        create :custom_field, field_type: :integer, code: :ref2_energy, name: :energy, workgroup: ref2.workgroup +      end +      it "should only initialize fields from the right workgroup" do +        ref1.switch +        expect(Chouette::VehicleJourney.new.custom_fields.keys).to eq ["ref1_energy"] +        expect(Chouette::VehicleJourney.new.custom_field_values["ref1_energy"]).to eq 12 +        ref2.switch +        expect(Chouette::VehicleJourney.new.custom_fields.keys).to eq ["ref2_energy"] +        expect(Chouette::VehicleJourney.new.custom_field_values).to_not have_key "ref1_energy" +      end +    end    end    context "with a 'list' field_type" do -    let!(:field){ [create(:custom_field, code: :energy, field_type: 'list', options: {list_values: %w(foo bar baz)})] } +    let!(:field){ [create(:custom_field, code: :energy, field_type: 'list', options: {list_values: %w(foo bar baz)}, workgroup: workgroup)] }      let!( :vj ){ create :vehicle_journey, custom_field_values: {energy: "1"} }      it "should cast the value" do        expect(vj.custom_fields[:energy].value).to eq 1 @@ -75,7 +97,7 @@ RSpec.describe CustomField, type: :model do    end    context "with an 'integer' field_type" do -    let!(:field){ [create(:custom_field, code: :energy, field_type: 'integer')] } +    let!(:field){ [create(:custom_field, code: :energy, field_type: 'integer', workgroup: workgroup)] }      let!( :vj ){ create :vehicle_journey, custom_field_values: {energy: "99"} }      it "should cast the value" do        expect(vj.custom_fields[:energy].value).to eq 99 @@ -101,7 +123,7 @@ RSpec.describe CustomField, type: :model do    end    context "with a 'string' field_type" do -    let!(:field){ [create(:custom_field, code: :energy, field_type: 'string')] } +    let!(:field){ [create(:custom_field, code: :energy, field_type: 'string', workgroup: workgroup)] }      let!( :vj ){ create :vehicle_journey, custom_field_values: {energy: 99} }      it "should cast the value" do        expect(vj.custom_fields[:energy].value).to eq '99' @@ -109,7 +131,7 @@ RSpec.describe CustomField, type: :model do    end    context "with a 'attachment' field_type" do -    let!(:field){ [create(:custom_field, code: :energy, field_type: 'attachment')] } +    let!(:field){ [create(:custom_field, code: :energy, field_type: 'attachment', workgroup: workgroup)] }      let( :vj ){ create :vehicle_journey, custom_field_values: {energy: File.open(Rails.root.join('spec', 'fixtures', 'users.json'))} }      it "should cast the value" do        expect(vj.custom_fields[:energy].value.class).to be CustomFieldAttachmentUploader @@ -129,7 +151,7 @@ RSpec.describe CustomField, type: :model do      end      context "with a whitelist" do -      let!(:field){ [create(:custom_field, code: :energy, field_type: 'attachment', options: {extension_whitelist: %w(zip)})] } +      let!(:field){ [create(:custom_field, code: :energy, field_type: 'attachment', options: {extension_whitelist: %w(zip)}, workgroup: workgroup)] }        it "should validate extension" do          expect(build(:vehicle_journey, custom_field_values: {energy: File.open(Rails.root.join('spec', 'fixtures', 'users.json'))})).to_not be_valid          expect(build(:vehicle_journey, custom_field_values: {energy: File.open(Rails.root.join('spec', 'fixtures', 'nozip.zip'))})).to be_valid | 
