class Translatable::Generators::ModelGenerator

Public Instance Methods

create_model() click to toggle source
# File lib/generators/translatable/model_generator.rb, line 14
def create_model
  self.attributes = attrs
  parse_attributes!
  invoke "active_record:model", [class_name], {migration: true, timestamps: true} unless model_exists?
end
inject_translatable_block() click to toggle source

all public methods in here will be run in order

# File lib/generators/translatable/model_generator.rb, line 21
def inject_translatable_block
  inject_into_class model_path, class_name, generate_translatable_block
end

Protected Instance Methods

generate_translatable_block() click to toggle source
# File lib/generators/translatable/model_generator.rb, line 27
def generate_translatable_block
  block = "  translatable do"
  attributes.each do |attr|
    block << "\n    field :#{attr.name}, :presence => true#, :uniqueness => true"
  end
  block << (options[:translated_model].nil? ?
      "\n    #class_name 'Translated#{class_name}'" :
      "\n    class_name '#{options[:translated_model]}'")
  block << (options[:origin].nil? ?
      "\n    #reflection_name :#{singular_table_name}" :
      "\n    reflection_name :#{options[:origin]}")
  block << "\n    #foreign_key :origin_id" 
  block << (options[:locale].nil? ?
      "\n    #locale_key :locale" :
      "\n    locale_key :#{options[:locale]}")
  block << "\n  end\n"

  block << "\n  #accepts_nested_attributes_for :translations, :current_translation"
  block << "\n  #attr_accessible :translations_attributes, :current_translation_attributes\n"
end