Created
December 22, 2011 12:54
-
-
Save ashmoran/1510220 to your computer and use it in GitHub Desktop.
Example of using the CanCan language described in https://gist.github.com/1509779
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ability | |
ReportAbility = AbilityBuilder.new do | |
def build | |
can :update_content, Report do |report| | |
report.being_edited_by?(user) | |
end | |
can :check_in_report, Report do |report| | |
report.being_edited_by?(user) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
# ... | |
class Ability | |
describe ReportAbility do | |
let(:user) { mock(User) } | |
let(:report) { Report.make } | |
subject { Ability.build(user, Ability::ReportAbility) } | |
stub_from_metadata :user | |
context "admin user", user: { admin?: true } do | |
stub_from_metadata :report | |
context "current editor of a report", report: { being_edited_by?: true } do | |
specify { | |
report.should_receive(:being_edited_by?).with(user) | |
subject.can? :update_content, report | |
} | |
it { should be_able_to(:update_content, report) } | |
it { should be_able_to(:check_in_report, report) } | |
end | |
context "not current editor of a report", report: { being_edited_by?: false } do | |
specify { | |
report.should_receive(:being_edited_by?).with(user) | |
subject.can? :update_content, report | |
} | |
it { should_not be_able_to(:update_content, report) } | |
it { should_not be_able_to(:check_in_report, report) } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment