removing extra white space at end of lines
This commit is contained in:
parent
c5737f6d28
commit
25637bb33a
|
@ -24,7 +24,7 @@ First, define a class called +Ability+ in "models/ability.rb". It should look so
|
|||
|
||||
class Ability
|
||||
include CanCan::Ability
|
||||
|
||||
|
||||
def initialize(user)
|
||||
if user.admin?
|
||||
can :manage, :all
|
||||
|
@ -55,7 +55,7 @@ Setting this for every action can be tedious, therefore the +load_and_authorize_
|
|||
|
||||
class ArticlesController < ApplicationController
|
||||
load_and_authorize_resource
|
||||
|
||||
|
||||
def show
|
||||
# @article is already loaded and authorized
|
||||
end
|
||||
|
|
24
Rakefile
24
Rakefile
|
@ -1,13 +1,13 @@
|
|||
require 'rubygems'
|
||||
require 'rake'
|
||||
require 'spec/rake/spectask'
|
||||
|
||||
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
|
||||
|
||||
require 'rubygems'
|
||||
require 'rake'
|
||||
require 'spec/rake/spectask'
|
||||
|
||||
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
|
||||
|
||||
task :default => :spec
|
|
@ -6,10 +6,10 @@ Gem::Specification.new do |s|
|
|||
s.homepage = "http://github.com/ryanb/cancan"
|
||||
s.summary = "Simple authorization solution for Rails."
|
||||
s.description = "Simple authorization solution for Rails which is completely decoupled from the user's roles. All permissions are stored in a single location for convenience."
|
||||
|
||||
|
||||
s.files = Dir["{lib,spec}/**/*", "[A-Z]*", "init.rb"]
|
||||
s.require_path = "lib"
|
||||
|
||||
|
||||
s.rubyforge_project = s.name
|
||||
s.required_rubygems_version = ">= 1.3.4"
|
||||
end
|
||||
|
|
|
@ -188,9 +188,9 @@ module CanCan
|
|||
def query(action, subject)
|
||||
Query.new(subject, relevant_can_definitions_for_query(action, subject))
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
|
||||
# Accepts a hash of aliased actions and returns an array of actions which match.
|
||||
# This should be called before "matches?" and other checking methods since they
|
||||
# rely on the actions to be expanded.
|
||||
|
|
|
@ -10,7 +10,7 @@ module CanCan
|
|||
@parent = parent
|
||||
@options = options
|
||||
end
|
||||
|
||||
|
||||
# Returns the class used for this resource. This can be overriden by the :resource option.
|
||||
# Sometimes one will use a symbol as the resource if a class does not exist for it. In that
|
||||
# case "find" and "build" should not be called on it.
|
||||
|
@ -24,27 +24,27 @@ module CanCan
|
|||
resource_class # could be a symbol
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def find(id)
|
||||
self.model_instance ||= base.find(id)
|
||||
end
|
||||
|
||||
|
||||
# Build a new instance of this resource. If it is a class we just call "new" otherwise
|
||||
# it's an associaiton and "build" is used.
|
||||
def build(attributes)
|
||||
self.model_instance ||= (base.kind_of?(Class) ? base.new(attributes) : base.build(attributes))
|
||||
end
|
||||
|
||||
|
||||
def model_instance
|
||||
@controller.instance_variable_get("@#{@name}")
|
||||
end
|
||||
|
||||
|
||||
def model_instance=(instance)
|
||||
@controller.instance_variable_set("@#{@name}", instance)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
|
||||
# The object that methods (such as "find", "new" or "build") are called on.
|
||||
# If there is a parent it will be the association, otherwise it will be the model's class.
|
||||
def base
|
||||
|
|
|
@ -7,16 +7,16 @@ module CanCan
|
|||
@sanitizer = sanitizer
|
||||
@can_definitions = can_definitions
|
||||
end
|
||||
|
||||
|
||||
# Returns a string of SQL conditions which match the ability query.
|
||||
#
|
||||
#
|
||||
# can :manage, User, :id => 1
|
||||
# can :manage, User, :manager_id => 1
|
||||
# cannot :manage, User, :self_managed => true
|
||||
# query(:manage, User).conditions # => "not (self_managed = 't') AND ((manager_id = 1) OR (id = 1))"
|
||||
#
|
||||
# Normally you will not call this method directly, but instead go through ActiveRecordAdditions#accessible_by.
|
||||
#
|
||||
#
|
||||
# If there is just one :can ability, it conditions returned untouched.
|
||||
def conditions
|
||||
if @can_definitions.size == 1 && @can_definitions.first.base_behavior
|
||||
|
@ -28,7 +28,7 @@ module CanCan
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Returns the associations used in conditions for the :joins option of a search
|
||||
# See ActiveRecordAdditions#accessible_by for use in Active Record.
|
||||
def joins
|
||||
|
@ -38,9 +38,9 @@ module CanCan
|
|||
end
|
||||
clean_joins(joins_hash) unless joins_hash.empty?
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
|
||||
def merge_conditions(sql, conditions_hash, behavior)
|
||||
if conditions_hash.blank?
|
||||
behavior ? true_sql : false_sql
|
||||
|
@ -68,7 +68,7 @@ module CanCan
|
|||
def sanitize_sql(conditions)
|
||||
@sanitizer.sanitize_sql(conditions)
|
||||
end
|
||||
|
||||
|
||||
def merge_joins(base, add)
|
||||
add.each do |name, nested|
|
||||
if base[name].is_a?(Hash) && !nested.empty?
|
||||
|
@ -78,7 +78,7 @@ module CanCan
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def clean_joins(joins_hash)
|
||||
joins = []
|
||||
joins_hash.each do |name, nested|
|
||||
|
|
|
@ -15,7 +15,7 @@ describe CanCan::Ability do
|
|||
it "should not have permission to do something it doesn't know about" do
|
||||
@ability.can?(:foodfight, String).should be_false
|
||||
end
|
||||
|
||||
|
||||
it "should pass true to `can?` when non false/nil is returned in block" do
|
||||
@ability.can :read, :all
|
||||
@ability.can :read, Symbol do |sym|
|
||||
|
@ -23,7 +23,7 @@ describe CanCan::Ability do
|
|||
end
|
||||
@ability.can?(:read, :some_symbol).should == true
|
||||
end
|
||||
|
||||
|
||||
it "should pass to previous can definition, if block returns false or nil" do
|
||||
@ability.can :read, Symbol
|
||||
@ability.can :read, Integer do |i|
|
||||
|
@ -151,7 +151,7 @@ describe CanCan::Ability do
|
|||
@ability.can?(:read, 3).should be_true
|
||||
@ability.can?(:read, 123).should be_false
|
||||
end
|
||||
|
||||
|
||||
it "should pass to previous can definition, if block returns false or nil" do
|
||||
#same as previous
|
||||
@ability.can :read, :all
|
||||
|
@ -162,9 +162,9 @@ describe CanCan::Ability do
|
|||
@ability.can?(:read, 3).should be_true
|
||||
@ability.can?(:read, 8).should be_false
|
||||
@ability.can?(:read, 123).should be_true
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
it "should always return `false` for single cannot definition" do
|
||||
@ability.cannot :read, Integer do |int|
|
||||
int > 10 ? nil : ( int > 5 )
|
||||
|
@ -174,7 +174,7 @@ describe CanCan::Ability do
|
|||
@ability.can?(:read, 8).should be_false
|
||||
@ability.can?(:read, 123).should be_false
|
||||
end
|
||||
|
||||
|
||||
it "should pass to previous cannot definition, if block returns false or nil" do
|
||||
@ability.cannot :read, :all
|
||||
@ability.can :read, Integer do |int|
|
||||
|
@ -238,7 +238,7 @@ describe CanCan::Ability do
|
|||
@ability.can?(:read, [[1, 2, 3]]).should be_true
|
||||
@ability.can?(:read, [[4, 5, 6]]).should be_false
|
||||
end
|
||||
|
||||
|
||||
it "should has eated cheezburger" do
|
||||
lambda {
|
||||
@ability.can? :has, :cheezburger
|
||||
|
|
|
@ -25,11 +25,11 @@ describe CanCan::ActiveRecordAdditions do
|
|||
stub(@model_class).scoped(:conditions => {:foos => {:bar => 1}}, :joins => [:foo]) { :found_records }
|
||||
@model_class.accessible_by(@ability).should == :found_records
|
||||
end
|
||||
|
||||
|
||||
it "should merge association joins and sanitize conditions" do
|
||||
@ability.can :read, @model_class, :foo => {:bar => 1}
|
||||
@ability.can :read, @model_class, :too => {:car => 1, :far => {:bar => 1}}
|
||||
|
||||
|
||||
condition_variants = [
|
||||
'(toos.far.bar=1 AND toos.car=1) OR (foos.bar=1)', # faked sql sanitizer is stupid ;-)
|
||||
'(toos.car=1 AND toos.far.bar=1) OR (foos.bar=1)'
|
||||
|
@ -38,7 +38,7 @@ describe CanCan::ActiveRecordAdditions do
|
|||
[:foo, {:too => [:far]}],
|
||||
[{:too => [:far]}, :foo]
|
||||
]
|
||||
|
||||
|
||||
condition_variants.each do |condition|
|
||||
joins_variants.each do |joins|
|
||||
stub(@model_class).scoped( :conditions => condition, :joins => joins ) { :found_records }
|
||||
|
|
|
@ -5,68 +5,68 @@ describe CanCan::Query do
|
|||
@ability = Object.new
|
||||
@ability.extend(CanCan::Ability)
|
||||
end
|
||||
|
||||
|
||||
it "should have false conditions if no abilities match" do
|
||||
@ability.query(:destroy, Person).conditions.should == "true=false"
|
||||
end
|
||||
|
||||
|
||||
it "should return hash for single `can` definition" do
|
||||
@ability.can :read, Person, :blocked => false, :user_id => 1
|
||||
@ability.query(:read, Person).conditions.should == { :blocked => false, :user_id => 1 }
|
||||
end
|
||||
|
||||
|
||||
it "should merge multiple can definitions into single SQL string joining with OR" do
|
||||
@ability.can :read, Person, :blocked => false
|
||||
@ability.can :read, Person, :admin => true
|
||||
@ability.query(:read, Person).conditions.should == "(admin=true) OR (blocked=false)"
|
||||
end
|
||||
|
||||
|
||||
it "should merge multiple can definitions into single SQL string joining with OR and AND" do
|
||||
@ability.can :read, Person, :blocked => false, :active => true
|
||||
@ability.can :read, Person, :admin => true
|
||||
@ability.query(:read, Person).conditions.should orderlessly_match("(blocked=false AND active=true) OR (admin=true)")
|
||||
end
|
||||
|
||||
|
||||
it "should merge multiple can definitions into single SQL string joining with OR and AND" do
|
||||
@ability.can :read, Person, :blocked => false, :active => true
|
||||
@ability.can :read, Person, :admin => true
|
||||
@ability.query(:read, Person).conditions.should orderlessly_match("(blocked=false AND active=true) OR (admin=true)")
|
||||
end
|
||||
|
||||
|
||||
it "should return false conditions for cannot clause" do
|
||||
@ability.cannot :read, Person
|
||||
@ability.query(:read, Person).conditions.should == "true=false"
|
||||
end
|
||||
|
||||
|
||||
it "should return SQL for single `can` definition in front of default `cannot` condition" do
|
||||
@ability.cannot :read, Person
|
||||
@ability.can :read, Person, :blocked => false, :user_id => 1
|
||||
@ability.query(:read, Person).conditions.should orderlessly_match("blocked=false AND user_id=1")
|
||||
end
|
||||
|
||||
|
||||
it "should return true condition for single `can` definition in front of default `can` condition" do
|
||||
@ability.can :read, Person
|
||||
@ability.can :read, Person, :blocked => false, :user_id => 1
|
||||
@ability.query(:read, Person).conditions.should == 'true=true'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
it "should return false condition for single `cannot` definition" do
|
||||
@ability.cannot :read, Person, :blocked => true, :user_id => 1
|
||||
@ability.query(:read, Person).conditions.should == 'true=false'
|
||||
end
|
||||
|
||||
|
||||
it "should return `false condition` for single `cannot` definition in front of default `cannot` condition" do
|
||||
@ability.cannot :read, Person
|
||||
@ability.cannot :read, Person, :blocked => true, :user_id => 1
|
||||
@ability.query(:read, Person).conditions.should == 'true=false'
|
||||
end
|
||||
|
||||
|
||||
it "should return `not (sql)` for single `cannot` definition in front of default `can` condition" do
|
||||
@ability.can :read, Person
|
||||
@ability.cannot :read, Person, :blocked => true, :user_id => 1
|
||||
@ability.query(:read, Person).conditions.should orderlessly_match("not (blocked=true AND user_id=1)")
|
||||
end
|
||||
|
||||
|
||||
it "should return appropriate sql conditions in complex case" do
|
||||
@ability.can :read, Person
|
||||
@ability.can :manage, Person, :id => 1
|
||||
|
@ -76,29 +76,29 @@ describe CanCan::Query do
|
|||
@ability.query(:manage, Person).conditions.should == {:id=>1}
|
||||
@ability.query(:read, Person).conditions.should == 'true=true'
|
||||
end
|
||||
|
||||
|
||||
it "should have nil joins if no can definitions" do
|
||||
@ability.query(:read, Person).joins.should be_nil
|
||||
end
|
||||
|
||||
|
||||
it "should have nil joins if no nested hashes specified in conditions" do
|
||||
@ability.can :read, Person, :blocked => false
|
||||
@ability.can :read, Person, :admin => true
|
||||
@ability.query(:read, Person).joins.should be_nil
|
||||
end
|
||||
|
||||
|
||||
it "should merge separate joins into a single array" do
|
||||
@ability.can :read, Person, :project => { :blocked => false }
|
||||
@ability.can :read, Person, :company => { :admin => true }
|
||||
@ability.query(:read, Person).joins.inspect.should orderlessly_match([:company, :project].inspect)
|
||||
end
|
||||
|
||||
|
||||
it "should merge same joins into a single array" do
|
||||
@ability.can :read, Person, :project => { :blocked => false }
|
||||
@ability.can :read, Person, :project => { :admin => true }
|
||||
@ability.query(:read, Person).joins.should == [:project]
|
||||
end
|
||||
|
||||
|
||||
it "should merge complex, nested joins" do
|
||||
@ability.can :read, Person, :project => { :bar => {:test => true} }, :company => { :bar => {:test => true} }
|
||||
@ability.can :read, Person, :project => { :foo => {:bar => true}, :bar => {:zip => :zap} }
|
||||
|
|
|
@ -23,14 +23,14 @@ end
|
|||
class Person
|
||||
def self.sanitize_sql(hash_cond)
|
||||
case hash_cond
|
||||
when Hash
|
||||
when Hash
|
||||
sanitize_hash(hash_cond).join(' AND ')
|
||||
when Array
|
||||
hash_cond.shift.gsub('?'){"#{hash_cond.shift.inspect}"}
|
||||
when String then hash_cond
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def self.sanitize_hash(hash)
|
||||
hash.map do |name, value|
|
||||
if Hash === value
|
||||
|
|
Loading…
Reference in New Issue
Block a user