Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove make_resourceful, reimplement admin/tags controller in vanilla…

… rails, specs pass
  • Loading branch information...
commit 9082f6f5a6641e85899fcd1cc9be1812ca670b8b 1 parent 4592177
@xaviershay authored
Showing with 16 additions and 4,888 deletions.
  1. +16 −12 app/controllers/admin/tags_controller.rb
  2. +0 −148 vendor/plugins/make_resourceful/DEFAULTS
  3. +0 −7 vendor/plugins/make_resourceful/LICENSE
  4. +0 −239 vendor/plugins/make_resourceful/README
  5. +0 −42 vendor/plugins/make_resourceful/Rakefile
  6. +0 −1  vendor/plugins/make_resourceful/VERSION
  7. +0 −87 vendor/plugins/make_resourceful/generators/resourceful_scaffold/resourceful_scaffold_generator.rb
  8. +0 −5 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/controller.rb
  9. +0 −10 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/fixtures.yml
  10. +0 −57 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/functional_test.rb
  11. +0 −2  vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/helper.rb
  12. +0 −13 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/migration.rb
  13. +0 −2  vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/model.rb
  14. +0 −7 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/unit_test.rb
  15. +0 −5 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view__form.haml
  16. +0 −11 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_edit.haml
  17. +0 −5 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_index.haml
  18. +0 −9 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_new.haml
  19. +0 −12 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_partial.haml
  20. +0 −14 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_show.haml
  21. +0 −3  vendor/plugins/make_resourceful/init.rb
  22. +0 −52 vendor/plugins/make_resourceful/lib/resourceful/base.rb
  23. +0 −333 vendor/plugins/make_resourceful/lib/resourceful/builder.rb
  24. +0 −344 vendor/plugins/make_resourceful/lib/resourceful/default/accessors.rb
  25. +0 −92 vendor/plugins/make_resourceful/lib/resourceful/default/actions.rb
  26. +0 −50 vendor/plugins/make_resourceful/lib/resourceful/default/callbacks.rb
  27. +0 −118 vendor/plugins/make_resourceful/lib/resourceful/default/responses.rb
  28. +0 −137 vendor/plugins/make_resourceful/lib/resourceful/default/urls.rb
  29. +0 −83 vendor/plugins/make_resourceful/lib/resourceful/maker.rb
  30. +0 −33 vendor/plugins/make_resourceful/lib/resourceful/response.rb
  31. +0 −181 vendor/plugins/make_resourceful/lib/resourceful/serialize.rb
  32. +0 −444 vendor/plugins/make_resourceful/spec/accessors_spec.rb
  33. +0 −283 vendor/plugins/make_resourceful/spec/actions_spec.rb
  34. +0 −12 vendor/plugins/make_resourceful/spec/base_spec.rb
  35. +0 −297 vendor/plugins/make_resourceful/spec/builder_spec.rb
  36. +0 −72 vendor/plugins/make_resourceful/spec/callbacks_spec.rb
  37. +0 −397 vendor/plugins/make_resourceful/spec/integration_spec.rb
  38. +0 −104 vendor/plugins/make_resourceful/spec/maker_spec.rb
  39. +0 −37 vendor/plugins/make_resourceful/spec/response_spec.rb
  40. +0 −314 vendor/plugins/make_resourceful/spec/responses_spec.rb
  41. +0 −35 vendor/plugins/make_resourceful/spec/rspec_on_rails/LICENSE
  42. +0 −81 vendor/plugins/make_resourceful/spec/rspec_on_rails/redirect_to.rb
  43. +0 −28 vendor/plugins/make_resourceful/spec/rspec_on_rails/render_template.rb
  44. +0 −133 vendor/plugins/make_resourceful/spec/serialize_spec.rb
  45. +0 −261 vendor/plugins/make_resourceful/spec/spec_helper.rb
  46. +0 −276 vendor/plugins/make_resourceful/spec/urls_spec.rb
