Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/spree_core/enumerable_constants.rb,
lib/spree_core/find_by_param.rb

Class Method Summary collapse

Class Method Details

.enumerable_constant(attribute_name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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
202
203
204
205
206
207
# File 'lib/spree_core/enumerable_constants.rb', line 146

def self.enumerable_constant(attribute_name, options={})
  raise ArgumentError, "you must specify a list of constants" unless options[:constants]
  set_name = attribute_name.to_s.camelize
  set_class = "#{self.name}::#{set_name}"
  
  constant_definitions = ""
  options[:constants].each do |constant|
    if constant.is_a? Hash
      # puts "hash"
      constant_name = constant.to_a[0][0]
      display_name = constant.to_a[0][1]
      # puts "constant name: #{constant_name}"
      # puts "display name: #{display_name}"
      # puts "c name: #{constant_name.to_s.underscore.upcase}"
      constant_definitions << "constant '#{constant_name.to_s.underscore.upcase}', nil, '#{display_name}'\n" 
    else
      # puts "constant '#{constant.to_s.underscore.upcase}'\n" 
      constant_definitions << "constant '#{constant.to_s.underscore.upcase}'\n" 
    end
  end
  
  # allow for :connector and :skip_last_comma as used in Array#to_sentence
  if options[:connector]
    connector = options[:connector] 
  else
    connector = 'or'
  end
  unless options[:skip_last_comma].kind_of? NilClass
    skip_last_comma = options[:skip_last_comma] 
  else
    skip_last_comma = true
  end
  
  if options[:base]
    use_base = options[:base]
  else
    use_base = 1
  end

  # keep glue_text for back compat
  if options[:glue_text]
    connector = options[:glue_text].strip
  end    

  class_eval <<-EOF
    class #{set_name} < EnumerableConstant::Base
      base #{use_base}
      #{constant_definitions}
    end
  EOF

  should_be_in_text = set_class.constantize.names.to_sentence :words_connector => connector, :last_word_connector => skip_last_comma

  unless options[:no_validation]
    class_eval <<-EOF  
      validates :#{attribute_name.to_s}, :presence => {
        :in => #{set_name}::values, 
        :message => "should be #{should_be_in_text}"
        }
    EOF
  end
end

.find_by_param(value, args = {}) ⇒ Object

default finders these are overwritten if you use make_permalink in your model



109
110
111
# File 'lib/spree_core/find_by_param.rb', line 109

def self.find_by_param(value,args={})
  find_by_id(value,args)
end

.find_by_param!(value, args = {}) ⇒ Object



112
113
114
# File 'lib/spree_core/find_by_param.rb', line 112

def self.find_by_param!(value,args={})
  find(value,args)
end