lib/enumerated.rb
# This is the method you call on the class (a'la Rails relationships) to set the defaults and create the associated methods.
This is the method you call on the class (a'la Rails relationships) to set the defaults and create the associated methods.
This is the method you call on the class ( a ' la Rails relationships ) to set the defaults and create the associated methods .
def enumeration_creator(hash)
      defaults = hash.has_key?(:defaults) ? hash.delete(:defaults) : {}
      autoinc_field = hash.delete(:autoinc)
      if @enum_translated
        enumerations.each_with_index do |(default, value), index|
          params = defaults.dup
          params.merge!(hash[default]) if hash[default]
          params[autoinc_field] = index + 1 if autoinc_field
          params.merge!(@enum_field => value) 
          exist_params = { @enum_field => value, language_id: Language.english.id }
          field_class = attribute_names.include?(@enum_field.to_s) ? self : @enum_translated_class
          unless field_class.send(:exists?,
                                  exist_params.select { |k,v| field_class.send(:attribute_names).include?(k.to_s) } )
            this = create!(params.select { |k,v| attribute_names.include?(k.to_s) } )
            trans_params = params.reverse_merge!(language_id: Language.english.id, @enum_foreign_key => this.id)
            trans = @enum_translated_class.send(:create!,
              trans_params.select { |k,v| @enum_translated_class.send(:attribute_names).include?(k.to_s) } )
          end
        end
      else
        enumerations.each_with_index do |(default, value), index|
          params = defaults.merge(hash[default]).merge(@enum_field => value)
          params[autoinc_field] = index + 1 if autoinc_field
          create!(params) unless exists?(@enum_field => value)
        end
      end
    end
def enumeration_creator ( hash ) defaults = hash . has_key? ( :defaults ) ? hash . delete ( :defaults ) : { } autoinc_field = hash . delete ( :autoinc ) if @enum_translated enumerations . each_with_index do | ( default , value ) , index | params = defaults . dup params . merge! ( hash [ default ] ) if hash [ default ] params [ autoinc_field ] = index + 1 if autoinc_field params . merge! ( @enum_field => value ) exist_params = { @enum_field => value , language_id : Language . english . id } field_class = attribute_names . include? ( @enum_field . to_s ) ? self : @enum_translated_class unless field_class . send ( :exists? , exist_params . select { | k , v | field_class . send ( :attribute_names ) . include? ( k . to_s ) } ) this = create! ( params . select { | k , v | attribute_names . include? ( k . to_s ) } ) trans_params = params . reverse_merge! ( language_id : Language . english . id , @enum_foreign_key => this . id ) trans = @enum_translated_class . send ( :create! , trans_params . select { | k , v | @enum_translated_class . send ( :attribute_names ) . include? ( k . to_s ) } ) end end else enumerations . each_with_index do | ( default , value ) , index | params = defaults . merge ( hash [ default ] ) . merge ( @enum_field => value ) params [ autoinc_field ] = index + 1 if autoinc_field create! ( params ) unless exists? ( @enum_field => value ) end end end
This is the method you call on the class (a'la Rails relationships) to set the defaults and create the associated methods.
This is the method you call on the class ( a ' la Rails relationships ) to set the defaults and create the associated methods .
#DEBUG: puts "++ #enumeration_creator" #DEBUG: puts "++ Defaults:" #DEBUG: pp defaults #DEBUG: puts "++ #{default} -> #{value}" #DEBUG: puts "++ #{default} -> #{value}" # Doing this last 'cause it *needs* to be the value... #DEBUG: pp params.select { |k,v| field_class.send(:attribute_names).include?(k.to_s) } #DEBUG: pp trans_params.select { |k,v| @enum_translated_class.send(:attribute_names).include?(k.to_s) }
hash
<not_specific>