Permalink
Browse files

Major refactoring. Added support for additional arguments for actions…

… and respond_to-formats that is not supported by the Rails-scaffold.
  • Loading branch information...
1 parent 5bddcb9 commit 570dbaa036a4c5f6482b8bb0482de95e60fafebd @grimen committed Apr 26, 2009
27 README.textile
@@ -4,12 +4,37 @@ h2. SYNOPSIS
...
-h2. INSTALLATION
+h2. FEATURES
...
+h2. INSTALLATION
+
+gem sources -a http://gems.github.com
+sudo gem install grimen-dry_scaffold
+
h2. BASIC USAGE
+./script/generate dry_scaffold ModelName [attribute:type attribute:type] [_actions:new,create,...] [_formats:html,json,...] [--skip-resourceful] [--skip-formtastic] [--skip-views] [--skip-helpers] [--skip-tests]
+
+h3. Model Name
+
+...
+
+h3. Model Attributes
+
+...
+
+h3. Controller Actions
+
+...
+
+h3. Controller Formats
+
+...
+
+h3. Options
+
...
h2. ADVANCED USAGE
View
2 Rakefile
@@ -19,7 +19,7 @@ begin
gem.email = EMAIL
gem.require_paths = %w{lib}
- gem.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob(File.join('{generators,tasks}', '**', '*'))
+ gem.files = %w(MIT-LICENSE README.textile Rakefile init.rb) + Dir.glob(File.join('{generators,rails,tasks}', '**', '*'))
gem.executables = %w()
gem.extra_rdoc_files = %w{README.textile}
end
View
10 TODO.textile
@@ -0,0 +1,10 @@
+h1. TODO
+
+h2. Most probably
+
+* Something like "model_generator_with_factories" (using machinist, factory_girl, ...). Source: http://github.com/vigetlabs/model_generator_with_factories/tree/master
+* Break up in different generators: dry_controller, dry_views, etc., and use as dependencies in dry_scaffold
+
+h2. Maybe
+
+* Check for overridden templates in: RAILS_ROOT/lib/dry_scaffold/templates/
View
2 generators/USAGE
@@ -5,4 +5,4 @@ DESCRIPTION
See README.
EXAMPLE
- ./script/generate dry_scaffold ModelName [attribute:type attribute:type] [--skip-resourceful] [--skip-formtastic] [--skip-views] [--skip-helpers] [--skip-tests]
+ ./script/generate dry_scaffold ModelName [attribute:type attribute:type] [_actions:new,create,...] [_formats:html,json,...] [--skip-resourceful] [--skip-formtastic] [--skip-views] [--skip-helpers] [--skip-tests]
View
141 generators/dry_scaffold/dry_scaffold_generator.rb
@@ -1,26 +1,59 @@
+require 'rubygems'
+
+begin
+ require 'formtastic'
+ FORMTASTIC = true
+rescue
+ FORMTASTIC = false
+end
+begin
+ require 'inherited_resources'
+ INHERITED_RESOURCES = true
+rescue
+ INHERITED_RESOURCES = false
+end
+
class DryScaffoldGenerator < Rails::Generator::NamedBase
- default_options :resourceful => true,
- :formtastic => true,
- :skip_tests => false,
- :skip_helpers => false,
- :skip_views => false,
- :include_layout => false
-
- CONTROLLERS_PATH = File.join('app', 'controllers').freeze
- HELPERS_PATH = File.join('app', 'helpers').freeze
- VIEWS_PATH = File.join('app', 'views').freeze
- LAYOUTS_PATH = File.join(VIEWS_PATH, 'layouts').freeze
- MODELS_PATH = File.join('app', 'models').freeze
+ DEFAULT_RESPOND_TO_FORMATS = [:html, :xml, :json].freeze
+ DEFAULT_MEMBER_ACTIONS = [:show, :new, :edit, :create, :update, :destroy].freeze
+ DEFAULT_MEMBER_AUTOLOAD_ACTIONS = (DEFAULT_MEMBER_ACTIONS - [:new, :create])
+ DEFAULT_COLLECTION_ACTIONS = [:index].freeze
+ DEFAULT_COLLECTION_AUTOLOAD_ACTIONS = DEFAULT_COLLECTION_ACTIONS
+ DEFAULT_CONTROLLER_ACTIONS = (DEFAULT_COLLECTION_ACTIONS + DEFAULT_MEMBER_ACTIONS)
+
+ DEFAULT_VIEW_TEMPLATE_FORMAT = :haml
+
+ CONTROLLERS_PATH = File.join('app', 'controllers').freeze
+ HELPERS_PATH = File.join('app', 'helpers').freeze
+ VIEWS_PATH = File.join('app', 'views').freeze
+ LAYOUTS_PATH = File.join(VIEWS_PATH, 'layouts').freeze
+ MODELS_PATH = File.join('app', 'models').freeze
FUNCTIONAL_TESTS_PATH = File.join('test', 'functional').freeze
- UNIT_TESTS_PATH = File.join('test', 'unit', 'helpers').freeze
+ UNIT_TESTS_PATH = File.join('test', 'unit', 'helpers').freeze
RESOURCEFUL_COLLECTION_NAME = 'collection'.freeze
- RESOURCEFUL_SINGULAR_NAME = 'resource'.freeze
+ RESOURCEFUL_SINGULAR_NAME = 'resource'.freeze
- PARTIALS = %w{form item}.freeze
- ACTIONS = %w{new edit show index}.freeze
+ ARG_KEY_VALUE_DIVIDER = ':'.freeze
+ NON_ATTR_ARG_KEY_PREFIX = '_'.freeze
+ NON_ATTR_ARG_VALUE_DIVIDER = ','.freeze
+ # :{action} => [:{partial}, ...]
+ VIEW_TEMPLATES = {
+ :index => [:item],
+ :show => [],
+ :new => [:form],
+ :edit => [:form]
+ }.freeze
+
+ default_options :resourceful => INHERITED_RESOURCES,
+ :formtastic => FORMTASTIC,
+ :skip_tests => false,
+ :skip_helpers => false,
+ :skip_views => false,
+ :include_layout => false
+
attr_reader :controller_name,
:controller_class_path,
:controller_file_path,
@@ -30,7 +63,10 @@ class DryScaffoldGenerator < Rails::Generator::NamedBase
:controller_underscore_name,
:controller_singular_name,
:controller_plural_name,
- :collection_name
+ :collection_name,
+ :view_template_format,
+ :actions,
+ :formats
alias_method :controller_file_name, :controller_underscore_name
alias_method :controller_table_name, :controller_plural_name
@@ -48,6 +84,25 @@ def initialize(runtime_args, runtime_options = {})
else
@controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
end
+
+ @view_template_format = DEFAULT_VIEW_TEMPLATE_FORMAT
+
+ @attributes ||= []
+
+ # Non-attribute args, i.e. "_actions:new,create". Add to options instead
+ @args.each do |arg|
+ arg_entities = arg.split(':')
+ if arg =~ /^_actions/
+ @actions = arg_entities[1].split(',').compact.collect { |action| action.to_sym }
+ elsif arg =~ /^_formats/ || arg =~ /^_respond_to/
+ @formats = arg_entities[1].split(',').compact.collect { |format| format.to_sym }
+ else
+ @attributes << Rails::Generator::GeneratedAttribute.new(*arg_entities)
+ end
+ end
+
+ @actions ||= DEFAULT_COLLECTION_ACTIONS + DEFAULT_MEMBER_ACTIONS
+ @formats ||= DEFAULT_RESPOND_TO_FORMATS
end
def manifest
@@ -60,7 +115,7 @@ def manifest
# Directories.
m.directory File.join(CONTROLLERS_PATH, controller_class_path)
m.directory File.join(HELPERS_PATH, controller_class_path) unless options[:skip_helpers]
- m.directory File.join(VIEWS_PATH, controller_class_path, controller_file_name.pluralize) unless options[:skip_views]
+ m.directory File.join(VIEWS_PATH, controller_class_path, controller_file_name) unless options[:skip_views]
m.directory File.join(FUNCTIONAL_TESTS_PATH, controller_class_path) unless options[:skip_tests]
m.directory File.join(UNIT_TESTS_PATH, controller_class_path) unless options[:skip_tests]
@@ -71,50 +126,54 @@ def manifest
# Controller Tests.
unless options[:skip_tests]
- m.template "controller_test_standard.rb",
+ m.template 'controller_test_standard.rb',
File.join(FUNCTIONAL_TESTS_PATH, controller_class_path, "#{controller_file_name}_controller_test.rb")
end
# Helpers.
unless options[:skip_helpers]
- m.template "helper_standard.rb",
+ m.template 'helper_standard.rb',
File.join(HELPERS_PATH, controller_class_path, "#{controller_file_name}_helper.rb")
# Helper Tests
unless options[:skip_tests]
- m.template "helper_test_standard.rb",
+ m.template 'helper_test_standard.rb',
File.join(UNIT_TESTS_PATH, controller_class_path, "#{controller_file_name}_helper_test.rb")
end
end
-
+
# Views.
unless options[:skip_views]
# View template for each action.
- ACTIONS.each do |action|
- m.template "view_#{action}.html.haml",
- File.join(VIEWS_PATH, controller_file_name, "#{action}.html.haml")
- end
- # View template for each partial.
- PARTIALS.each do |partial|
- m.template "view__#{partial}.html.haml",
- File.join(VIEWS_PATH, controller_file_name, "_#{partial}.html.haml")
+ (actions & VIEW_TEMPLATES.keys).each do |action|
+ m.template "view_#{action}.html.#{view_template_format}", File.join(VIEWS_PATH, controller_file_name, "#{action}.html.#{view_template_format}")
+ # View template for each partial - if not already copied.
+ (VIEW_TEMPLATES[action] || []).each do |partial|
+ m.template "view__#{partial}.html.#{view_template_format}",
+ File.join(VIEWS_PATH, controller_file_name, "_#{partial}.html.#{view_template_format}")
+ end
end
end
+ # Layout.
if options[:include_layout]
- m.template "view_layout.html.haml",
- File.join(LAYOUTS_PATH, "#{controller_file_name}.html.haml")
+ m.template "view_layout.html.#{view_template_format}",
+ File.join(LAYOUTS_PATH, "#{controller_file_name}.html.#{view_template_format}")
end
# Routes.
m.route_resources controller_file_name
# Models - use Rails default generator.
- m.dependency 'model', [name] + @args, :collision => :skip
+ m.dependency 'model', [name] + attributes, :collision => :skip
end
end
protected
+ def symbol_array_to_expression(array)
+ ":#{array.compact.join(', :')}" if array.present?
+ end
+
def assign_names!(name)
super
@collection_name = options[:resourceful] ? RESOURCEFUL_COLLECTION_NAME : @plural_name
@@ -151,12 +210,6 @@ def add_options!(opt)
opt.on('--include-layout', "Generate layout.") do |v|
options[:include_layout] = v
end
-
- # TODO: How to specify arguments?
- opt.on('-r', '--respond-to', "Skip generation of tests.") do |v|
- puts v
- options[:respond_to] = v
- end
end
def model_name
@@ -165,8 +218,14 @@ def model_name
def banner
"Usage: #{$0} dry_scaffold ModelName [field:type field:type ...]" +
- " [--skip-resourceful] [--skip-formtastic]" +
- " [--skip-views] [--skip-helpers] [--skip-tests] [--include-layout]"
+ " [_actions:new,create,...]" +
+ " [_formats:html,json,...]" +
+ " [--skip-resourceful]" +
+ " [--skip-formtastic]" +
+ " [--skip-views]" +
+ " [--skip-helpers]" +
+ " [--skip-tests]" +
+ " [--include-layout]"
end
end
View
18 generators/dry_scaffold/templates/controller_inherited_resources.rb
@@ -1,19 +1,29 @@
class <%= controller_class_name %>Controller < InheritedResources::Base
- actions :index, :show, :new, :create, :edit, :update, :destroy
- respond_to :html, :xml, :json
+<% if actions -%>
+ actions <%= symbol_array_to_expression(actions) %>
+<% end -%>
+<% if formats -%>
+ respond_to <%= symbol_array_to_expression(formats) %>
+<% end -%>
+<% (actions - DryScaffoldGenerator::DEFAULT_CONTROLLER_ACTIONS).each do |action| -%>
+ # GET /<%= plural_name %>/<%= action.to_s %>
+ def <%= action.to_s %>
+ end
+
+<% end -%>
protected
def collection
paginate_options ||= {}
paginate_options[:page] ||= (params[:page] || 1)
paginate_options[:per_page] ||= (params[:per_page] || 20)
- @<%= plural_name %> ||= end_of_association_chain.paginate(paginate_options)
+ @collection = @<%= plural_name %> ||= end_of_association_chain.paginate(paginate_options)
end
def resource
- @<%= singular_name %> ||= end_of_association_chain.find(params[:id])
+ @resource = @<%= singular_name %> ||= end_of_association_chain.find(params[:id])
end
end
View
212 generators/dry_scaffold/templates/controller_standard.rb
@@ -1,127 +1,237 @@
class <%= controller_class_name %>Controller < ApplicationController
- before_filter :load_resource, :except => [:index, :new, :create]
- before_filter :load_and_paginate_resources, :only => [:index]
+<% if actions -%>
+ before_filter :load_resource, :only => [<%= symbol_array_to_expression(actions & DryScaffoldGenerator::DEFAULT_MEMBER_AUTOLOAD_ACTIONS) %>]
+<% end -%>
+<% if actions -%>
+ before_filter :load_and_paginate_resources, :only => [<%= symbol_array_to_expression(actions & DryScaffoldGenerator::DEFAULT_COLLECTION_AUTOLOAD_ACTIONS) %>]
- # GET /<%= plural_name %>
- # GET /<%= plural_name %>.xml
- # GET /<%= plural_name %>.json
+<% end -%>
+<% if actions.include?(:index) -%>
+<% formats.each do |_format| -%>
+ # GET /<%= plural_name %><%= ".#{_format}" unless _format == :html %>
+<% end -%>
def index
respond_to do |format|
+<% formats.each do |_format| -%>
+<% case _format when :html then -%>
format.html # index.html.haml
- #format.js # index.js.rjs
- format.xml { render :xml => @<%= plural_name %> }
- format.json { render :json => @<%= plural_name %> }
+<% when :js then -%>
+ format.js # index.js.rjs
+<% when :xml, :json then -%>
+ format.<%= _format %> { render :<%= _format %> => @<%= plural_name %> }
+<% when :yaml then -%>
+ format.yaml { render :text => @<%= plural_name %>.to_yaml, :content_type => :'text/yaml' }
+<% when :yaml then -%>
+ format.txt { render :text => @<%= plural_name %>.to_s, :content_type => :text }
+<% else -%>
+ format.<%= _format %> { }
+<% end -%>
+<% end -%>
end
end
- # GET /<%= plural_name %>/1
- # GET /<%= plural_name %>/1.xml
- # GET /<%= plural_name %>/1.json
+<% end -%>
+<% if actions.include?(:show) -%>
+<% formats.each do |_format| -%>
+ # GET /<%= plural_name %>/:id<%= ".#{_format}" unless _format == :html %>
+<% end -%>
def show
respond_to do |format|
+<% formats.each do |_format| -%>
+<% case _format when :html then -%>
format.html # show.html.haml
- #format.js # show.js.rjs
- format.xml { render :xml => @<%= singular_name %> }
- format.json { render :json => @<%= singular_name %> }
+<% when :js then -%>
+ format.js # show.js.rjs
+<% when :xml, :json then -%>
+ format.<%= _format %> { render :<%= _format %> => @<%= plural_name %> }
+<% when :yaml then -%>
+ format.yaml { render :text => @<%= plural_name %>.to_yaml, :content_type => :'text/yaml' }
+<% when :yaml then -%>
+ format.txt { render :text => @<%= plural_name %>.to_s, :content_type => :text }
+<% else -%>
+ format.<%= _format %> { }
+<% end -%>
+<% end -%>
end
end
- # GET /<%= plural_name %>/new
- # GET /<%= plural_name %>/new.xml
- # GET /<%= plural_name %>/new.json
+<% end -%>
+<% if actions.include?(:new) -%>
+<% formats.each do |_format| -%>
+ # GET /<%= plural_name %>/new<%= ".#{_format}" unless _format == :html %>
+<% end -%>
def new
@<%= singular_name %> = <%= class_name %>.new
respond_to do |format|
+<% formats.each do |_format| -%>
+<% case _format when :html then -%>
format.html # new.html.haml
- #format.js # new.js.rjs
- format.xml { render :xml => @<%= singular_name %> }
- format.json { render :json => @<%= singular_name %> }
+<% when :js then -%>
+ format.js # new.js.rjs
+<% when :xml, :json then -%>
+ format.<%= _format %> { render :<%= _format %> => @<%= singular_name %> }
+<% when :yaml then -%>
+ format.yaml { render :text => @<%= singular_name %>.to_yaml, :content_type => :'text/yaml' }
+<% when :yaml then -%>
+ format.txt { render :text => @<%= singular_name %>.to_s, :content_type => :text }
+<% else -%>
+ format.<%= _format %> { }
+<% end -%>
+<% end -%>
end
end
- # GET /<%= plural_name %>/1/edit
+<% end -%>
+<% if actions.include?(:edit) -%>
+ # GET /<%= plural_name %>/:id/edit
def edit
end
- # POST /<%= plural_name %>
- # POST /<%= plural_name %>.xml
- # POST /<%= plural_name %>.json
+<% end -%>
+<% if actions.include?(:create) -%>
+<% formats.each do |_format| -%>
+ # POST /<%= plural_name %><%= ".#{_format}" unless _format == :html %>
+<% end -%>
def create
@<%= singular_name %> = <%= class_name %>.new(params[:<%= singular_name %>])
respond_to do |format|
if @<%= singular_name %>.save
flash[:notice] = '<%= singular_name.humanize %> was successfully created.'
+<% formats.each do |_format| -%>
+<% case _format when :html then -%>
format.html { redirect_to(@<%= singular_name %>) }
- #format.js # create.js.rjs
- format.xml { render :xml => @<%= singular_name %>, :status => :created, :location => @<%= singular_name %> }
- format.json { render :json => @<%= singular_name %>, :status => :created, :location => @<%= singular_name %> }
+<% when :js then -%>
+ format.js # create.js.rjs
+<% when :xml, :json then -%>
+ format.<%= _format %> { render :<%= _format %> => @<%= singular_name %>, :status => :created, :location => @<%= singular_name %> }
+<% when :yaml then -%>
+ format.yaml { render :text => @<%= singular_name %>.to_yaml, :content_type => :'text/yaml', :status => :created, :location => @<%= singular_name %> }
+<% when :txt then -%>
+ format.txt { render :text => @<%= singular_name %>.to_s, :content_type => :text, :status => :created, :location => @<%= singular_name %> }
+<% else -%>
+ format.<%= _format %> { }
+<% end -%>
+<% end -%>
else
#flash[:error] = '<%= singular_name.humanize %> could not be created.'
+<% formats.each do |_format| -%>
+<% case _format when :html then -%>
format.html { render 'new' }
- #format.js # create.js.rjs
- format.xml { render :xml => @<%= singular_name %>.errors, :status => :unprocessable_entity }
- format.json { render :json => @<%= singular_name %>.errors, :status => :unprocessable_entity }
+<% when :js then -%>
+ format.js # create.js.rjs
+<% when :xml, :json then -%>
+ format.<%= _format %> { render :<%= _format %> => @<%= singular_name %>.errors, :status => :unprocessable_entity }
+<% when :yaml then -%>
+ format.yaml { render :text => @<%= singular_name %>.errors.to_yaml, :content_type => :'text/yaml', :status => :unprocessable_entity }
+<% when :txt then -%>
+ format.txt { render :text => @<%= singular_name %>.errors.to_s, :content_type => :text, :status => :unprocessable_entity }
+<% else -%>
+ format.<%= _format %> { }
+<% end -%>
+<% end -%>
end
end
end
- # PUT /<%= plural_name %>/1
- # PUT /<%= plural_name %>/1.xml
- # PUT /<%= plural_name %>/1.json
+<% end -%>
+<% if actions.include?(:update) -%>
+<% formats.each do |_format| -%>
+ # PUT /<%= plural_name %>/:id<%= ".#{_format}" unless _format == :html %>
+<% end -%>
def update
respond_to do |format|
if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
- flash[:notice] = '<%= singular_name.humanize %> was successfully updated.'
+<% formats.each do |_format| -%>
+<% case _format when :html then -%>
format.html { redirect_to(@<%= singular_name %>) }
- #format.js # update.js.rjs
- format.xml { head :ok }
- format.json { head :ok }
+<% when :js then -%>
+ format.js # update.js.rjs
+<% when :xml, :json, :yaml, :txt then -%>
+ format.<%= _format %> { head :ok }
+<% else -%>
+ format.<%= _format %> { head :ok }
+<% end -%>
+<% end -%>
else
#flash[:error] = '<%= singular_name.humanize %> could not be updated.'
+<% formats.each do |_format| -%>
+<% case _format when :html then -%>
format.html { render 'edit' }
- #format.js # update.js.rjs
- format.xml { render :xml => @<%= singular_name %>.errors, :status => :unprocessable_entity }
- format.json { render :json => @<%= singular_name %>.errors, :status => :unprocessable_entity }
+<% when :js then -%>
+ format.js # update.js.rjs
+<% when :xml, :json then -%>
+ format.<%= _format %> { render :<%= _format %> => @<%= singular_name %>.errors, :status => :unprocessable_entity }
+<% when :yaml then -%>
+ format.yaml { render :text => @<%= singular_name %>.errors.to_yaml, :status => :unprocessable_entity }
+<% when :txt then -%>
+ format.txt { render :text => @<%= singular_name %>.errors.to_s, :status => :unprocessable_entity }
+<% else -%>
+ format.<%= _format %> { head :unprocessable_entity }
+<% end -%>
+<% end -%>
end
end
end
- # DELETE /<%= plural_name %>/1
- # DELETE /<%= plural_name %>/1.xml
- # DELETE /<%= plural_name %>/1.json
+<% end -%>
+<% if actions.include?(:destroy) -%>
+<% formats.each do |_format| -%>
+ # DELETE /<%= plural_name %>/:id<%= ".#{_format}" unless _format == :html %>
+<% end -%>
def destroy
respond_to do |format|
if @<%= singular_name %>.destroy
flash[:notice] = '<%= singular_name.humanize %> was successfully destroyed.'
+<% formats.each do |_format| -%>
+<% case _format when :html then -%>
format.html { redirect_to(<%= plural_name %>_url) }
- #format.js # destroy.js.rjs
- format.xml { head :ok }
- format.json { head :ok }
+<% when :js then -%>
+ format.js # destroy.js.rjs
+<% when :xml, :json, :yaml, :txt then -%>
+ format.<%= _format %> { head :ok }
+<% else -%>
+ format.<%= _format %> { head :ok }
+<% end -%>
+<% end -%>
else
flash[:error] = '<%= singular_name.humanize %> could not be destroyed.'
+<% formats.each do |_format| -%>
+<% case _format when :html then -%>
format.html { redirect_to(<%= singular_name %>_url(@<%= singular_name %>)) }
- #format.js # destroy.js.rjs
- format.xml { head :unprocessable_entity }
- format.json { head :unprocessable_entity }
+<% when :js then -%>
+ format.js # destroy.js.rjs
+<% when :xml, :json, :yaml, :txt then -%>
+ format.<%= _format %> { head :unprocessable_entity }
+<% else -%>
+ format.<%= _format %> { head :unprocessable_entity }
+<% end -%>
+<% end -%>
end
end
end
+<% end -%>
+<% (actions - DryScaffoldGenerator::DEFAULT_CONTROLLER_ACTIONS).each do |action| -%>
+ # GET /<%= plural_name %>/<%= action.to_s %>
+ def <%= action.to_s %>
+ end
+
+<% end -%>
protected
def collection
paginate_options ||= {}
paginate_options[:page] ||= (params[:page] || 1)
paginate_options[:per_page] ||= (params[:per_page] || 20)
- @<%= plural_name %> ||= <%= class_name %>.paginate(paginate_options)
+ @collection = @<%= plural_name %> ||= <%= class_name %>.paginate(paginate_options)
end
alias :load_and_paginate_resources :collection
def resource
- @<%= singular_name %> ||= <%= class_name %>.find(params[:id])
+ @resource = @<%= singular_name %> ||= <%= class_name %>.find(params[:id])
end
alias :load_resource :resource
View
14 generators/dry_scaffold/templates/controller_test_standard.rb
@@ -2,6 +2,7 @@
class <%= controller_class_name %>ControllerTest < ActionController::TestCase
+<% if actions.include?(:create) -%>
test "create" do
<%= class_name %>.any_instance.expects(:save).returns(true)
post :create, :<%= file_name %> => { }
@@ -14,6 +15,8 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
assert_template 'new'
end
+<% end -%>
+<% if actions.include?(:destroy) -%>
test "destroy" do
<%= class_name %>.any_instance.expects(:destroy).returns(true)
delete :destroy, :id => <%= table_name %>(:one).to_param
@@ -28,27 +31,37 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
assert_response :redirect
end
+<% end -%>
+<% if actions.include?(:edit) -%>
test "edit" do
get :edit, :id => <%= table_name %>(:one).to_param
assert_response :success
end
+<% end -%>
+<% if actions.include?(:index) -%>
test "index" do
get :index
assert_response :success
assert_not_nil assigns(:<%= table_name %>)
end
+<% end -%>
+<% if actions.include?(:new) -%>
test "new" do
get :new
assert_response :success
end
+<% end -%>
+<% if actions.include?(:show) -%>
test "show" do
get :show, :id => <%= table_name %>(:one).to_param
assert_response :success
end
+<% end -%>
+<% if actions.include?(:update) -%>
test "update" do
<%= class_name %>.any_instance.expects(:save).returns(true)
put :update, :id => <%= table_name %>(:one).to_param, :<%= file_name %> => { }
@@ -61,4 +74,5 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase
assert_template 'edit'
end
+<% end -%>
end
View
1 generators/dry_scaffold/templates/helper_standard.rb
@@ -1,2 +1,3 @@
module <%= controller_class_name %>Helper
+
end
View
1 generators/dry_scaffold/templates/helper_test_standard.rb
@@ -1,4 +1,5 @@
require 'test_helper'
class <%= controller_class_name %>HelperTest < ActionView::TestCase
+
end
View
8 generators/dry_scaffold/templates/prototypes/controller_inherited_resources.rb
@@ -3,17 +3,21 @@ class ResourcesController < InheritedResources::Base
actions :index, :show, :new, :create, :edit, :update, :destroy
respond_to :html, :xml, :json
+ # GET /resources/custom_action
+ def custom_action
+ end
+
protected
def collection
paginate_options ||= {}
paginate_options[:page] ||= (params[:page] || 1)
paginate_options[:per_page] ||= (params[:per_page] || 20)
- @projects ||= end_of_association_chain.paginate(paginate_options)
+ @collection = @resources ||= end_of_association_chain.paginate(paginate_options)
end
def resource
- @project ||= end_of_association_chain.find(params[:id])
+ @resource = @resource ||= end_of_association_chain.find(params[:id])
end
end
View
30 generators/dry_scaffold/templates/prototypes/controller_standard.rb
@@ -1,6 +1,6 @@
class ResourcesController < ApplicationController
- before_filter :load_resource, :except => [:index, :new, :create]
+ before_filter :load_resource, :only => [:show, :edit, :update, :destroy]
before_filter :load_and_paginate_resources, :only => [:index]
# GET /resources
@@ -15,9 +15,9 @@ def index
end
end
- # GET /resources/1
- # GET /resources/1.xml
- # GET /resources/1.json
+ # GET /resources/:id
+ # GET /resources/:id.xml
+ # GET /resources/:id.json
def show
respond_to do |format|
format.html # show.html.haml
@@ -41,7 +41,7 @@ def new
end
end
- # GET /resources/1/edit
+ # GET /resources/:id/edit
def edit
end
@@ -68,9 +68,9 @@ def create
end
end
- # PUT /resources/1
- # PUT /resources/1.xml
- # PUT /resources/1.json
+ # PUT /resources/:id
+ # PUT /resources/:id.xml
+ # PUT /resources/:id.json
def update
respond_to do |format|
if @resource.update_attributes(params[:resource])
@@ -89,9 +89,9 @@ def update
end
end
- # DELETE /resources/1
- # DELETE /resources/1.xml
- # DELETE /resources/1.json
+ # DELETE /resources/:id
+ # DELETE /resources/:id.xml
+ # DELETE /resources/:id.json
def destroy
respond_to do |format|
if @resource.destroy
@@ -110,18 +110,22 @@ def destroy
end
end
+ # GET /resources/custom_action
+ def custom_action
+ end
+
protected
def collection
paginate_options ||= {}
paginate_options[:page] ||= (params[:page] || 1)
paginate_options[:per_page] ||= (params[:per_page] || 20)
- @resources ||= Resource.paginate(paginate_options)
+ @collection = @resources ||= Resource.paginate(paginate_options)
end
alias :load_and_paginate_resources :collection
def resource
- @resource ||= Resource.find(params[:id])
+ @resource = @resource = ||= Resource.find(params[:id])
end
alias :load_resource :resource
View
1 generators/dry_scaffold/templates/prototypes/helper_standard.rb
@@ -1,2 +1,3 @@
module ResourcesHelper
+
end
View
1 generators/dry_scaffold/templates/prototypes/helper_test_standard.rb
@@ -1,4 +1,5 @@
require 'test_helper'
class ResourcesHelperTest < ActionView::TestCase
+
end
View
6 generators/dry_scaffold/templates/prototypes/view__form.html.haml
@@ -1,3 +1,3 @@
-- f.inputs do
- = f.input :title
- = f.input :description
+- form.inputs do
+ = form.input :title
+ = form.input :description
View
8 generators/dry_scaffold/templates/prototypes/view_edit.html.haml
@@ -1,10 +1,10 @@
%h1.heading
= "Editing resource %s" % @resource.id
-- semantic_form_for(@resource) do |f|
- = render 'form', :f => f
- - f.buttons do
- = f.commit_button 'Update'
+- semantic_form_for(@resource) do |form|
+ = render 'form', :form => form
+ - form.buttons do
+ = form.commit_button 'Update'
%p.actions
= link_to 'Cancel', resources_url
View
8 generators/dry_scaffold/templates/prototypes/view_new.html.haml
@@ -1,10 +1,10 @@
%h1.heading
= 'New resource'
-- semantic_form_for(@resource) do |f|
- = render 'form', :f => f
- - f.buttons do
- = f.commit_button 'Create'
+- semantic_form_for(@resource) do |form|
+ = render 'form', :form => form
+ - form.buttons do
+ = form.commit_button 'Create'
%p.actions
= link_to 'Cancel', resources_url
View
8 generators/dry_scaffold/templates/view__form.html.haml
@@ -1,13 +1,13 @@
<% if options[:formtastic] -%>
-- f.inputs do
+- form.inputs do
<% attributes.each do |attribute| -%>
- = f.input :<%= attribute.name %>, :label => '<%= attribute.name.humanize %>'
+ = form.input :<%= attribute.name %>, :label => '<%= attribute.name.humanize %>'
<% end -%>
<% else -%>
%fieldset
%dl
<% attributes.each do |attribute| -%>
- %dt.label= f.label :<%= attribute.name %>, '<%= attribute.name.humanize %>'
- %dd.<%= attribute.name %>= f.<%= attribute.field_type %> :<%= attribute.name %>
+ %dt.label= form.label :<%= attribute.name %>, '<%= attribute.name.humanize %>'
+ %dd.<%= attribute.name %>= form.<%= attribute.field_type %> :<%= attribute.name %>
<% end -%>
<% end -%>
View
2 generators/dry_scaffold/templates/view__item.html.haml
@@ -1,4 +1,4 @@
-- content_tag_for(:tr, <%= singular_name -%>) do
+- content_tag_for(:tr, <%= singular_name -%>, :class => cycle(:odd, :even)) do
<% attributes.each do |attribute| -%>
%td.<%= attribute.name %>= h <%= singular_name %>.try(:<%= attribute.name %>)
<% end -%>
View
20 generators/dry_scaffold/templates/view_edit.html.haml
@@ -2,17 +2,17 @@
= "Editing <%= singular_name %> %s" % @<%= singular_name %>.id
<% if options[:formtastic] -%>
-- semantic_form_for(@<%= singular_name %>) do |f|
- = render 'form', :f => f
- - f.buttons do
- = f.commit_button 'Update'
+- semantic_form_for(@<%= singular_name %>) do |form|
+ = render form
+ - form.buttons do
+ = form.commit_button 'Update'
<% else -%>
-- form_for(@<%= singular_name %>) do |f|
- = f.error_messages
- = render 'form', :f => f
+- form_for(@<%= singular_name %>) do |form|
+ = form.error_messages
+ = render form
%p.buttons
- = f.submit 'Update'
+ = form.submit 'Update'
<% end -%>
-
+
%p.actions
- = link_to 'Cancel', <%= plural_name %>_url
+ = link_to 'Cancel', <%= collection_name %>_url
View
18 generators/dry_scaffold/templates/view_new.html.haml
@@ -2,17 +2,17 @@
= 'New <%= singular_name.humanize %>'
<% if options[:formtastic] -%>
-- semantic_form_for(@<%= singular_name %>) do |f|
- = render 'form', :f => f
- - f.buttons do
- = f.commit_button 'Create'
+- semantic_form_for(@<%= singular_name %>) do |form|
+ = render form
+ - form.buttons do
+ = form.commit_button 'Create'
<% else -%>
-- form_for(@<%= singular_name %>) do |f|
- = f.error_messages
- = render 'form', :f => f
+- form_for(@<%= singular_name %>) do |form|
+ = form.error_messages
+ = render form
%p.buttons
- = f.submit 'Create'
+ = form.submit 'Create'
<% end -%>
%p.actions
- = link_to 'Cancel', <%= plural_name %>_url
+ = link_to 'Cancel', <%= collection_name %>_url
View
2 generators/dry_scaffold/templates/view_show.html.haml
@@ -10,4 +10,4 @@
%p.actions
= link_to 'Edit', edit_<%= singular_name %>_url(@<%= singular_name %>)
|
- = link_to 'Index', <%= plural_name %>_url
+ = link_to 'Index', <%= collection_name %>_url
View
3 rails/init.rb
@@ -0,0 +1,3 @@
+Dir[File.expand_path(File.join(File.dirname(__FILE__), 'lib', '**', '*.rb'))].uniq.each do |file|
+ require file
+end

0 comments on commit 570dbaa

Please sign in to comment.