View
28 app/controllers/admin/tags_controller.rb
@@ -1,22 +1,26 @@
class Admin::TagsController < Admin::BaseController
- make_resourceful do
- actions :all
+ def index
+ @tags = Tag.paginate(:order => 'name', :page => params[:page])
+ end
- after(:update) do
- flash[:notice] = "Tag updated"
- end
+ def show
+ end
- response_for(:update) do
- redirect_to(:action => 'index')
+ def edit
+ end
+
+ def update
+ if @tag.update_attributes(params[:tag])
+ flash[:notice] = "Tag updated"
+ redirect_to :action => 'index'
+ else
+ render :action => 'edit'
end
end
protected
- def current_objects
- @current_object ||= current_model.paginate(
- :order => "name",
- :page => params[:page]
- )
+ def find_tag
+ @tag = Tag.find(params[:id])
end
end
View
148 vendor/plugins/make_resourceful/DEFAULTS
@@ -1,148 +0,0 @@
-There's a rough equivalence between a basic @make_resourceful@ call
-and a hand-made controller.
-This:
-
- class PostsController < ApplicationController
- make_resourceful { actions :all }
- end
-
-Creates a controller that works more or less like the one that follows.
-Note that the real code generated by make_resourceful
-is more extensible in various ways.
-Thus whenever possible, there are comments in the following controller
-indicating how to customize various bits of the controller.
-
- class PostsController < ApplicationController
- def index
- # Override #current_objects to change this
- @posts = Post.find(:all)
-
- # Use before :index to add something here
-
- # Use response_for :index to change this
- respond_to { |f| f.html; f.js }
- end
-
- def show
- # Override #current_object to change this
- @post = Post.find(params[:id])
-
- # Use before :show to add something here
-
- # Use response_for :show to change this
- respond_to { |f| f.html; f.js }
- end
-
- def create
- # Override #build_object to change this
- @post = Post.new(params[:post])
-
- # Use before :create to add something here
-
- if @post.save
- # Use after :create to add something here
-
- # Use response_for :create to change this
- respond_to do |f|
- f.html do
- flash[:notice] = "Create successful!"
- redirect_to post_path(@post)
- end
- f.js
- end
- else
- # Use after :create_fails to add something here
-
- # Use response_for :create_fails to change this
- respond_to do |f|
- format.html
- flash[:error] = "There was a problem!"
- render :action => :new, :status => 422
- end
- format.js
- end
- end
- end
-
- def update
- # Override #current_object to change this
- @post = Post.find(params[:id])
-
- # Use before :update to do something here
-
- if @post.update_attributes params[:post]
- # Use after :update to add something here
-
- # Use response_for :update to change this
- respond_to do |f|
- f.html do
- flash[:notice] = "Save successful!"
- redirect_to post_path(@post)
- end
- f.js
- end
- else
- # Use after :update_fails to add something here
-
- # Use response_for :update_fails to change this
- respond_to do |f|
- format.html
- flash[:error] = "There was a problem saving!"
- render :action => :edit, :status => 422
- end
- format.js
- end
- end
- end
-
- def new
- # Override #build_object to change this
- @post = Post.new(params[:post])
-
- # Use before :new to add something here
-
- # Use response_for :new to change this
- respond_to { |f| f.html; f.js }
- end
-
- def edit
- # Override #current_object to change this
- @post = Post.find(params[:id])
-
- # Use before :edit to add something here
-
- # Use response_for :edit to change this
- respond_to { |f| f.html; f.js }
- end
-
- def destroy
- # Override #current_object to change this
- @post = Post.find(params[:id])
-
- # Use before :destroy to do something here
-
- if @post.destroy
- # Use after :destroy to add something here
-
- # Use response_for :destroy to change this
- respond_to do |f|
- f.html do
- flash[:notice] = "Record deleted!"
- redirect_to posts_path(@post)
- end
- f.js
- end
- else
- # Use after :destroy_fails to add something here
-
- # Use response_for :destroy_fails to change this
- respond_to do |f|
- format.html
- flash[:error] = "There was a problem deleting!"
- render :back
- end
- format.js
- end
- end
- end
- end
View
7 vendor/plugins/make_resourceful/LICENSE
@@ -1,7 +0,0 @@
-Copyright (c) 2007 Hampton Catlin
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
239 vendor/plugins/make_resourceful/README
@@ -1,239 +0,0 @@
-= make_resourceful
-===== Take back control of your Controllers. Make them awesome. Make them sleek. Make them resourceful.
-
-REST is a fine pattern for designing controllers,
-but it can be pretty repetitive.
-Who wants to write out the same actions and copy the same model lookup logic
-all over their application?
-
-make_resourceful handles all that for you.
-It sets up all your RESTful actions and responses with next to no code.
-Everything has full, sensible default functionality.
-
-Of course, no controller _only_ uses the defaults.
-So make_resourceful can be massively customized,
-while still keeping your controllers trim and readable.
-
-== Get it!
-
-Rails
-
- $ ruby script/plugin install http://svn.hamptoncatlin.com/make_resourceful/trunk
- $ mv vendor/plugins/trunk vendor/plugins/make_resourceful
-
-Subversion
-
- $ svn co http://svn.hamptoncatlin.com/make_resourceful/trunk make_resourceful
-
-== Use it!
-
-The easiest way to start with make_resourceful
-is to run the resource_scaffold generator.
-It uses the same syntax as the Rails scaffold_resource generator:
-
- $ script/generate resource_scaffold post title:string body:text
-
-It does, however, require Haml[http://haml.hamptoncatlin.com].
-You _are_ using Haml, right? No?
-I'll wait here while you go fall in love with it.
-
-If you want to try make_resourceful on one of your current controllers,
-just replace the mess of repetition with this:
-
- class FooController < ApplicationController
- make_resourceful do
- actions :all
- end
- end
-
-Those three lines will replace the entire default controller
-that comes out of the scaffold_resource generator.
-
-=== Really?
-
-Yes.
-
-=== Can I do nested resources?
-
- make_resourceful do
- actions :all
- belongs_to :post
- end
-
-=== What if I want to use fancy permalinks?
-
- def current_object
- @current_object ||= current_model.find_by_permalink(params[:id])
- end
-
-=== What about paging?
-
- def current_objects
- @current_object ||= current_model.find(:all,
- :order => "created_at DESC", :page => {:current => params[:page], :size => 10 } )
- end
-
-=== What if I want to do something in the middle of an action?
-
- before :show, :index do
- @page_title = "Awesome!"
- end
-
- after :create_fails do
- @page_title = "Not So Awesome!"
- end
-
-=== What about all of my awesome respond_to blocks for my XML APIs and RJS responses?
-
- response_for :show do |format|
- format.html
- format.js
- format.xml
- end
-
- response_for :update_fails do |format|
- format.html { render :action => 'edit' }
- format.json { render :json => false.to_json, :status => 422 }
- end
-
-=== So I guess I have to write responses for all my actions?
-
-Nope! make_resourceful makes them do the right thing by default.
-You only need to customize them if you want to do something special.
-
-=== Seriously?!
-
-Yes!
-
-== Grok it!
-
-=== +make_resourceful+ the Method
-
-The +make_resourceful+ block is where most of the action happens.
-Here you specify which actions you want to auto-generate,
-what code you want to run for given callbacks,
-and so forth.
-
-You also use the block to declare various bits of information about your controller.
-For instance, if the controller is nested, you'd call +belongs_to+.
-If you wanted to expose your models as some sort of text format,
-you'd call +publish+.
-
-Check out the documentation of Resourceful::Builder
-for more information on the methods you can call here.
-
-=== Helper Methods
-
-make_resourceful provides lots of useful methods
-that can be used in your callbacks and in your views.
-They range from accessing the records you're looking up
-to easily generating URLs for a record
-to getting information about the action itself.
-
-Two of the most useful methods are +current_object+ and +current_objects+
-(note the subtle plurality difference).
-+current_objects+ only works for +index+,
-and returns all the records in the current model.
-+current_object+ works for all actions other than +index+,
-and returns the record that's currently being dealt with.
-
-The full documentation of the helper methods
-is in Resourceful::Default::Accessors and Resourceful::Default::URLs.
-
-=== Nested Resources
-
-make_resourceful supports easy management of nested resources.
-This is set up with the Resourceful::Builder#belongs_to declaration.
-Pass in the name of the parent model,
-
- belongs_to :user
-
-and everything will be taken care of.
-When +index+ is run for GET /users/12/albums,
-parent_object[link:classes/Resourceful/Accessors.html#M000024]
-will get <tt>User.find(params[:user_id])</tt>,
-and current_objects[link:classes/Resourceful/Default/Accessors.html#M000010]
-will get <tt>parent_object.albums</tt>.
-When +create+ is run for POST /users/12/albums,
-the newly created Album will automatically belong to the user
-with id 12.
-
-The normal non-scoped actions still work, too.
-GET /albums/15 runs just fine.
-make_resourceful knows that since there's no <tt>params[:user_id]</tt>,
-you just want to deal with the album.
-
-You can even have a single resource nested under several different resources.
-Just pass multiple parent names to the Resourceful::Builder#belongs_to, like
-
- belongs_to :user, :artist
-
-Then /users/15/albums and /artists/7/albums will both work.
-
-This does, however, mean that make_resourceful only supports one level of nesting.
-There's no automatic handling of /users/15/collections/437/albums.
-However, this is really the best way to organize most resources anyway;
-see this article[http://weblog.jamisbuck.org/2007/2/5/nesting-resources].
-
-If you really need a deeply nested controller,
-it should be easy enough to set up on your own.
-Just override current_model[link:classes/Resourceful/Default/Accessors.html#M000018].
-See the next section for more details.
-
-=== Overriding Methods
-
-Not only are helper methods useful to the developer to use,
-they're used internally by the actions created by make_resourceful.
-Thus one of the main ways make_resourceful can be customized
-is by overriding accessors.
-
-For instance, if you want to only look up the 10 most recent records for +index+,
-you're override +current_objects+.
-If you wanted to use a different model than that suggested by the name of the controller,
-you'd override +current_model+.
-
-When you're overriding methods that do SQL lookups, though, be a little cautious.
-By default, these methods cache their values in instance variables
-so that multiple SQL queries aren't run on multiple calls.
-When overriding them, it's wise for you to do the same.
-For instance,
-
- def current_object
- @current_object ||= current_model.find_by_name(params[:name])
- end
-
-=== For More Information...
-
-Haven't found all the information you need in the RDoc?
-Still a little confused about something?
-Don't despair, there are still more resources available!
-
-* Nathan Weizenbaum periodically makes blog posts about new features and versions of make_resourceful.
- They may be a little outdated, but they should still be useful and explanatory.
- * On nesting and associations: here[http://nex-3.com/posts/55-nesting-and-make_resourceful].
- * An overview of make_resourceful 0.2.0 and 0.2.2: here[http://localhost:3000/posts/54-make_resourceful-0-2-0].
- * On Resourceful::Builder#publish[link:classes/Resourceful/Builder.html#M000061]
- and Resourceful::Serialize:
- here[http://nex-3.com/posts/35-make_resourceful-the-basics-of-publish] and
- here[http://nex-3.com/posts/36-make_resourceful-publish-extras].
-* There's an excellent, active Google Group (link[http://groups.google.com/group/make_resourceful])
- where people will be happy to answer your questions.
-* Read the source code!
- It's very straightforward,
- and make_resourceful is built to encourage overriding methods
- and hacking the source.
-
----
-
-Copyright 2007 Hampton Catlin, Nathan Weizenbaum, and Jeff Hardy.
-
-Contributions by:
-
-* Russell Norris
-* Jonathan Linowes
-* Cristi Balan
-* Mike Ferrier
-* James Golick
-* Don Petersen
-* Alex Ross
-* Tom Stuart
View
42 vendor/plugins/make_resourceful/Rakefile
@@ -1,42 +0,0 @@
-require 'rake'
-require 'rake/testtask'
-require 'rake/rdoctask'
-require 'spec/rake/spectask'
-
-desc 'Default: run unit tests.'
-task :default => :test
-
-spec_files = Rake::FileList["spec/**/*_spec.rb"]
-
-desc "Run specs"
-Spec::Rake::SpecTask.new do |t|
- t.spec_files = spec_files
- t.spec_opts = ["-c"]
-end
-
-desc "Generate code coverage"
-Spec::Rake::SpecTask.new(:coverage) do |t|
- t.spec_files = spec_files
- t.rcov = true
- t.rcov_opts = ['--exclude', 'spec,/var/lib/gems']
-end
-
-desc 'Test the make_resourceful plugin.'
-task :test do
- Dir.chdir(File.dirname(__FILE__) + '/test')
- tests = IO.popen('rake test')
-
- while byte = tests.read(1)
- print byte
- end
-end
-
-desc 'Generate documentation for the make_resourceful plugin.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'make_resourceful'
- rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.main = 'README'
- rdoc.rdoc_files.include(FileList.new('*').exclude(/[^A-Z0-9]/))
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
View
1  vendor/plugins/make_resourceful/VERSION
@@ -1 +0,0 @@
-0.2.3.0
View
87 ...lugins/make_resourceful/generators/resourceful_scaffold/resourceful_scaffold_generator.rb
@@ -1,87 +0,0 @@
-class ResourcefulScaffoldGenerator < Rails::Generator::NamedBase
- attr_reader :controller_class_path,
- :controller_file_path,
- :controller_class_nesting,
- :controller_class_nesting_depth,
- :controller_class_name,
- :controller_underscore_name,
- :controller_plural_name
- alias_method :controller_file_name, :controller_underscore_name
- alias_method :controller_table_name, :controller_plural_name
-
- def initialize(runtime_args, runtime_options = {})
- super
-
- base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@name.pluralize)
- @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name)
-
- if @controller_class_nesting.empty?
- @controller_class_name = @controller_class_name_without_nesting
- else
- @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
- end
- end
-
- def manifest
- record do |m|
- # Check for class naming collisions.
- m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
- m.class_collisions(class_path, "#{class_name}")
-
- # Controller, helper, views, and test directories.
- m.directory(File.join('app/models', class_path))
- m.directory(File.join('app/controllers', controller_class_path))
- m.directory(File.join('app/helpers', controller_class_path))
- m.directory(File.join('app/views', controller_class_path, controller_file_name))
- m.directory(File.join('test/functional', controller_class_path))
- m.directory(File.join('test/unit', class_path))
- m.directory(File.join('test/fixtures', class_path))
-
- # Views
- for action in scaffold_views
- m.template("view_#{action}.haml", File.join('app/views', controller_class_path, controller_file_name, "#{action}.haml"))
- end
- m.template('view_partial.haml', File.join('app/views', controller_class_path, controller_file_name, "_#{singular_name}.haml"))
-
- # Helper
- m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb"))
-
- # Model
- m.template('model.rb', File.join('app/models', class_path, "#{file_name}.rb"))
-
- unless options[:skip_migration]
- m.migration_template('migration.rb', 'db/migrate',
- :assigns => {
- :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}",
- :attributes => attributes
- },
- :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}")
- end
-
- # Controller
- m.template('controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb"))
-
- # Tests
- m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
- m.template('unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb"))
- m.template('fixtures.yml', File.join('test/fixtures', "#{table_name}.yml"))
-
- # Route
- m.route_resources controller_file_name
- end
- end
-
- protected
-
- def banner
- "Usage: #{$0} resourcefulscaffold ModelName [field:type, field:type]"
- end
-
- def scaffold_views
- %w[ index show new edit _form ]
- end
-
- def model_name
- class_name.demodulize
- end
-end
View
5 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/controller.rb
@@ -1,5 +0,0 @@
-class <%= controller_class_name %>Controller < ApplicationController
- make_resourceful do
- actions :all
- end
-end
View
10 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/fixtures.yml
@@ -1,10 +0,0 @@
-one:
- id: 1
-<% for attribute in attributes -%>
- <%= attribute.name %>: <%= attribute.default %>
-<% end -%>
-two:
- id: 2
-<% for attribute in attributes -%>
- <%= attribute.name %>: <%= attribute.default %>
-<% end -%>
View
57 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/functional_test.rb
@@ -1,57 +0,0 @@
-require File.dirname(__FILE__) + '<%= '/..' * controller_class_nesting_depth %>/../test_helper'
-require '<%= controller_file_path %>_controller'
-
-# Re-raise errors caught by the controller.
-class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
-
-class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
- fixtures :<%= table_name %>
-
- def setup
- @controller = <%= controller_class_name %>Controller.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
- end
-
- def test_should_get_index
- get :index
- assert_response :success
- assert assigns(:<%= table_name %>)
- end
-
- 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
-
- def test_should_show_<%= file_name %>
- get :show, :id => 1
- assert_response :success
- end
-
- 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
View
2  vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/helper.rb
@@ -1,2 +0,0 @@
-module <%= controller_class_name %>Helper
-end
View
13 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/migration.rb
@@ -1,13 +0,0 @@
-class <%= migration_name %> < ActiveRecord::Migration
- def self.up
- create_table :<%= table_name %>, :force => true do |t|
-<% for attribute in attributes -%>
- t.column :<%= attribute.name %>, :<%= attribute.type %>
-<% end -%>
- end
- end
-
- def self.down
- drop_table :<%= table_name %>
- end
-end
View
2  vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/model.rb
@@ -1,2 +0,0 @@
-class <%= class_name %> < ActiveRecord::Base
-end
View
7 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/unit_test.rb
@@ -1,7 +0,0 @@
-require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
-
-class <%= class_name %>Test < Test::Unit::TestCase
- def test_truth
- assert true
- end
-end
View
5 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view__form.haml
@@ -1,5 +0,0 @@
-<%- for attribute in attributes -%>
-%p
- %label{:for => "<%= singular_name %>_<%= attribute.name %>"} <%= attribute.column.human_name %>:
- = f.<%= attribute.field_type %> :<%= attribute.name %>
-<% end -%>
View
11 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_edit.haml
@@ -1,11 +0,0 @@
-%h1 Editing <%= singular_name %>
-
-= error_messages_for :<%= singular_name %>
-
-- 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_path
-|
-= link_to 'Back', objects_path
View
5 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_index.haml
@@ -1,5 +0,0 @@
-%h1 Listing <%= plural_name %>
-
-= render :partial => '<%= singular_name %>', :collection => current_objects
-
-= link_to 'New <%= singular_name %>', new_object_path
View
9 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_new.haml
@@ -1,9 +0,0 @@
-%h1 Creating <%= singular_name %>
-
-= error_messages_for :<%= singular_name %>
-
-- form_for(:<%= singular_name %>, :url => objects_url) do |f|
- = render :partial => "form", :locals => {:f => f}
- %p= submit_tag "Create"
-
-= link_to 'Back', objects_path
View
12 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_partial.haml
@@ -1,12 +0,0 @@
-%div[<%= singular_name %>]
-<% for attribute in attributes -%>
- %p.<%= attribute.name %>
- %strong <%= attribute.column.human_name %>
- = h <%= singular_name %>.<%= attribute.name %>
-<% end -%>
-
- = link_to 'Show', object_path(<%= singular_name %>)
- |
- = link_to 'Edit', edit_object_path(<%= singular_name %>)
- |
- = link_to 'Destroy', object_path(<%= singular_name %>), :confirm => 'Really destroy <%= singular_name %>?', :method => :delete
View
14 vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_show.haml
@@ -1,14 +0,0 @@
-%h1 Viewing <%= singular_name %>
-
-%div[current_object]
-<% for attribute in attributes -%>
- %p.<%= attribute.name %>
- %strong <%= attribute.column.human_name %>
- = h current_object.<%= attribute.name %>
-<% end -%>
-
-= link_to 'Edit', edit_object_path
-|
-= link_to 'Destroy', object_path, :confirm => 'Really destroy <%= singular_name %>?', :method => :delete
-|
-= link_to 'Back', objects_path
View
3  vendor/plugins/make_resourceful/init.rb
@@ -1,3 +0,0 @@
-require 'resourceful/maker'
-
-ActionController::Base.extend Resourceful::Maker
View
52 vendor/plugins/make_resourceful/lib/resourceful/base.rb
@@ -1,52 +0,0 @@
-# The module containing all the code for make_resourceful.
-#
-# For methods available in the +make_resourceful+ block,
-# see Resourceful::Builder.
-#
-# For helper methods and methods you can override
-# to customize the behavior of your actions,
-# see Resourceful::Default::Accessors
-# and Resourceful::Default::URLs.
-module Resourceful
- # We want to define some stuff before we load other modules
-
- # The default actions generated by make_resourceful.
- ACTIONS = [:index, :show, :edit, :update, :create, :new, :destroy]
-
- # The actions that modify the database.
- MODIFYING_ACTIONS = [:update, :create, :destroy]
-
- # The actions that act on multiple records.
- PLURAL_ACTIONS = [:index]
-
- # The actions that act on just one record.
- SINGULAR_ACTIONS = ACTIONS - PLURAL_ACTIONS
-end
-
-require 'resourceful/default/accessors'
-require 'resourceful/default/responses'
-require 'resourceful/default/callbacks'
-require 'resourceful/default/urls'
-
-# All modules included by this module
-# are made available to the controller as accessors.
-module Resourceful::Base
- @@made_resourceful_callbacks = []
-
- # This method is meant to be called by included classes.
- # It takes a block of the same form as that given to Maker#make_resourceful.
- # The Maker will then run that block
- # along with the blocks given by the individual controllers.
- def self.made_resourceful(&block)
- if block
- @@made_resourceful_callbacks << block
- else
- @@made_resourceful_callbacks
- end
- end
-
- include Resourceful::Default::Accessors
- include Resourceful::Default::Responses
- include Resourceful::Default::Callbacks
- include Resourceful::Default::URLs
-end
View
333 vendor/plugins/make_resourceful/lib/resourceful/builder.rb
@@ -1,333 +0,0 @@
-require 'resourceful/response'
-require 'resourceful/serialize'
-require 'resourceful/default/actions'
-
-module Resourceful
- # The Maker#make_resourceful block is evaluated in the context
- # of an instance of this class.
- # It provides various methods for customizing the behavior of the actions
- # built by make_resourceful.
- #
- # All instance methods of this class are available in the +make_resourceful+ block.
- class Builder
- # The klass of the controller on which the builder is working.
- attr :controller, true
-
- # The constructor is only meant to be called internally.
- #
- # This takes the klass (class object) of a controller
- # and constructs a Builder ready to apply the make_resourceful
- # additions to the controller.
- def initialize(kontroller)
- @controller = kontroller
- @inherited = !kontroller.read_inheritable_attribute(:resourceful_responses).blank?
- @action_module = Resourceful::Default::Actions.dup
- @ok_actions = []
- @callbacks = {:before => {}, :after => {}}
- @responses = {}
- @publish = {}
- @parents = []
- end
-
- # This method is only meant to be called internally.
- #
- # Applies all the changes that have been declared
- # via the instance methods of this Builder
- # to the kontroller passed in to the constructor.
- def apply
- apply_publish
-
- kontroller = @controller
- Resourceful::ACTIONS.each do |action_named|
- # See if this is a method listed by #actions
- unless @ok_actions.include? action_named
- # If its not listed, then remove the method
- # No one can hit it... if its DEAD!
- @action_module.send :remove_method, action_named
- end
- end
-
- kontroller.hidden_actions.reject! &@ok_actions.method(:include?)
- kontroller.send :include, @action_module
-
- kontroller.read_inheritable_attribute(:resourceful_callbacks).merge! @callbacks
- kontroller.read_inheritable_attribute(:resourceful_responses).merge! @responses
- kontroller.write_inheritable_attribute(:made_resourceful, true)
-
- kontroller.write_inheritable_attribute(:parents, @parents)
- kontroller.before_filter :load_parent_object, :only => @ok_actions
- end
-
- # :call-seq:
- # actions(*available_actions)
- # actions :all
- #
- # Adds the default RESTful actions to the controller.
- #
- # If the only argument is <tt>:all</tt>,
- # adds all the actions listed in Resourceful::ACTIONS[link:classes/Resourceful.html]
- # (or Resourceful::SINGULAR_ACTIONS[link:classes/Resourceful.html]
- # for a singular controller).
- #
- # Otherwise, this adds all actions
- # whose names were passed as arguments.
- #
- # For example:
- #
- # actions :show, :new, :create
- #
- # This adds the +show+, +new+, and +create+ actions
- # to the controller.
- #
- # The available actions are defined in Default::Actions.
- def actions(*available_actions)
- if available_actions.first == :all
- available_actions = controller.new.plural? ? ACTIONS : SINGULAR_ACTIONS
- end
-
- available_actions.each { |action| @ok_actions << action.to_sym }
- end
- alias build actions
-
- # :call-seq:
- # before(*events) { ... }
- #
- # Sets up a block of code to run before one or more events.
- #
- # All the default actions can be used as +before+ events:
- # <tt>:index</tt>, <tt>:show</tt>, <tt>:create</tt>, <tt>:update</tt>, <tt>:new</tt>, <tt>:edit</tt>, and <tt>:destroy</tt>.
- #
- # +before+ events are run after any objects are loaded[link:classes/Resourceful/Default/Accessors.html#M000015],
- # but before any database operations or responses.
- #
- # For example:
- #
- # before :show, :edit do
- # @page_title = current_object.title
- # end
- #
- # This will set the <tt>@page_title</tt> variable
- # to the current object's title
- # for the show and edit actions.
- def before(*events, &block)
- events.each do |event|
- @callbacks[:before][event.to_sym] = block
- end
- end
-
- # :call-seq:
- # after(*events) { ... }
- #
- # Sets up a block of code to run after one or more events.
- #
- # There are two sorts of +after+ events.
- # <tt>:create</tt>, <tt>:update</tt>, and <tt>:destroy</tt>
- # are run after their respective database operations
- # have been completed successfully.
- # <tt>:create_fails</tt>, <tt>:update_fails</tt>, and <tt>:destroy_fails</tt>,
- # on the other hand,
- # are run after the database operations fail.
- #
- # +after+ events are run after the database operations
- # but before any responses.
- #
- # For example:
- #
- # after :create_fails, :update_fails do
- # current_object.password = nil
- # end
- #
- # This will nillify the password of the current object
- # if the object creation/modification failed.
- def after(*events, &block)
- events.each do |event|
- @callbacks[:after][event.to_sym] = block
- end
- end
-
- # :call-seq:
- # response_for(*actions) { ... }
- # response_for(*actions) { |format| ... }
- #
- # Sets up a block of code to run
- # instead of the default responses for one or more events.
- #
- # If the block takes a format parameter,
- # it has the same semantics as Rails' +respond_to+ method.
- # Various format methods are called on the format object
- # with blocks that say what to do for each format.
- # For example:
- #
- # response_for :index do |format|
- # format.html
- # format.atom do
- # headers['Content-Type'] = 'application/atom+xml; charset=utf-8'
- # render :action => 'atom', :layout => false
- # end
- # end
- #
- # This doesn't do anything special for the HTML
- # other than ensure that the proper view will be rendered,
- # but for ATOM it sets the proper content type
- # and renders the atom template.
- #
- # If you only need to set the HTML response,
- # you can omit the format parameter.
- # For example:
- #
- # response_for :new do
- # render :action => 'edit'
- # end
- #
- # This is the same as
- #
- # response_for :new do |format|
- # format.html { render :action => 'edit' }
- # end
- #
- # The default responses are defined by
- # Default::Responses.included[link:classes/Resourceful/Default/Responses.html#M000011].
- def response_for(*actions, &block)
- if block.arity < 1
- response_for(*actions) do |format|
- format.html(&block)
- end
- else
- response = Response.new
- block.call response
-
- actions.each do |action|
- @responses[action.to_sym] = response.formats
- end
- end
- end
-
- # :call-seq:
- # publish *formats, options = {}, :attributes => [ ... ]
- #
- # publish allows you to easily expose information about resourcess in a variety of formats.
- # The +formats+ parameter is a list of formats
- # in which to publish the resources.
- # The formats supported by default are +xml+, +yaml+, and +json+,
- # but other formats may be added by defining +to_format+ methods
- # for the Array and Hash classes
- # and registering the mime type with Rails' Mime::Type.register[http://api.rubyonrails.org/classes/Mime/Type.html#M001115].
- # See Resourceful::Serialize for more details..
- #
- # The <tt>:attributes</tt> option is mandatory.
- # It takes an array of attributes (as symbols) to make public.
- # These attributes can refer to any method on current_object;
- # they aren't limited to database fields.
- # For example:
- #
- # # posts_controller.rb
- # publish :yaml, :attributes => [:title, :created_at, :rendered_content]
- #
- # Then GET /posts/12.yaml would render
- #
- # ---
- # post:
- # title: Cool Stuff
- # rendered_content: |-
- # <p>This is a post.</p>
- # <p>It's about <strong>really</strong> cool stuff.</p>
- # created_at: 2007-04-28 04:32:08 -07:00
- #
- # The <tt>:attributes</tt> array can even contain attributes
- # that are themselves models.
- # In this case, you must use a hash to specify their attributes as well.
- # For example:
- #
- # # person_controller.rb
- # publish :xml, :json, :attributes => [
- # :name, :favorite_color, {
- # :pet_cat => [:name, :breed],
- # :hat => [:type]
- # }]
- #
- # Then GET /people/18.xml would render
- #
- # <?xml version="1.0" encoding="UTF-8"?>
- # <person>
- # <name>Nathan</name>
- # <favorite-color>blue</favorite_color>
- # <pet-cat>
- # <name>Jasmine</name>
- # <breed>panther</breed>
- # </pet-cat>
- # <hat>
- # <type>top</type>
- # </hat>
- # </person>
- #
- # publish will also allow the +index+ action
- # to render lists of objects.
- # An example would be too big,
- # but play with it a little on your own to see.
- #
- # publish takes only one optional option: <tt>only</tt>.
- # This specifies which action to publish the resources for.
- # By default, they're published for both +show+ and +index+.
- # For example:
- #
- # # cats_controller.rb
- # publish :json, :only => :index, :attributes => [:name, :breed]
- #
- # Then GET /cats.json would work, but GET /cats/294.json would fail.
- def publish(*formats)
- options = {
- :only => [:show, :index]
- }.merge(Hash === formats.last ? formats.pop : {})
- raise "Must specify :attributes option" unless options[:attributes]
-
- Array(options.delete(:only)).each do |action|
- @publish[action] ||= []
- formats.each do |format|
- format = format.to_sym
- @publish[action] << [format, proc do
- render_action = [:json, :xml].include?(format) ? format : :text
- render render_action => (plural_action? ? current_objects : current_object).serialize(format, options)
- end]
- end
- end
- end
-
- # Specifies parent resources for the current resource.
- # Each of these parents will be loaded automatically
- # if the proper id parameter is given.
- # For example,
- #
- # # cake_controller.rb
- # belongs_to :baker, :customer
- #
- # Then on GET /bakers/12/cakes,
- #
- # params[:baker_id] #=> 12
- # parent? #=> true
- # parent_name #=> "baker"
- # parent_model #=> Baker
- # parent_object #=> Baker.find(12)
- # current_objects #=> Baker.find(12).cakes
- #
- def belongs_to(*parents)
- @parents = parents.map(&:to_s)
- end
-
- # This method is only meant to be called internally.
- #
- # Returns whether or not the Builder's controller
- # inherits make_resourceful settings from a parent controller.
- def inherited?
- @inherited
- end
-
- private
-
- def apply_publish
- @publish.each do |action, types|
- @responses[action.to_sym] ||= []
- @responses[action.to_sym] += types
- end
- end
- end
-end
View
344 vendor/plugins/make_resourceful/lib/resourceful/default/accessors.rb
@@ -1,344 +0,0 @@
-module Resourceful
- # This module contains various methods
- # that are available from actions and callbacks.
- # Default::Accessors and Default::URLs are the most useful to users;
- # the rest are mostly used internally.
- #
- # However, if you want to poke around the internals a little,
- # check out Default::Actions, which has the default Action definitions,
- # and Default::Responses.included, which defines the default response_for[link:classes/Resourceful/Builder.html#M000061] blocks.
- module Default
- # This module contains all sorts of useful methods
- # that allow access to the resources being worked with,
- # metadata about the controller and action,
- # and so forth.
- #
- # Many of these accessors call other accessors
- # and are called by the default make_resourceful actions[link:classes/Resourceful/Default/Actions.html].
- # This means that overriding one method
- # can affect everything else.
- #
- # This can be dangerous, but it can also be very powerful.
- # make_resourceful is designed to take advantage of overriding,
- # so as long as the new methods accomplish the same purpose as the old ones,
- # everything will just work.
- # Even if you make a small mistake,
- # it's hard to break the controller in any unexpected ways.
- #
- # For example, suppose your controller is called TagsController,
- # but your model is called PhotoTag.
- # All you have to do is override current_model_name:
- #
- # def current_model_name
- # "PhotoTag"
- # end
- #
- # Then current_model will return the PhotoTag model,
- # current_object will call <tt>PhotoTag.find</tt>,
- # and so forth.
- #
- # Overriding current_objects and current_object is particularly useful
- # for providing customized model lookup logic.
- module Accessors
- # Returns an array of all the objects of the model corresponding to the controller.
- # For UsersController, it essentially runs <tt>User.find(:all)</tt>.
- #
- # However, there are a few important differences.
- # First, this method caches is results in the <tt>@current_objects</tt> instance variable.
- # That way, multiple calls won't run multiple queries.
- #
- # Second, this method uses the current_model accessor,
- # which provides a lot of flexibility
- # (see the documentation for current_model for details).
- def current_objects
- @current_objects ||= current_model.find(:all)
- end
-
- # Calls current_objects and stores
- # the result in an instance variable
- # named after the controller.
- #
- # This is called automatically by the default make_resourceful actions.
- # You shouldn't need to use it directly unless you're creating a new action.
- #
- # For example, in UsersController,
- # calling +load_objects+ sets <tt>@users = current_objects</tt>.
- def load_objects
- instance_variable_set("@#{instance_variable_name}", current_objects)
- end
-
- # Returns the object referenced by the id parameter
- # (or the newly-created object for the +new+ and +create+ actions).
- # For UsersController, it essentially runs <tt>User.find(params[:id])</tt>.
- #
- # However, there are a few important differences.
- # First, this method caches is results in the <tt>@current_objects</tt> instance variable.
- # That way, multiple calls won't run multiple queries.
- #
- # Second, this method uses the current_model accessor,
- # which provides a lot of flexibility
- # (see the documentation for current_model for details).
- #
- # Note that this is different for a singleton controller,
- # where there's only one resource per parent resource.
- # Then this just returns that resource.
- # For example, if Person has_one Hat,
- # then in HatsController current_object essentially runs <tt>Person.find(params[:person_id]).hat</tt>.
- def current_object
- @current_object ||= if plural?
- current_model.find(params[:id])
- else
- parent_object.send(instance_variable_name)
- end
- end
-
-
- # Calls current_object and stores
- # the result in an instance variable
- # named after the controller.
- #
- # This is called automatically by the default make_resourceful actions.
- # You shouldn't need to use it directly unless you're creating a new action.
- #
- # For example, in UsersController,
- # calling +load_object+ sets <tt>@user = current_object</tt>.
- def load_object
- instance_variable_set("@#{instance_variable_name.singularize}", current_object)
- end
-
- # Creates a new object of the type of the current model
- # with the current object's parameters.
- # +current_object+ then returns this object for this action
- # instead of looking up a new object.
- #
- # This is called automatically by the default make_resourceful actions.
- # You shouldn't need to use it directly unless you're creating a new action.
- #
- # Note that if a parent object exists,
- # the newly created object will automatically be a child of the parent object.
- # For example, on POST /people/4/things,
- #
- # build_object
- # current_object.person.id #=> 4
- #
- def build_object
- @current_object = if current_model.respond_to? :build
- current_model.build(object_parameters)
- else
- current_model.new(object_parameters)
- end
- end
-
- # The string name of the current model.
- # By default, this is derived from the name of the controller.
- def current_model_name
- controller_name.singularize.camelize
- end
-
- # An array of namespaces under which the current controller is.
- # For example, in Admin::Content::PagesController:
- #
- # namespaces #=> [:admin, :content]
- #
- def namespaces
- @namespaces ||= self.class.name.split('::').slice(0...-1).map(&:underscore).map(&:to_sym)
- end
-
- # The name of the instance variable that load_object and load_objects should assign to.
- def instance_variable_name
- controller_name
- end
-
- # The class of the current model.
- # Note that if a parent object exists,
- # this instead returns the association object.
- # For example, in HatsController where Person has_many :hats,
- #
- # current_model #=> Person.find(params[:person_id]).hats
- #
- # This is useful because the association object uses duck typing
- # to act like a model class.
- # It supplies a find method that's automatically scoped
- # to ensure that the object returned is actually a child of the parent,
- # and so forth.
- def current_model
- if !parent? || singular?
- current_model_name.constantize
- else
- parent_object.send(instance_variable_name)
- end
- end
-
- # Returns the hash passed as HTTP parameters
- # that defines the new (or updated) attributes
- # of the current object.
- # This is only meaningful for +create+ or +update+.
- def object_parameters
- params[current_model_name.underscore]
- end
-
- # Returns a list of the names of all the potential parents of the current model.
- # For a non-nested controller, this is <tt>[]</tt>.
- # For example, in HatsController where Rack has_many :hats and Person has_many :hats,
- #
- # parents #=> ["rack", "person"]
- #
- # Note that the parents must be declared via Builder#belongs_to.
- def parent_names
- self.class.read_inheritable_attribute :parents
- end
-
- # Returns true if an appropriate parent id parameter has been supplied.
- # For example, in HatsController where Rack has_many :hats and Person has_many :hats,
- # if <tt>params[:rack_id]</tt> or <tt>params[:person_id]</tt> is given,
- #
- # parent? #=> true
- #
- # Otherwise, if both <tt>params[:rack_id]</tt> and <tt>params[:rack_id]</tt> are nil,
- #
- # parent? #=> false
- #
- # Note that parents must be declared via Builder#belongs_to.
- def parent?
- !!parent_name
- end
-
- # Returns the name of the current parent object if a parent id is given, or nil otherwise.
- # For example, in HatsController where Rack has_many :hats and Person has_many :hats,
- # if <tt>params[:rack_id]</tt> is given,
- #
- # parent_name #=> "rack"
- #
- # If <tt>params[:person_id]</tt> is given,
- #
- # parent_name #=> "person"
- #
- # If both <tt>params[:rack_id]</tt> and <tt>params[:rack_id]</tt> are nil,
- #
- # parent_name #=> nil
- #
- # There are several things to note about this method.
- # First, make_resourceful only supports single-level model nesting.
- # Thus, if neither <tt>params[:rack_id]</tt> nor <tt>params[:rack_id]</tt> are nil,
- # the return value of +parent_name+ is undefined.
- #
- # Second, don't use parent_name to check whether a parent id is given.
- # It's better to use the more semantic parent? method.
- #
- # Third, parent_name caches its return value in the <tt>@parent_name</tt> variable,
- # which you should keep in mind if you're overriding it.
- # However, because <tt>@parent_name == nil</tt> could mean that there is no parent
- # _or_ that the method hasn't been run yet,
- # it uses <tt>defined?(@parent_name)</tt> to do the caching
- # rather than <tt>@parent_name ||=</tt>. See the source code.
- #
- # Finally, note that parents must be declared via Builder#belongs_to.
- def parent_name
- return @parent_name if defined?(@parent_name)
- @parent_name = parent_names.find { |name| params["#{name}_id"] }
- end
-
- # Returns the model class of the current parent.
- # For example, in HatsController where Person has_many :hats,
- # if <tt>params[:person_id]</tt> is given,
- #
- # parent_models #=> Rack
- #
- # Note that parents must be declared via Builder#belongs_to.
- def parent_model
- parent_name.camelize.constantize
- end
-
- # Returns the current parent object for the current object.
- # For example, in HatsController where Person has_many :hats,
- # if <tt>params[:person_id]</tt> is given,
- #
- # parent_object #=> Person.find(params[:person_id])
- #
- # Note that parents must be declared via Builder#belongs_to.
- #
- # Note also that the results of this method are cached
- # so that multiple calls don't result in multiple SQL queries.
- def parent_object
- @parent_object ||= parent_model.find(params["#{parent_name}_id"])
- end
-
- # Assigns the current parent object, as given by parent_objects,
- # to its proper instance variable, as given by parent_name.
- #
- # This is automatically added as a before_filter.
- # You shouldn't need to use it directly unless you're creating a new action.
- def load_parent_object
- instance_variable_set("@#{parent_name}", parent_object) if parent?
- end
-
- # Renders a 422 error if no parent id is given.
- # This is meant to be used with before_filter
- # to ensure that some actions are only called with a parent id.
- # For example:
- #
- # before_filter :ensure_parent_exists, :only => [:create, :update]
- #
- def ensure_parent_exists
- return true if parent?
- render :text => 'No parent id given', :status => 422
- return false
- end
-
- # Returns whether or not the database update in the +create+, +update+, and +destroy+
- # was completed successfully.
- def save_succeeded?
- @save_succeeded
- end
-
- # Declares that the current databse update was completed successfully.
- # Causes subsequent calls to <tt>save_succeeded?</tt> to return +true+.
- #
- # This is mostly meant to be used by the default actions,
- # but it can be used by user-defined actions as well.
- def save_succeeded!
- @save_succeeded = true
- end
-
- # Declares that the current databse update was not completed successfully.
- # Causes subsequent calls to <tt>save_succeeded?</tt> to return +false+.
- #
- # This is mostly meant to be used by the default actions,
- # but it can be used by user-defined actions as well.
- def save_failed!
- @save_succeeded = false
- end
-
- # Returns whether or not the current action acts upon multiple objects.
- # By default, the only such action is +index+.
- def plural_action?
- PLURAL_ACTIONS.include?(params[:action].to_sym)
- end
-
- # Returns whether or not the current action acts upon a single object.
- # By default, this is the case for all actions but +index+.
- def singular_action?
- !plural_action?
- end
-
- # Returns whether the controller is a singleton,
- # implying that there is only one such resource for each parent resource.
- #
- # Note that the way this is determined is based on the singularity of the controller name,
- # so it may yield false positives for oddly-named controllers and need to be overridden.
- def singular?
- instance_variable_name.singularize == instance_variable_name
- end
-
- # Returns whether the controller is a normal plural controller,
- # implying that there are multiple resources for each parent resource.
- #
- # Note that the way this is determined is based on the singularity of the controller name,
- # so it may yield false negatives for oddly-named controllers.
- # If this is the case, the singular? method should be overridden.
- def plural?
- !singular?
- end
- end
- end
-end
View
92 vendor/plugins/make_resourceful/lib/resourceful/default/actions.rb
@@ -1,92 +0,0 @@
-module Resourceful
- module Default
- # Contains the definitions of the default resourceful actions.
- # These are made available with the Builder#actions method.
- #
- # These methods are very compact,
- # so the best way to understand them is just to look at their source.
- # Check out Resourceful::Accessors and Resourceful::Callbacks
- # for the documentation of the methods called within the actions.
- #
- # Along with each action is listed the RESTful method
- # which corresponds to the action.
- # The controller in the examples is FoosController,
- # and the id for single-object actions is 12.
- module Actions
- # GET /foos
- def index
- load_objects
- before :index
- response_for :index
- end
-
- # GET /foos/12
- def show
- load_object
- before :show
- response_for :show
- rescue
- response_for :show_fails
- end
-
- # POST /foos
- def create
- build_object
- load_object
- before :create
- if current_object.save
- save_succeeded!
- after :create
- response_for :create
- else
- save_failed!
- after :create_fails
- response_for :create_fails
- end
- end
-
- # PUT /foos/12
- def update
- load_object
- before :update
- if current_object.update_attributes object_parameters
- save_succeeded!
- after :update
- response_for :update
- else
- save_failed!
- after :update_fails
- response_for :update_fails
- end
- end
-
- # GET /foos/new
- def new
- build_object
- load_object
- before :new
- response_for :new
- end
-
- # GET /foos/12/edit
- def edit
- load_object
- before :edit
- response_for :edit
- end
-
- # DELETE /foos/12
- def destroy
- load_object
- before :destroy
- if current_object.destroy
- after :destroy
- response_for :destroy
- else
- after :destroy_fails
- response_for :destroy_fails
- end
- end
- end
- end
-end
View
50 vendor/plugins/make_resourceful/lib/resourceful/default/callbacks.rb
@@ -1,50 +0,0 @@
-require 'resourceful/builder'
-
-module Resourceful
- module Default
- # This module is mostly meant to be used by the make_resourceful default actions.
- # It provides various methods that declare where callbacks set in the +make_resourceful+ block,
- # like Builder#before and Builder#response_for,
- # should be called.
- module Callbacks
- # Calls any +before+ callbacks set in the +make_resourceful+ block for the given event.
- def before(event)
- resourceful_fire(:before, event.to_sym)
- end
-
- # Calls any +after+ callbacks set in the +make_resourceful+ block for the given event.
- def after(event)
- resourceful_fire(:after, event.to_sym)
- end
-
- # Calls any +response_for+ callbacks set in the +make_resourceful+ block for the given event.
- # Note that these aren't called directly,
- # but instead passed along to Rails' respond_to method.
- def response_for(event)
- if responses = self.class.read_inheritable_attribute(:resourceful_responses)[event.to_sym]
- respond_to do |format|
- responses.each do |key, value|
- format.send(key, &scope(value))
- end
- end
- end
- end
-
- # Returns a block identical to the given block,
- # but in the context of the current controller.
- # The returned block accepts no arguments,
- # even if the given block accepted them.
- def scope(block)
- lambda do
- instance_eval(&(block || lambda {}))
- end
- end
-
- private
-
- def resourceful_fire(type, name)
- scope(self.class.read_inheritable_attribute(:resourceful_callbacks)[type][name]).call
- end
- end
- end
-end
View
118 vendor/plugins/make_resourceful/lib/resourceful/default/responses.rb
@@ -1,118 +0,0 @@
-module Resourceful
- module Default
- module Responses
- # Sets the default flash message.
- # This message can be overridden by passing in
- # an HTTP parameter of the form "_flash[type]" via POST or GET.
- #
- # You can use this to easily have multiple forms
- # post to the same create/edit/destroy actions
- # but display different flash notices -
- # without modifying the controller code at all.
- #
- # By default, the flash types are +notice+ when the database operation completes successfully
- # and +error+ when it fails.
- #
- #--
- # TODO: Move this out of here
- #++
- def set_default_flash(type, message)
- flash[type] ||= (params[:_flash] && params[:_flash][type]) || message
- end
-
- # Sets the default redirect
- # (the argument passed to +redirect_to+).
- # This message can be overridden by passing in
- # an HTTP parameter of the form "_redirect_on[status]" via POST or GET.
- #
- # You can use this to easily have multiple forms
- # post to the same create/edit/destroy actions
- # but redirect to different URLs -
- # without modifying the controller code at all.
- #
- # By default, the redirect statuses are +success+ when the database operation completes successfully
- # and +failure+ when it fails.
- # Use the <tt>:status</tt> option to specify which status to run the redirect for.
- # For example:
- #
- # set_default_redirect "/posts", :status => :failure
- #
- # This will run <tt>redirect_to params[:_redirect_on][:failure]</tt> if the parameter exists,
- # or <tt>redirect_to "/posts"</tt> otherwise.
- #
- #--
- # TODO: Move this out of here
- #++
- def set_default_redirect(to, options = {})
- status = options[:status] || :success
- redirect_to (params[:_redirect_on] && params[:_redirect_on][status]) || to
- end
-
- # This method is automatically run when this module is included in Resourceful::Base.
- # It sets up the default responses for the default actions.
- def self.included(base)
- base.made_resourceful do
- response_for(:show, :index, :edit, :new) do |format|
- format.html
- format.js
- end
-
- response_for(:show_fails) do |format|
- not_found = Proc.new { render :text => "No item found", :status => 404 }
- format.html &not_found
- format.js &not_found
- format.xml &not_found
- end
-
- response_for(:create) do |format|
- format.html do
- set_default_flash(:notice, "Create successful!")
- set_default_redirect object_path
- end
- format.js
- end
-
- response_for(:create_fails) do |format|
- format.html do
- set_default_flash :error, "There was a problem!"
- render :action => :new, :status => 422
- end
- format.js
- end
-
- response_for(:update) do |format|
- format.html do
- set_default_flash :notice, "Save successful!"
- set_default_redirect object_path
- end
- format.js
- end
-
- response_for(:update_fails) do |format|
- format.html do
- set_default_flash :error, "There was a problem saving!"
- render :action => :edit, :status => 422
- end
- format.js
- end
-
- response_for(:destroy) do |format|
- format.html do
- set_default_flash :notice, "Record deleted!"
- set_default_redirect objects_path
- end
- format.js
- end
-
- response_for(:destroy_fails) do |format|
- format.html do
- set_default_flash :error, "There was a problem deleting!"
- set_default_redirect :back, :status => :failure
- end
- format.js
- end
- end
- end
- end
- end
-end
View
137 vendor/plugins/make_resourceful/lib/resourceful/default/urls.rb
@@ -1,137 +0,0 @@
-module Resourceful
- module Default
- # This file contains various methods to make URL helpers less painful.
- # They provide methods analogous to the standard foo_url and foo_path helpers.
- # However, they use make_resourceful's knowledge of the structure of the controller
- # to allow you to avoid figuring out which method to call and which parent objects it should be passed.
- module URLs
- # This returns the path for the given object,
- # by default current_object[link:classes/Resourceful/Default/Accessors.html#M000012].
- # For example, in HatsController the following are equivalent:
- #
- # object_path #=> "/hats/12"
- # hat_path(@hat) #=> "/hats/12"
- #
- def object_path(object = current_object); object_route(object, 'path'); end
- # Same as object_path, but with the protocol and hostname.
- def object_url (object = current_object); object_route(object, 'url'); end
-
- # This is the same as object_path,
- # unless a parent exists.
- # Then it returns the nested path for the object.
- # For example, in HatsController where Person has_many :hats and <tt>params[:person_id] == 42</tt>,
- # the following are equivalent:
- #
- # nested_object_path #=> "/person/42/hats/12"
- # person_hat_path(@person, @hat) #=> "/person/42/hats/12"
- #
- def nested_object_path(object = current_object); nested_object_route(object, 'path'); end
- # Same as nested_object_path, but with the protocol and hostname.
- def nested_object_url (object = current_object); nested_object_route(object, 'url'); end
-
- # This returns the path for the edit action for the given object,
- # by default current_object[link:classes/Resourceful/Default/Accessors.html#M000012].
- # For example, in HatsController the following are equivalent:
- #
- # edit_object_path #=> "/hats/12/edit"
- # edit_person_hat_path(@person, @hat) #=> "/hats/12/edit"
- #
- def edit_object_path(object = current_object); edit_object_route(object, 'path'); end
- # Same as edit_object_path, but with the protocol and hostname.
- def edit_object_url (object = current_object); edit_object_route(object, 'url'); end
-
- # This returns the path for the collection of the current controller.
- # For example, in HatsController where Person has_many :hats and <tt>params[:person_id] == 42</tt>,
- # the following are equivalent:
- #
- # objects_path #=> "/people/42/hats"
- # person_hats_path(@person) #=> "/people/42/hats"
- #
- def objects_path; objects_route('path'); end
- # Same as objects_path, but with the protocol and hostname.
- def objects_url ; objects_route('url'); end
-
- # This returns the path for the new action for the current controller.
- # For example, in HatsController where Person has_many :hats and <tt>params[:person_id] == 42</tt>,
- # the following are equivalent:
- #
- # new_object_path #=> "/people/42/hats/new"
- # new_person_hat_path(@person) #=> "/people/42/hats/new"
- #
- def new_object_path; new_object_route('path'); end
- # Same as new_object_path, but with the protocol and hostname.
- def new_object_url ; new_object_route('url'); end
-
- # This returns the path for the parent object.
- #
- def parent_path(object = parent_object)
- instance_route(parent_name, object, 'path')
- end
- # Same as parent_path, but with the protocol and hostname.
- def parent_url(object = parent_object)
- instance_route(parent_name, object, 'url')
- end
-
- # This prefix is added to the Rails URL helper names
- # before they're called.
- # By default, it's the underscored list of namespaces of the current controller,
- # or nil if there are no namespaces defined.
- # However, it can be overridden if another prefix is needed.
- # Note that if this is overridden,
- # the new method should return a string ending in an underscore.
- #
- # For example, in Admin::Content::PagesController:
- #
- # url_helper_prefix #=> "admin_content_"
- #
- # Then object_path is the same as <tt>admin_content_page_path(current_object)</tt>.
- def url_helper_prefix
- namespaces.empty? ? nil : "#{namespaces.join('_')}_"
- end
-
- # This prefix is added to the Rails URL helper names
- # for the make_resourceful collection URL helpers,
- # objects_path and new_object_path.
- # It's only added if url_helper_prefix returns nil.
- # By default, it's the parent name followed by an underscore if a parent is given,
- # and the empty string otherwise.
- #
- # See also url_helper_prefix.
- def collection_url_prefix
- parent? ? "#{parent_name}_" : ''
- end
-
- private
-
- def object_route(object, type)
- instance_route(current_model_name.underscore, object, type)
- end
-
- def nested_object_route(object, type)
- return object_route(object, type) unless parent?
- send("#{url_helper_prefix}#{parent_name}_#{current_model_name.underscore}_#{type}", parent_object, object)
- end
-
- def edit_object_route(object, type)
- instance_route(current_model_name.underscore, object, type, "edit")
- end
-
- def objects_route(type)
- collection_route(current_model_name.pluralize.underscore, type)
- end
-
- def new_object_route(type)
- collection_route(current_model_name.underscore, type, "new")
- end
-
- def instance_route(name, object, type, action = nil)
- send("#{action ? action + '_' : ''}#{url_helper_prefix}#{name}_#{type}", object)
- end
-
- def collection_route(name, type, action = nil)
- send("#{action ? action + '_' : ''}#{url_helper_prefix || collection_url_prefix}#{name}_#{type}",
- *(parent? ? [parent_object] : []))
- end
- end
- end
-end
View
83 vendor/plugins/make_resourceful/lib/resourceful/maker.rb
@@ -1,83 +0,0 @@
-require 'resourceful/builder'
-require 'resourceful/base'
-
-module Resourceful
- # This module is extended by the ActionController::Base class object.
- # It provides the actual +make_resourceful+ method
- # and sets up the controller so that everything will work.
- module Maker
- # Called automatically on ActionController::Base.
- # Initializes various inheritable attributes.
- def self.extended(base)
- base.write_inheritable_attribute :resourceful_callbacks, {}
- base.write_inheritable_attribute :resourceful_responses, {}
- base.write_inheritable_attribute :parents, []
- base.write_inheritable_attribute :made_resourceful, false
- end
-
- # :call-seq:
- # make_resourceful(options = {}) { ... }
- #
- # This is the central method, and namesake, of make_resourceful.
- # It takes a block and evaluates it in the context of a Builder,
- # allowing the controller to be customized extensively.
- #
- # See Resourceful::Builder for documentation on the methods available
- # in the context of the block.
- #
- # The only option currently available is <tt>:include</tt>.
- # It takes an object that responds to to_proc
- # (or an array of such objects)
- # and evaluates that proc in the same context as the block.
- # For example:
- #
- # make_resourceful :include => proc { actions :all } do
- # before :show do
- # current_object.current_user = current_user
- # end
- # end
- #
- # This is the same as:
- #
- # make_resourceful do
- # actions :all
- # before :show do
- # current_object.current_user = current_user
- # end
- # end
- #
- def make_resourceful(options = {}, &block)
- # :stopdoc:
- include Resourceful::Base
- # :startdoc:
-
- builder = Resourceful::Builder.new(self)
- unless builder.inherited?
- Resourceful::Base.made_resourceful.each { |proc| builder.instance_eval(&proc) }
- end
- Array(options[:include]).each { |proc| builder.instance_eval(&proc) }
- builder.instance_eval(&block)
-
- builder.apply
-
- add_helpers
- end
-
- # Returns whether or not make_resourceful has been called
- # on this controller or any controllers it inherits from.
- def made_resourceful?
- read_inheritable_attribute(:made_resourceful)
- end
-
- private
-
- def add_helpers
- helper_method(:object_path, :objects_path, :new_object_path, :edit_object_path,
- :object_url, :objects_url, :new_object_url, :edit_object_url,
- :parent_path, :parent_url,
- :current_objects, :current_object, :current_model, :current_model_name,
- :namespaces, :instance_variable_name, :parent_names, :parent_name,
- :parent?, :parent_model, :parent_object, :save_succeeded?)
- end
- end
-end
View
33 vendor/plugins/make_resourceful/lib/resourceful/response.rb
@@ -1,33 +0,0 @@
-module Resourceful
- # This is the class of the object passed to the Builder#response_for method.
- # It shouldn't be used by users.
- #
- # The Response collects format procs
- # and returns them with the format method,
- # in the order they were given.
- # For example:
- #
- # response.html { redirect_to '/' }
- # response.xml { render :xml => current_object.to_xml }
- # response.js
- # response.formats #=> [[:html, #<Proc>], [:xml, #<Proc>], [:js, #<Proc>]]
- #
- # Note that the <tt>:js</tt> response is the empty proc -
- # the same as <tt>proc {}</tt>.
- class Response # :nodoc:
- # Returns a list of pairs of formats and procs
- # representing the formats passed to the response object.
- # See class description.
- attr :formats
-
- # Returns a new Response with no format data.
- def initialize
- @formats = []
- end
-
- # Used to dispatch the individual format methods.
- def method_missing(name, &block)
- @formats.push([name, block || proc {}]) unless @formats.any? {|n,b| n == name}
- end
- end
-end
View
181 vendor/plugins/make_resourceful/lib/resourceful/serialize.rb
@@ -1,181 +0,0 @@
-require 'resourceful/builder'
-
-module Resourceful
- # This module contains mixin modules
- # used to implement the object serialization
- # used for the Builder#publish method.
- # They can also be used to get serialized representations of objects
- # in other contexts.
- #
- # Serialization makes use of duck typing.
- # Each class that can be serialized
- # (just Array and ActiveRecord::Base by default)
- # implements the +serialize+ and +to_serializable+ methods.
- # These methods are implemented differently by the different classes,
- # but the semantics of the implementations are consistent,
- # so they can be used consistently.
- #
- # +to_serializable+ returns an object that can be directly serialized
- # with a call to +to_xml+, +to_yaml+, or +to_json+.
- # This object is either a hash or an array,
- # and all the elements are either values, like strings and integers,
- # or other serializable objects.
- # This is useful for getting a model into a simple data structure format.
- # The +attributes+ argument uses the same semantics
- # as the <tt>:attributes</tt> option for Builder#publish.
- # For example:
- #
- # c = Cake.new(:flavor => 'chocolate', :text => 'Happy birthday, Chris!')
- # c.recipient = User.new(:name => 'Chris', :password => 'not very secure')
- # c.to_serializable [
- # :flavor, :text,
- # :recipient => :name
- # ]
- #
- # This would return the Ruby hash
- #
- # { :flavor => 'chocolate', :text => 'Happy birthday, Chris!',
- # :user => {:name => 'Chris'} }
- #
- # +serialize+ takes a format (<tt>:xml</tt>, <tt>:yaml</tt>, or <tt>:json</tt> - see New Formats below)
- # and a hash of options.
- # The only option currently recognized is <tt>:attributes</tt>,
- # which has the same semantics
- # as the <tt>:attributes</tt> option for Builder#publish.
- # +serialize+ returns a string containing the target
- # serialized in the given format.
- # For example:
- #
- # c = CandyBag.new(:title => 'jellybag')
- # c.candies << Candy.new(:type => 'jellybean', :flavor => 'root beer')
- # c.candies << Candy.new(:type => 'jellybean', :flavor => 'pear')
- # c.candies << Candy.new(:type => 'licorice', :flavor => 'anisey')
- # c.serialize :xml, :attributes => [:title, {:candies => [:type, :flavor]}]
- #
- # This would return a Ruby string containing
- #
- # <?xml version="1.0" encoding="UTF-8"?>
- # <candy-bag>
- # <title>jellybag</title>
- # <candies>
- # <candy>
- # <type>jellybean</type>
- # <flavor>root beer</flavor>
- # </candy>
- # <candy>
- # <type>jellybean</type>
- # <flavor>pear</flavor>
- # </candy>
- # <candy>
- # <type>licorice</type>
- # <flavor>anisey</flavor>
- # </candy>
- # </candies>
- # </candy-bag>
- #
- module Serialize
-
- # Takes an attributes option in the form passed to Builder#publish
- # and returns a hash (or nil, if attributes is nil)
- # containing the same data,
- # but in a more consistent format.
- # All keys are converted to symbols,
- # and all lists are converted to hashes.
- # For example:
- #
- # Resourceful::Serialize.normalize_attributes([:foo, :bar, {"baz" => ["boom"]}])
- # #=> {"baz"=>["boom"], :foo=>nil, :bar=>nil}
- #
- def self.normalize_attributes(attributes) # :nodoc:
- return nil if attributes.nil?
- return {attributes.to_sym => nil} if String === attributes
- return {attributes => nil} if !attributes.respond_to?(:inject)
-
- attributes.inject({}) do |hash, attr|
- if Array === attr
- hash[attr[0]] = attr[1]
- hash
- else
- hash.merge normalize_attributes(attr)
- end
- end
- end
-
- # This module contains the definitions of +serialize+ and +to_serializable+
- # that are included in ActiveRecord::Base.
- module Model
- # :call-seq:
- # serialize format, options = {}, :attributes => [ ... ]
- #
- # See the module documentation for Serialize for details.
- def serialize(format, options)
- raise "Must specify :attributes option" unless options[:attributes]
- hash = self.to_serializable(options[:attributes])
- root = self.class.to_s.underscore
- if format == :xml
- hash.send("to_#{format}", :root => root)
- else
- {root => hash}.send("to_#{format}")
- end
- end
-
- # See the module documentation for Serialize for details.
- def to_serializable(attributes)
- raise "Must specify attributes for #{self.inspect}.to_serializable" if attributes.nil?
-
- Serialize.normalize_attributes(attributes).inject({}) do |hash, (key, value)|
- hash[key.to_s] = attr_hash_value(self.send(key), value)
- hash
- end
- end
-
- private
-
- # Given an attribute value
- # and a normalized (see above) attribute hash,
- # returns the serializable form of that attribute.
- def attr_hash_value(attr, sub_attributes)
- if attr.respond_to?(:to_serializable)
- attr.to_serializable(sub_attributes)
- else
- attr
- end
- end
- end
-
- # This module contains the definitions of +serialize+ and +to_serializable+
- # that are included in ActiveRecord::Base.
- module Array
- # :call-seq:
- # serialize format, options = {}, :attributes => [ ... ]
- #
- # See the module documentation for Serialize for details.
- def serialize(format, options)
- raise "Not all elements respond to to_serializable" unless all? { |e| e.respond_to? :to_serializable }
- raise "Must specify :attributes option" unless options[:attributes]
-
- serialized = map { |e| e.to_serializable(options[:attributes]) }
- root = first.class.to_s.pluralize.underscore
-
- if format == :xml
- serialized.send("to_#{format}", :root => root)
- else
- {root => serialized}.send("to_#{format}")
- end
- end
-
- # See the module documentation for Serialize for details.
- def to_serializable(attributes)
- if first.respond_to?(:to_serializable)
- attributes = Serialize.normalize_attributes(attributes)
- map { |e| e.to_serializable(attributes) }
- else
- self
- end
- end
- end
- end
-end
-
-class ActiveRecord::Base; include Resourceful::Serialize::Model; end
-class Array; include Resourceful::Serialize::Array; end
View
444 vendor/plugins/make_resourceful/spec/accessors_spec.rb
@@ -1,444 +0,0 @@
-require File.dirname(__FILE__) + '/spec_helper'
-
-describe Resourceful::Default::Accessors, "#current_objects" do
- include ControllerMocks
- before :each do
- mock_controller Resourceful::Default::Accessors
- @objects = stub_list 5, 'object'
- @model = stub
- @controller.stubs(:current_model).returns(@model)
- end
-
- it "should look up all objects in the current model" do
- @model.expects(:find).with(:all).returns(@objects)
- @controller.current_objects.should == @objects
- end
-
- it "should cache the result, so subsequent calls won't run multiple queries" do
- @model.expects(:find).once.returns(@objects)
- @controller.current_objects
- @controller.current_objects
- end
-
- it "shouldn't run a query if @current_objects is set" do
- @controller.instance_variable_set('@current_objects', @objects)
- @model.expects(:find).never
- @controller.current_objects.should == @objects
- end
-end
-
-describe Resourceful::Default::Accessors, "#load_objects" do
- include ControllerMocks
- before :each do
- mock_controller Resourceful::Default::Accessors
- @objects = stub_list 5, 'object'
- @controller.stubs(:current_objects).returns(@objects)
- @controller.stubs(:instance_variable_name).returns("posts")
- end
-
- it "should set the current instance variable to the object collection" do
- @controller.load_objects
- @controller.instance_variable_get('@posts').should == @objects
- end
-end
-
-describe Resourceful::Default::Accessors, "#current_object on a plural controller" do
- include ControllerMocks
- before :each do
- mock_controller Resourceful::Default::Accessors
- @controller.stubs(:plural?).returns(true)
- @controller.stubs(:params).returns(:id => "12")
-
- @object = stub
- @model = stub
- @controller.stubs(:current_model).returns(@model)
- end
-
- it "should look up the object specified by the :id parameter in the current model" do
- @model.expects(:find).with('12').returns(@object)
- @controller.current_object.should == @object
- end
-
- it "should cache the result, so subsequent calls won't run multiple queries" do
- @model.expects(:find).once.returns(@object)
- @controller.current_object
- @controller.current_object
- end
-
- it "shouldn't run a query if @current_object is set" do
- @controller.instance_variable_set('@current_object', @object)
- @model.expects(:find).never
- @controller.current_object.should == @object
- end
-end
-
-describe Resourceful::Default::Accessors, "#current_object on a singular controller" do
- include ControllerMocks
- before :each do
- mock_controller Resourceful::Default::Accessors
- @controller.stubs(:plural?).returns(false)
- @controller.stubs(:instance_variable_name).returns("post")
-
- @parent = stub('parent')
- @controller.stubs(:parent_object).returns(@parent)
- @controller.stubs(:parent?).returns(true)
-
- @object = stub
- end
-
- it "should look up the instance object of the parent object" do
- @parent.expects(:post).returns(@object)
- @controller.current_object.should == @object
- end
-end
-
-describe Resourceful::Default::Accessors, "#load_object" do
- include ControllerMocks
- before :each do
- mock_controller Resourceful::Default::Accessors
- @object = stub
- @controller.stubs(:current_object).returns(@object)
- @controller.stubs(:instance_variable_name).returns("posts")
- end
-
- it "should set the current singular instance variable to the current object" do
- @controller.load_object
- @controller.instance_variable_get('@post').should == @object
- end
-end
-
-describe Resourceful::Default::Accessors, "#build_object with a #build-able model" do
- include ControllerMocks
- before :each do
- mock_controller Resourceful::Default::Accessors
- @params = {:name => "Bob", :password => "hideously insecure"}
- @controller.stubs(:object_parameters).returns(@params)
-
- @object = stub
- @model = stub
- @controller.stubs(:current_model).returns(@model)
-
- @model.stubs(:build).returns(@object)
- end
-
- it "should return a new object built with current_model from the object parameters" do
- @model.expects(:build).with(@params).returns(@object)
- @controller.build_object.should == @object
- end
-
- it "should make current_object return the newly built object" do
- @controller.build_object
- @controller.current_object.should == @object
- end
-end
-
-describe Resourceful::Default::Accessors, "#build_object with a non-#build-able model" do
- include ControllerMocks
- before :each do
- mock_controller Resourceful::Default::Accessors
- @params = {:name => "Bob", :password => "hideously insecure"}
- @controller.stubs(:object_parameters).returns(@params)
-
- @object = stub
- @model = stub
- @controller.stubs(:current_model).returns(@model)
-
- @model.stubs(:new).returns(@object)
- end
-
- it "should return a new instance of the current_model built with the object parameters" do
- @model.expects(:new).with(@params).returns(@object)
- @controller.build_object.should == @object