aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/custom_field.rb
diff options
context:
space:
mode:
authorZog2018-03-16 13:04:38 +0100
committerJohan Van Ryseghem2018-04-04 11:12:34 +0200
commit6d15b7ec25592dc34cd95c738ef0854fdd1b94d2 (patch)
tree29660f2f43956e39fe00ee2dadde4d7d234a4c56 /app/models/custom_field.rb
parentf0f619353c23775613fe154cb40d54cb55c979bb (diff)
downloadchouette-core-6d15b7ec25592dc34cd95c738ef0854fdd1b94d2.tar.bz2
Refs #6196; Add Custom Fields to companies
Diffstat (limited to 'app/models/custom_field.rb')
-rw-r--r--app/models/custom_field.rb15
1 files changed, 10 insertions, 5 deletions
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index 9fcfa9cd9..1db351135 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -5,13 +5,13 @@ class CustomField < ActiveRecord::Base
enumerize :field_type, in: %i{list integer string attachment}
validates :name, uniqueness: {scope: [:resource_type, :workgroup_id]}
- validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false}
+ validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false}, presence: true
scope :for_workgroup, ->(workgroup){ where workgroup_id: workgroup.id }
class Collection < HashWithIndifferentAccess
- def initialize object
- vals = object.class.custom_fields.map do |v|
+ 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
super Hash[*vals.flatten]
@@ -166,14 +166,19 @@ class CustomField < ActiveRecord::Base
end
def display_value
- options["list_values"][value.to_i]
+ return unless value
+ k = options["list_values"].is_a?(Hash) ? value.to_s : value.to_i
+ options["list_values"][k]
end
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)
super.update({
selected: value,
- collection: options["list_values"].map(&:reverse)
+ collection: collection
})
end
end