Sha256: 9c391a7b12386afa617422bf74d1562a817f3f4a25f208eefd9b84d0fd3977b6

Contents?: true

Size: 1.49 KB

Versions: 7

Compression:

Stored size: 1.49 KB

Contents

require 'rails/generators/active_record'
module AbAdmin
  module Generators
    class GlobGenerator < ActiveRecord::Generators::Base
      desc 'Generates migration for models without globalize tables'

      source_root File.expand_path('../templates', __FILE__)

      argument :name, :type => :string, :default => 'fake'

      def create_migration
        migration_template 'migration.erb', "db/migrate/#{migration_name}.rb"
      end

      protected

      def model_attrs
        @model_attrs ||= begin
          models.each_with_object({}) do |m, h|
            h[m.name] = m.translated_attribute_names.map { |attr| ":#{attr} => :#{get_type(attr)}" }.join(', ')
          end
        end
      end

      def migration_name
        "create_globalize_#{models.map { |m| m.model_name.singular }.join('_')}"
      end

      def models
        @models ||= begin
          all_translated.reject { |m| conn.table_exists? m.translations_table_name }
        end
      end

      def all_translated
        all_models.find_all { |m| m.translates? }
      end

      def all_models
        Dir.glob(Rails.root.to_s + '/app/models/**/*.rb').each { |file| require file }
        ActiveRecord::Base.subclasses.find_all { |model| model.table_exists? }
      end

      def conn
        @conn ||= ActiveRecord::Base.connection
      end

      def get_type(col)
        case col.to_sym
          when :description, :content, :content_short
            :text
          else
            :string
        end
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ab_admin-0.2.3 lib/generators/ab_admin/glob/glob_generator.rb
ab_admin-0.2.2 lib/generators/ab_admin/glob/glob_generator.rb
ab_admin-0.2.1 lib/generators/ab_admin/glob/glob_generator.rb
ab_admin-0.2.0 lib/generators/ab_admin/glob/glob_generator.rb
ab_admin-0.1.2 lib/generators/ab_admin/glob/glob_generator.rb
ab_admin-0.1.1 lib/generators/ab_admin/glob/glob_generator.rb
ab_admin-0.1.0 lib/generators/ab_admin/glob/glob_generator.rb