Sha256: 39e585c8e5b37a982b9b3a8cfcd46e346da0ccd3650cb6410c16617e9919db4a

Contents?: true

Size: 1.22 KB

Versions: 15

Compression:

Stored size: 1.22 KB

Contents

module EnumerateIt
  module Generators
    class EnumGenerator < Rails::Generators::NamedBase
      source_root File.expand_path('../templates', __FILE__)

      argument :attributes, :type => 'array', :default => []

      class_option :singular, :type => 'string', :desc => 'Singular name for i18n'

      class_option :lang, :type => 'string', :desc => 'Lang to use in i18n', :default => 'en'

      desc "Creates a locale file on config/locales"
      def create_locale
        template "locale.yml", File.join('config/locales', "#{singular_name}.yml")
      end

      desc "Creates an initializer file on config/initializers that extends ActiveRecord::Base with Enumerate_it"
      def create_enumerate_it
        template "enumerate_it.rb", File.join('app/enumerations', "#{singular_name}.rb")
      end

      protected

      def default_lang
        options[:lang]
      end

      def singular
        singular_name
      end

      def locale_fields
        attributes.map(&:name)
      end

      def fields
        if attributes.first.type == :string
          attributes.map(&:name)
        else
          attributes.map do |attribute|
            [attribute.name, attribute.type]
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
enumerate_it-1.2.9 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.2.8 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.2.7 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.2.6 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.2.5 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.2.4 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.2.3 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.2.2 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.2.1 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.2.0 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.1.1 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.1.0 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.0.3 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.0.2 lib/generators/enumerate_it/enum/enum_generator.rb
enumerate_it-1.0.1 lib/generators/enumerate_it/enum/enum_generator.rb