diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 3f5cc49..f4180b8 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -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 diff --git a/README.rdoc b/README.rdoc index 1ef2236..b993464 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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. diff --git a/lib/cancan/ability.rb b/lib/cancan/ability.rb index 938c50b..5ba5d59 100644 --- a/lib/cancan/ability.rb +++ b/lib/cancan/ability.rb @@ -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 diff --git a/lib/cancan/active_record_additions.rb b/lib/cancan/active_record_additions.rb index 7b4bb0d..2a688e2 100644 --- a/lib/cancan/active_record_additions.rb +++ b/lib/cancan/active_record_additions.rb @@ -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) diff --git a/spec/cancan/active_record_additions_spec.rb b/spec/cancan/active_record_additions_spec.rb index 317a53b..8e995df 100644 --- a/spec/cancan/active_record_additions_spec.rb +++ b/spec/cancan/active_record_additions_spec.rb @@ -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