Skip to content

Instantly share code, notes, and snippets.

@aishek
Created August 17, 2014 14:09
Show Gist options
  • Save aishek/335572506a403af71547 to your computer and use it in GitHub Desktop.
Save aishek/335572506a403af71547 to your computer and use it in GitHub Desktop.
six rules ror app usage example
# app/models/user.rb
class User < ActiveRecord::Base
include Allowed::UserRules
end
# 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