Sha256: 98d4c6decaec47b764820760008b5f175e7fda1e12fdedaf1889cedab4b964e5

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require "translatable/generator_helper"

module Translatable
  module Generators
    class ModelGenerator < Rails::Generators::NamedBase
      include Translatable::GeneratorHelper

      desc "Creates ActiveRecord model and injects translatable block into it"

      class_option :translated_model, :type => :string, :desc => "Defines the model responsible for translations"
      class_option :origin,           :type => :string, :desc => "Defines the association name for translation record that deals with origin"
      class_option :locale,           :type => :string, :desc => "Defines the column for translation record that keeps the locale"

      def create_model
        self.attributes = attrs
        parse_attributes!
        invoke "active_record:model", [class_name], {migration: true, timestamps: true} unless model_exists?
      end

      # all public methods in here will be run in order
      def inject_translatable_block
        inject_into_class model_path, class_name, generate_translatable_block
      end

      protected

      def generate_translatable_block
        block = "  translatable do"
        attributes.each do |attr|
          block << "\n    translatable :#{attr.name}, :presence => true#, :uniqueness => true"
        end
        block << (options[:translated_model].nil? ?
            "\n    #translatable_model 'Translated#{class_name}'" :
            "\n    translatable_model '#{options[:translated_model]}'")
        block << (options[:origin].nil? ?
            "\n    #translatable_origin :#{singular_table_name}" :
            "\n    translatable_origin :#{options[:origin]}")
        block << (options[:locale].nil? ?
            "\n    #translatable_locale :locale" :
            "\n    translatable_locale :#{options[:locale]}")
        block << "\n  end\n"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
translatable-0.3.1 lib/generators/translatable/model_generator.rb
translatable-0.3.0 lib/generators/translatable/model_generator.rb