diff options
| -rw-r--r-- | app/models/concerns/custom_fields_support.rb | 4 | ||||
| -rw-r--r-- | app/models/custom_field.rb | 17 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 5 |
3 files changed, 20 insertions, 6 deletions
diff --git a/app/models/concerns/custom_fields_support.rb b/app/models/concerns/custom_fields_support.rb index d562cf227..6c76bd653 100644 --- a/app/models/concerns/custom_fields_support.rb +++ b/app/models/concerns/custom_fields_support.rb @@ -9,9 +9,7 @@ module CustomFieldsSupport end def custom_fields - HashWithIndifferentAccess[*self.class.custom_fields.map do |v| - [v.code, CustomField::Value.new(self, v, custom_field_value(v.code))] - end.flatten] + CustomField::Collection.new self end def custom_field_value key diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 279f7cb39..4a840744e 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -7,6 +7,19 @@ class CustomField < ActiveRecord::Base validates :name, uniqueness: {scope: [:resource_type, :workgroup_id]} validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false} + class Collection < HashWithIndifferentAccess + def initialize object + vals = object.class.custom_fields.map do |v| + [v.code, CustomField::Value.new(object, v, object.custom_field_value(v.code))] + end + super Hash[*vals.flatten] + end + + def to_hash + HashWithIndifferentAccess[*self.map{|k, v| [k, v.to_hash]}.flatten(1)] + end + end + class Value def self.new owner, custom_field, value field_type = custom_field.options["field_type"] @@ -43,6 +56,10 @@ class CustomField < ActiveRecord::Base def errors_key "custom_fields.#{code}" end + + def to_hash + HashWithIndifferentAccess[*%w(code name field_type options value).map{|k| [k, send(k)]}.flatten(1)] + end end class Integer < Base diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 76e73d9cf..c69655bd4 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -260,7 +260,7 @@ describe Chouette::VehicleJourney, :type => :model do item['purchase_windows'] = [] item['footnotes'] = [] item['purchase_windows'] = [] - item['custom_fields'] = vj.custom_fields + item['custom_fields'] = vj.custom_fields.to_hash vj.vehicle_journey_at_stops.each do |vjas| item['vehicle_journey_at_stops'] << vehicle_journey_at_stop_to_state(vjas) @@ -282,7 +282,6 @@ describe Chouette::VehicleJourney, :type => :model do Chouette::VehicleJourney.state_update(route, collection) }.to change {Chouette::VehicleJourney.count}.by(1) - obj = Chouette::VehicleJourney.last expect(obj).to receive(:after_commit_objectid).and_call_original @@ -292,7 +291,7 @@ describe Chouette::VehicleJourney, :type => :model do expect(collection.last['objectid']).to eq obj.objectid expect(obj.published_journey_name).to eq 'dummy' - expect(obj.custom_fields["energy"]["value"]).to eq 99 + expect(obj.custom_fields["energy"].value).to eq 99 end it 'should expect local times' do |
