aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/models/concerns/custom_fields_support.rb22
-rw-r--r--app/models/custom_field.rb4
-rw-r--r--app/views/stop_areas/_form.html.slim5
3 files changed, 20 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