diff --git a/README.rdoc b/README.rdoc index 5b3ca5b..4812b8b 100644 --- a/README.rdoc +++ b/README.rdoc @@ -7,21 +7,21 @@ resource_controller makes RESTful controllers easier, more maintainable, and sup Add it as a gem dependency config.gem 'resource_controller' - + ...or for the development version - + config.gem 'giraffesoft-resource_controller', :lib => 'resource_controller', :source => 'http://gems.github.com' - -Or install it as a gem manually - + +Or install it as a gem manually + sudo gem install resource_controller - + ...or for the development version - + sudo gem install giraffesoft-resource_controller - + Or grab the source - + git clone git://github.com/giraffesoft/resource_controller.git = Usage @@ -30,7 +30,7 @@ Creating a basic RESTful controller is as easy as... class PostsController < ResourceController::Base end - + ...or if you prefer, you can use the method-call syntax. If you need to inherit from some other class, this syntax is definitely for you: class PostsController < ApplicationController @@ -38,8 +38,8 @@ Creating a basic RESTful controller is as easy as... end Both syntaxes are identical in their behavior. Just make sure you call resource_controller before you use any other r_c functionality in your controller. - - + + Nobody just uses the default RESTful controller, though. resource_controller provides a simple API for customizations. == Action Lifecycle @@ -51,17 +51,17 @@ It's really easy to make changes to the lifecycle of your actions. === Before and After class ProjectsController < ResourceController::Base - + new_action.before do 3.times { object.tasks.build } end - + create.after do object.creator = current_user end - + end - + === Flash class ProjectsController < ResourceController::Base @@ -71,20 +71,20 @@ It's really easy to make changes to the lifecycle of your actions. === respond_to You can add to what's already there... - - class ProjectsController < ResourceController::Base + + class ProjectsController < ResourceController::Base create.wants.js { render :template => "show.rjs" } end - + Or you can create a whole new block. This syntax destroys everything that's there, and starts again... - class ProjectsController < ResourceController::Base + class ProjectsController < ResourceController::Base create.response do |wants| wants.html wants.js { render :template => "show.rjs" } end end - + === Scoping Because sometimes you want to make a bunch of customizations at once, most of the helpers accept blocks that make grouping calls really easy. Is it a DSL? Maybe; maybe not. But, it's definitely awesome. @@ -92,25 +92,25 @@ Because sometimes you want to make a bunch of customizations at once, most of th With actions that can fail, the scoping defaults to success. That means that create.flash == create.success.flash. class ProjectsController < ResourceController::Base - + create do flash "Object successfully created!" wants.js { render :template => "show.rjs" } - + failure.wants.js { render :template => "display_errors.rjs" } end - + destroy do flash "You destroyed your project. Good work." - + failure do flash "You cannot destroy that project. Stop trying!" wants.js { render :template => "display_errors.rjs" } end end - + end - + == Singleton Resource If you want to create a singleton RESTful controller inherit from ResourceController::Singleton. @@ -129,7 +129,7 @@ Loading objects in singletons is similar to plural controllers with one exceptio end end -In other cases you can use the default logic and override it only if you use permalinks or anything special. +In other cases you can use the default logic and override it only if you use permalinks or anything special. Singleton nesting with both :has_many and :has_one associations is provided... @@ -159,7 +159,7 @@ You want to add something like pagination to your controller... @collection ||= end_of_association_chain.find(:all, :page => {:size => 10, :current => params[:page]}) end end - + Or maybe you used a permalink... class PostsController < ResourceController::Base @@ -179,7 +179,7 @@ Maybe you have some alternative way of building objects... @object ||= end_of_association_chain.build_my_object_some_funky_way object_params end end - + ...and there are tons more helpers in the ResourceController::Helpers == Nested Resources @@ -189,7 +189,7 @@ Nested controllers can be a pain, especially if routing is such that you may or class CommentsController < ResourceController::Base belongs_to :post end - + All of the finding, and creation, and everything will be done at the scope of the post automatically. == Namespaced Resources @@ -199,18 +199,18 @@ All of the finding, and creation, and everything will be done at the scope of th == Polymorphic Resources Everything, including url generation is handled completely automatically. Take this example... - + ## comment.rb class Comment belongs_to :commentable, :polymorphic => true end - + ## comments_controller.rb class CommentsController < ResourceController::Base belongs_to :post, :product, :user end *Note:* Your model doesn't have to be polymorphic in the ActiveRecord sense. It can be associated in whichever way you want. - + ## routes.rb map.resources :posts, :has_many => :comments map.resources :products, :has_many => :comments @@ -243,7 +243,7 @@ The most common example is where the resource has a different name than the asso 'photo_tag' end end - + In the above example, the variable, and params will be set to @tag, @tags, and params[:tag]. If you'd like to change that, override object_name. def object_name @@ -262,7 +262,7 @@ If you're using a non-standard controller name, but everything else is standard, 'tag' end end - + Finally, the route_name helper is used by Urligence to determine which url helper to call, so if you have non-standard route names, override it. map.resources :tags, :controller => "taggings" @@ -284,7 +284,7 @@ No matter what your controller looks like... [edit_|new_]object_url # is the equivalent of saying [edit_|new_]post_url(@post) [edit_|new_]object_url(some_other_object) # allows you to specify an object, but still maintain any paths or namespaces that are present - + collection_url # is like saying posts_url Url helpers are especially useful when working with polymorphic controllers. @@ -294,19 +294,19 @@ Url helpers are especially useful when working with polymorphic controllers. object_url(comment) # => /posts/1/comments/#{comment.to_param} edit_object_url # => /posts/1/comments/#{@comment.to_param}/edit collection_url # => /posts/1/comments - + # /products/1/comments object_url # => /products/1/comments/#{@comment.to_param} object_url(comment) # => /products/1/comments/#{comment.to_param} edit_object_url # => /products/1/comments/#{@comment.to_param}/edit collection_url # => /products/1/comments - + # /comments object_url # => /comments/#{@comment.to_param} object_url(comment) # => /comments/#{comment.to_param} edit_object_url # => /comments/#{@comment.to_param}/edit collection_url # => /comments - + Or with namespaced, nested controllers... # /admin/products/1/options @@ -314,7 +314,7 @@ Or with namespaced, nested controllers... object_url(option) # => /admin/products/1/options/#{option.to_param} edit_object_url # => /admin/products/1/options/#{@option.to_param}/edit collection_url # => /admin/products/1/options - + You get the idea. Everything is automagical! All parameters are inferred. == Credits diff --git a/Rakefile b/Rakefile index 11b7709..87b5c8b 100644 --- a/Rakefile +++ b/Rakefile @@ -26,10 +26,10 @@ end task :upload_docs => :rdoc do puts 'Deleting previous rdoc' `ssh jamesgolick.com 'rm -Rf /home/apps/jamesgolick.com/public/resource_controller/rdoc'` - + puts "Uploading current rdoc" `scp -r rdoc jamesgolick.com:/home/apps/jamesgolick.com/public/resource_controller` - + puts "Deleting rdoc" `rm -Rf rdoc` end diff --git a/generators/scaffold_resource/USAGE b/generators/scaffold_resource/USAGE index 9ccf4d9..c94b85e 100644 --- a/generators/scaffold_resource/USAGE +++ b/generators/scaffold_resource/USAGE @@ -4,20 +4,20 @@ Description: conventions to exploit the full set of HTTP verbs (GET/POST/PUT/DELETE) and is prepared for multi-client access (like one view for HTML, one for an XML API, one for ATOM, etc). Everything comes with sample unit and functional tests as well. - - The generator takes the name of the model as its first argument. This model name is then pluralized to get the + + The generator takes the name of the model as its first argument. This model name is then pluralized to get the controller name. So "scaffold_resource post" will generate a Post model and a PostsController and will be intended for URLs like /posts and /posts/45. - + As additional parameters, the generator will take attribute pairs described by name and type. These attributes will be used to prepopulate the migration to create the table for the model and to give you a set of templates for the view. For example, "scaffold_resource post title:string created_on:date body:text published:boolean" will give - you a model with those four attributes, forms to create and edit those models from, and an index that'll list them + you a model with those four attributes, forms to create and edit those models from, and an index that'll list them all. - - You don't have to think up all attributes up front, but it's a good idea of adding just the baseline of what's + + You don't have to think up all attributes up front, but it's a good idea of adding just the baseline of what's needed to start really working with the resource. - + Once the generator has run, you'll need to add a declaration to your config/routes.rb file to hook up the rules that'll point URLs to this new resource. If you create a resource like "scaffold_resource post", you'll need to add "map.resources :posts" (notice the plural form) in the routes file. Then your new resource is accessible from diff --git a/generators/scaffold_resource/scaffold_resource_generator.rb b/generators/scaffold_resource/scaffold_resource_generator.rb index ed5d3b6..50ecd48 100644 --- a/generators/scaffold_resource/scaffold_resource_generator.rb +++ b/generators/scaffold_resource/scaffold_resource_generator.rb @@ -25,14 +25,14 @@ def initialize(runtime_args, runtime_options = {}) end @generator_default_file_extension = (defined? Haml )? "haml" : "erb" - + # we want to call erb templates .rhtml or .haml if this is rails 1 if RAILS_GEM_VERSION.to_i == 1 @default_file_extension = @generator_default_file_extension == 'erb' ? 'rhtml' : @generator_default_file_extension else @default_file_extension = "html.#{@generator_default_file_extension}" end - + @controller_name = @name.pluralize base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name) @@ -67,7 +67,7 @@ def manifest m.directory(File.join('test/functional', controller_class_path)) m.directory(File.join('test/unit', class_path)) end - + scaffold_views.each do |action| m.template( "view_#{action}.#{generator_default_file_extension}", @@ -104,13 +104,13 @@ def manifest unless options[:skip_migration] migration_template = RAILS_GEM_VERSION.to_i == 1 ? 'old_migration.rb' : 'migration.rb' - + m.migration_template( - migration_template, 'db/migrate', + migration_template, 'db/migrate', :assigns => { :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}", :attributes => attributes - }, + }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}" ) end @@ -123,7 +123,7 @@ def manifest def has_rspec? options[:rspec] || (File.exist?('spec') && File.directory?('spec')) end - + protected # Override with your own usage banner. def banner @@ -133,12 +133,12 @@ def banner def rspec_views %w[ index show new edit ] end - + def scaffold_views rspec_views + %w[ _form ] end - def model_name + def model_name class_name.demodulize end @@ -164,7 +164,7 @@ def default_value when :boolean then "false" else "" - end + end end def input_type @@ -172,7 +172,7 @@ def input_type when :text then "textarea" else "input" - end + end end end end diff --git a/generators/scaffold_resource/templates/functional_test.rb b/generators/scaffold_resource/templates/functional_test.rb index 0c68fd3..553eed3 100644 --- a/generators/scaffold_resource/templates/functional_test.rb +++ b/generators/scaffold_resource/templates/functional_test.rb @@ -23,12 +23,12 @@ def test_should_get_new get :new assert_response :success end - + def test_should_create_<%= file_name %> old_count = <%= class_name %>.count post :create, :<%= file_name %> => { } assert_equal old_count+1, <%= class_name %>.count - + assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>)) end @@ -41,17 +41,17 @@ def test_should_get_edit get :edit, :id => 1 assert_response :success end - + def test_should_update_<%= file_name %> put :update, :id => 1, :<%= file_name %> => { } assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>)) end - + def test_should_destroy_<%= file_name %> old_count = <%= class_name %>.count delete :destroy, :id => 1 assert_equal old_count-1, <%= class_name %>.count - + assert_redirected_to <%= table_name %>_path end end diff --git a/generators/scaffold_resource/templates/rspec/functional_spec.rb b/generators/scaffold_resource/templates/rspec/functional_spec.rb index 6592e8c..cf5255a 100644 --- a/generators/scaffold_resource/templates/rspec/functional_spec.rb +++ b/generators/scaffold_resource/templates/rspec/functional_spec.rb @@ -7,11 +7,11 @@ @<%= file_name %> = mock_model(<%= controller_class_name.singularize %>) <%= controller_class_name.singularize %>.stub!(:find).and_return([@<%= file_name %>]) end - + def do_get get :index end - + it "should be successful" do do_get response.should be_success @@ -21,12 +21,12 @@ def do_get do_get response.should render_template('index') end - + it "should find all <%= table_name %>" do <%= controller_class_name.singularize %>.should_receive(:find).with(:all).and_return([@<%= file_name %>]) do_get end - + it "should assign the found <%= table_name %> for the view" do do_get assigns[:<%= table_name %>].should == [@<%= file_name %>] @@ -39,7 +39,7 @@ def do_get @<%= file_name %> = mock_model(<%= controller_class_name.singularize %>) <%= controller_class_name.singularize %>.stub!(:find).and_return(@<%= file_name %>) end - + def do_get get :show, :id => "1" end @@ -48,17 +48,17 @@ def do_get do_get response.should be_success end - + it "should render show template" do do_get response.should render_template('show') end - + it "should find the <%= file_name %> requested" do <%= controller_class_name.singularize %>.should_receive(:find).with("1").and_return(@<%= file_name %>) do_get end - + it "should assign the found <%= file_name %> for the view" do do_get assigns[:<%= file_name %>].should equal(@<%= file_name %>) @@ -71,7 +71,7 @@ def do_get @<%= file_name %> = mock_model(<%= controller_class_name.singularize %>) <%= controller_class_name.singularize %>.stub!(:new).and_return(@<%= file_name %>) end - + def do_get get :new end @@ -80,22 +80,22 @@ def do_get do_get response.should be_success end - + it "should render new template" do do_get response.should render_template('new') end - + it "should create an new <%= file_name %>" do <%= controller_class_name.singularize %>.should_receive(:new).and_return(@<%= file_name %>) do_get end - + it "should not save the new <%= file_name %>" do @<%= file_name %>.should_not_receive(:save) do_get end - + it "should assign the new <%= file_name %> for the view" do do_get assigns[:<%= file_name %>].should equal(@<%= file_name %>) @@ -108,7 +108,7 @@ def do_get @<%= file_name %> = mock_model(<%= controller_class_name.singularize %>) <%= controller_class_name.singularize %>.stub!(:find).and_return(@<%= file_name %>) end - + def do_get get :edit, :id => "1" end @@ -117,17 +117,17 @@ def do_get do_get response.should be_success end - + it "should render edit template" do do_get response.should render_template('edit') end - + it "should find the <%= file_name %> requested" do <%= controller_class_name.singularize %>.should_receive(:find).and_return(@<%= file_name %>) do_get end - + it "should assign the found <%= controller_class_name %> for the view" do do_get assigns[:<%= file_name %>].should equal(@<%= file_name %>) @@ -140,14 +140,14 @@ def do_get @<%= file_name %> = mock_model(<%= controller_class_name.singularize %>, :to_param => "1") <%= controller_class_name.singularize %>.stub!(:new).and_return(@<%= file_name %>) end - + describe "with successful save" do - + def do_post @<%= file_name %>.should_receive(:save).and_return(true) post :create, :<%= file_name %> => {} end - + it "should create a new <%= file_name %>" do <%= controller_class_name.singularize %>.should_receive(:new).with({}).and_return(@<%= file_name %>) do_post @@ -157,21 +157,21 @@ def do_post do_post response.should redirect_to(<%= table_name.singularize %>_url("1")) end - + end - + describe "with failed save" do def do_post @<%= file_name %>.should_receive(:save).and_return(false) post :create, :<%= file_name %> => {} end - + it "should re-render 'new'" do do_post response.should render_template('new') end - + end end @@ -181,7 +181,7 @@ def do_post @<%= file_name %> = mock_model(<%= controller_class_name.singularize %>, :to_param => "1") <%= controller_class_name.singularize %>.stub!(:find).and_return(@<%= file_name %>) end - + describe "with successful update" do def do_put @@ -210,7 +210,7 @@ def do_put end end - + describe "with failed update" do def do_put @@ -232,7 +232,7 @@ def do_put @<%= file_name %> = mock_model(<%= controller_class_name.singularize %>, :destroy => true) <%= controller_class_name.singularize %>.stub!(:find).and_return(@<%= file_name %>) end - + def do_delete delete :destroy, :id => "1" end @@ -241,12 +241,12 @@ def do_delete <%= controller_class_name.singularize %>.should_receive(:find).with("1").and_return(@<%= file_name %>) do_delete end - + it "should call destroy on the found <%= file_name %>" do - @<%= file_name %>.should_receive(:destroy).and_return(true) + @<%= file_name %>.should_receive(:destroy).and_return(true) do_delete end - + it "should redirect to the <%= table_name %> list" do do_delete response.should redirect_to(<%= table_name %>_url) diff --git a/generators/scaffold_resource/templates/rspec/helper_spec.rb b/generators/scaffold_resource/templates/rspec/helper_spec.rb index 197bbc6..dca68a6 100644 --- a/generators/scaffold_resource/templates/rspec/helper_spec.rb +++ b/generators/scaffold_resource/templates/rspec/helper_spec.rb @@ -1,11 +1,11 @@ require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * controller_class_nesting_depth %>/../spec_helper') describe <%= controller_class_name %>Helper do - + #Delete this example and add some real ones or delete this file it "should be included in the object returned by #helper" do included_modules = (class << helper; self; end).send :included_modules included_modules.should include(<%= controller_class_name %>Helper) end - + end diff --git a/generators/scaffold_resource/templates/rspec/routing_spec.rb b/generators/scaffold_resource/templates/rspec/routing_spec.rb index 83cb674..eea9504 100644 --- a/generators/scaffold_resource/templates/rspec/routing_spec.rb +++ b/generators/scaffold_resource/templates/rspec/routing_spec.rb @@ -6,23 +6,23 @@ it "should map { :controller => '<%= table_name %>', :action => 'index' } to /<%= table_name %>" do route_for(:controller => "<%= table_name %>", :action => "index").should == "/<%= table_name %>" end - + it "should map { :controller => '<%= table_name %>', :action => 'new' } to /<%= table_name %>/new" do route_for(:controller => "<%= table_name %>", :action => "new").should == "/<%= table_name %>/new" end - + it "should map { :controller => '<%= table_name %>', :action => 'show', :id => 1 } to /<%= table_name %>/1" do route_for(:controller => "<%= table_name %>", :action => "show", :id => 1).should == "/<%= table_name %>/1" end - + it "should map { :controller => '<%= table_name %>', :action => 'edit', :id => 1 } to /<%= table_name %>/1<%= resource_edit_path %>" do route_for(:controller => "<%= table_name %>", :action => "edit", :id => 1).should == "/<%= table_name %>/1<%= resource_edit_path %>" end - + it "should map { :controller => '<%= table_name %>', :action => 'update', :id => 1} to /<%= table_name %>/1" do route_for(:controller => "<%= table_name %>", :action => "update", :id => 1).should == "/<%= table_name %>/1" end - + it "should map { :controller => '<%= table_name %>', :action => 'destroy', :id => 1} to /<%= table_name %>/1" do route_for(:controller => "<%= table_name %>", :action => "destroy", :id => 1).should == "/<%= table_name %>/1" end @@ -33,27 +33,27 @@ it "should generate params { :controller => '<%= table_name %>', action => 'index' } from GET /<%= table_name %>" do params_from(:get, "/<%= table_name %>").should == {:controller => "<%= table_name %>", :action => "index"} end - + it "should generate params { :controller => '<%= table_name %>', action => 'new' } from GET /<%= table_name %>/new" do params_from(:get, "/<%= table_name %>/new").should == {:controller => "<%= table_name %>", :action => "new"} end - + it "should generate params { :controller => '<%= table_name %>', action => 'create' } from POST /<%= table_name %>" do params_from(:post, "/<%= table_name %>").should == {:controller => "<%= table_name %>", :action => "create"} end - + it "should generate params { :controller => '<%= table_name %>', action => 'show', id => '1' } from GET /<%= table_name %>/1" do params_from(:get, "/<%= table_name %>/1").should == {:controller => "<%= table_name %>", :action => "show", :id => "1"} end - + it "should generate params { :controller => '<%= table_name %>', action => 'edit', id => '1' } from GET /<%= table_name %>/1;edit" do params_from(:get, "/<%= table_name %>/1<%= resource_edit_path %>").should == {:controller => "<%= table_name %>", :action => "edit", :id => "1"} end - + it "should generate params { :controller => '<%= table_name %>', action => 'update', id => '1' } from PUT /<%= table_name %>/1" do params_from(:put, "/<%= table_name %>/1").should == {:controller => "<%= table_name %>", :action => "update", :id => "1"} end - + it "should generate params { :controller => '<%= table_name %>', action => 'destroy', id => '1' } from DELETE /<%= table_name %>/1" do params_from(:delete, "/<%= table_name %>/1").should == {:controller => "<%= table_name %>", :action => "destroy", :id => "1"} end diff --git a/generators/scaffold_resource/templates/rspec/views/edit_spec.rb b/generators/scaffold_resource/templates/rspec/views/edit_spec.rb index 120b952..95f8ac6 100644 --- a/generators/scaffold_resource/templates/rspec/views/edit_spec.rb +++ b/generators/scaffold_resource/templates/rspec/views/edit_spec.rb @@ -2,7 +2,7 @@ describe "/<%= table_name %>/edit.<%= default_file_extension %>" do include <%= controller_class_name %>Helper - + before do @<%= file_name %> = mock_model(<%= class_name %>) <% for attribute in attributes -%> @@ -10,13 +10,13 @@ <% end -%> assigns[:<%= file_name %>] = @<%= file_name %> - template.should_receive(:object_url).twice.and_return(<%= file_name %>_path(@<%= file_name %>)) - template.should_receive(:collection_url).and_return(<%= file_name.pluralize %>_path) + template.should_receive(:object_url).twice.and_return(<%= file_name %>_path(@<%= file_name %>)) + template.should_receive(:collection_url).and_return(<%= file_name.pluralize %>_path) end it "should render edit form" do render "/<%= table_name %>/edit.<%= default_file_extension %>" - + response.should have_tag("form[action=#{<%= file_name %>_path(@<%= file_name %>)}][method=post]") do <% for attribute in attributes -%><% unless attribute.name =~ /_id/ || [:datetime, :timestamp, :time, :date].index(attribute.type) -%> with_tag('<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>[name=?]', "<%= file_name %>[<%= attribute.name %>]") diff --git a/generators/scaffold_resource/templates/rspec/views/index_spec.rb b/generators/scaffold_resource/templates/rspec/views/index_spec.rb index 44c05bb..5a51207 100644 --- a/generators/scaffold_resource/templates/rspec/views/index_spec.rb +++ b/generators/scaffold_resource/templates/rspec/views/index_spec.rb @@ -2,7 +2,7 @@ describe "/<%= table_name %>/index.<%= default_file_extension %>" do include <%= controller_class_name %>Helper - + before(:each) do <% [98,99].each do |id| -%> <%= file_name %>_<%= id %> = mock_model(<%= class_name %>) @@ -11,9 +11,9 @@ <% end -%><% end %> assigns[:<%= table_name %>] = [<%= file_name %>_98, <%= file_name %>_99] - template.stub!(:object_url).and_return(<%= file_name %>_path(@<%= file_name %>)) - template.stub!(:new_object_url).and_return(new_<%= file_name %>_path) - template.stub!(:edit_object_url).and_return(edit_<%= file_name %>_path(@<%= file_name %>)) + template.stub!(:object_url).and_return(<%= file_name %>_path(@<%= file_name %>)) + template.stub!(:new_object_url).and_return(new_<%= file_name %>_path) + template.stub!(:edit_object_url).and_return(edit_<%= file_name %>_path(@<%= file_name %>)) end it "should render list of <%= table_name %>" do diff --git a/generators/scaffold_resource/templates/rspec/views/new_spec.rb b/generators/scaffold_resource/templates/rspec/views/new_spec.rb index 76f6346..ed384e0 100644 --- a/generators/scaffold_resource/templates/rspec/views/new_spec.rb +++ b/generators/scaffold_resource/templates/rspec/views/new_spec.rb @@ -2,7 +2,7 @@ describe "/<%= table_name %>/new.<%= default_file_extension %>" do include <%= controller_class_name %>Helper - + before(:each) do @<%= file_name %> = mock_model(<%= class_name %>) @<%= file_name %>.stub!(:new_record?).and_return(true) @@ -12,13 +12,13 @@ assigns[:<%= file_name %>] = @<%= file_name %> - template.stub!(:object_url).and_return(<%= file_name %>_path(@<%= file_name %>)) - template.stub!(:collection_url).and_return(<%= file_name.pluralize %>_path) + template.stub!(:object_url).and_return(<%= file_name %>_path(@<%= file_name %>)) + template.stub!(:collection_url).and_return(<%= file_name.pluralize %>_path) end it "should render new form" do render "/<%= table_name %>/new.<%= default_file_extension %>" - + response.should have_tag("form[action=?][method=post]", <%= table_name %>_path) do <% for attribute in attributes -%><% unless attribute.name =~ /_id/ || [:datetime, :timestamp, :time, :date].index(attribute.type) -%> with_tag("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>[name=?]", "<%= file_name %>[<%= attribute.name %>]") diff --git a/generators/scaffold_resource/templates/rspec/views/show_spec.rb b/generators/scaffold_resource/templates/rspec/views/show_spec.rb index b17677f..9b33793 100644 --- a/generators/scaffold_resource/templates/rspec/views/show_spec.rb +++ b/generators/scaffold_resource/templates/rspec/views/show_spec.rb @@ -2,7 +2,7 @@ describe "/<%= table_name %>/show.<%= default_file_extension %>" do include <%= controller_class_name %>Helper - + before(:each) do @<%= file_name %> = mock_model(<%= class_name %>) <% for attribute in attributes -%> @@ -11,8 +11,8 @@ assigns[:<%= file_name %>] = @<%= file_name %> - template.stub!(:edit_object_url).and_return(edit_<%= file_name %>_path(@<%= file_name %>)) - template.stub!(:collection_url).and_return(<%= file_name.pluralize %>_path) + template.stub!(:edit_object_url).and_return(edit_<%= file_name %>_path(@<%= file_name %>)) + template.stub!(:collection_url).and_return(<%= file_name.pluralize %>_path) end it "should render attributes in

