Skip to content

Commit

Permalink
Use cover? for ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
allenwq committed Nov 19, 2015
1 parent 3e3c366 commit 30a25d7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/cancan/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def condition_match?(attribute, value)
case value
when Hash then hash_condition_match?(attribute, value)
when String then attribute == value
when Range then value.cover?(attribute)
when Enumerable then value.include?(attribute)
else attribute == value
end
Expand Down
7 changes: 7 additions & 0 deletions spec/cancan/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@
expect(@ability.can?(:read, 4..40)).to be(false)
end

it "allows a range of time in conditions hash" do
@ability.can :read, Range, :begin => 1.day.from_now..3.days.from_now
expect(@ability.can?(:read, 1.day.from_now..10.days.from_now)).to be(true)
expect(@ability.can?(:read, 2.days.from_now..20.days.from_now)).to be(true)
expect(@ability.can?(:read, 4.days.from_now..40.days.from_now)).to be(false)
end

it "allows nested hashes in conditions hash" do
@ability.can :read, Range, :begin => { :to_i => 5 }
expect(@ability.can?(:read, 5..7)).to be(true)
Expand Down

0 comments on commit 30a25d7

Please sign in to comment.