Sha256: 12fbeb6f130d20cae491e43927fb29250008994993b5fc4c4eea132a5ef4cca3

Contents?: true

Size: 1.7 KB

Versions: 22

Compression:

Stored size: 1.7 KB

Contents

require 'active_support'
require 'active_support/core_ext'
require 'string/builder'
require 'thor'
require 'eucalypt/migration/helpers'

module Eucalypt
  module Generators
    module Create
      class Table < Thor::Group
        include Thor::Actions
        include Eucalypt::Helpers
        include Eucalypt::Migration::Helpers
        using String::Builder

        def self.source_root
          File.join File.dirname(File.dirname(File.dirname __dir__))
        end

        def generate(columns: [], options: {}, name:)
          name = Inflect.resources(name.to_s)
          columns.map!{|c| c.split(?:).map{|i| Inflect.resource_keep_inflection(i)}}

          sleep 1
          migration_name = "create_#{name}"
          migration = Eucalypt::Helpers::Migration[title: migration_name, template: 'migration_base.tt']
          return unless migration.create_anyway? if migration.exists?
          config = {migration_class_name: migration_name.camelize}
          template migration.template, migration.file_path, config

          sanitized_options = sanitize_table_options(options)

          insert_into_file migration.file_path, :after => "def change\n" do
            String.build do |s|
              s << "    create_table :#{name}"
              s << ', ' unless sanitized_options.empty?
              s << sanitized_options.map{|opt| "#{opt.first}: #{opt.last}"}*', '
              unless columns.empty?
                s << " do |t|\n"
                columns.each do |column|
                  n, t = column
                  s << "      t.#{t} :#{n}\n"
                end
                s << "    end"
              end
              s << "\n"
            end
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
eucalypt-0.9.0 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.8.0 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.7.2 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.7.1 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.7.0 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.6.2 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.6.1 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.6.0 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.5.4 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.5.3 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.5.2 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.5.1 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.5.0 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.4.2 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.4.1 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.4.0 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.3.5 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.3.4 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.3.3 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb
eucalypt-0.3.2 lib/eucalypt/migration/namespaces/migration-create/generators/table.rb