" do diff --git a/generators/scaffold_resource/templates/view_edit.haml b/generators/scaffold_resource/templates/view_edit.haml index 838ae42..270dd24 100644 --- a/generators/scaffold_resource/templates/view_edit.haml +++ b/generators/scaffold_resource/templates/view_edit.haml @@ -5,7 +5,7 @@ - form_for(:<%= singular_name %>, :url => object_url, :html => { :method => :put }) do |f| = render :partial => "form", :locals => { :f => f } %p= submit_tag "Update" - + = link_to 'Show', object_url | = link_to 'Back', collection_url \ No newline at end of file diff --git a/generators/scaffold_resource/templates/view_index.erb b/generators/scaffold_resource/templates/view_index.erb index 66a99d3..dad452e 100644 --- a/generators/scaffold_resource/templates/view_index.erb +++ b/generators/scaffold_resource/templates/view_index.erb @@ -10,7 +10,7 @@ <%- for attribute in attributes -%> <%%=h <%= singular_name %>.<%= attribute.name %> %> - <%- end -%> + <%- end -%> <%%=link_to 'Show', object_url(<%= singular_name %>) %> <%%=link_to 'Edit', edit_object_url(<%= singular_name %>) %> <%%=link_to 'Destroy', object_url(<%= singular_name %>), :confirm => 'Are you sure?', :method => :delete %> diff --git a/generators/scaffold_resource/templates/view_index.haml b/generators/scaffold_resource/templates/view_index.haml index 2fcee6d..af4d269 100644 --- a/generators/scaffold_resource/templates/view_index.haml +++ b/generators/scaffold_resource/templates/view_index.haml @@ -9,7 +9,7 @@ %tr <% for attribute in attributes -%> %td= h <%= singular_name %>.<%= attribute.name %> - <% end -%> + <% end -%> %td= link_to 'Show', object_url(<%= singular_name %>) %td= link_to 'Edit', edit_object_url(<%= singular_name %>) %td= link_to 'Destroy', object_url(<%= singular_name %>), :confirm => 'Are you sure?', :method => :delete diff --git a/lib/resource_controller.rb b/lib/resource_controller.rb index 4a724be..6744635 100644 --- a/lib/resource_controller.rb +++ b/lib/resource_controller.rb @@ -17,18 +17,18 @@ module ResourceController ACTIONS = [:index, :show, :new_action, :create, :edit, :update, :destroy].freeze SINGLETON_ACTIONS = (ACTIONS - [:index]).freeze FAILABLE_ACTIONS = ACTIONS - [:index, :new_action, :edit].freeze - NAME_ACCESSORS = [:model_name, :route_name, :object_name] - + NAME_ACCESSORS = [:model_name, :route_name, :object_name] + module ActionControllerExtension unloadable - + def resource_controller(*args) include ResourceController::Controller - + if args.include?(:singleton) include ResourceController::Helpers::SingletonCustomizations end - end + end end end diff --git a/lib/resource_controller/accessors.rb b/lib/resource_controller/accessors.rb index 38cd978..cf1e069 100644 --- a/lib/resource_controller/accessors.rb +++ b/lib/resource_controller/accessors.rb @@ -10,16 +10,16 @@ def #{block_accessor}(*args, &block) args.push block if block_given? @#{block_accessor} = [args].flatten end - + @#{block_accessor} end end_eval end end - + def scoping_reader(*accessor_names) - accessor_names.each do |accessor_name| + accessor_names.each do |accessor_name| class_eval <<-"end_eval", __FILE__, __LINE__ def #{accessor_name}(&block) @#{accessor_name}.instance_eval &block if block_given? @@ -28,10 +28,10 @@ def #{accessor_name}(&block) end_eval end end - + def class_scoping_reader(accessor_name, start_value) write_inheritable_attribute accessor_name, start_value - + class_eval <<-"end_eval", __FILE__, __LINE__ def self.#{accessor_name}(&block) read_inheritable_attribute(:#{accessor_name}).instance_eval(&block) if block_given? @@ -39,7 +39,7 @@ def self.#{accessor_name}(&block) end end_eval end - + def reader_writer(accessor_name) class_eval <<-"end_eval", __FILE__, __LINE__ def #{accessor_name}(*args, &block) @@ -49,7 +49,7 @@ def #{accessor_name}(*args, &block) end end_eval end - + def class_reader_writer(*accessor_names) accessor_names.each do |accessor_name| class_eval <<-"end_eval", __FILE__, __LINE__ @@ -58,16 +58,16 @@ def self.#{accessor_name}(*args) write_inheritable_attribute :#{accessor_name}, args.first if args.length == 1 write_inheritable_attribute :#{accessor_name}, args if args.length > 1 end - + read_inheritable_attribute :#{accessor_name} end - + def #{accessor_name}(*args) unless args.empty? self.class.write_inheritable_attribute :#{accessor_name}, args.first if args.length == 1 self.class.write_inheritable_attribute :#{accessor_name}, args if args.length > 1 end - + self.class.read_inheritable_attribute :#{accessor_name} end end_eval diff --git a/lib/resource_controller/action_options.rb b/lib/resource_controller/action_options.rb index 203b01d..3fe45f3 100644 --- a/lib/resource_controller/action_options.rb +++ b/lib/resource_controller/action_options.rb @@ -1,32 +1,32 @@ module ResourceController class ActionOptions extend ResourceController::Accessors - + reader_writer :flash reader_writer :flash_now - + block_accessor :after, :before - + def initialize @collector = ResourceController::ResponseCollector.new end - + def response(*args, &block) if !args.empty? || block_given? @collector.clear args.flatten.each { |symbol| @collector.send(symbol) } block.call(@collector) if block_given? end - + @collector.responses end alias_method :respond_to, :response alias_method :responds_to, :response - + def wants @collector end - + def dup returning self.class.new do |duplicate| duplicate.instance_variable_set(:@collector, wants.dup) diff --git a/lib/resource_controller/actions.rb b/lib/resource_controller/actions.rb index 3600e1f..6e1503e 100644 --- a/lib/resource_controller/actions.rb +++ b/lib/resource_controller/actions.rb @@ -1,12 +1,12 @@ module ResourceController module Actions - + def index load_collection before :index response_for :index end - + def show load_object before :show @@ -70,6 +70,6 @@ def destroy response_for :destroy_fails end end - + end end diff --git a/lib/resource_controller/base.rb b/lib/resource_controller/base.rb index f990a32..340c06b 100644 --- a/lib/resource_controller/base.rb +++ b/lib/resource_controller/base.rb @@ -1,12 +1,12 @@ module ResourceController - + # == ResourceController::Base - # + # # Inherit from this class to create your RESTful controller. See the README for usage. - # + # class Base < ApplicationController unloadable - + def self.inherited(subclass) super subclass.class_eval { resource_controller } diff --git a/lib/resource_controller/class_methods.rb b/lib/resource_controller/class_methods.rb index cd7f3d8..14d79f4 100644 --- a/lib/resource_controller/class_methods.rb +++ b/lib/resource_controller/class_methods.rb @@ -11,14 +11,14 @@ def actions(*opts) config.merge!(opts.pop) if opts.last.is_a?(Hash) all_actions = (singleton? ? ResourceController::SINGLETON_ACTIONS : ResourceController::ACTIONS) - [:new_action] + [:new] - + actions_to_remove = [] - actions_to_remove += all_actions - opts unless opts.first == :all + actions_to_remove += all_actions - opts unless opts.first == :all actions_to_remove += [*config[:except]] if config[:except] actions_to_remove.uniq! actions_to_remove.each { |action| undef_method(action)} end - + end end \ No newline at end of file diff --git a/lib/resource_controller/controller.rb b/lib/resource_controller/controller.rb index 860e585..c6227ff 100644 --- a/lib/resource_controller/controller.rb +++ b/lib/resource_controller/controller.rb @@ -1,12 +1,12 @@ module ResourceController - module Controller + module Controller def self.included(subclass) subclass.class_eval do include ResourceController::Helpers include ResourceController::Actions extend ResourceController::Accessors extend ResourceController::ClassMethods - + class_reader_writer :belongs_to, *NAME_ACCESSORS NAME_ACCESSORS.each { |accessor| send(accessor, controller_name.singularize.underscore) } @@ -14,18 +14,18 @@ def self.included(subclass) class_scoping_reader action, FAILABLE_ACTIONS.include?(action) ? FailableActionOptions.new : ActionOptions.new end - self.helper_method :object_url, :edit_object_url, :new_object_url, :collection_url, :object, :collection, - :parent, :parent_type, :parent_object, :parent_model, :model_name, :model, :object_path, - :edit_object_path, :new_object_path, :collection_path, :hash_for_collection_path, :hash_for_object_path, - :hash_for_edit_object_path, :hash_for_new_object_path, :hash_for_collection_url, + self.helper_method :object_url, :edit_object_url, :new_object_url, :collection_url, :object, :collection, + :parent, :parent_type, :parent_object, :parent_model, :model_name, :model, :object_path, + :edit_object_path, :new_object_path, :collection_path, :hash_for_collection_path, :hash_for_object_path, + :hash_for_edit_object_path, :hash_for_new_object_path, :hash_for_collection_url, :hash_for_object_url, :hash_for_edit_object_url, :hash_for_new_object_url, :parent?, :collection_url_options, :object_url_options, :new_object_url_options - + end - + init_default_actions(subclass) end - + private def self.init_default_actions(klass) klass.class_eval do @@ -57,7 +57,7 @@ def self.init_default_actions(klass) flash "Successfully removed!" wants.html { redirect_to collection_url } end - + class << self def singleton? false diff --git a/lib/resource_controller/failable_action_options.rb b/lib/resource_controller/failable_action_options.rb index 0ec39dc..ba46f1c 100644 --- a/lib/resource_controller/failable_action_options.rb +++ b/lib/resource_controller/failable_action_options.rb @@ -1,19 +1,19 @@ module ResourceController class FailableActionOptions extend ResourceController::Accessors - + scoping_reader :success, :fails alias_method :failure, :fails - + block_accessor :before - + def initialize @success = ActionOptions.new @fails = ActionOptions.new end - + delegate :flash, :flash_now, :after, :response, :wants, :to => :success - + def dup returning self.class.new do |duplicate| duplicate.instance_variable_set(:@success, success.dup) diff --git a/lib/resource_controller/helpers.rb b/lib/resource_controller/helpers.rb index a8dd2ff..28d600d 100644 --- a/lib/resource_controller/helpers.rb +++ b/lib/resource_controller/helpers.rb @@ -8,7 +8,7 @@ module ResourceController # == ResourceController::Helpers # # Included in Base. - # + # # These helpers are used internally to manage objects, generate urls, and manage parent resource associations. # # If you want to customize certain controller behaviour, like member-object, and collection fetching, overriding these methods is all it takes. @@ -16,9 +16,9 @@ module ResourceController # See the docs below, and the README for examples # # *Please Note: many of these helpers build on top of each other, and require that behaviour to be maintained, in order for other functionality to work properly.* - # + # # e.g. All fetching must be done on top of the method end_of_association_chain, or else parent resources (including polymorphic ones) won't function correctly. - # + # # class PostsController < ResourceController::Base # private # def object diff --git a/lib/resource_controller/helpers/current_objects.rb b/lib/resource_controller/helpers/current_objects.rb index 9cba6b3..b60fded 100644 --- a/lib/resource_controller/helpers/current_objects.rb +++ b/lib/resource_controller/helpers/current_objects.rb @@ -2,13 +2,13 @@ module ResourceController module Helpers module CurrentObjects protected - # Used internally to return the model for your resource. + # Used internally to return the model for your resource. # def model model_name.to_s.camelize.constantize end - + # Used to fetch the collection for the index method # # In order to customize the way the collection is fetched, to add something like pagination, for example, override this method. @@ -16,9 +16,9 @@ def model def collection end_of_association_chain.find(:all) end - + # Returns the current param. - # + # # Defaults to params[:id]. # # Override this method if you'd like to use an alternate param name. @@ -26,7 +26,7 @@ def collection def param params[:id] end - + # Used to fetch the current member object in all of the singular methods that operate on an existing member. # # Override this method if you'd like to fetch your objects in some alternate way, like using a permalink. @@ -42,27 +42,27 @@ def object @object ||= end_of_association_chain.find(param) unless param.nil? @object end - + # Used internally to load the member object in to an instance variable @#{model_name} (i.e. @post) # def load_object instance_variable_set "@#{parent_type}", parent_object if parent? instance_variable_set "@#{object_name}", object end - + # Used internally to load the collection in to an instance variable @#{model_name.pluralize} (i.e. @posts) # def load_collection instance_variable_set "@#{parent_type}", parent_object if parent? instance_variable_set "@#{object_name.to_s.pluralize}", collection end - + # Returns the form params. Defaults to params[model_name] (i.e. params["post"]) # def object_params params["#{object_name}"] end - + # Builds the object, but doesn't save it, during the new, and create action. # def build_object diff --git a/lib/resource_controller/helpers/internal.rb b/lib/resource_controller/helpers/internal.rb index 72c7ca2..a236d24 100644 --- a/lib/resource_controller/helpers/internal.rb +++ b/lib/resource_controller/helpers/internal.rb @@ -1,5 +1,5 @@ # Internal action lifecycle management. -# +# # All of these methods are used internally to execute the options, set by the user in ActionOptions and FailableActionOptions # module ResourceController @@ -31,14 +31,14 @@ def after(action) def before(action) invoke_callbacks *self.class.send(action).before end - + # Sets the flash and flash_now for the action, if it is present. # def set_flash(action) set_normal_flash(action) set_flash_now(action) end - + # Sets the regular flash (i.e. flash[:notice] = '...') # def set_normal_flash(action) @@ -46,7 +46,7 @@ def set_normal_flash(action) flash[:notice] = f.is_a?(Proc) ? instance_eval(&f) : options_for(action).flash end end - + # Sets the flash.now (i.e. flash.now[:notice] = '...') # def set_flash_now(action) @@ -54,7 +54,7 @@ def set_flash_now(action) flash.now[:notice] = f.is_a?(Proc) ? instance_eval(&f) : options_for(action).flash_now end end - + # Returns the options for an action, which is a symbol. # # Manages splitting things like :create_fails. @@ -63,18 +63,18 @@ def options_for(action) action = action == :new_action ? [action] : "#{action}".split('_').map(&:to_sym) options = self.class.send(action.first) options = options.send(action.last == :fails ? :fails : :success) if ResourceController::FAILABLE_ACTIONS.include? action.first - + options end - + def invoke_callbacks(*callbacks) unless callbacks.empty? callbacks.select { |callback| callback.is_a? Symbol }.each { |symbol| send(symbol) } - + block = callbacks.detect { |callback| callback.is_a? Proc } instance_eval &block unless block.nil? end - end + end end end end diff --git a/lib/resource_controller/helpers/nested.rb b/lib/resource_controller/helpers/nested.rb index 1af6ec8..9a5667f 100644 --- a/lib/resource_controller/helpers/nested.rb +++ b/lib/resource_controller/helpers/nested.rb @@ -3,60 +3,60 @@ module ResourceController module Helpers module Nested - protected + protected # Returns the relevant association proxy of the parent. (i.e. /posts/1/comments # => @post.comments) # def parent_association @parent_association ||= parent_object.send(model_name.to_s.pluralize.to_sym) end - + # Returns the type of the current parent # def parent_type @parent_type ||= parent_type_from_params || parent_type_from_request end - + # Returns the type of the current parent extracted from params - # + # def parent_type_from_params [*belongs_to].find { |parent| !params["#{parent}_id".to_sym].nil? } end - + # Returns the type of the current parent extracted form a request path - # + # def parent_type_from_request [*belongs_to].find { |parent| request.path.split('/').include? parent.to_s } end - + # Returns true/false based on whether or not a parent is present. # def parent? !parent_type.nil? end - + # Returns true/false based on whether or not a parent is a singleton. - # + # def parent_singleton? !parent_type_from_request.nil? && parent_type_from_params.nil? end - + # Returns the current parent param, if there is a parent. (i.e. params[:post_id]) def parent_param params["#{parent_type}_id".to_sym] end - + # Like the model method, but for a parent relationship. - # + # def parent_model parent_type.to_s.camelize.constantize end - + # Returns the current parent object if a parent object is present. # def parent_object parent? && !parent_singleton? ? parent_model.find(parent_param) : nil end - + # If there is a parent, returns the relevant association proxy. Otherwise returns model. # def end_of_association_chain diff --git a/lib/resource_controller/helpers/singleton_customizations.rb b/lib/resource_controller/helpers/singleton_customizations.rb index 8a433ec..3919745 100644 --- a/lib/resource_controller/helpers/singleton_customizations.rb +++ b/lib/resource_controller/helpers/singleton_customizations.rb @@ -1,16 +1,16 @@ # Singleton Resource Helpers -# +# # Used internally to transform a plural RESTful controller into a singleton # module ResourceController module Helpers module SingletonCustomizations def self.included(subclass) - subclass.class_eval do - methods_to_undefine = [:param, :index, :collection, :load_collection, :collection_url, + subclass.class_eval do + methods_to_undefine = [:param, :index, :collection, :load_collection, :collection_url, :collection_path, :hash_for_collection_url, :hash_for_collection_path] methods_to_undefine.each { |method| undef_method(method) if method_defined? method } - + class << self def singleton? true @@ -18,7 +18,7 @@ def singleton? end end end - + protected # Used to fetch the current object in a singleton controller. # @@ -31,29 +31,29 @@ def singleton? # @object ||= Account.find(session[:account_id]) # end # end - # + # def object @object ||= parent? ? end_of_association_chain : nil end # Returns the :has_one association proxy of the parent. (i.e. /users/1/image # => @user.image) - # + # def parent_association @parent_association ||= parent_object.send(model_name.to_sym) end - + # Used internally to provide the options to smart_url in a singleton controller. - # + # def object_url_options(action_prefix = nil, alternate_object = nil) [action_prefix] + namespaces + [parent_url_options, route_name.to_sym] end - + # Builds the object, but doesn't save it, during the new, and create action. # def build_object @object ||= singleton_build_object_base.send parent? ? "build_#{model_name}".to_sym : :new, object_params end - + # Singleton controllers don't build off of association proxy, so we can't use end_of_association_chain here # def singleton_build_object_base diff --git a/lib/resource_controller/helpers/urls.rb b/lib/resource_controller/helpers/urls.rb index b899c61..a929959 100644 --- a/lib/resource_controller/helpers/urls.rb +++ b/lib/resource_controller/helpers/urls.rb @@ -1,40 +1,40 @@ # Thanks to Urligence, you get some free url helpers. -# +# # No matter what your controller looks like... -# +# # [edit_|new_]object_url # is the equivalent of saying [edit_|new_]post_url(@post) # [edit_|new_]object_url(some_other_object) # allows you to specify an object, but still maintain any paths or namespaces that are present -# +# # collection_url # is like saying posts_url -# +# # Url helpers are especially useful when working with polymorphic controllers. -# +# # # /posts/1/comments # object_url #=> /posts/1/comments/#{@comment.to_param} # object_url(comment) #=> /posts/1/comments/#{comment.to_param} # edit_object_url #=> /posts/1/comments/#{@comment.to_param}/edit # collection_url #=> /posts/1/comments -# +# # # /products/1/comments # object_url #=> /products/1/comments/#{@comment.to_param} # object_url(comment) #=> /products/1/comments/#{comment.to_param} # edit_object_url #=> /products/1/comments/#{@comment.to_param}/edit # collection_url #=> /products/1/comments -# +# # # /comments # object_url #=> /comments/#{@comment.to_param} # object_url(comment) #=> /comments/#{comment.to_param} # edit_object_url #=> /comments/#{@comment.to_param}/edit # collection_url #=> /comments -# +# # Or with namespaced, nested controllers... -# +# # # /admin/products/1/options # object_url #=> /admin/products/1/options/#{@option.to_param} # object_url(option) #=> /admin/products/1/options/#{option.to_param} # edit_object_url #=> /admin/products/1/options/#{@option.to_param}/edit # collection_url #=> /admin/products/1/options -# +# # You get the idea. Everything is automagical! All parameters are inferred. # module ResourceController @@ -43,74 +43,74 @@ module Urls protected ['', 'edit_'].each do |type| symbol = type.blank? ? nil : type.gsub(/_/, '').to_sym - + define_method("#{type}object_url") do |*alternate_object| smart_url *object_url_options(symbol, alternate_object.first) end - + define_method("#{type}object_path") do |*alternate_object| smart_path *object_url_options(symbol, alternate_object.first) end - + define_method("hash_for_#{type}object_url") do |*alternate_object| hash_for_smart_url *object_url_options(symbol, alternate_object.first) end - + define_method("hash_for_#{type}object_path") do |*alternate_object| hash_for_smart_path *object_url_options(symbol, alternate_object.first) end end - + def new_object_url smart_url *new_object_url_options end - + def new_object_path smart_path *new_object_url_options end - + def hash_for_new_object_url hash_for_smart_url *new_object_url_options end - + def hash_for_new_object_path hash_for_smart_path *new_object_url_options end - + def collection_url smart_url *collection_url_options end - + def collection_path smart_path *collection_url_options end - + def hash_for_collection_url hash_for_smart_url *collection_url_options end - + def hash_for_collection_path hash_for_smart_path *collection_url_options end - + # Used internally to provide the options to smart_url from Urligence. # def collection_url_options namespaces + [parent_url_options, route_name.to_s.pluralize.to_sym] end - + # Used internally to provide the options to smart_url from Urligence. # def object_url_options(action_prefix = nil, alternate_object = nil) [action_prefix] + namespaces + [parent_url_options, [route_name.to_sym, alternate_object || object]] end - + # Used internally to provide the options to smart_url from Urligence. # def new_object_url_options [:new] + namespaces + [parent_url_options, route_name.to_sym] end - + def parent_url_options if parent? parent_singleton? ? parent_type.to_sym : [parent_type.to_sym, parent_object] @@ -118,13 +118,13 @@ def parent_url_options nil end end - + # Returns all of the current namespaces of the current controller, symbolized, in array form. # def namespaces names = self.class.name.split("::") names.pop - + names.map(&:underscore).map(&:to_sym) end end diff --git a/lib/resource_controller/response_collector.rb b/lib/resource_controller/response_collector.rb index 774da6c..1337ae6 100644 --- a/lib/resource_controller/response_collector.rb +++ b/lib/resource_controller/response_collector.rb @@ -1,23 +1,23 @@ module ResourceController class ResponseCollector - + attr_reader :responses - + delegate :clear, :to => :responses - + def initialize @responses = [] end - + def method_missing(method_name, &block) @responses.delete self[method_name] @responses << [method_name, block || nil] end - + def [](symbol) @responses.find { |method, block| method == symbol } end - + def dup returning ResponseCollector.new do |duplicate| duplicate.instance_variable_set(:@responses, responses.dup) diff --git a/lib/resource_controller/singleton.rb b/lib/resource_controller/singleton.rb index 5ce5282..d7d681d 100644 --- a/lib/resource_controller/singleton.rb +++ b/lib/resource_controller/singleton.rb @@ -1,12 +1,12 @@ module ResourceController - + # == ResourceController::Singleton - # + # # Inherit from this class to create your RESTful singleton controller. See the README for usage. - # + # class Singleton < ApplicationController unloadable - + def self.inherited(subclass) super subclass.class_eval { resource_controller :singleton } diff --git a/lib/resource_controller/version.rb b/lib/resource_controller/version.rb index d5a5358..dfe5e27 100644 --- a/lib/resource_controller/version.rb +++ b/lib/resource_controller/version.rb @@ -3,7 +3,7 @@ module VERSION MAJOR = 0 MINOR = 5 TINY = 3 - + STRING = [MAJOR, MINOR, TINY].join('.') end end diff --git a/lib/urligence.rb b/lib/urligence.rb index 8f40123..1812499 100644 --- a/lib/urligence.rb +++ b/lib/urligence.rb @@ -2,25 +2,25 @@ module Urligence def smart_url(*objects) urligence(*objects.push(:url)) end - + def smart_path(*objects) urligence(*objects.push(:path)) end - + def hash_for_smart_url(*objects) urligence(*objects.unshift(:hash_for).push(:url).push({:type => :hash})) end - + def hash_for_smart_path(*objects) urligence(*objects.unshift(:hash_for).push(:path).push({:type => :hash})) end - + def urligence(*objects) config = {} config.merge!(objects.pop) if objects.last.is_a?(Hash) - + objects.reject! { |object| object.nil? } - + url_fragments = objects.collect do |obj| if obj.is_a? Symbol obj @@ -30,7 +30,7 @@ def urligence(*objects) obj.class.name.underscore.to_sym end end - + unless config[:type] == :hash send url_fragments.join("_"), *objects.flatten.select { |obj| !obj.is_a? Symbol } else @@ -43,7 +43,7 @@ def urligence(*objects) params.merge!((obj.is_a? Array) ? {:id => obj[1].to_param} : {:id => obj.to_param}) end end - + send url_fragments.join("_"), params end end diff --git a/rails/init.rb b/rails/init.rb index 7f63084..58d2d5b 100644 --- a/rails/init.rb +++ b/rails/init.rb @@ -1,6 +1,6 @@ ActionController::Base.class_eval do include Urligence helper_method :smart_url - + extend ResourceController::ActionControllerExtension end diff --git a/resource_controller.gemspec b/resource_controller.gemspec index 70762b3..c3ede55 100644 --- a/resource_controller.gemspec +++ b/resource_controller.gemspec @@ -1,16 +1,16 @@ ---- !ruby/object:Gem::Specification +--- !ruby/object:Gem::Specification name: resource_controller -version: !ruby/object:Gem::Version +version: !ruby/object:Gem::Version version: 0.5.3 platform: ruby -authors: +authors: - James Golick -autorequire: +autorequire: bindir: bin cert_chain: [] date: 2008-09-22 00:00:00 -04:00 -default_executable: +default_executable: dependencies: [] description: Rails RESTful controller abstraction plugin. @@ -21,7 +21,7 @@ extensions: [] extra_rdoc_files: [] -files: +files: - README.rdoc - LICENSE - init.rb @@ -316,28 +316,28 @@ files: - rails/init.rb has_rdoc: true homepage: http://jamesgolick.com/resource_controller -post_install_message: +post_install_message: rdoc_options: [] -require_paths: +require_paths: - lib -required_ruby_version: !ruby/object:Gem::Requirement - requirements: +required_ruby_version: !ruby/object:Gem::Requirement + requirements: - - ">=" - - !ruby/object:Gem::Version + - !ruby/object:Gem::Version version: 1.8.5 - version: -required_rubygems_version: !ruby/object:Gem::Requirement - requirements: + version: +required_rubygems_version: !ruby/object:Gem::Requirement + requirements: - - ">=" - - !ruby/object:Gem::Version + - !ruby/object:Gem::Version version: "0" - version: + version: requirements: [] rubyforge_project: giraffesoft rubygems_version: 1.2.0 -signing_key: +signing_key: specification_version: 2 summary: resource_controller makes RESTful controllers easier, more maintainable, and super readable. With the RESTful controller pattern hidden away, you can focus on what makes your controller special. test_files: [] diff --git a/tasks/gem.rake b/tasks/gem.rake index 361a96b..47daaa6 100644 --- a/tasks/gem.rake +++ b/tasks/gem.rake @@ -18,7 +18,7 @@ spec = Gem::Specification.new do |s| s.files = %w(README.rdoc LICENSE init.rb Rakefile) + Dir.glob("{lib,test,generators,rails}/**/*") - + s.require_path = "lib" end @@ -48,18 +48,18 @@ namespace :gem do sh "rubyforge add_release giraffesoft resource_controller #{ResourceController::VERSION::STRING} pkg/#{spec.full_name}.gem" sh "rubyforge add_file giraffesoft resource_controller #{ResourceController::VERSION::STRING} pkg/#{spec.full_name}.gem" end - + desc "Update the gemspec for GitHub's gem server" task :github do File.open("resource_controller.gemspec", 'w'){|f| f.puts YAML::dump(spec) } puts "gemspec generated here: resource_controller.gemspec" end - + desc "Build and install the gem locally." task :install => [:clobber, :package] do sh "sudo gem install pkg/#{spec.full_name}.gem" end - + desc "Remove the gem." task :uninstall => :clean do sh "sudo gem uninstall -v #{ResourceController::VERSION::STRING} -x #{ResourceController::NAME}" diff --git a/test/app/controllers/cms/photos_controller.rb b/test/app/controllers/cms/photos_controller.rb index 0d9d638..5208057 100644 --- a/test/app/controllers/cms/photos_controller.rb +++ b/test/app/controllers/cms/photos_controller.rb @@ -1,6 +1,6 @@ class Cms::PhotosController < ResourceController::Base actions :all, :except => :update - + belongs_to :personnel - create.flash { "#{@photo.title} was created!" } + create.flash { "#{@photo.title} was created!" } end diff --git a/test/app/controllers/options_controller.rb b/test/app/controllers/options_controller.rb index acbd38f..91e8541 100644 --- a/test/app/controllers/options_controller.rb +++ b/test/app/controllers/options_controller.rb @@ -1,6 +1,6 @@ class OptionsController < ResourceController::Base belongs_to :account - + protected def parent_object Account.find(:first) diff --git a/test/app/controllers/people_controller.rb b/test/app/controllers/people_controller.rb index 0ae346f..e625294 100644 --- a/test/app/controllers/people_controller.rb +++ b/test/app/controllers/people_controller.rb @@ -1,7 +1,7 @@ class PeopleController < ResourceController::Base create.before :name_person model_name :account - + private def name_person @person.name = "Bob Loblaw" diff --git a/test/app/controllers/photos_controller.rb b/test/app/controllers/photos_controller.rb index 9471a31..4b42068 100644 --- a/test/app/controllers/photos_controller.rb +++ b/test/app/controllers/photos_controller.rb @@ -1,9 +1,9 @@ class PhotosController < ResourceController::Base actions :all, :except => :update - + belongs_to :user create.flash { "#{@photo.title} was created!" } - + private def parent_model Account diff --git a/test/app/controllers/posts_controller.rb b/test/app/controllers/posts_controller.rb index 0b167d7..112f1c4 100644 --- a/test/app/controllers/posts_controller.rb +++ b/test/app/controllers/posts_controller.rb @@ -1,8 +1,8 @@ class PostsController < ResourceController::Base actions :all - + create.before(:name_post) { @post.body = '...' } - + private def name_post @post.title = 'a great post' diff --git a/test/app/controllers/tags_controller.rb b/test/app/controllers/tags_controller.rb index 35a8329..b9577b9 100644 --- a/test/app/controllers/tags_controller.rb +++ b/test/app/controllers/tags_controller.rb @@ -1,12 +1,12 @@ class TagsController < ResourceController::Base belongs_to :photo - + index.wants.js - + index do before { @products = Product.find :all } end - + create.after do @photo.tags << @tag if parent_type == :photo end diff --git a/test/app/controllers/users_controller.rb b/test/app/controllers/users_controller.rb index ebd2fe3..4f7fc6f 100644 --- a/test/app/controllers/users_controller.rb +++ b/test/app/controllers/users_controller.rb @@ -1,11 +1,11 @@ class UsersController < ResourceController::Base object_name :dude route_name :dude - private + private def route_name 'dude' end - + def model_name 'account' end diff --git a/test/app/views/cms/options/index.rhtml b/test/app/views/cms/options/index.rhtml index 75c3e0e..2309d76 100644 --- a/test/app/views/cms/options/index.rhtml +++ b/test/app/views/cms/options/index.rhtml @@ -4,7 +4,7 @@ Title - + <% for option in @options %> <%=h option.title %> diff --git a/test/app/views/cms/photos/index.rhtml b/test/app/views/cms/photos/index.rhtml index 49131be..23c5d0e 100644 --- a/test/app/views/cms/photos/index.rhtml +++ b/test/app/views/cms/photos/index.rhtml @@ -4,7 +4,7 @@ Title - + <% for photo in @photos %> <%=h photo.title %> diff --git a/test/app/views/cms/products/index.rhtml b/test/app/views/cms/products/index.rhtml index cd2c79b..e0fa3ff 100644 --- a/test/app/views/cms/products/index.rhtml +++ b/test/app/views/cms/products/index.rhtml @@ -4,7 +4,7 @@ Name - + <% for product in @products %> <%=h product.name %> diff --git a/test/app/views/comments/index.rhtml b/test/app/views/comments/index.rhtml index ee42b8e..2b7afde 100644 --- a/test/app/views/comments/index.rhtml +++ b/test/app/views/comments/index.rhtml @@ -6,7 +6,7 @@ Author Body - + <% for comment in @comments %> <%=h comment.post_id %> diff --git a/test/app/views/options/index.html.erb b/test/app/views/options/index.html.erb index cc1a010..5b2cd70 100644 --- a/test/app/views/options/index.html.erb +++ b/test/app/views/options/index.html.erb @@ -9,7 +9,7 @@ <%=h option.account_id %> <%=h option.title %> - + <%=link_to 'Show', object_url(option) %> <%=link_to 'Edit', edit_object_url(option) %> <%=link_to 'Destroy', object_url(option), :confirm => 'Are you sure?', :method => :delete %> diff --git a/test/app/views/people/index.rhtml b/test/app/views/people/index.rhtml index fb60f99..a1dc747 100644 --- a/test/app/views/people/index.rhtml +++ b/test/app/views/people/index.rhtml @@ -4,7 +4,7 @@ Name - + <% for person in @people %> <%=h person.name %> diff --git a/test/app/views/photos/index.rhtml b/test/app/views/photos/index.rhtml index 49131be..23c5d0e 100644 --- a/test/app/views/photos/index.rhtml +++ b/test/app/views/photos/index.rhtml @@ -4,7 +4,7 @@ Title - + <% for photo in @photos %> <%=h photo.title %> diff --git a/test/app/views/posts/index.rhtml b/test/app/views/posts/index.rhtml index a9c6b3a..7771b82 100644 --- a/test/app/views/posts/index.rhtml +++ b/test/app/views/posts/index.rhtml @@ -5,7 +5,7 @@ Title Body - + <% for post in @posts %> <%=h post.title %> diff --git a/test/app/views/projects/index.rhtml b/test/app/views/projects/index.rhtml index 3087ffc..4f63ab0 100644 --- a/test/app/views/projects/index.rhtml +++ b/test/app/views/projects/index.rhtml @@ -4,7 +4,7 @@ Title - + <% for project in @projects %> <%=h project.title %> diff --git a/test/app/views/somethings/index.rhtml b/test/app/views/somethings/index.rhtml index c69b9da..f5c745c 100644 --- a/test/app/views/somethings/index.rhtml +++ b/test/app/views/somethings/index.rhtml @@ -4,7 +4,7 @@ Title - + <% for something in @somethings %> <%=h something.title %> diff --git a/test/app/views/tags/index.rhtml b/test/app/views/tags/index.rhtml index d888a9e..d8be16a 100644 --- a/test/app/views/tags/index.rhtml +++ b/test/app/views/tags/index.rhtml @@ -4,7 +4,7 @@ Name - + <% for tag in @tags %> <%=h tag.name %> diff --git a/test/app/views/users/index.rhtml b/test/app/views/users/index.rhtml index 831eb77..00c1eed 100644 --- a/test/app/views/users/index.rhtml +++ b/test/app/views/users/index.rhtml @@ -4,7 +4,7 @@ Name - + <% for dude in @dudes %> <%=h dude.name %> diff --git a/test/config/environment.rb b/test/config/environment.rb index a44c817..b54a370 100644 --- a/test/config/environment.rb +++ b/test/config/environment.rb @@ -1,6 +1,6 @@ # Be sure to restart your web server when you modify this file. -# Uncomment below to force Rails into production mode when +# Uncomment below to force Rails into production mode when # you don't control web/app server and can't set it the proper way # ENV['RAILS_ENV'] ||= 'production' @@ -12,7 +12,7 @@ Rails::Initializer.run do |config| # Settings in config/environments/* take precedence over those specified here - + # Skip frameworks you're not going to use (only works if using vendor/rails) # config.frameworks -= [ :action_web_service, :action_mailer ] @@ -22,7 +22,7 @@ # Add additional load paths for your own custom dirs config.load_paths += %W( #{RAILS_ROOT}/../lib ) - # Force all environments to use the same logger level + # Force all environments to use the same logger level # (by default production uses :info, the others :debug) # config.log_level = :debug @@ -31,7 +31,7 @@ # config.action_controller.session_store = :active_record_store # Use SQL instead of Active Record's schema dumper when creating the test database. - # This is necessary if your schema can't be completely dumped by the schema dumper, + # This is necessary if your schema can't be completely dumped by the schema dumper, # like if you have constraints or database-specific column types # config.active_record.schema_format = :sql @@ -40,10 +40,10 @@ # Make Active Record use UTC-base instead of local time # config.active_record.default_timezone = :utc - + # See Rails::Configuration for more options - + config.action_controller.session = { :session_key => "_myapp_session", :secret => "6c1372e61789239a727cdbc8252eb6da" } - + config.gem 'resource_controller' end diff --git a/test/config/initializers/inflections.rb b/test/config/initializers/inflections.rb index 6e70fc5..9dce77f 100644 --- a/test/config/initializers/inflections.rb +++ b/test/config/initializers/inflections.rb @@ -1,6 +1,6 @@ # Be sure to restart your server when you modify this file. -# Add new inflection rules using the following format +# Add new inflection rules using the following format # (all these examples are active by default): # Inflector.inflections do |inflect| # inflect.plural /^(ox)$/i, '\1en' diff --git a/test/config/routes.rb b/test/config/routes.rb index 9154410..9d3cdc6 100644 --- a/test/config/routes.rb +++ b/test/config/routes.rb @@ -2,7 +2,7 @@ map.resources :projects map.resources :people - + map.resources :dudes, :controller => "users" map.resources :users do |user| @@ -15,9 +15,9 @@ map.resources :photos do |photo| photo.resources :tags, :name_prefix => "photo_" end - + map.resources :tags - + map.namespace :cms do |cms| cms.resources :products, :has_many => :options cms.resources :personnel do |personnel| @@ -28,17 +28,17 @@ map.resources :posts do |post| post.resources :comments, :name_prefix => "post_" end - + map.resources :comments - + map.resource :account, :has_many => :options - + map.resource :image - + map.resources :options - + # The priority is based upon order of creation: first created -> highest priority. - + # Sample of regular route: # map.connect 'products/:id', :controller => 'catalog', :action => 'view' # Keep in mind you can assign values other than :controller and :action @@ -47,7 +47,7 @@ # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase' # This route can be invoked with purchase_url(:id => product.id) - # You can have the root of your site routed by hooking up '' + # You can have the root of your site routed by hooking up '' # -- just remember to delete public/index.html. # map.connect '', :controller => "welcome" diff --git a/test/db/migrate/006_create_tags.rb b/test/db/migrate/006_create_tags.rb index 246ece9..b61a416 100644 --- a/test/db/migrate/006_create_tags.rb +++ b/test/db/migrate/006_create_tags.rb @@ -3,7 +3,7 @@ def self.up create_table :tags do |t| t.column :name, :string end - + create_table :photos_tags, :id => :false do |t| t.column :photo_id, :integer t.column :tag_id, :integer diff --git a/test/db/migrate/011_create_images.rb b/test/db/migrate/011_create_images.rb index b770ec1..b02c6f6 100644 --- a/test/db/migrate/011_create_images.rb +++ b/test/db/migrate/011_create_images.rb @@ -1,7 +1,7 @@ class CreateImages < ActiveRecord::Migration def self.up create_table :images, :force => true do |t| - t.references :user + t.references :user t.timestamps end end diff --git a/test/db/schema.rb b/test/db/schema.rb index 63678e4..eb56409 100644 --- a/test/db/schema.rb +++ b/test/db/schema.rb @@ -1,4 +1,4 @@ -# This file is auto-generated from the current state of the database. Instead of editing this file, +# This file is auto-generated from the current state of the database. Instead of editing this file, # please use the migrations feature of Active Record to incrementally modify your database, and # then regenerate this schema definition. # diff --git a/test/test/functional/cms/photos_controller_test.rb b/test/test/functional/cms/photos_controller_test.rb index 975245d..f8fd282 100644 --- a/test/test/functional/cms/photos_controller_test.rb +++ b/test/test/functional/cms/photos_controller_test.rb @@ -11,7 +11,7 @@ def setup @response = ActionController::TestResponse.new @photo = Photo.find 1 end - + context "with personnel as parent" do context "on get to :index" do setup do @@ -26,7 +26,7 @@ def setup assert assigns(:photos).all? { |photo| photo.personnel.id == 1 } end end - + context "on post to :create" do setup do post :create, :personnel_id => 1, :photo => {} diff --git a/test/test/functional/comments_controller_test.rb b/test/test/functional/comments_controller_test.rb index c5877f9..d95e95c 100644 --- a/test/test/functional/comments_controller_test.rb +++ b/test/test/functional/comments_controller_test.rb @@ -11,15 +11,15 @@ def setup @response = ActionController::TestResponse.new @comment = Comment.find 1 end - + context "with parent post" do should_be_restful do |resource| resource.formats = [:html] - + resource.parent = :post end end - + should_be_restful do |resource| resource.formats = [:html] end diff --git a/test/test/functional/images_controller_test.rb b/test/test/functional/images_controller_test.rb index 889190c..fb2a86b 100644 --- a/test/test/functional/images_controller_test.rb +++ b/test/test/functional/images_controller_test.rb @@ -13,7 +13,7 @@ def setup end context "with user as parent" do - + context "on post to :create" do setup do post :create, :user_id => 1, :photo => {} @@ -26,9 +26,9 @@ def setup assert users(:one), assigns(:image).user end end - - end - + + end + should "not respond to show" do assert_raise(ActionController::UnknownAction) do get :show diff --git a/test/test/functional/people_controller_test.rb b/test/test/functional/people_controller_test.rb index a6f0402..f088a3d 100644 --- a/test/test/functional/people_controller_test.rb +++ b/test/test/functional/people_controller_test.rb @@ -16,12 +16,12 @@ def setup resource.formats = [:html] resource.klass = Account resource.object = :person - + resource.create.redirect = 'person_url(@person)' resource.update.redirect = 'person_url(@person)' resource.destroy.redirect = 'people_url' end - + context "before create" do setup do post :create, :person => {} diff --git a/test/test/functional/photos_controller_test.rb b/test/test/functional/photos_controller_test.rb index 68959f7..8aa8434 100644 --- a/test/test/functional/photos_controller_test.rb +++ b/test/test/functional/photos_controller_test.rb @@ -17,15 +17,15 @@ def setup assert !@controller.respond_to?(:update) end end - + should_be_restful do |resource| resource.formats = [:html] - + resource.actions = [:index, :new, :create, :destroy, :show, :edit] resource.create.params = {:title => 'Some Photo Title'} resource.create.flash = "Some Photo Title was created!" end - + context "with user as parent" do context "on get to :index" do setup do @@ -40,7 +40,7 @@ def setup assert assigns(:photos).all? { |photo| photo.user.id == 1 } end end - + context "on post to :create" do setup do post :create, :user_id => 1, :photo => {} @@ -54,9 +54,9 @@ def setup end end end - + # url helpers integration - + context "url, path, and hash_for helpers" do setup do get :index @@ -65,66 +65,66 @@ def setup should "return collection url" do assert_equal photos_url, @controller.send(:collection_url) end - + should "return collection path" do assert_equal photos_path, @controller.send(:collection_path) end - + should "return hash for collection url" do assert_equal hash_for_photos_url, @controller.send(:hash_for_collection_url) end - + should "return hash for collection path" do assert_equal hash_for_photos_path, @controller.send(:hash_for_collection_path) end - + should "return object url" do assert_equal photo_url(photos(:one)), @controller.send(:object_url, photos(:one)) end - + should "return object path" do assert_equal photo_path(photos(:one)), @controller.send(:object_path, photos(:one)) end - + should "return hash_for object url" do assert_equal hash_for_photo_url(:id => @photo.to_param), @controller.send(:hash_for_object_url, photos(:one)) end - + should "return hash_for object path" do assert_equal hash_for_photo_path(:id => @photo.to_param), @controller.send(:hash_for_object_path, photos(:one)) end - + should "return edit object url" do assert_equal edit_photo_url(photos(:one)), @controller.send(:edit_object_url, photos(:one)) end - + should "return edit object path" do assert_equal edit_photo_path(photos(:one)), @controller.send(:edit_object_path, photos(:one)) end - + should "return hash_for_edit object url" do assert_equal hash_for_edit_photo_url(:id => @photo.to_param), @controller.send(:hash_for_edit_object_url, photos(:one)) end - + should "return hash_for_edit object path" do assert_equal hash_for_edit_photo_path(:id => @photo.to_param), @controller.send(:hash_for_edit_object_path, photos(:one)) end - + should "return new object url" do assert_equal new_photo_url, @controller.send(:new_object_url) end - + should "return new object path" do assert_equal new_photo_path, @controller.send(:new_object_path) end - + should "return hash_for_new object url" do assert_equal hash_for_new_photo_url, @controller.send(:hash_for_new_object_url) end - + should "return hash_for_new object path" do assert_equal hash_for_new_photo_path, @controller.send(:hash_for_new_object_path) end end - + end diff --git a/test/test/functional/posts_controller_test.rb b/test/test/functional/posts_controller_test.rb index 8988257..b08376a 100644 --- a/test/test/functional/posts_controller_test.rb +++ b/test/test/functional/posts_controller_test.rb @@ -11,13 +11,13 @@ def setup @response = ActionController::TestResponse.new @post = Post.find 1 end - + should_be_restful do |resource| resource.formats = [:html] resource.actions = :all end - + context "on post to :create" do setup do post :create, :post => {} @@ -26,7 +26,7 @@ def setup should "name the post 'a great post'" do assert_equal 'a great post', assigns(:post).title end - + should "give the post a body of '...'" do assert_equal '...', assigns(:post).body end diff --git a/test/test/functional/somethings_controller_test.rb b/test/test/functional/somethings_controller_test.rb index 01ba903..a699d91 100644 --- a/test/test/functional/somethings_controller_test.rb +++ b/test/test/functional/somethings_controller_test.rb @@ -19,10 +19,10 @@ def setup end end end - + should_be_restful do |resource| resource.formats = [:html] - + resource.actions = [:index, :show] end end diff --git a/test/test/functional/tags_controller_test.rb b/test/test/functional/tags_controller_test.rb index fd6f4ce..baca236 100644 --- a/test/test/functional/tags_controller_test.rb +++ b/test/test/functional/tags_controller_test.rb @@ -11,7 +11,7 @@ def setup @response = ActionController::TestResponse.new @tag = Tag.find 1 end - + context "with photo as parent" do context "get to :index" do setup do @@ -21,12 +21,12 @@ def setup should_assign_to :products should_render_template "index" should_respond_with :success - + should "respond with html" do assert_equal 'text/html', @response.content_type end end - + context "xhr to :index" do setup do xhr :get, :index, :photo_id => 1 @@ -39,7 +39,7 @@ def setup assert_equal 'text/javascript', @response.content_type end end - + context "post to create" do setup do post :create, :photo_id => 1, :tag => {:name => "Hello!"} @@ -50,12 +50,12 @@ def setup end end end - + context "without photo as parent" do should_be_restful do |resource| resource.formats = [:html] end - + should "render text for a missing object" do get :show, :id => 50000 assert @response.body.match(/not found/i), @response.body diff --git a/test/test/functional/users_controller_test.rb b/test/test/functional/users_controller_test.rb index b6f5d31..451858e 100644 --- a/test/test/functional/users_controller_test.rb +++ b/test/test/functional/users_controller_test.rb @@ -16,9 +16,9 @@ def setup resource.formats = [:html] resource.klass = Account resource.object = :dude - + resource.create.redirect = 'dude_url(@dude)' resource.update.redirect = 'dude_url(@dude)' resource.destroy.redirect = 'dudes_url' - end + end end diff --git a/test/test/test_helper.rb b/test/test/test_helper.rb index 58987e4..ba7e7de 100644 --- a/test/test/test_helper.rb +++ b/test/test/test_helper.rb @@ -7,6 +7,6 @@ class Test::Unit::TestCase self.use_transactional_fixtures = true self.use_instantiated_fixtures = false - + load_all_fixtures end diff --git a/test/test/unit/accessors_test.rb b/test/test/unit/accessors_test.rb index 1f072c7..46e2536 100644 --- a/test/test/unit/accessors_test.rb +++ b/test/test/unit/accessors_test.rb @@ -6,22 +6,22 @@ def setup extend ResourceController::Accessors end end - + context "scoping reader" do setup do PostsController.class_eval do class_scoping_reader :create, ResourceController::ActionOptions.new end end - + should "access create as usual" do PostsController.class_eval do create.flash "asdf" end - + assert_equal "asdf", PostsController.create.flash end - + should "scope to create object in a block" do PostsController.class_eval do create do @@ -29,16 +29,16 @@ def setup end end - assert_equal "asdf", PostsController.create.flash + assert_equal "asdf", PostsController.create.flash end end - + context "reader/writer method" do setup do PostsController.class_eval do reader_writer :flash end - + @controller = PostsController.new end @@ -47,16 +47,16 @@ def setup assert_equal "something", @controller.flash end end - + context "class reader/writer method" do setup do PostsController.class_eval do class_reader_writer :flash end - + @controller = PostsController.new end - + should "initialize var" do assert_nil PostsController.flash assert_nil @controller.flash @@ -67,7 +67,7 @@ def setup assert_equal "something", PostsController.flash end end - + context "block accessor" do setup do PostsController.class_eval do @@ -80,7 +80,7 @@ def setup @controller.something {} assert @controller.something.first end - + should "store symbols as well" do @controller.something(:method, :method_two) {} assert_equal :method, @controller.something[0] @@ -88,13 +88,13 @@ def setup assert @controller.something[2].is_a?(Proc) end end - + context "reader writer" do setup do PostsController.class_eval do reader_writer :rw end - + @controller = PostsController.new end @@ -102,9 +102,9 @@ def setup @controller.rw do "asdf" end - + assert @controller.rw.is_a?(Proc), @controller.rw end end - + end \ No newline at end of file diff --git a/test/test/unit/action_options_test.rb b/test/test/unit/action_options_test.rb index a325ab2..4cabcb9 100644 --- a/test/test/unit/action_options_test.rb +++ b/test/test/unit/action_options_test.rb @@ -5,12 +5,12 @@ def setup @controller = PostsController.new @create = ResourceController::ActionOptions.new end - + should "have attr accessor for flash" do @create.flash "Successfully created." assert_equal "Successfully created.", @create.flash end - + should "have attr accessor for flash_now" do @create.flash_now "Successfully created." assert_equal "Successfully created.", @create.flash_now @@ -21,30 +21,30 @@ def setup @create.send(accessor) do "return_something" end - + assert_equal "return_something", @create.send(accessor).first.call(nil) end end - + context "response yielding to response collector" do setup do @create.response do |wants| wants.html end end - + should "accept symbols" do @create.response :html, :js, :xml assert @create.wants[:html] assert @create.wants[:js] assert @create.wants[:xml] end - + should "accept symbols and blocks" do @create.responds_to :html do |wants| # note the aliasing of response here wants.js end - + assert @create.wants[:html] assert @create.wants[:js] end @@ -52,23 +52,23 @@ def setup should "collect responses" do assert @create.wants[:html] end - + should "clear the collector on a subsequent call" do @create.respond_to do |wants| # note the other aliasing of response wants.js end - + assert_nil @create.wants[:html] assert @create.wants[:js] end - + should "add response without clearing" do @create.wants.js assert @create.wants[:js] assert @create.wants[:html] end end - + context "duplicating action options" do setup do @opts = ResourceController::ActionOptions.new @@ -84,26 +84,26 @@ def setup assert !@opts.wants.equal?(@dup.wants) assert @dup.wants[:js] end - + should "duplicate the after block" do assert !@opts.after.equal?(@dup.after) assert @dup.after end - + should "duplicate the before block" do assert !@opts.before.equal?(@dup.before) assert @dup.before end - + should "duplicate the flash" do assert !@opts.flash.equal?(@dup.flash) assert @dup.flash end - + should "duplicate the flash_now" do assert !@opts.flash_now.equal?(@dup.flash_now) assert @dup.flash_now end end - + end diff --git a/test/test/unit/base_test.rb b/test/test/unit/base_test.rb index 7c0fc6c..5be0b81 100644 --- a/test/test/unit/base_test.rb +++ b/test/test/unit/base_test.rb @@ -4,8 +4,8 @@ class BaseTest < Test::Unit::TestCase def setup @controller = ResourceController::Base.new end - + def test_case_name - + end end diff --git a/test/test/unit/failable_action_options_test.rb b/test/test/unit/failable_action_options_test.rb index 66fc4aa..7d17009 100644 --- a/test/test/unit/failable_action_options_test.rb +++ b/test/test/unit/failable_action_options_test.rb @@ -5,56 +5,56 @@ def setup @controller = PostsController.new @create = ResourceController::FailableActionOptions.new end - + should "have success and fails" do assert ResourceController::ActionOptions, @create.success.class assert ResourceController::ActionOptions, @create.fails.class end - + %w(before).each do |accessor| should "have a block accessor for #{accessor}" do @create.send(accessor) do "return_something" end - + assert_equal "return_something", @create.send(accessor).first.call(nil) end end - + should "delegate flash to success" do @create.flash "Successfully created." assert_equal "Successfully created.", @create.success.flash end - + should "delegate after to success" do @create.after do "something" end - + assert_equal "something", @create.success.after.first.call end - + should "delegate response to success" do @create.response do |wants| wants.html end - + assert @create.wants[:html] end - + should "delegate wants to success" do @create.wants.html - + assert @create.wants[:html] end - + context "duplication" do setup do @opts = ResourceController::FailableActionOptions.new @opts.wants.js @opts.failure.wants.js @opts.before {} - + @dup = @opts.dup end @@ -62,16 +62,16 @@ def setup assert !@dup.success.equal?(@opts.success) assert @dup.success.wants[:js] end - + should "duplicate failure" do assert !@dup.failure.equal?(@opts.failure) assert @dup.failure.wants[:js] end - + should "duplicate before" do assert !@dup.before.equal?(@opts.before) assert @dup.before end end - + end \ No newline at end of file diff --git a/test/test/unit/helpers/current_objects_test.rb b/test/test/unit/helpers/current_objects_test.rb index 5216418..b8f140d 100644 --- a/test/test/unit/helpers/current_objects_test.rb +++ b/test/test/unit/helpers/current_objects_test.rb @@ -6,50 +6,50 @@ def setup @params = stub :[] => "1" @controller.stubs(:params).returns(@params) - + @request = stub :path => "" - @controller.stubs(:request).returns(@request) + @controller.stubs(:request).returns(@request) @object = Post.new Post.stubs(:find).with("1").returns(@object) - + @collection = mock() Post.stubs(:find).with(:all).returns(@collection) end - + context "model helper" do should "return constant" do assert_equal Post, @controller.send(:model) end end - + context "collection helper" do should "find all" do assert_equal @collection, @controller.send(:collection) end end - + context "param helper" do should "return the correct param" do assert_equal "1", @controller.send(:param) end end - - context "object helper" do + + context "object helper" do should "find the correct object" do assert_equal @object, @controller.send(:object) end end - + context "load object helper" do setup do @controller.send(:load_object) end - + should "load object as instance variable" do assert_equal @object, @controller.instance_variable_get("@post") end - + context "with an alternate object_name" do setup do @controller.stubs(:object_name).returns('asdf') @@ -61,7 +61,7 @@ def setup end end end - + context "load_collection helper" do context "with resource_name" do setup do @@ -73,7 +73,7 @@ def setup end end end - + context "object params helper" do context "without alternate variable name" do setup do @@ -84,7 +84,7 @@ def setup assert_equal 2, @controller.send(:object_params) end end - + context "with alternate object_name" do setup do @params.expects(:[]).with("something").returns(3) @@ -96,18 +96,18 @@ def setup end end end - + context "build object helper" do context "with no parents" do setup do Post.expects(:new).with("1").returns("a new post") end - + should "build new object" do assert_equal "a new post", @controller.send(:build_object) end end - + context "with parent" do setup do @comments_controller = CommentsController.new @@ -115,10 +115,10 @@ def setup @comment_params.stubs(:[]).with(:post_id).returns 2 @comment_params.stubs(:[]).with('comment').returns "" @comments_controller.stubs(:params).returns(@comment_params) - + @request = stub :path => "" - @comments_controller.stubs(:request).returns(@request) - + @comments_controller.stubs(:request).returns(@request) + Post.expects(:find).with(2).returns(Post.new) @comments = stub() @comments.expects(:build).with("").returns("a new comment") diff --git a/test/test/unit/helpers/internal_test.rb b/test/test/unit/helpers/internal_test.rb index 499206d..24e7c18 100644 --- a/test/test/unit/helpers/internal_test.rb +++ b/test/test/unit/helpers/internal_test.rb @@ -9,11 +9,11 @@ def setup @object = Post.new Post.stubs(:find).with("1").returns(@object) - + @collection = mock() Post.stubs(:find).with(:all).returns(@collection) end - + context "response_for" do setup do @options = ResourceController::ActionOptions.new @@ -22,17 +22,17 @@ def setup @controller.stubs(:options_for).with(:create).returns( @options ) end - should "yield a wants object to the response block" do + should "yield a wants object to the response block" do @controller.send :response_for, :create end end - + context "after" do setup do @options = ResourceController::FailableActionOptions.new @options.success.after { } @controller.stubs(:options_for).with(:create).returns( @options ) - @nil_options = ResourceController::FailableActionOptions.new + @nil_options = ResourceController::FailableActionOptions.new @controller.stubs(:options_for).with(:non_existent).returns(@nil_options) end @@ -46,19 +46,19 @@ def setup end end end - + context "before" do setup do PostsController.stubs(:non_existent).returns ResourceController::ActionOptions.new end - + should "not choke if there is no block" do assert_nothing_raised do @controller.send :before, :non_existent end end end - + context "get options for action" do setup do @create = ResourceController::FailableActionOptions.new @@ -68,30 +68,30 @@ def setup should "get correct object for failure action" do assert_equal @create.fails, @controller.send(:options_for, :create_fails) end - + should "get correct object for successful action" do assert_equal @create.success, @controller.send(:options_for, :create) end - + should "get correct object for non-failable action" do @index = ResourceController::ActionOptions.new PostsController.stubs(:index).returns @index assert_equal @index, @controller.send(:options_for, :index) end - + should "understand new_action to mean new" do @new_action = ResourceController::ActionOptions.new PostsController.stubs(:new_action).returns @new_action assert_equal @new_action, @controller.send(:options_for, :new_action) end end - + context "flash now helper" do setup do klass = Class.new do include ResourceController::Helpers::Internal end - + @c = klass.new @c.stubs(:options_for).returns(stub(:flash_now => 'something')) flash_now = mock() diff --git a/test/test/unit/helpers/nested_test.rb b/test/test/unit/helpers/nested_test.rb index 10151ab..0cd300a 100644 --- a/test/test/unit/helpers/nested_test.rb +++ b/test/test/unit/helpers/nested_test.rb @@ -19,36 +19,36 @@ def setup @params = stub :[] => "1" @controller.stubs(:params).returns(@params) - + @request = stub :path => "" - @controller.stubs(:request).returns(@request) + @controller.stubs(:request).returns(@request) @object = Post.new Post.stubs(:find).with("1").returns(@object) - + @collection = mock() Post.stubs(:find).with(:all).returns(@collection) end - + context "parent type helper" do setup do @comments_controller = CommentsControllerMock.new @comment_params = stub() @comment_params.stubs(:[]).with(:post_id).returns 2 - + @comments_controller.stubs(:params).returns(@comment_params) end should "get the params for the current parent" do assert_equal :post, @comments_controller.send(:parent_type) end - + context "with multiple possible parents" do setup do CommentsControllerMock.class_eval do belongs_to :post, :product end - + @comment_params = stub() @comment_params.stubs(:[]).with(:product_id).returns 5 @comment_params.stubs(:[]).with(:post_id).returns nil @@ -59,21 +59,21 @@ def setup assert_equal :product, @comments_controller.send(:parent_type) end end - + context "with no possible parent" do should "return nil" do assert_nil @controller.send(:parent_type) end end end - + context "parent object helper" do setup do @comments_controller = CommentsControllerMock.new @comment_params = stub() @comment_params.stubs(:[]).with(:post_id).returns 2 @request = stub :path => "" - @comments_controller.stubs(:request).returns(@request) + @comments_controller.stubs(:request).returns(@request) @comments_controller.stubs(:params).returns(@comment_params) @post = Post.new Post.stubs(:find).with(2).returns @post diff --git a/test/test/unit/helpers/singleton_current_objects_test.rb b/test/test/unit/helpers/singleton_current_objects_test.rb index 48d151f..236035e 100644 --- a/test/test/unit/helpers/singleton_current_objects_test.rb +++ b/test/test/unit/helpers/singleton_current_objects_test.rb @@ -14,11 +14,11 @@ class Helpers::SingletonCurrentObjectsTest < Test::Unit::TestCase @user = stub() User.expects(:find).with(2).returns(@user) @image = stub() - end + end context "build object helper with parent" do should "build new object" do - @user.expects(:build_image).with("").returns("a new image") + @user.expects(:build_image).with("").returns("a new image") assert_equal "a new image", @image_controller.send(:build_object) end end @@ -28,41 +28,41 @@ class Helpers::SingletonCurrentObjectsTest < Test::Unit::TestCase @user.expects(:image).returns(@image) assert_equal @image, @image_controller.send(:object) end - end + end end - + context "with singleton parent" do setup do @options_controller = OptionsController.new @options_params = stub :[] => "1" @options_params.stubs(:[]).with('option').returns "" - @options_params.stubs(:[]).with(:id).returns 1 + @options_params.stubs(:[]).with(:id).returns 1 @options_controller.stubs(:params).returns(@options_params) @option = Option.new - - @account = Account.new + + @account = Account.new @options_controller.stubs(:parent_object).returns(@account) @request = stub :path => "account/options/1" - @options_controller.stubs(:request).returns(@request) - + @options_controller.stubs(:request).returns(@request) + @options = stub() - Account.any_instance.stubs(:options).returns(@options) + Account.any_instance.stubs(:options).returns(@options) end - + context "build object helper" do should "build new object" do - @options.expects(:build).with("").returns("a new option") + @options.expects(:build).with("").returns("a new option") assert_equal "a new option", @options_controller.send(:build_object) end end context "object helper" do should "fetch the correct object" do - @options.expects(:find).with(1).returns(@option) + @options.expects(:find).with(1).returns(@option) assert_equal @option, @options_controller.send(:object) end - end + end end end diff --git a/test/test/unit/helpers/singleton_nested_test.rb b/test/test/unit/helpers/singleton_nested_test.rb index b0fa0c4..44ef950 100644 --- a/test/test/unit/helpers/singleton_nested_test.rb +++ b/test/test/unit/helpers/singleton_nested_test.rb @@ -9,9 +9,9 @@ class UsersControllerMock class ImagesControllerMock include ResourceController::Helpers extend ResourceController::Accessors - include ResourceController::Helpers::SingletonCustomizations + include ResourceController::Helpers::SingletonCustomizations class_reader_writer :belongs_to - belongs_to :user + belongs_to :user end class Helpers::SingletonNestedTest < Test::Unit::TestCase @@ -20,9 +20,9 @@ def setup @params = stub :[] => "1" @controller.stubs(:params).returns(@params) @request = stub :path => "" - @controller.stubs(:request).returns(@request) + @controller.stubs(:request).returns(@request) end - + context "singleton parent_type helper" do setup do @image_controller = ImagesControllerMock.new @@ -34,7 +34,7 @@ def setup should "get the params for the current parent" do assert_equal :user, @image_controller.send(:parent_type) end - + context "with multiple possible parents" do setup do ImagesControllerMock.class_eval do @@ -50,21 +50,21 @@ def setup assert_equal :group, @image_controller.send(:parent_type) end end - + context "with no possible parent" do should "return nil" do assert_nil @controller.send(:parent_type) end end end - + context "singleton parent_object helper" do setup do @image_controller = ImagesControllerMock.new @request = stub :path => "" - @image_controller.stubs(:request).returns(@request) + @image_controller.stubs(:request).returns(@request) @image_params = stub() - @image_params.stubs(:[]).with(:user_id).returns 2 + @image_params.stubs(:[]).with(:user_id).returns 2 @image_controller.stubs(:params).returns(@image_params) @user = User.new User.stubs(:find).with(2).returns @user @@ -73,5 +73,5 @@ def setup should "return image with id 2" do assert_equal @user, @image_controller.send(:parent_object) end - end + end end \ No newline at end of file diff --git a/test/test/unit/helpers/singleton_urls_test.rb b/test/test/unit/helpers/singleton_urls_test.rb index e29447c..fe87f7c 100644 --- a/test/test/unit/helpers/singleton_urls_test.rb +++ b/test/test/unit/helpers/singleton_urls_test.rb @@ -1,64 +1,64 @@ require File.dirname(__FILE__)+'/../../test_helper' class Helpers::SingletonUrlsTest < Test::Unit::TestCase - + context "*_url_options helpers" do setup do @account_controller = AccountsController.new @params = stub :[] => "" @account_controller.stubs(:params).returns(@params) @request = stub :path => "" - @account_controller.stubs(:request).returns(@request) - end - + @account_controller.stubs(:request).returns(@request) + end + should "return the correct object options" do assert_equal [nil, nil, :account], @account_controller.send(:object_url_options) end - + context "with parent" do setup do @user = mock - @image = stub() + @image = stub() @image_controller = ImagesController.new @image_params = stub() @image_params.stubs(:[]).with(:user_id).returns 2 @image_controller.stubs(:params).returns(@image_params) - @image_controller.expects(:parent_object).returns @user + @image_controller.expects(:parent_object).returns @user @image_request = stub :path => "" @image_controller.stubs(:request).returns(@image_request) end - + should "return the correct object options" do assert_equal [:edit, [:user, @user], :image], @image_controller.send(:object_url_options, :edit) - end + end end - + context "with singleton parent" do setup do @options_controller = OptionsController.new @options_params = stub() @options_params.stubs(:[]).with('option').returns "" - @options_params.stubs(:[]).with(:id).returns 1 - @options_params.stubs(:[]).with(:account_id).returns nil + @options_params.stubs(:[]).with(:id).returns 1 + @options_params.stubs(:[]).with(:account_id).returns nil @options_controller.stubs(:params).returns(@options_params) @option = Option.new - @account = Account.new + @account = Account.new @options_controller.stubs(:parent_object).returns(@account) @request = stub :path => "account/options/1" - @options_controller.stubs(:request).returns(@request) + @options_controller.stubs(:request).returns(@request) @options = stub() Account.any_instance.stubs(:options).returns(@options) end - + should "return the correct object options" do @options.expects(:find).with(1).returns(@option) assert_equal [:edit, :account, [:option, @option]], @options_controller.send(:object_url_options, :edit) end - + should "return the correct object options for collection" do assert_equal [:account, :options], @options_controller.send(:collection_url_options) end diff --git a/test/test/unit/helpers/urls_test.rb b/test/test/unit/helpers/urls_test.rb index 7ec526f..608560e 100644 --- a/test/test/unit/helpers/urls_test.rb +++ b/test/test/unit/helpers/urls_test.rb @@ -6,53 +6,53 @@ def setup @params = stub :[] => "1" @controller.stubs(:params).returns(@params) - + @request = stub :path => "" - @controller.stubs(:request).returns(@request) + @controller.stubs(:request).returns(@request) @object = Post.new Post.stubs(:find).with("1").returns(@object) - + @collection = mock() Post.stubs(:find).with(:all).returns(@collection) end - + context "*_url_options helpers" do setup do @products_controller = ::Cms::ProductsController.new - + @products_controller.stubs(:params).returns(@params) @request = stub :path => "" - @products_controller.stubs(:request).returns(@request) + @products_controller.stubs(:request).returns(@request) @product = Product.new Product.stubs(:find).with("1").returns(@product) end - + should "return the correct collection options" do assert_equal [nil, :posts], @controller.send(:collection_url_options) end - + should "return the correct object options" do assert_equal [nil, nil, [:post, @object]], @controller.send(:object_url_options) end - + should "return the correct collection options for a namespaced controller" do assert_equal [:cms, nil, :products], @products_controller.send(:collection_url_options) end - + should "return the correct object options for a namespaced controller" do assert_equal [nil, :cms, nil, [:product, @product]], @products_controller.send(:object_url_options) end - + should "return the correct object options when passed an action" do assert_equal [:edit, :cms, nil, [:product, @product]], @products_controller.send(:object_url_options, :edit) end - + should "accept an alternate object when passed one" do p = Product.new assert_equal [nil, :cms, nil, [:product, p]], @products_controller.send(:object_url_options, nil, p) end - + context "with parent" do setup do @params = stub :parent_type => 'user' @@ -66,7 +66,7 @@ def setup @controller.expects(:object).returns @object assert_equal [:edit, [:user, @user], [:post, @object]], @controller.send(:object_url_options, :edit) end - + should "return the correct object options for collection" do assert_equal [[:user, @user], :posts], @controller.send(:collection_url_options) end diff --git a/test/test/unit/helpers_test.rb b/test/test/unit/helpers_test.rb index 2540e04..6406c2c 100644 --- a/test/test/unit/helpers_test.rb +++ b/test/test/unit/helpers_test.rb @@ -1,7 +1,7 @@ require File.dirname(__FILE__)+'/../test_helper' class HelpersTest < Test::Unit::TestCase - + def setup @controller = PostsController.new @@ -10,11 +10,11 @@ def setup @object = Post.new Post.stubs(:find).with("1").returns(@object) - + @collection = mock() Post.stubs(:find).with(:all).returns(@collection) end - + ResourceController::NAME_ACCESSORS.each do |accessor| context "#{accessor} accessor" do should "default to returning the singular name of the controller" do diff --git a/test/test/unit/response_collector_test.rb b/test/test/unit/response_collector_test.rb index 5caea84..c6bd187 100644 --- a/test/test/unit/response_collector_test.rb +++ b/test/test/unit/response_collector_test.rb @@ -17,18 +17,18 @@ class ResponseCollectorTest < Test::Unit::TestCase assert_equal Proc, @collector[:js][1].class, @collector[:js].inspect assert @collector[:xml][1].nil?, @collector[:xml].inspect end - + should "clear responses with clear method" do @collector.clear assert @collector.responses.empty? end - + should "destroy methods before readding them, if they're already there" do @collector.html assert @collector[:html][1].nil? end end - + context "duplicating a response collector" do setup do @collector = ResourceController::ResponseCollector.new @@ -40,10 +40,10 @@ class ResponseCollectorTest < Test::Unit::TestCase should "not bleed in to the original" do assert @duplicate[:css].nil? end - + should "duplicate existing responses at the time of duplication" do assert_equal :js, @duplicate[:js].first end end - + end \ No newline at end of file diff --git a/test/test/unit/urligence_test.rb b/test/test/unit/urligence_test.rb index 6d0b0e1..e774a72 100644 --- a/test/test/unit/urligence_test.rb +++ b/test/test/unit/urligence_test.rb @@ -5,67 +5,67 @@ class PhotosController include Urligence end -class UrligenceTest < Test::Unit::TestCase +class UrligenceTest < Test::Unit::TestCase def setup @controller = PhotosController.new @tag = stub(:class => stub(:name => "Tag"), :to_param => 'awesomestuff') @photo = stub(:class => stub(:name => "Photo"), :to_param => 1) end - + context "with one object" do setup do setup_mocks "/photos/#{@photo.to_param}", :photo, @photo end - + context "urligence" do should "return the correct path" do assert_equal @expected_path, @controller.urligence(@photo, :path) end end - + context "smart_url" do should "return the correct url" do assert_equal @expected_url, @controller.smart_url(@photo) end end - + context "smart_path" do should "return the correct path" do assert_equal @expected_path, @controller.smart_path(@photo) end end end - + context "with two objects" do setup do setup_mocks "/tags/#{@tag.to_param}/photos/#{@photo.to_param}", :tag_photo, @tag, @photo end - + should "return the correct path" do assert_equal @expected_path, @controller.urligence(@tag, @photo, :path) end end - + context "with a namespace as first param" do setup do setup_mocks "/admin/tags/#{@tag.to_param}/photos/#{@photo.to_param}", :admin_tag_photo, @tag, @photo end - + should "return the correct path" do assert_equal @expected_path, @controller.urligence(:admin, @tag, @photo, :path) end end - + context "with many nil options anywhere in the arguments" do setup do setup_mocks "/tags/#{@tag.to_param}/photos/#{@photo.to_param}", :tag_photo, @tag, @photo end - + should "return the correct path" do assert_equal @expected_path, @controller.urligence(nil, nil, nil, @tag, nil, @photo, nil, :path) end end - + context "with a symbol as the last parameter" do setup do setup_mocks "/tags/#{@tag.to_param}/photos", :tag_photos, @tag @@ -75,7 +75,7 @@ def setup assert_equal @expected_path, @controller.urligence(@tag, :photos, :path) end end - + context "with a symbol as the only parameter" do setup do setup_mocks "/photos", :photos @@ -85,7 +85,7 @@ def setup assert_equal @expected_path, @controller.urligence(nil, :photos, :path) end end - + context "with a namespace, and a plural symbol" do setup do setup_mocks "/admin/products", :admin_products @@ -95,41 +95,41 @@ def setup assert_equal @expected_path, @controller.urligence(:admin, :products, :path) end end - + context "with only symbols" do setup do setup_mocks '/admin/products/new', :new_admin_products end - + should "return the correct path" do assert_equal @expected_path, @controller.urligence(:new, :admin, :products, :path) end end - + context "with array parameters for specifying the names of routes that don't match the class name of the object" do setup do setup_mocks '/something_tags/1', :something_tag, @tag end - + context "urligence" do should "use the name of the symbol as the url fragment" do assert_equal @expected_path, @controller.urligence([:something_tag, @tag], :path) end end - + context "smart_url" do should "return the correct path" do assert_equal @expected_url, @controller.smart_url([:something_tag, @tag]) end end - + context "smart_path" do should "return the correct path" do assert_equal @expected_path, @controller.smart_path([:something_tag, @tag]) end end end - + context "with array parameters and a namespace" do setup do setup_mocks '/admin/something_tags/1', :admin_something_tag, @tag @@ -139,7 +139,7 @@ def setup assert_equal @expected_path, @controller.urligence(:admin, [:something_tag, @tag], :path) end end - + context "with array parameters, a namespace, and an ending symbol" do setup do setup_mocks '/admin/something_tags/1/photos', :admin_something_tag_photos, @tag @@ -149,7 +149,7 @@ def setup assert_equal @expected_path, @controller.urligence(:admin, [:something_tag, @tag], :photos, :path) end end - + context "with array parameters, a symbol namespace, and normal model parameters" do setup do setup_mocks '/admin/something_tags/1/photos/1', :admin_something_tag_photo, @tag, @photo @@ -159,7 +159,7 @@ def setup assert_equal @expected_path, @controller.urligence(:admin, [:something_tag, @tag], @photo, :path) end end - + context "hash_for" do context "url" do setup do @@ -170,7 +170,7 @@ def setup assert_equal "something", @controller.hash_for_smart_url(@photo, @tag) end end - + context "path" do setup do @photo_tag = stub(:class => stub(:name => "PhotoTag"), :to_param => 'awesomestuff') @@ -181,7 +181,7 @@ def setup assert_equal "something", @controller.hash_for_smart_path(@photo, [:tag, @photo_tag]) end end - + context "collection path" do setup do @controller.stubs(:hash_for_photos_path).with({}).returns('something') @@ -192,7 +192,7 @@ def setup end end end - + private def setup_mocks(expected_path, method, *params) @expected_path = expected_path diff --git a/test/vendor/plugins/shoulda/lib/shoulda.rb b/test/vendor/plugins/shoulda/lib/shoulda.rb index a90ff18..3c5c3b4 100644 --- a/test/vendor/plugins/shoulda/lib/shoulda.rb +++ b/test/vendor/plugins/shoulda/lib/shoulda.rb @@ -11,7 +11,7 @@ possible_config_paths << File.join(ENV["HOME"], ".shoulda.conf") if ENV["HOME"] possible_config_paths << "shoulda.conf" possible_config_paths << File.join("test", "shoulda.conf") -possible_config_paths << File.join(RAILS_ROOT, "test", "shoulda.conf") if defined?(RAILS_ROOT) +possible_config_paths << File.join(RAILS_ROOT, "test", "shoulda.conf") if defined?(RAILS_ROOT) possible_config_paths.each do |config_file| if File.exists? config_file @@ -23,7 +23,7 @@ require 'shoulda/color' if shoulda_options[:color] module Test # :nodoc: all - module Unit + module Unit class TestCase include ThoughtBot::Shoulda::General @@ -36,7 +36,7 @@ class TestCase module ActionController #:nodoc: all module Integration - class Session + class Session include ThoughtBot::Shoulda::General end end diff --git a/test/vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb b/test/vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb index 1ad8a7f..bff684b 100644 --- a/test/vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb +++ b/test/vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb @@ -8,9 +8,9 @@ module Shoulda # :nodoc: # should_require_attributes :name, :phone_number # should_not_allow_values_for :phone_number, "abcd", "1234" # should_allow_values_for :phone_number, "(123) 456-7890" - # + # # should_protect_attributes :password - # + # # should_have_one :profile # should_have_many :dogs # should_have_many :messes, :through => :dogs @@ -23,7 +23,7 @@ module ActiveRecord # Ensures that the model cannot be saved if one of the attributes listed is not present. # # Options: - # * :message - value the test expects to find in errors.on(:attribute). + # * :message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /blank/ # # Example: @@ -33,7 +33,7 @@ def should_require_attributes(*attributes) message = get_options!(attributes, :message) message ||= /blank/ klass = model_class - + attributes.each do |attribute| should "require #{attribute} to be set" do object = klass.new @@ -49,7 +49,7 @@ def should_require_attributes(*attributes) # Requires an existing record # # Options: - # * :message - value the test expects to find in errors.on(:attribute). + # * :message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /taken/ # # Example: @@ -58,36 +58,36 @@ def should_require_attributes(*attributes) def should_require_unique_attributes(*attributes) message, scope = get_options!(attributes, :message, :scoped_to) message ||= /taken/ - + klass = model_class attributes.each do |attribute| attribute = attribute.to_sym should "require unique value for #{attribute}#{" scoped to #{scope}" if scope}" do assert existing = klass.find(:first), "Can't find first #{klass}" object = klass.new - + object.send(:"#{attribute}=", existing.send(attribute)) if scope assert_respond_to object, :"#{scope}=", "#{klass.name} doesn't seem to have a #{scope} attribute." object.send(:"#{scope}=", existing.send(scope)) end - + assert !object.valid?, "#{klass.name} does not require a unique value for #{attribute}." assert object.errors.on(attribute), "#{klass.name} does not require a unique value for #{attribute}." - + assert_contains(object.errors.on(attribute), message) - + # Now test that the object is valid when changing the scoped attribute # TODO: There is a chance that we could change the scoped field # to a value that's already taken. An alternative implementation # could actually find all values for scope and create a unique - # one. + # one. if scope # Assume the scope is a foreign key if the field is nil object.send(:"#{scope}=", existing.send(scope).nil? ? 1 : existing.send(scope).next) object.errors.clear object.valid? - assert_does_not_contain(object.errors.on(attribute), message, + assert_does_not_contain(object.errors.on(attribute), message, "after :#{scope} set to #{object.send(scope.to_sym)}") end end @@ -116,12 +116,12 @@ def should_protect_attributes(*attributes) end end end - + # Ensures that the attribute cannot be set to the given values # Requires an existing record # # Options: - # * :message - value the test expects to find in errors.on(:attribute). + # * :message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /invalid/ # # Example: @@ -141,7 +141,7 @@ def should_not_allow_values_for(attribute, *bad_values) end end end - + # Ensures that the attribute can be set to the given values. # Requires an existing record # @@ -165,9 +165,9 @@ def should_allow_values_for(attribute, *good_values) # Requires an existing record # # Options: - # * :short_message - value the test expects to find in errors.on(:attribute). + # * :short_message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /short/ - # * :long_message - value the test expects to find in errors.on(:attribute). + # * :long_message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /long/ # # Example: @@ -177,7 +177,7 @@ def should_ensure_length_in_range(attribute, range, opts = {}) short_message, long_message = get_options!([opts], :short_message, :long_message) short_message ||= /short/ long_message ||= /long/ - + klass = model_class min_length = range.first max_length = range.last @@ -189,7 +189,7 @@ def should_ensure_length_in_range(attribute, range, opts = {}) assert object = klass.find(:first), "Can't find first #{klass}" object.send("#{attribute}=", min_value) assert !object.save, "Saved #{klass} with #{attribute} set to \"#{min_value}\"" - assert object.errors.on(attribute), + assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{min_value}\"" assert_contains(object.errors.on(attribute), short_message, "when set to \"#{min_value}\"") end @@ -204,13 +204,13 @@ def should_ensure_length_in_range(attribute, range, opts = {}) assert_does_not_contain(object.errors.on(attribute), short_message, "when set to \"#{min_value}\"") end end - + should "not allow #{attribute} to be more than #{max_length} chars long" do max_value = "x" * (max_length + 1) assert object = klass.find(:first), "Can't find first #{klass}" object.send("#{attribute}=", max_value) assert !object.save, "Saved #{klass} with #{attribute} set to \"#{max_value}\"" - assert object.errors.on(attribute), + assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{max_value}\"" assert_contains(object.errors.on(attribute), long_message, "when set to \"#{max_value}\"") end @@ -224,13 +224,13 @@ def should_ensure_length_in_range(attribute, range, opts = {}) assert_does_not_contain(object.errors.on(attribute), long_message, "when set to \"#{max_value}\"") end end - end - + end + # Ensures that the length of the attribute is at least a certain length # Requires an existing record # # Options: - # * :short_message - value the test expects to find in errors.on(:attribute). + # * :short_message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /short/ # # Example: @@ -239,9 +239,9 @@ def should_ensure_length_in_range(attribute, range, opts = {}) def should_ensure_length_at_least(attribute, min_length, opts = {}) short_message = get_options!([opts], :short_message) short_message ||= /short/ - + klass = model_class - + if min_length > 0 min_value = "x" * (min_length - 1) should "not allow #{attribute} to be less than #{min_length} chars long" do @@ -264,9 +264,9 @@ def should_ensure_length_at_least(attribute, min_length, opts = {}) # Requires an existing record # # Options: - # * :low_message - value the test expects to find in errors.on(:attribute). + # * :low_message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /included/ - # * :high_message - value the test expects to find in errors.on(:attribute). + # * :high_message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /included/ # # Example: @@ -276,7 +276,7 @@ def should_ensure_value_in_range(attribute, range, opts = {}) low_message, high_message = get_options!([opts], :low_message, :high_message) low_message ||= /included/ high_message ||= /included/ - + klass = model_class min = range.first max = range.last @@ -314,13 +314,13 @@ def should_ensure_value_in_range(attribute, range, opts = {}) object.save assert_does_not_contain(object.errors.on(attribute), high_message, "when set to \"#{v}\"") end - end - + end + # Ensure that the attribute is numeric # Requires an existing record # # Options: - # * :message - value the test expects to find in errors.on(:attribute). + # * :message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /number/ # # Example: @@ -344,7 +344,7 @@ def should_only_allow_numeric_values_for(*attributes) # Ensures that the has_many relationship exists. Will also test that the # associated table has the required columns. Works with polymorphic # associations. - # + # # Options: # * :through - association name for has_many :through # @@ -368,7 +368,7 @@ def should_have_many(*associations) assert through_reflection, "#{klass.name} does not have any relationship to #{through}" assert_equal(through, reflection.options[:through]) end - + unless reflection.options[:through] # This is not a through association, so check for the existence of the foreign key on the other table if reflection.options[:foreign_key] @@ -400,7 +400,7 @@ def should_have_one(*associations) reflection = klass.reflect_on_association(association) assert reflection, "#{klass.name} does not have any relationship to #{association}" assert_equal :has_one, reflection.macro - + associated_klass = (reflection.options[:class_name] || association.to_s.camelize).constantize if reflection.options[:foreign_key] @@ -408,17 +408,17 @@ def should_have_one(*associations) elsif reflection.options[:as] fk = reflection.options[:as].to_s.foreign_key fk_type = fk.gsub(/_id$/, '_type') - assert associated_klass.column_names.include?(fk_type), - "#{associated_klass.name} does not have a #{fk_type} column." + assert associated_klass.column_names.include?(fk_type), + "#{associated_klass.name} does not have a #{fk_type} column." else fk = klass.name.foreign_key end - assert associated_klass.column_names.include?(fk.to_s), - "#{associated_klass.name} does not have a #{fk} foreign key." + assert associated_klass.column_names.include?(fk.to_s), + "#{associated_klass.name} does not have a #{fk} foreign key." end end end - + # Ensures that the has_and_belongs_to_many relationship exists, and that the join # table is in place. # @@ -438,7 +438,7 @@ def should_have_and_belong_to_many(*associations) end end end - + # Ensure that the belongs_to relationship exists. # # should_belong_to :parent @@ -460,7 +460,7 @@ def should_belong_to(*associations) end end end - + # Ensure that the given class methods are defined on the model. # # should_have_class_methods :find, :destroy @@ -507,10 +507,10 @@ def should_have_db_columns(*columns) end # Ensure that the given column is defined on the models backing SQL table. The options are the same as - # the instance variables defined on the column definition: :precision, :limit, :default, :null, + # the instance variables defined on the column definition: :precision, :limit, :default, :null, # :primary, :type, :scale, and :sql_type. # - # should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255, + # should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255, # :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)' # def should_have_db_column(name, opts = {}) @@ -528,7 +528,7 @@ def should_have_db_column(name, opts = {}) # Ensures that there are DB indices on the given columns or tuples of columns. # Also aliased to should_have_index for readability - # + # # should_have_indices :email, :name, [:commentable_type, :commentable_id] # should_have_index :age # @@ -545,11 +545,11 @@ def should_have_indices(*columns) end alias_method :should_have_index, :should_have_indices - + # Ensures that the model cannot be saved if one of the attributes listed is not accepted. # # Options: - # * :message - value the test expects to find in errors.on(:attribute). + # * :message - value the test expects to find in errors.on(:attribute). # Regexp or string. Default = /must be accepted/ # # Example: @@ -559,7 +559,7 @@ def should_require_acceptance_of(*attributes) message = get_options!(attributes, :message) message ||= /must be accepted/ klass = model_class - + attributes.each do |attribute| should "require #{attribute} to be accepted" do object = klass.new @@ -571,9 +571,9 @@ def should_require_acceptance_of(*attributes) end end end - + private - + include ThoughtBot::Shoulda::Private end end diff --git a/test/vendor/plugins/shoulda/lib/shoulda/color.rb b/test/vendor/plugins/shoulda/lib/shoulda/color.rb index 1ccfad2..ea45882 100644 --- a/test/vendor/plugins/shoulda/lib/shoulda/color.rb +++ b/test/vendor/plugins/shoulda/lib/shoulda/color.rb @@ -2,17 +2,17 @@ # Completely stolen from redgreen gem # -# Adds colored output to your tests. Specify color: true in +# Adds colored output to your tests. Specify color: true in # your ~/.shoulda.conf file to enable. # -# *Bug*: for some reason, this adds another line of output to the end of -# every rake task, as though there was another (empty) set of tests. +# *Bug*: for some reason, this adds another line of output to the end of +# every rake task, as though there was another (empty) set of tests. # A fix would be most welcome. # -module ThoughtBot::Shoulda::Color +module ThoughtBot::Shoulda::Color COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 } # :nodoc: def self.method_missing(color_name, *args) # :nodoc: - color(color_name) + args.first + color(:clear) + color(color_name) + args.first + color(:clear) end def self.color(color) # :nodoc: "\e[#{COLORS[color.to_sym]}m" @@ -34,7 +34,7 @@ class AutoRunner # :nodoc: alias :old_initialize :initialize def initialize(standalone) old_initialize(standalone) - @runner = proc do |r| + @runner = proc do |r| Test::Unit::UI::Console::RedGreenTestRunner end end @@ -67,7 +67,7 @@ def output_single(something, level=NORMAL) when 'E' then ThoughtBot::Shoulda::Color.yellow("E") else something end - @io.write(something) + @io.write(something) @io.flush end end diff --git a/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb b/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb index f0059c0..9e08f2b 100644 --- a/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb +++ b/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb @@ -10,17 +10,17 @@ def self.included(other) # :nodoc: end end end - + # = Macro test helpers for your controllers # # By using the macro helpers you can quickly and easily create concise and easy to read test suites. - # + # # This code segment: # context "on GET to :show for first record" do # setup do # get :show, :id => 1 # end - # + # # should_assign_to :user # should_respond_with :success # should_render_template :show @@ -44,16 +44,16 @@ module ClassMethods VALID_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy] # :doc: # A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller. - # + # # Example: # class UsersControllerTest < Test::Unit::TestCase # load_all_fixtures - # + # # def setup # ...normal setup code... # @user = User.find(:first) # end - # + # # should_be_restful do |resource| # resource.identifier = :id # resource.klass = User @@ -61,17 +61,17 @@ module ClassMethods # resource.parent = [] # resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy] # resource.formats = [:html, :xml] - # + # # resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13} # resource.update.params = { :name => "sue" } - # + # # resource.create.redirect = "user_url(@user)" # resource.update.redirect = "user_url(@user)" # resource.destroy.redirect = "users_url" - # + # # resource.create.flash = /created/i # resource.update.flash = /updated/i - # resource.destroy.flash = /removed/i + # resource.destroy.flash = /removed/i # end # end # @@ -81,7 +81,7 @@ class ResourceOptions # Configuration options for the create, update, destroy actions under should_be_restful class ActionOptions # String evaled to get the target of the redirection. - # All of the instance variables set by the controller will be available to the + # All of the instance variables set by the controller will be available to the # evaled code. # # Example: @@ -97,7 +97,7 @@ class ActionOptions # create:: /created/ # update:: /updated/ attr_accessor :flash - + # Hash describing the params that should be sent in with this action. attr_accessor :params end @@ -109,19 +109,19 @@ class ActionOptions # setup do # @request.session[:logged_in] = false # end - # + # # should_be_restful do |resource| # resource.parent = :user - # + # # resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy] # resource.denied.flash = /get outta here/i # resource.denied.redirect = 'new_session_url' - # end + # end # end # class DeniedOptions # String evaled to get the target of the redirection. - # All of the instance variables set by the controller will be available to the + # All of the instance variables set by the controller will be available to the # evaled code. # # Example: @@ -140,15 +140,15 @@ class DeniedOptions attr_accessor :actions end - # Name of key in params that references the primary key. + # Name of key in params that references the primary key. # Will almost always be :id (default), unless you are using a plugin or have patched rails. attr_accessor :identifier - + # Name of the ActiveRecord class this resource is responsible for. Automatically determined from # test class if not explicitly set. UserTest => "User" attr_accessor :klass - # Name of the instantiated ActiveRecord object that should be used by some of the tests. + # Name of the instantiated ActiveRecord object that should be used by some of the tests. # Defaults to the underscored name of the AR class. CompanyManager => :company_manager attr_accessor :object @@ -179,7 +179,7 @@ class DeniedOptions attr_accessor :parent alias parents parent alias parents= parent= - + # Actions that should be tested. Must be a subset of VALID_ACTIONS (default). # Tests for each actionw will only be generated if the action is listed here. # The special value of :all will test all of the REST actions. @@ -195,7 +195,7 @@ class DeniedOptions # Example: # resource.actions = [:html, :xml] attr_accessor :formats - + # ActionOptions object specifying options for the create action. attr_accessor :create @@ -231,15 +231,15 @@ def normalize!(target) # :nodoc: @denied.actions = VALID_ACTIONS if @denied.actions == :all @actions = VALID_ACTIONS if @actions == :all @formats = VALID_FORMATS if @formats == :all - + @denied.actions = @denied.actions.map(&:to_sym) @actions = @actions.map(&:to_sym) @formats = @formats.map(&:to_sym) - + ensure_valid_members(@actions, VALID_ACTIONS, 'actions') ensure_valid_members(@denied.actions, VALID_ACTIONS, 'denied.actions') ensure_valid_members(@formats, VALID_FORMATS, 'formats') - + @identifier ||= :id @klass ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize @object ||= @klass.name.tableize.singularize @@ -256,9 +256,9 @@ def normalize!(target) # :nodoc: @update.redirect ||= "#{member_helper}(#{member_args})" @denied.redirect ||= "new_session_url" end - + private - + def ensure_valid_members(ary, valid_members, name) # :nodoc: invalid = ary - valid_members raise ArgumentError, "Unsupported #{name}: #{invalid.inspect}" unless invalid.empty? @@ -268,12 +268,12 @@ def ensure_valid_members(ary, valid_members, name) # :nodoc: # :section: should_be_restful # Generates a full suite of tests for a restful controller. # - # The following definition will generate tests for the +index+, +show+, +new+, + # The following definition will generate tests for the +index+, +show+, +new+, # +edit+, +create+, +update+ and +destroy+ actions, in both +html+ and +xml+ formats: # # should_be_restful do |resource| # resource.parent = :user - # + # # resource.create.params = { :title => "first post", :body => 'blah blah blah'} # resource.update.params = { :title => "changed" } # end @@ -287,7 +287,7 @@ def ensure_valid_members(ary, valid_members, name) # :nodoc: # "on GET to :show as xml should have ContentType set to 'application/xml'." # "on GET to :show as xml should respond with success." # "on GET to :show as xml should return as the root element." - # The +resource+ parameter passed into the block is a ResourceOptions object, and + # The +resource+ parameter passed into the block is a ResourceOptions object, and # is used to configure the tests for the details of your resources. # def should_be_restful(&blk) # :yields: resource @@ -298,7 +298,7 @@ def should_be_restful(&blk) # :yields: resource resource.formats.each do |format| resource.actions.each do |action| if self.respond_to? :"make_#{action}_#{format}_tests" - self.send(:"make_#{action}_#{format}_tests", resource) + self.send(:"make_#{action}_#{format}_tests", resource) else should "test #{action} #{format}" do flunk "Test for #{action} as #{format} not implemented" @@ -309,7 +309,7 @@ def should_be_restful(&blk) # :yields: resource end # :section: Test macros - + # Macro that creates a test asserting that the flash contains the given value. # val can be a String, a Regex, or nil (indicating that the flash should not be set) # @@ -321,7 +321,7 @@ def should_be_restful(&blk) # :yields: resource def should_set_the_flash_to(val) if val should "have #{val.inspect} in the flash" do - assert_contains flash.values, val, ", Flash: #{flash.inspect}" + assert_contains flash.values, val, ", Flash: #{flash.inspect}" end else should "not set the flash" do @@ -329,13 +329,13 @@ def should_set_the_flash_to(val) end end end - + # Macro that creates a test asserting that the flash is empty. Same as # @should_set_the_flash_to nil@ def should_not_set_the_flash should_set_the_flash_to nil end - + # Macro that creates a test asserting that the controller assigned to @name # # Example: @@ -367,13 +367,13 @@ def should_respond_with(response) assert_response response end end - + # Macro that creates a test asserting that the controller rendered the given template. # Example: # # should_render_template :new def should_render_template(template) - should "render template #{template.inspect}" do + should "render template #{template.inspect}" do assert_template template.to_s end end @@ -392,19 +392,19 @@ def should_redirect_to(url) end end end - + # Macro that creates a test asserting that the rendered view contains a

