Skip to content

Instantly share code, notes, and snippets.

@RichardJordan
Created February 17, 2014 22:37
Show Gist options
  • Save RichardJordan/9060638 to your computer and use it in GitHub Desktop.
Save RichardJordan/9060638 to your computer and use it in GitHub Desktop.
Registration of user and account: Role.rb, which manages roles for permissions and other uses
# see https://gist.github.com/RichardJordan/9059633 for explanation
# -----------------------------------------------------------------
############################################
# create_table "roles", force: true do |t| #
# t.integer "account_id", null: false #
# t.integer "user_id", null: false #
# t.string "name", null: false #
# t.datetime "created_at" #
# t.datetime "updated_at" #
# end #
############################################
class Role < ActiveRecord::Base
belongs_to :account
belongs_to :user
has_many :grant_events
validates :name, presence: true
validates :user, presence: true
validates :account, presence: true
def add_event(event)
save
end
def grant(granter)
new_grant(granter: granter).allow
end
def new_grant(*args)
grant_source.call(*args).tap do |ge|
ge.role = self
end
end
def revoke(granter)
new_grant(granter: granter).deny
end
private
def grant_source
@grant_source ||= GrantEvent.public_method(:new)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment