class Forest::ScaffoldGenerator
Attributes
join_tables[R]
migration_action[R]
Public Class Methods
next_migration_number(dirname)
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 14 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_controller()
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 45 def create_controller template 'admin_controller.rb', "app/controllers/admin/#{plural_name}_controller.rb" unless options.skip_public? template 'controller.rb', "app/controllers/#{plural_name}_controller.rb" end route_lines = [] route_lines << "# TODO: sort these new admin routes" route_lines << "namespace :admin do" route_lines << " resources :#{plural_name}" route_lines << "end\n" unless options.skip_public? route_lines << "# TODO: sort these new public routes" route_lines << "resources :#{plural_name}, only: [:index, :show]\n\n" end route route_lines.join("\n") end
create_migration_file()
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 39 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/scaffold/scaffold_generator.rb, line 19 def create_model_file template 'model.rb', File.join('app/models', "#{file_name}.rb") end
create_policy()
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 66 def create_policy template 'policy.rb', "app/policies/#{singular_name}_policy.rb" end
create_view_files()
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 23 def create_view_files admin_views.each do |view| filename = "#{view}.html.erb" template "views/admin/#{filename}", File.join("app/views/admin", plural_name, filename) end template "views/admin/index.json.jbuilder", File.join("app/views/admin", plural_name, 'index.json.jbuilder') unless options.skip_public? public_views.each do |view| filename = "#{view}.html.erb" template "views/public/#{filename}", File.join("app/views", plural_name, filename) end end end
Private Instance Methods
admin_views()
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 126 def admin_views %w(_form edit index new show) end
attributes_with_index()
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 110 def attributes_with_index attributes.select { |a| !a.reference? && a.has_index? } end
index_name_for(attribute)
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 102 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/scaffold/scaffold_generator.rb, line 122 def normalize_table_name(_table_name) pluralize_table_names? ? _table_name.pluralize : _table_name.singularize end
public_views()
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 130 def public_views %w(index show) end
set_index_names()
click to toggle source
# File lib/generators/forest/scaffold/scaffold_generator.rb, line 96 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/scaffold/scaffold_generator.rb, line 77 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/scaffold/scaffold_generator.rb, line 116 def validate_file_name! unless /^[_a-z0-9]+$/.match?(file_name) raise IllegalMigrationNameError.new(file_name) end end