Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/spree_core/enumerable_constants.rb
Direct Known Subclasses
Address, Adjustment, Asset, Calculator, Configuration, Country, Creditcard, InventoryUnit, LineItem, LogEntry, MailMethod, OptionType, OptionValue, Order, Payment, PaymentMethod, Preference, Product, ProductGroup, ProductOptionType, ProductProperty, ProductScope, Property, Prototype, ReturnAuthorization, Role, Shipment, ShippingCategory, ShippingMethod, State, StateEvent, TaxCategory, TaxRate, Taxon, Taxonomy, Tracker, User, Variant, Zone, ZoneMember
Class Method Summary collapse
Class Method Details
.enumerable_constant(attribute_name, options = {}) ⇒ Object
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, ={}) raise ArgumentError, "you must specify a list of constants" unless [:constants] set_name = attribute_name.to_s.camelize set_class = "#{self.name}::#{set_name}" constant_definitions = "" [: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 [:connector] connector = [:connector] else connector = 'or' end unless [:skip_last_comma].kind_of? NilClass skip_last_comma = [:skip_last_comma] else skip_last_comma = true end if [:base] use_base = [:base] else use_base = 1 end # keep glue_text for back compat if [:glue_text] connector = [: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 [: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 |