Skip to content
Browse files

Find can be scoped to an owner

  • Loading branch information...
1 parent 9292f60 commit 0c044d0b1ab348151f380a127261fc5be0570cc6 @mattyoho committed Oct 18, 2011
View
62 features/restful_rails_scopes_find_to_owner.feature
@@ -0,0 +1,62 @@
+Feature: Restful Rails Scopes Find To Owner
+
+ Background:
+ Given a file named "app/controllers/authenticating_controller.rb" with:
+ """
+ class User
+ attr_accessor :id
+ end
+
+ class AuthenticatingController < ApplicationController
+ def current_user
+ User.new.tap {|u| u.id = 42}
+ end
+ end
+ """
+ And a file named "app/controllers/widgets_controller.rb" with:
+ """
+ class WidgetsController < AuthenticatingController
+ default_assumption :restful_rails
+ assume :widget, :owner => :current_user, :raise_error => true
+
+ rescue_from ActiveRecord::RecordNotFound do
+ render :text => "Not Found", :status => 404
+ end
+ end
+ """
+ And a file named "app/views/widgets/show.html.erb" with:
+ """
+ <span><%= widget.name %></span>
+ """
+
+ Scenario: Scoping with a symbol success
+ Given a file named "features/widget_is_viewed_by_owner.feature" with:
+ """
+ Feature: Find A Record Owned By You
+ Scenario: success
+ Given I own a widget named "sprocket"
+ When I view the widget
+ Then I should see "sprocket"
+ """
+ When I run `cucumber features/widget_is_viewed_by_owner.feature` with a clean Bundler environment
+ Then the output should contain:
+ """
+ 1 scenario (1 passed)
+ 3 steps
+ """
+
+ Scenario: Scoping with a symbol failure
+ Given a file named "features/widget_is_viewed_by_owner.feature" with:
+ """
+ Feature: Don't Find Record Not Belonging To You
+ Scenario: success
+ Given a widget named "spacely"
+ When I view the widget
+ Then I should see "Not Found"
+ """
+ When I run `cucumber features/widget_is_viewed_by_owner.feature` with a clean Bundler environment
+ Then the output should contain:
+ """
+ 1 scenario (1 passed)
+ 3 steps
+ """
View
1 lib/basic_assumption/default_assumption/rails.rb
@@ -61,6 +61,7 @@ def initialize(name=nil, context={}, request=nil) #:nodoc:
def block
klass = self.class
Proc.new do |name, context|
+ context[:controller] = self
klass.new(name, context, request).result
end
end
View
13 lib/basic_assumption/default_assumption/restful_rails.rb
@@ -57,7 +57,18 @@ def result #:nodoc:
elsif make?
model_class.new(resource_attributes)
elsif lookup?
- model_class.find(lookup_id)
+ begin
+ if owner_method = context[:owner]
+ owner = context[:controller].send(owner_method)
+ conditions = {owner.class.name.downcase + '_id' => owner.id}
+ model_class.where(conditions).find(lookup_id)
+ else
+ model_class.find(lookup_id)
+ end
+ rescue
+ raise if settings[:raise_error]
+ nil
+ end
end
end
View
2 templates/generate_custom.rb
@@ -1,4 +1,4 @@
-generate('resource widget name:string')
+generate('resource widget name:string user_id:integer')
generate('resource derived_widget')
run('rake db:reset')
View
4 templates/rails/features/step_definitions/custom_steps.rb
@@ -2,6 +2,10 @@
@widget = Widget.create!(:name => widget_name)
end
+Given /^I own a widget named "(.+)"$/ do |widget_name|
+ @widget = Widget.create!(:name => widget_name, :user_id => 42)
+end
+
Given /^the following widgets:$/ do |table|
table.hashes.each do |widget_hash|
Widget.create!(widget_hash)

0 comments on commit 0c044d0

Please sign in to comment.
Something went wrong with that request. Please try again.