Skip to content

Instantly share code, notes, and snippets.

@RichardJordan
Created February 17, 2014 22:55
Show Gist options
  • Save RichardJordan/9060915 to your computer and use it in GitHub Desktop.
Save RichardJordan/9060915 to your computer and use it in GitHub Desktop.
Registration of user and account: the GrantEvent class for managing the granting of roles
# see https://gist.github.com/RichardJordan/9059633 for explanation
# -----------------------------------------------------------------
###################################################
# create_table "grant_events", force: true do |t| #
# t.integer "role_id", null: false #
# t.integer "user_id" #
# t.string "name", null: false #
# t.datetime "created_at" #
# t.datetime "updated_at" #
# end #
###################################################
class GrantEvent < ActiveRecord::Base
belongs_to :role
belongs_to :user
attr_writer :user_fetcher
def allow
create_event :grant
end
def deny
create_event :revoke
end
def granter
user_id ? user_fetcher.call(user_id) : CustomerSupportUser.new
end
def granter=(granter_user)
self.user_id = granter_user.id
end
private
def create_event(state)
self.name = state.to_s
role.add_event(self)
end
def user_fetcher
@user_fetcher ||= User.public_method(:find_by_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment