class Dust::Generators::ScaffoldGenerator

Attributes

controller_actions[RW]
model_attributes[RW]
model_name[RW]

Public Class Methods

new(*args, &block) click to toggle source
Calls superclass method
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 26
def initialize(*args, &block)
  super

  @controller_actions = []
  @model_attributes = []
  @skip_model = options.skip_model?
  @invert_actions = options.invert?


  args_for_c_m.each do |arg|
    if arg == '!'
      @invert_actions = true
    elsif arg.include?(':')
      @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
    else
      @controller_actions << arg
      @controller_actions << 'create' if arg == 'new'
      @controller_actions << 'update' if arg == 'edit'
    end
  end

  @controller_actions.uniq!
  @model_attributes.uniq!

  if @invert_actions || @controller_actions.empty?
    @controller_actions = all_actions - @controller_actions
  end

  if @model_attributes.empty?
    @skip_model = true # skip model if no attributes
    if model_exists?
      model_columns_for_attributes.each do |column|
        @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
      end
    else
      @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
    end
  end
end

Public Instance Methods

add_gems() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 66
def add_gems
  gem "mocha", :group => :test unless File.read(destination_path("Gemfile")).include? "mocha"
end
create_controller() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 89
def create_controller
  unless options.skip_controller?
    template 'controller.rb', "app/controllers/app/#{plural_name}_controller.rb"

    #template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"

    controller_actions.each do |action|
      if %w[index show new edit].include?(action) # Actions with templates
        template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/app/#{plural_name}/#{action}.html.#{view_language}"
      end
    end

    template "views/#{view_language}/_search.html.#{view_language}", "app/views/app/#{plural_name}/_search.html.#{view_language}"

    if form_partial?
      template "views/#{view_language}/_form.html.#{view_language}", "app/views/app/#{plural_name}/_form.html.#{view_language}"
    end

    unless options[:skip_frontend]
      template "view_controller.rb", "app/controllers/front_end/app/#{plural_name}_controller.rb"

      route("match \"all-#{plural_name}\" => \"front_end/app/#{plural_name}#index\", :as => :front_end_app_#{plural_name}")
      route("match \"#{singular_name}/:filename\" => \"front_end/app/#{plural_name}#show\", :as => :front_end_app_#{singular_name}")

      template "views/front_end/#{view_language}/index.html.#{view_language}", "app/views/front_end/app/#{plural_name}/index.html.#{view_language}"
      template "views/front_end/#{view_language}/show.html.#{view_language}", "app/views/front_end/app/#{plural_name}/show.html.#{view_language}"
    end

    route "namespace :app do resources #{plural_name.to_sym.inspect} end"

    #if test_framework == :rspec
      #template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
    #else
      #template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
    #end
  end
end
create_migration() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 83
def create_migration
  unless @skip_model || options.skip_migration?
    migration_template 'migration.rb', "db/migrate/create_#{plural_name}.rb"
  end
end
create_model() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 70
def create_model
  unless @skip_model
    template 'model.rb', "app/models/app/#{singular_name}.rb"
    #if test_framework == :rspec
      #template "tests/rspec/model.rb", "spec/models/#{singular_name}_spec.rb"
      #template 'fixtures.yml', "spec/fixtures/#{plural_name}.yml"
    #else
      #template "tests/#{test_framework}/model.rb", "test/unit/#{singular_name}_test.rb"
      #template 'fixtures.yml', "test/fixtures/#{plural_name}.yml"
    #end
  end
end

Private Instance Methods

action?(name) click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 137
def action?(name)
  controller_actions.include? name.to_s
end
actions?(*names) click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 141
def actions?(*names)
  names.all? { |name| action? name }
end
all_actions() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 133
def all_actions
  %w[index show new create edit update destroy]
end
class_name() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 153
def class_name
  model_name.camelize
end
controller_methods(dir_name) click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 161
def controller_methods(dir_name)
  controller_actions.map do |action|
    read_template("#{dir_name}/#{action}.rb")
  end.join("  \n").strip
end
default_test_framework() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 234
def default_test_framework
  File.exist?(destination_path("spec")) ? :rspec : :testunit
end
destination_path(path) click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 246
def destination_path(path)
  File.join(destination_root, path)
end
form_partial?() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 129
def form_partial?
  actions? :new, :edit
end
item_path(suffix = 'path') click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 187
def item_path(suffix = 'path')
  if action? :show
    "@#{singular_name}"
  else
    items_path(suffix)
  end
end
item_path_for_spec(suffix = 'path') click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 195
def item_path_for_spec(suffix = 'path')
  if action? :show
    "#{singular_name}_#{suffix}(assigns[:#{singular_name}])"
  else
    items_path(suffix)
  end
end
item_path_for_test(suffix = 'path') click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 203
def item_path_for_test(suffix = 'path')
  if action? :show
    "#{singular_name}_#{suffix}(assigns(:#{singular_name}))"
  else
    items_path(suffix)
  end
end
items_path(suffix = 'path') click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 179
def items_path(suffix = 'path')
  if action? :index
    "#{plural_name}_#{suffix}"
  else
    "root_#{suffix}"
  end
end
model_columns_for_attributes() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 211
def model_columns_for_attributes
  class_name.constantize.columns.reject do |column|
    column.name.to_s =~ /^(id|created_at|updated_at)$/
  end
end
model_exists?() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 238
def model_exists?
  File.exist? destination_path("app/models/#{singular_name}.rb")
end
plural_class_name() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 157
def plural_class_name
  plural_name.camelize
end
plural_name() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 149
def plural_name
  model_name.underscore.pluralize
end
read_template(relative_path) click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 242
def read_template(relative_path)
  ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
end
render_form() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 167
def render_form
  if form_partial?
    if options.erb?
      "<%= render 'form' %>"
    else
      "= render 'form'"
    end
  else
    read_template("views/#{view_language}/_form.html.#{view_language}")
  end
end
singular_name() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 145
def singular_name
  model_name.underscore
end
test_framework() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 221
def test_framework
  return @test_framework if defined?(@test_framework)
  if options.testunit?
    return @test_framework = :testunit
  elsif options.rspec?
    return @test_framework = :rspec
  elsif options.shoulda?
    return @test_framework = :shoulda
  else
    return @test_framework = default_test_framework
  end
end
view_language() click to toggle source
# File lib/generators/dust/scaffold/scaffold_generator.rb, line 217
def view_language
  options.erb? ? 'erb' : 'haml'
end