Sha256: 26c33ea086e06ee7a5134962a6f748620a19c1237d5e7129372ace839406925f

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

require 'rails/generators/active_record'
require 'generators/adminpanel/resource/resource_generator_helper'
require 'generators/adminpanel/migration/migration_generator_helper'

module Adminpanel
  class MigrationGenerator < ActiveRecord::Generators::Base
    include ResourceGeneratorHelper
    include MigrationGeneratorHelper

    source_root File.expand_path("../templates", __FILE__)
    desc "Generate a migration files and updates the model and controller"
    argument :fields, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"

    def change_field_aliases
      fields.each do |attribute|
        type = attribute.split(':').second
        case type
        when 'wysiwyg'
          fields.delete(attribute)
          fields << attribute.split(':').first + ':' + 'text'
        end
      end
    end

    def generate_migration
      parameters = fields
      parameters.delete_if{ |pair| pair.split(':').second == 'has_many' }
      invoke :migration, [migration_name, parameters]
    end

    def inject_attributes_into_file
      inject_into_file(
        "app/models/adminpanel/#{resource_migrating}.rb",
        after: '      ['
      ) do
        indent ("\n" + get_attribute_hash + ','), 8
      end
    end

    def puts_messages
      puts "don't forget to migrate your database"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
adminpanel-2.2.2 lib/generators/adminpanel/migration/migration_generator.rb
adminpanel-2.2.1 lib/generators/adminpanel/migration/migration_generator.rb
adminpanel-2.2.0 lib/generators/adminpanel/migration/migration_generator.rb
adminpanel-2.1.7 lib/generators/adminpanel/migration/migration_generator.rb
adminpanel-2.1.6 lib/generators/adminpanel/migration/migration_generator.rb
adminpanel-2.1.5 lib/generators/adminpanel/migration/migration_generator.rb
adminpanel-2.1.3 lib/generators/adminpanel/migration/migration_generator.rb