Permalink
Browse files

update_all in nested controllers

  • Loading branch information...
1 parent 4350ad1 commit 18d7c89d83eb1d54f03c2a26d9bb681e160d14ad Andrew Carpenter committed May 27, 2009
@@ -14,6 +14,8 @@ def self.included(subclass)
end
def update_all
+ instance_variable_set "@#{parent_type}", parent_object if parent?
+
before :update_all
objects_params = params["#{object_name.pluralize}"]
@@ -23,7 +25,7 @@ def update_all
@collection = []
objects_params.each do |attributes|
id = attributes.delete("id")
- object = model.find(id)
+ object = end_of_association_chain.find(id)
@collection.push object
object.update_attributes!(attributes)
end
@@ -1,49 +1,60 @@
require File.dirname(__FILE__) + '/../test_helper'
ActionController::Routing::Routes.draw do |map|
- map.resources :projects, :collection => {:update_all => :put}, :controller => 'update_all_action_extensions_test/projects'
+ map.resources :projects, :controller => 'update_all_action_extensions_test/projects' do |project|
+ project.resources :tasks, :controller => 'update_all_action_extensions_test/tasks', :collection => {:update_all => :put}
+ end
end
class UpdateAllActionExtensionsTest < ActionController::TestCase
class ProjectsController < ActionController::Base
resource_controller
add_extensions :update_all
+ end
+
+ class TasksController < ActionController::Base
+ resource_controller
+ add_extensions :update_all
+ belongs_to :project
- def update_all_action_extensions_test_projects_url
+ def update_all_action_extensions_test_project_tasks_url(*args)
projects_url
end
end
- tests ProjectsController
+
+
+ tests TasksController
context "A controller with the :update_all extension added" do
should "have a update_all action" do
- assert ProjectsController.new.respond_to?(:update_all)
+ assert TasksController.new.respond_to?(:update_all)
end
context "successful update_all action" do
setup do
- Project.create(:name => 'First', :position => 1)
- Project.create(:name => 'Second', :position => 2)
-
- put :update_all, :projects => [{:id => 1, :position => 2}, {:id => 2, :position => 1}]
+ @project = Project.create!(:name => 'First')
+ @task_1 = @project.tasks.create!(:name => 'A', :complete => false)
+ @task_2 = @project.tasks.create!(:name => 'B', :complete => false)
+ put :update_all, :project_id => @project.id, :tasks => [{:id => @task_1.id, :complete => true}, {:id => @task_2.id, :complete => true}]
end
should "update the records as appropriate" do
- assert(2,Project.find(1).position)
- assert(1,Project.find(2).position)
+ assert(true,Task.find(@task_1).complete)
+ assert(true,Task.find(@task_2).complete)
end
should_redirect_to "the collection url" do
projects_url
end
should_set_the_flash_to "All successfully updated!"
+ should_assign_to :project
should_assign_to :collection
- should_assign_to :projects
+ should_assign_to :tasks
end
context "failed update_all action" do
end
-
end
+
end
View
@@ -4,12 +4,11 @@ class CreateSchema < ActiveRecord::Migration
def self.up
create_table :projects, :force => true do |t|
t.string :name
- t.integer :position
end
create_table :tasks, :force => true do |t|
t.string :name
+ t.integer :position
t.integer :project_id
- t.integer :person_id
t.boolean :complete
end
create_table :category_projects, :force => true do |t|
@@ -33,7 +32,6 @@ class Project < ActiveRecord::Base
class Task < ActiveRecord::Base
belongs_to :project
- belongs_to :person
end
class CategoryProject < ActiveRecord::Base

0 comments on commit 18d7c89

Please sign in to comment.