Sha256: 9437893e9f2218f8d5da5ca5cb9c6ef1ec5c8566bd309d09ed8ec32d169b32ec

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'roles/railtie' if defined?(Rails)
require 'roles/role'
require 'roles/resource'

module Roles
  attr_accessor :role_cname
  attr_accessor :user_cname

  def rolify(options = {})
    include Role
    
    options.reverse_merge!({:role_cname => 'Role'})
    options.reverse_merge!({:user_cname => 'User'})

    roles_options = { :class_name => options[:role_cname].camelize, :dependent => :destroy }
    roles_options.merge!(options.select{ |k,v| [:before_add, :after_add, :before_remove, :after_remove].include? k.to_sym })

    has_many :roles, roles_options

    self.role_cname = options[:role_cname]
    self.user_cname = options[:user_cname]
  end

  def resourcify(options = {})
    include Resource
    
    options.reverse_merge!({ :role_cname => 'Role', :dependent => :destroy, :user_cname => 'User' })
    resourcify_options = { :class_name => options[:role_cname].camelize, :as => :resource, :dependent => options[:dependent] }
    has_many :roles, resourcify_options
    
    self.role_cname = options[:role_cname]
    self.user_cname = options[:user_cname]
  end
  
  def user_class
    self.user_cname.constantize
  end

  def role_class
    self.role_cname.constantize
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roles-0.1.0 lib/roles.rb