| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 | class CustomField < ActiveRecord::Base
  extend Enumerize
  belongs_to :workgroup
  enumerize :field_type, in: %i{list}
  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"]
      klass_name = field_type && "CustomField::Value::#{field_type.classify}"
      klass = klass_name && const_defined?(klass_name) ? klass_name.constantize : CustomField::Value::Base
      klass.new owner, custom_field, value
    end
    class Base
      def initialize owner, custom_field, value
        @custom_field = custom_field
        @raw_value = value
        @owner = owner
        @errors = []
        @validated = false
        @valid = false
      end
      delegate :code, :name, :field_type, :options, to: :@custom_field
      def validate
        @valid = true
      end
      def valid?
        validate unless @validated
        @valid
      end
      def value
        @raw_value
      end
      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
      def value
        @raw_value.to_i
      end
      def validate
        @valid = true
        unless @raw_value =~ /\A\d*\Z/
          @owner.errors.add errors_key, "'#{@raw_value}' is not a valid integer"
          @valid = false
        end
      end
    end
    class String < Base
      def value
        "#{@raw_value}"
      end
    end
  end
end
 |