class Redde::Generators::ScaffoldGenerator

Attributes

base_name[RW]
controller_class_nesting[RW]
controller_class_nesting_depth[RW]
controller_class_path[RW]
controller_file_path[RW]
controller_routing_path[RW]
model_name[RW]

Public Class Methods

new(args, *options) click to toggle source
Calls superclass method
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 16
def initialize(args, *options)
  super(args, *options)
  initialize_views_variables
end

Public Instance Methods

copy_views() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 21
def copy_views
  generate_views
end
generate_translations() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 25
def generate_translations
  template 'ru.yml', "config/locales/#{resource_name}/ru.yml"
end

Protected Instance Methods

capital_resource_name() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 54
def capital_resource_name
  resource_name.capitalize
end
column_names() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 126
def column_names
  @model_name.constantize.column_names
end
columns() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 72
def columns
  model_name
    .constantize
    .columns
    .reject { |c| exclude_column?(c.name) }
    .sort { |a, b| sort_priority(a.name) <=> sort_priority(b.name) }
    .map { |c| convert_column(c) }
end
contr_path(action) click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 148
def contr_path(action)
  "app/views/admin/#{controller_file_path}/#{action}.html.#{ext}"
end
convert_column(column) click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 118
def convert_column(column)
  ::Rails::Generators::GeneratedAttribute.new(column.name, column.type.to_s)
end
default_name_for(column) click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 81
def default_name_for(column)
  case column.to_s
  when 'title' then 'Заголовок'
  when 'name' then 'Название'
  when 'slug' then 'URL'
  when 'position' then 'Позиция'
  when 'visible' then 'Отображать на сайте'
  when 'short_desc' then 'Аннотация'
  when 'comment' then 'Комментарий'
  when 'email' then 'E-mail'
  when 'image', 'src' then 'Изображение'
  when 'phone' then 'Телефон'
  when 'text', 'body', 'desc' then 'Описание'
  when 'parent_id' then 'Родительская категория'
  when 'company_id' then 'Компания'
  when 'city_id' then 'Город'
  when 'url' then 'Ссылка'
  when 'value' then 'Значение'
  when 'articul' then 'Артикул'
  when 'guid' then 'GUID'
  when 'tag_list' then 'Тэги'
  when 'category_id' then 'Категория'
  when 'message' then 'Сообщение'
  when 'product_id' then 'Товар'
  when 'price' then 'Цена'
  when 'stock', 'in_stock' then 'Наличие'
  when 'color', 'color_id' then 'Цвет'
  when 'size', 'size_id' then 'Размер'
  when 'parameter_id' then 'Параметр'
  else column.capitalize.to_s
  end
end
exclude_column?(name) click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 122
def exclude_column?(name)
  excluded_column_names.include?(name) || name.index('_id')
end
excluded_column_names() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 114
def excluded_column_names
  %w(id created_at updated_at)
end
ext() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 159
def ext
  :haml
end
extract_modules(name) click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 130
def extract_modules(name)
  modules = name.include?('/') ? name.split('/') : name.split('::')
  name    = modules.pop
  path    = modules.map(&:underscore)
  file_path = (path + [name.underscore]).join('/')
  nesting = modules.map(&:camelize).join('::')
  [name, path, file_path, nesting, modules.size]
end
generate_controller() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 163
def generate_controller
  template 'controllers/controller.rb', "app/controllers/admin/#{plural_resource_name}_controller.rb"
end
generate_erb(views) click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 152
def generate_erb(views)
  views.each do |template_name, output_path|
    template template_name, output_path
  end
  generate_controller
end
generate_views() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 139
def generate_views
  views = {
    # "index.html.#{ext}" => contr_path('index'),
    "edit.html.#{ext}" => contr_path('edit')
  }
  selected_views = views
  options.engine == generate_erb(selected_views)
end
index_header() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 58
def index_header
  model_name.constantize.model_name.human(count: 'other')
end
initialize_views_variables() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 31
def initialize_views_variables
  @base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(controller_path)
  @controller_routing_path = @controller_file_path.gsub(/\//, '_')
  @model_name = @controller_class_nesting + "::#{@base_name.singularize.camelize}" unless @model_name
  @model_name = @model_name.camelize
end
plural_model_name() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 42
def plural_model_name
  model_name.pluralize
end
plural_resource_name() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 50
def plural_resource_name
  resource_name.pluralize
end
resource_name() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 46
def resource_name
  model_name.demodulize.underscore
end
singular_controller_routing_path() click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 38
def singular_controller_routing_path
  controller_routing_path.singularize
end
sort_priority(column_name) click to toggle source
# File lib/generators/redde/scaffold/scaffold_generator.rb, line 62
def sort_priority(column_name)
  case column_name
  when 'position' then 1
  when 'visible' then 2
  when 'name' then 3
  when 'title' then 3
  else 5
  end
end