Created
January 26, 2013 19:52
-
-
Save anga/4644188 to your computer and use it in GitHub Desktop.
declarative_authorization rspec matcher
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
RSpec::Matchers.define :permit do |action| | |
match do |object| | |
read_user object | |
permit?(action, user: @user, context: @object) | |
end | |
def read_user(object) | |
return if @user | |
if @user.kind_of? User | |
@user = object | |
else | |
@user = User.current_user unless @user | |
end | |
end | |
chain :to do |user| | |
@user = user | |
end | |
chain :user do |user| | |
@user = user | |
end | |
chain :object do |model| | |
@object = model | |
end | |
chain :context do |model| | |
@object = model | |
end | |
failure_message_for_should do |actual| | |
"expected that user #{@user} permit to #{@action} to object #{@object}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment