class Forest::BlockGenerator

Attributes

join_tables[R]
migration_action[R]

Public Class Methods

next_migration_number(dirname) click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 8
def self.next_migration_number(dirname)
  next_migration_number = current_migration_number(dirname) + 1
  ActiveRecord::Migration.next_migration_number(next_migration_number)
end

Public Instance Methods

create_migration_file() click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 22
def create_migration_file
  set_local_assigns!
  validate_file_name!
  migration_template 'create_table_migration.rb', "db/migrate/create_#{table_name}.rb"
end
create_model_file() click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 13
def create_model_file
  template 'model.rb', File.join('app/models/blocks', "#{file_name}.rb")
end
create_stylesheet() click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 28
def create_stylesheet
  template '_block.scss.erb', File.join('app/assets/stylesheets/blocks', "_#{file_name}.scss")
end
create_view_files() click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 17
def create_view_files
  template "_block.html.erb", File.join('app/views/blocks', file_name, "_show.html.erb")
  template "_block_edit_fields.html.erb", File.join('app/views/blocks', file_name, "_edit.html.erb")
end

Private Instance Methods

attributes_with_index() click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 72
def attributes_with_index
  attributes.select { |a| !a.reference? && a.has_index? }
end
block_category() click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 92
def block_category
  escape_quotes (ENV['BLOCK_CATEGORY'].presence || 'default')
end
block_description() click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 96
def block_description
  escape_quotes (ENV['BLOCK_DESCRIPTION'].presence || '')
end
block_record_type() click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 88
def block_record_type
  escape_quotes (ENV['BLOCK_RECORD_TYPE'].presence || 'default')
end
escape_quotes(string) click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 100
def escape_quotes(string)
  string.gsub(/[']/, '\\\\\'')
end
index_name_for(attribute) click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 64
def index_name_for(attribute)
  if attribute.foreign_key?
    attribute.name
  else
    attribute.name.singularize.foreign_key
  end.to_sym
end
normalize_table_name(_table_name) click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 84
def normalize_table_name(_table_name)
  pluralize_table_names? ? _table_name.pluralize : _table_name.singularize
end
set_index_names() click to toggle source
# File lib/generators/forest/block/block_generator.rb, line 58
def set_index_names
  attributes.each_with_index do |attr, i|
    attr.index_name = [attr, attributes[i - 1]].map { |a| index_name_for(a) }
  end
end
set_local_assigns!() click to toggle source

Sets the default migration template that is being used for the generation of the migration. Depending on command line arguments, the migration template and the table name instance variables are set up.

# File lib/generators/forest/block/block_generator.rb, line 39
def set_local_assigns!
  @migration_template = "migration.rb"
  case file_name
  when /^(add|remove)_.*_(?:to|from)_(.*)/
    @migration_action = $1
    @table_name       = normalize_table_name($2)
  when /join_table/
    if attributes.length == 2
      @migration_action = "join"
      @join_tables      = pluralize_table_names? ? attributes.map(&:plural_name) : attributes.map(&:singular_name)

      set_index_names
    end
  when /^create_(.+)/
    @table_name = normalize_table_name($1)
    @migration_template = "create_table_migration.rb"
  end
end
validate_file_name!() click to toggle source

A migration file name can only contain underscores (_), lowercase characters, and numbers 0-9. Any other file name will raise an IllegalMigrationNameError.

# File lib/generators/forest/block/block_generator.rb, line 78
def validate_file_name!
  unless /^[_a-z0-9]+$/.match?(file_name)
    raise IllegalMigrationNameError.new(file_name)
  end
end