element. def should_render_a_form should "display a form" do - assert_select "form", true, "The template doesn't contain a element" + assert_select "form", true, "The template doesn't contain a element" end end end module InstanceMethods # :nodoc: - + private # :enddoc: - + SPECIAL_INSTANCE_VARIABLES = %w{ _cookies _flash @@ -431,7 +431,7 @@ module InstanceMethods # :nodoc: url variables_added }.map(&:to_s) - + def instantiate_variables_from_assigns(*names, &blk) old = {} names = (@response.template.assigns.keys - SPECIAL_INSTANCE_VARIABLES) if names.empty? @@ -447,7 +447,7 @@ def instantiate_variables_from_assigns(*names, &blk) def get_existing_record(res) # :nodoc: returning(instance_variable_get("@#{res.object}")) do |record| - assert(record, "This test requires you to set @#{res.object} in your setup block") + assert(record, "This test requires you to set @#{res.object} in your setup block") end end @@ -461,7 +461,7 @@ def make_parent_params(resource, record = nil, parent_names = nil) # :nodoc: end end - end + end end end diff --git a/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb b/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb index ba92bba..c6f8c1c 100644 --- a/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb +++ b/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb @@ -1,5 +1,5 @@ -module ThoughtBot # :nodoc: - module Shoulda # :nodoc: +module ThoughtBot # :nodoc: + module Shoulda # :nodoc: module Controller # :nodoc: module HTML # :nodoc: all def self.included(other) @@ -7,8 +7,8 @@ def self.included(other) extend ThoughtBot::Shoulda::Controller::HTML::ClassMethods end end - - module ClassMethods + + module ClassMethods def controller_name_from_class self.name.gsub(/Test/, '') end @@ -18,7 +18,7 @@ def make_show_html_tests(res) setup do record = get_existing_record(res) parent_params = make_parent_params(res, record) - get :show, parent_params.merge({ res.identifier => record.to_param }) + get :show, parent_params.merge({ res.identifier => record.to_param }) end if res.denied.actions.include?(:show) @@ -26,7 +26,7 @@ def make_show_html_tests(res) should_redirect_to res.denied.redirect should_set_the_flash_to res.denied.flash else - should_assign_to res.object + should_assign_to res.object should_respond_with :success should_render_template :show should_not_set_the_flash @@ -39,15 +39,15 @@ def make_edit_html_tests(res) setup do @record = get_existing_record(res) parent_params = make_parent_params(res, @record) - get :edit, parent_params.merge({ res.identifier => @record.to_param }) + get :edit, parent_params.merge({ res.identifier => @record.to_param }) end - + if res.denied.actions.include?(:edit) should_not_assign_to res.object should_redirect_to res.denied.redirect should_set_the_flash_to res.denied.flash else - should_assign_to res.object + should_assign_to res.object should_respond_with :success should_render_template :edit should_not_set_the_flash @@ -64,13 +64,13 @@ def make_index_html_tests(res) setup do record = get_existing_record(res) rescue nil parent_params = make_parent_params(res, record) - get(:index, parent_params) + get(:index, parent_params) end if res.denied.actions.include?(:index) should_not_assign_to res.object.to_s.pluralize should_redirect_to res.denied.redirect - should_set_the_flash_to res.denied.flash + should_set_the_flash_to res.denied.flash else should_respond_with :success should_assign_to res.object.to_s.pluralize @@ -85,7 +85,7 @@ def make_new_html_tests(res) setup do record = get_existing_record(res) rescue nil parent_params = make_parent_params(res, record) - get(:new, parent_params) + get(:new, parent_params) end if res.denied.actions.include?(:new) @@ -109,11 +109,11 @@ def make_destroy_html_tests(res) parent_params = make_parent_params(res, @record) delete :destroy, parent_params.merge({ res.identifier => @record.to_param }) end - + if res.denied.actions.include?(:destroy) should_redirect_to res.denied.redirect should_set_the_flash_to res.denied.flash - + should "not destroy record" do assert_nothing_raised { assert @record.reload } end @@ -142,15 +142,15 @@ def make_create_html_tests(res) @count = res.klass.count post :create, parent_params.merge(res.object => res.create.params) end - + if res.denied.actions.include?(:create) should_redirect_to res.denied.redirect should_set_the_flash_to res.denied.flash should_not_assign_to res.object - + should "not create new record" do assert_equal @count, res.klass.count - end + end else should_assign_to res.object should_set_the_flash_to res.create.flash @@ -161,9 +161,9 @@ def make_create_html_tests(res) end should "not have errors on @#{res.object}" do - assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:" + assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:" end - end + end end end @@ -187,7 +187,7 @@ def make_update_html_tests(res) else should_redirect_to res.update.redirect end - + should "not have errors on @#{res.object}" do assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:" end diff --git a/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb b/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb index f3c16f1..9afb1f9 100644 --- a/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb +++ b/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb @@ -1,13 +1,13 @@ -module ThoughtBot # :nodoc: - module Shoulda # :nodoc: +module ThoughtBot # :nodoc: + module Shoulda # :nodoc: module Controller # :nodoc: - module XML + module XML def self.included(other) #:nodoc: other.class_eval do extend ThoughtBot::Shoulda::Controller::XML::ClassMethods end end - + module ClassMethods # Macro that creates a test asserting that the controller responded with an XML content-type # and that the XML contains ++ as the root element. @@ -15,7 +15,7 @@ def should_respond_with_xml_for(name = nil) should "have ContentType set to 'application/xml'" do assert_xml_response end - + if name should "return <#{name}/> as the root element" do body = @response.body.first(100).map {|l| " #{l}"} @@ -24,23 +24,23 @@ def should_respond_with_xml_for(name = nil) end end alias should_respond_with_xml should_respond_with_xml_for - + protected - + def make_show_xml_tests(res) # :nodoc: context "on GET to #{controller_name_from_class}#show as xml" do setup do request_xml record = get_existing_record(res) parent_params = make_parent_params(res, record) - get :show, parent_params.merge({ res.identifier => record.to_param }) + get :show, parent_params.merge({ res.identifier => record.to_param }) end if res.denied.actions.include?(:show) should_not_assign_to res.object should_respond_with 401 else - should_assign_to res.object + should_assign_to res.object should_respond_with :success should_respond_with_xml_for res.object end @@ -60,12 +60,12 @@ def make_index_xml_tests(res) # :nodoc: setup do request_xml parent_params = make_parent_params(res) - get(:index, parent_params) + get(:index, parent_params) end if res.denied.actions.include?(:index) should_not_assign_to res.object.to_s.pluralize - should_respond_with 401 + should_respond_with 401 else should_respond_with :success should_respond_with_xml_for res.object.to_s.pluralize @@ -82,10 +82,10 @@ def make_destroy_xml_tests(res) # :nodoc: parent_params = make_parent_params(res, @record) delete :destroy, parent_params.merge({ res.identifier => @record.to_param }) end - + if res.denied.actions.include?(:destroy) should_respond_with 401 - + should "not destroy record" do assert @record.reload end @@ -107,21 +107,21 @@ def make_create_xml_tests(res) # :nodoc: @count = res.klass.count post :create, parent_params.merge(res.object => res.create.params) end - + if res.denied.actions.include?(:create) should_respond_with 401 should_not_assign_to res.object - + should "not create new record" do assert_equal @count, res.klass.count - end + end else should_assign_to res.object should "not have errors on @#{res.object}" do - assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:" + assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:" end - end + end end end @@ -152,18 +152,18 @@ def make_update_xml_tests(res) # :nodoc: def request_xml @request.accept = "application/xml" end - + # Asserts that the controller's response was 'application/xml' def assert_xml_response content_type = (@response.headers["Content-Type"] || @response.headers["type"]).to_s regex = %r{\bapplication/xml\b} msg = "Content Type '#{content_type.inspect}' doesn't match '#{regex.inspect}'\n" - msg += "Body: #{@response.body.first(100).chomp} ..." + msg += "Body: #{@response.body.first(100).chomp} ..." assert_match regex, content_type, msg end - + end end end diff --git a/test/vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb b/test/vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb index d1fab1d..26dca6c 100644 --- a/test/vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb +++ b/test/vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb @@ -10,14 +10,14 @@ class << self # = Should statements # - # Should statements are just syntactic sugar over normal Test::Unit test methods. A should block - # contains all the normal code and assertions you're used to seeing, with the added benefit that + # Should statements are just syntactic sugar over normal Test::Unit test methods. A should block + # contains all the normal code and assertions you're used to seeing, with the added benefit that # they can be wrapped inside context blocks (see below). # # == Example: # # class UserTest << Test::Unit::TestCase - # + # # def setup # @user = User.new("John", "Doe") # end @@ -25,9 +25,9 @@ class << self # should "return its full name" # assert_equal 'John Doe', @user.full_name # end - # + # # end - # + # # ...will produce the following test: # * "test: User should return its full name. " # @@ -55,8 +55,8 @@ def should_eventually(name, &blk) end # = Contexts - # - # A context block groups should statements under a common set of setup/teardown methods. + # + # A context block groups should statements under a common set of setup/teardown methods. # Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability # and readability of your test code. # @@ -67,7 +67,7 @@ def should_eventually(name, &blk) # setup do # @user = User.find(:first) # end - # + # # should "return its full name" # assert_equal 'John Doe', @user.full_name # end @@ -76,7 +76,7 @@ def should_eventually(name, &blk) # # This code will produce the method "test: A User instance should return its full name. ". # - # Contexts may be nested. Nested contexts run their setup blocks from out to in before each + # Contexts may be nested. Nested contexts run their setup blocks from out to in before each # should statement. They then run their teardown blocks from in to out after each should statement. # # class UserTest << Test::Unit::TestCase @@ -84,16 +84,16 @@ def should_eventually(name, &blk) # setup do # @user = User.find(:first) # end - # + # # should "return its full name" # assert_equal 'John Doe', @user.full_name # end - # + # # context "with a profile" do # setup do # @user.profile = Profile.find(:first) # end - # + # # should "return true when sent :has_profile?" # assert @user.has_profile? # end @@ -101,11 +101,11 @@ def should_eventually(name, &blk) # end # end # - # This code will produce the following methods + # This code will produce the following methods # * "test: A User instance should return its full name. " # * "test: A User instance with a profile should return true when sent :has_profile?. " # - # Just like should statements, a context block can exist next to normal def test_the_old_way; end + # Just like should statements, a context block can exist next to normal def test_the_old_way; end # tests. This means you do not have to fully commit to the context/should syntax in a test file. def context(name, &blk) @@ -179,9 +179,9 @@ def create_test_from_should_hash(should) test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym if test_unit_class.instance_methods.include?(test_name.to_s) - warn " * WARNING: '#{test_name}' is already defined" + warn " * WARNING: '#{test_name}' is already defined" end - + context = self test_unit_class.send(:define_method, test_name) do |*args| begin @@ -230,7 +230,7 @@ def method_missing(method, *args, &blk) end module Test # :nodoc: all - module Unit + module Unit class TestCase extend Thoughtbot::Shoulda end diff --git a/test/vendor/plugins/shoulda/lib/shoulda/general.rb b/test/vendor/plugins/shoulda/lib/shoulda/general.rb index 7b1b36b..7e8118b 100644 --- a/test/vendor/plugins/shoulda/lib/shoulda/general.rb +++ b/test/vendor/plugins/shoulda/lib/shoulda/general.rb @@ -6,17 +6,17 @@ def self.included(other) # :nodoc: extend ThoughtBot::Shoulda::General::ClassMethods end end - + module ClassMethods # Loads all fixture files (test/fixtures/*.yml) def load_all_fixtures - all_fixtures = Dir.glob(File.join(Test::Unit::TestCase.fixture_path, "*.yml")).collect do |f| + all_fixtures = Dir.glob(File.join(Test::Unit::TestCase.fixture_path, "*.yml")).collect do |f| File.basename(f, '.yml').to_sym end fixtures *all_fixtures end end - + # Prints a message to stdout, tagged with the name of the calling method. def report!(msg = "") puts("#{caller.first}: #{msg}") @@ -48,7 +48,7 @@ def assert_contains(collection, x, extra_msg = "") case x when Regexp: assert(collection.detect { |e| e =~ x }, msg) else assert(collection.include?(x), msg) - end + end end # Asserts that the given collection does not contain item x. If x is a regular expression, ensure that @@ -59,9 +59,9 @@ def assert_does_not_contain(collection, x, extra_msg = "") case x when Regexp: assert(!collection.detect { |e| e =~ x }, msg) else assert(!collection.include?(x), msg) - end + end end - + # Asserts that the given object can be saved # # assert_save User.new(params) @@ -76,21 +76,21 @@ def assert_save(obj) def assert_valid(obj) assert obj.valid?, "Errors: #{pretty_error_messages obj}" end - + # Asserts that an email was delivered. Can take a block that can further - # narrow down the types of emails you're expecting. + # narrow down the types of emails you're expecting. # - # assert_sent_email + # assert_sent_email # # Passes if ActionMailer::Base.deliveries has an email - # + # # assert_sent_email do |email| # email.subject =~ /hi there/ && email.to.include?('none@none.com') # end - # + # # Passes if there is an email with subject containing 'hi there' and # 'none@none.com' as one of the recipients. - # + # def assert_sent_email emails = ActionMailer::Base.deliveries assert !emails.empty?, "No emails were sent" @@ -112,7 +112,7 @@ def assert_did_not_send_email def pretty_error_messages(obj) obj.errors.map { |a, m| "#{a} #{m} (#{obj.send(a).inspect})" } end - + end end end