diff options
| author | Zog | 2018-04-04 11:59:23 +0200 |
|---|---|---|
| committer | Johan Van Ryseghem | 2018-04-04 12:21:56 +0200 |
| commit | 95fbdc197b71610dba005578533ea93753a525c9 (patch) | |
| tree | 7ec9a1851f23833a533839c077d0b685c3acbd38 | |
| parent | 103fb7e051898e486d15a61d0b85356466b2c544 (diff) | |
| download | chouette-core-95fbdc197b71610dba005578533ea93753a525c9.tar.bz2 | |
Small fixes on Custom fields
| -rw-r--r-- | app/models/concerns/custom_fields_support.rb | 22 | ||||
| -rw-r--r-- | app/models/custom_field.rb | 4 | ||||
| -rw-r--r-- | app/views/stop_areas/_form.html.slim | 5 | ||||
| -rw-r--r-- | config/locales/stop_areas.en.yml | 1 | ||||
| -rw-r--r-- | config/locales/stop_areas.fr.yml | 1 |
5 files changed, 22 insertions, 11 deletions
diff --git a/app/models/concerns/custom_fields_support.rb b/app/models/concerns/custom_fields_support.rb index 152f26256..ef6b4f4af 100644 --- a/app/models/concerns/custom_fields_support.rb +++ b/app/models/concerns/custom_fields_support.rb @@ -5,13 +5,22 @@ module CustomFieldsSupport validate :custom_fields_values_are_valid after_initialize :initialize_custom_fields - def self.custom_fields workgroup=nil + def self.custom_fields workgroup=:all fields = CustomField.where(resource_type: self.name.split("::").last) - fields = fields.where(workgroup_id: workgroup.id) if workgroup.present? + fields = fields.where(workgroup_id: workgroup&.id) if workgroup != :all fields end - def custom_fields workgroup=nil + def method_missing method_name, *args + if method_name =~ /custom_field_*/ && !@custom_fields_initialized + initialize_custom_fields + send method_name, *args + else + super method_name, *args + end + end + + def custom_fields workgroup=:all CustomField::Collection.new self, workgroup end @@ -25,10 +34,11 @@ module CustomFieldsSupport def initialize_custom_fields self.custom_field_values ||= {} - custom_fields.values.each &:initialize_custom_field - custom_fields.each do |k, v| + custom_fields(:all).values.each &:initialize_custom_field + custom_fields(:all).each do |k, v| custom_field_values[k] ||= v.default_value end + @custom_fields_initialized = true end def custom_field_value key @@ -37,7 +47,7 @@ module CustomFieldsSupport private def custom_fields_values_are_valid - custom_fields.values.all?{|cf| cf.valid?} + custom_fields(:all).values.all?{|cf| cf.valid?} end end end diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 60aa25e85..8347d84f9 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -12,7 +12,7 @@ class CustomField < ActiveRecord::Base scope :for_workgroup, ->(workgroup){ where workgroup_id: workgroup.id } class Collection < HashWithIndifferentAccess - def initialize object, workgroup=nil + def initialize object, workgroup=:all vals = object.class.custom_fields(workgroup).map do |v| [v.code, CustomField::Instance.new(object, v, object.custom_field_value(v.code))] end @@ -176,8 +176,8 @@ class CustomField < ActiveRecord::Base class Input < Base::Input def form_input_options collection = options["list_values"] - collection = collection.map(&:reverse) if collection.is_a?(Hash) collection = collection.each_with_index.to_a if collection.is_a?(Array) + collection = collection.map(&:reverse) if collection.is_a?(Hash) super.update({ selected: value, collection: collection diff --git a/app/views/stop_areas/_form.html.slim b/app/views/stop_areas/_form.html.slim index 90dd187ee..00f2ad8bb 100644 --- a/app/views/stop_areas/_form.html.slim +++ b/app/views/stop_areas/_form.html.slim @@ -62,11 +62,10 @@ = f.input :stairs_availability, as: :select, :collection => [[t("true"), true], [t("false"), false]], :include_blank => true = f.input :lift_availability, as: :select, :collection => [[t("true"), true], [t("false"), false]], :include_blank => true - // XXX filter on the workgroup - - if resource.custom_fields.any? + - if resource.custom_fields(resource.stop_area_referential.workgroup).any? .custom_fields h3 = t("stop_areas.stop_area.custom_fields") - - resource.custom_fields.each do |code, field| + - resource.custom_fields(resource.stop_area_referential.workgroup).each do |code, field| = field.input(f).to_s .separator diff --git a/config/locales/stop_areas.en.yml b/config/locales/stop_areas.en.yml index 342fa3c68..ddb2d940c 100644 --- a/config/locales/stop_areas.en.yml +++ b/config/locales/stop_areas.en.yml @@ -18,6 +18,7 @@ en: general: "General" localisation: "Localisation" accessibility: "Accessibility" + custom_fields: "Custom fields" actions: new: "Add a new stop area" create: "Add a new stop area" diff --git a/config/locales/stop_areas.fr.yml b/config/locales/stop_areas.fr.yml index a532bdc40..6a5fbf24b 100644 --- a/config/locales/stop_areas.fr.yml +++ b/config/locales/stop_areas.fr.yml @@ -19,6 +19,7 @@ fr: general: "General" localisation: "Localisation" accessibility: "Accessibilité" + custom_fields: "Champs personnalisés" actions: new: "Ajouter un arrêt" create: "Ajouter un arrêt" |
