renaming ActiveRecordAdditions#can method to accessible_by since it flows better and makes more sense
This commit is contained in:
parent
ef5900c5b1
commit
240c281061
|
@ -2,7 +2,7 @@
|
|||
|
||||
* Adding caching to current_ability controller method, if you're overriding this be sure to add caching too.
|
||||
|
||||
* Adding "can" method to Active Record for fetching records matching a specific ability
|
||||
* Adding "accessible_by" method to Active Record for fetching records matching a specific ability
|
||||
|
||||
* Adding conditions behavior to Ability#can and fetch with Ability#conditions - see issue #53
|
||||
|
||||
|
|
|
@ -180,13 +180,13 @@ Here the @book instance variable is already set so it will not be loaded again f
|
|||
|
||||
== Fetching Records
|
||||
|
||||
Sometimes you need to restrict which records are returned from the database based on what the user is able to access. This can be done with the +can+ method on any Active Record model. Simply pass it the current ability and an action.
|
||||
Sometimes you need to restrict which records are returned from the database based on what the user is able to access. This can be done with the +accessible_by+ method on any Active Record model. Simply pass it the current ability and an action.
|
||||
|
||||
@articles = Article.can(current_ability, :read)
|
||||
@articles = Article.accessible_by(current_ability, :read)
|
||||
|
||||
The action defaults to :read so that can optionally be left out.
|
||||
|
||||
@articles = Article.can(current_ability)
|
||||
@articles = Article.accessible_by(current_ability)
|
||||
|
||||
Here only the records which the user can read will be returned. This is an Active Record scope so other scopes and pagination can be chained onto it.
|
||||
|
||||
|
|
|
@ -191,7 +191,7 @@ module CanCan
|
|||
# can :read, Article, :visible => true
|
||||
# conditions :read, Article # returns { :visible => true }
|
||||
#
|
||||
# Normally you will not call this method directly, but instead go through ActiveRecordAdditions#can method.
|
||||
# Normally you will not call this method directly, but instead go through ActiveRecordAdditions#accessible_by method.
|
||||
#
|
||||
# If the ability is not defined then false is returned so be sure to take that into consideration.
|
||||
# If the ability is defined using a block then this will raise an exception since a hash of conditions cannot be
|
||||
|
|
|
@ -6,7 +6,7 @@ module CanCan
|
|||
# can perform a given action on. The action defaults to :read. This
|
||||
# is usually called from a controller and passed the +current_ability+.
|
||||
#
|
||||
# @articles = Article.can(current_ability)
|
||||
# @articles = Article.accessible_by(current_ability)
|
||||
#
|
||||
# Here only the articles which the user is able to read will be returned.
|
||||
# If the user does not have permission to read any articles then an empty
|
||||
|
@ -15,11 +15,11 @@ module CanCan
|
|||
#
|
||||
# An alternative action can optionally be passed as a second argument.
|
||||
#
|
||||
# @articles = Article.can(current_ability, :update)
|
||||
# @articles = Article.accessible_by(current_ability, :update)
|
||||
#
|
||||
# Here only the articles which the user can update are returned. This
|
||||
# internally uses Ability#conditions method, see that for more information.
|
||||
def can(ability, action = :read)
|
||||
def accessible_by(ability, action = :read)
|
||||
conditions = ability.conditions(action, self) || {:id => nil}
|
||||
if respond_to? :where
|
||||
where(conditions)
|
||||
|
|
|
@ -11,18 +11,18 @@ describe CanCan::ActiveRecordAdditions do
|
|||
|
||||
it "should call where(:id => nil) when no ability is defined so no records are found" do
|
||||
stub(@model_class).where(:id => nil) { :no_where }
|
||||
@model_class.can(@ability, :read).should == :no_where
|
||||
@model_class.accessible_by(@ability, :read).should == :no_where
|
||||
end
|
||||
|
||||
it "should call where with matching ability conditions" do
|
||||
@ability.can :read, @model_class, :foo => 1
|
||||
stub(@model_class).where(:foo => 1) { :found_records }
|
||||
@model_class.can(@ability, :read).should == :found_records
|
||||
@model_class.accessible_by(@ability, :read).should == :found_records
|
||||
end
|
||||
|
||||
it "should default to :read ability and use scoped when where isn't available" do
|
||||
@ability.can :read, @model_class, :foo => 1
|
||||
stub(@model_class).scoped(:conditions => {:foo => 1}) { :found_records }
|
||||
@model_class.can(@ability).should == :found_records
|
||||
@model_class.accessible_by(@ability).should == :found_records
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user