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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/code-box/code_attribute.rb', line 49
def code_attribute(*code_names)
options = code_names.
opts = DefaultOptions.merge(options)
lookup_type = opts.delete(:lookup_type)
code_attr_suffix = (opts.delete(:code_attribute_suffix) || "code").to_s
foreign_code_attr_name = opts.delete(:foreign_code_attribute)
enum = opts.delete(:enum)
code_names.each do |code_name|
opts_copy = opts.dup
code_attr_name = (opts_copy.delete(:attribute_name) || "#{code_name}_#{code_attr_suffix}").to_s
code_class_name = opts_copy.delete(:class_name) || "::Codes::#{code_name.to_s.camelize}"
case lookup_type
when :lookup
case enum
when :set
class_eval <<-RUBY_
# getter
def #{code_name}
code_value = #{code_attr_name}
return [] if code_value.nil?
codes = code_value.split(',').map(&:strip)
codes.map{ |code| #{code_class_name}.for_code(code) }
end
# setter
def #{code_name}=(code_obj)
code_objs = Array(code_obj)
value = code_objs.map{ |code_obj| code_obj.#{foreign_code_attr_name} }.join(',')
self.#{code_attr_name} = value
end
# getter raw
# not getter raw - it's defined already
def #{code_attr_name}_list=(codes)
self.#{code_attr_name} = Array(codes).join(',')
end
RUBY_
when :binary
raise ArgumentError, "#:binary is not yet supported enum option"
when false
class_eval <<-RUBY_
# getter
def #{code_name}
#{code_class_name}.for_code(#{code_attr_name})
end
# setter
def #{code_name}=(code)
value = code.#{foreign_code_attr_name}
self.#{code_attr_name} = value
end
RUBY_
else
raise ArgumentError, "#{enum} is not a valid enum: option"
end
when :associated
association_options = opts_copy.merge({
:class_name => "#{code_class_name}",
:foreign_key => "#{code_attr_name}".to_sym,
:primary_key => "#{foreign_code_attr_name}"
})
belongs_to "#{code_name}".to_sym, association_options
when :i18n
class_eval <<-RUBY_
# getter
def #{code_name}(locale=I18n.locale)
code = self.#{code_attr_name}
self.class.translate_#{code_attr_name}(code, :locale => locale)
end
# setter
def #{code_name}=(code)
raise "#{code_name} is a i18n code and can not be set. Use the the correct method '#{code_attr_name}='' instead."
end
# translator
class << self
def translate_#{code_attr_name}(*code)
options = code.extract_options!
locale = options[:locale] || I18n.locale
codes = code.first
is_paramter_array = codes.kind_of? Array
codes = Array(codes)
translated_codes = codes.map { |code|
code_key = code.nil? ? :null_value : code
I18n.t("\#{self._code_box_i18n_model_segment}.values.\#{self.name.underscore}.#{code_attr_name}.\#{code_key}", :locale => locale)
}
if options[:build] == :zip
translated_codes.zip(codes)
else
is_paramter_array ? translated_codes : translated_codes.first
end
end
end
RUBY_
else
raise ArgumentError, "'#{lookup_type}' is not valid. Must be one of [:code_cache, :association]"
end
end
end
|