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 add_create_enumerated_method
      if @enum_translated && @enum_translated_class.attribute_names.include?(@enum_field.to_s)
        define_singleton_method(:create_enumerated) do
          enumerations.each do |name, value|
            field_class = attribute_names.include?(@enum_field.to_s) ? self : @enum_translated_class
            trans_params = { @enum_field => value, language_id: Language.english.id }
            unless field_class.send(:exists?, { @enum_field => value, language_id: Language.english.id } )
              id = maximum(:id) || 0
              connection.execute("INSERT INTO #{table_name} (id) VALUES (#{id + 1})")
              this = last 
              trans_params.merge!(@enum_foreign_key => this.id)
              trans = @enum_translated_class.send(:create!, trans_params)
            end
          end
        end
      else
        define_singleton_method(:create_enumerated) do
          enumerations.values.each do |value|
            params = { @enum_field => value }
            create!(params) unless exists?(@enum_field => value)
          end
        end
      end
    end
def add_create_enumerated_method if @enum_translated && @enum_translated_class . attribute_names . include? ( @enum_field . to_s ) define_singleton_method ( :create_enumerated ) do enumerations . each do | name , value | field_class = attribute_names . include? ( @enum_field . to_s ) ? self : @enum_translated_class trans_params = { @enum_field => value , language_id : Language . english . id } unless field_class . send ( :exists? , { @enum_field => value , language_id : Language . english . id } ) id = maximum ( :id ) || 0 connection . execute ( "INSERT INTO #{table_name} (id) VALUES (#{id + 1})" ) this = last trans_params . merge! ( @enum_foreign_key => this . id ) trans = @enum_translated_class . send ( :create! , trans_params ) end end end else define_singleton_method ( :create_enumerated ) do enumerations . values . each do | value | params = { @enum_field => value } create! ( params ) unless exists? ( @enum_field => value ) end 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 .
# NOTE: Stupid hack. Our DB wasn't auto_inc'ing the id when you # just insert the default ID the way Rails does it natively. # STUPID! Has to be a problem with the DB, but no time to # investigate. Soooo, work around. # without workaround: this.create!