Skip to content

Instantly share code, notes, and snippets.

@ashmoran
Created December 22, 2011 12:54
Show Gist options
  • Save ashmoran/1510220 to your computer and use it in GitHub Desktop.
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
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
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