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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/code-box/acts_as_code.rb', line 57
def acts_as_code(*codes_and_or_options)
options = codes_and_or_options.
codes = codes_and_or_options
opts = DefaultOptions.merge(options)
code_attr = opts[:code_attribute].to_s
position_attr = opts[:position_attribute]
case_sensitive = opts[:uniqueness_case_sensitive]
define_test_methods = opts[:define_test_methods]
model_type = self.ancestors.include?('ActiveRecord::Base'.constantize) ? :active_record : :poro
class_eval <<-RUBY_
def translated_#{code_attr}(locale = I18n.locale, *options)
locale_options = options.extract_options!
locale_options.merge!({:locale => locale})
self.class.translate_#{code_attr}(#{code_attr}, locale_options.merge({:locale => locale}))
end
# translator
class << self
attr_accessor :code_box_i18n_options_select_key
def translate_#{code_attr}(*codes_and_options)
options = codes_and_options.extract_options!
codes = codes_and_options.first
is_parameter_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}.\#{code_key}", options)
}
if options[:build] == :zip
translated_codes.zip(codes)
else
is_parameter_array ? translated_codes : translated_codes.first
end
end
end
def self.for_code(code)
code_cache[code]
end
def self.build_select_options(*args)
options = args.extract_options!
codes = args.empty? ? #{code_attr.pluralize.camelize}::All : args
include_empty = options[:include_empty] || false
locale = options.fetch(:locale, I18n.locale)
label, value = case include_empty
when Hash
[
include_empty.fetch(:label, "i18n.#{CodeBox.i18n_empty_options_key}"),
include_empty.fetch(:value, nil)
]
when TrueClass
[ "i18n.#{CodeBox.i18n_empty_options_key}", nil ]
when String
[ include_empty, nil ]
else # is something falsish
[]
end
# If starts with 'i18n.' it is considered an I18n key, else the label itself
options = translate_#{code_attr}(codes, build: :zip)
if include_empty
label = I18n.t(label[5..-1], locale: locale) if label.starts_with?('i18n.')
options.unshift [label, value]
end
options
end
def self.initialize_cache
Hash[all.map{ |code| [code.#{code_attr}, code] }]
end
RUBY_
instance_eval <<-CODE
class << self
def _code_box_code_attr_name
'#{code_attr.to_s}'
end
def code_cache
@code_cache ||= initialize_cache
end
def clear_code_cache
@code_cache = nil
end
end
CODE
case model_type
when :active_record
order_expression = if self.attribute_names.include?(position_attr) then
"coalesce(#{position_attr.to_s}, #{code_attr.to_s})"
else
code_attr.to_s
end
class_eval <<-CODE
validates_presence_of :#{code_attr}
validates_uniqueness_of :#{code_attr}#{opts[:sti] ? ', :scope => :type' : ' '}, :case_sensitive => #{case_sensitive}
default_scope -> { order('#{order_expression}') }
CODE
when :poro
order_attr = position_attr ? position_attr.to_s : code_attr.to_s
class_eval <<-CODE
attr_accessor :#{code_attr}
def initialize(#{code_attr})
self.#{code_attr} = #{code_attr}
end
def self.all
raise 'Class responsibility - implement method .all returning all code models.'
end
def hash
[self.class.name, #{code_attr}].hash
end
def eql?(other)
other.is_a?(self.class) && self.hash == other.hash
end
def ==(other)
self.equal? other
end
CODE
else
raise ArgumentError, "'#{model_type}' is not a valid type. Use :active_record or :poro(default) instead"
end
define_codes(*codes, define_test_methods) unless codes.empty?
end
|