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