aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/custom_field.rb
diff options
context:
space:
mode:
authorZog2018-02-26 10:25:49 +0100
committerZog2018-02-26 10:25:49 +0100
commitb9c3c0a47db7d733a80001f2b80ddcb7d69ec852 (patch)
tree78e1073552692e1d529518c7a536c1101865df22 /app/models/custom_field.rb
parent3b84483f292c5b880bb2f72bd185f505dabbc478 (diff)
downloadchouette-core-b9c3c0a47db7d733a80001f2b80ddcb7d69ec852.tar.bz2
Fix specs
Diffstat (limited to 'app/models/custom_field.rb')
-rw-r--r--app/models/custom_field.rb17
1 files changed, 17 insertions, 0 deletions
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