Created
August 17, 2014 14:09
-
-
Save aishek/335572506a403af71547 to your computer and use it in GitHub Desktop.
six rules ror app usage example
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
# app/models/user.rb | |
class User < ActiveRecord::Base | |
include Allowed::UserRules | |
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
# lib/allowed/user_rules.rb | |
module Allowed::UserRules | |
extend ActiveSupport::Concern | |
def allowed(object, subject) | |
rules = [] | |
return rules unless subject.kind_of?(::User) | |
if object && object.persisted? | |
rules << :show | |
if subject == object | |
rules << :friends | |
rules << :update | |
end | |
else | |
rules << :create | |
end | |
rules | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment