Sha256: fec72d7839d0bc2d761aed0789fead59e8f60b0706e94abfe9ac223f0eaea848

Contents?: true

Size: 1.1 KB

Versions: 37

Compression:

Stored size: 1.1 KB

Contents

class UserRole < ActiveRecord::Base
  belongs_to :user
  belongs_to :role

  serialize :priveleges

  validates :role, :presence => true

  def method_missing(method, *args)
    method = method.to_s

    if method.starts_with? 'can_'
      role_keyword = method[method.index('_')+1..method.rindex('_')-1]
      privelege    = method[method.rindex('_')+1..-1]

      self.priveleges = [] unless self.priveleges.is_a?(Array)

      if method.ends_with? '='
        privelege = privelege[0..-2]

        if role && role.keyword == role_keyword
          if args[0] && args[0] != 'false'
            self.priveleges = (self.priveleges + [privelege]).uniq
          else
            self.priveleges = self.priveleges.select{|x| x != privelege}
          end
        end
      else
        return false if !role || role.keyword != role_keyword
        self.priveleges.include?(privelege)
      end
    else
      super(method.to_sym, *args)
    end
  end

  def respond_to?(method, *args)
    return true if method.to_s.starts_with? 'can_'
    super
  end

  def priveleged?(privelege)
    priveleges.include?(privelege.to_s)
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
smartkiosk-server-0.12.1 app/models/user_role.rb
smartkiosk-server-0.12 app/models/user_role.rb
smartkiosk-server-0.11.11 app/models/user_role.rb
smartkiosk-server-0.11.10 app/models/user_role.rb
smartkiosk-server-0.11.9 app/models/user_role.rb
smartkiosk-server-0.11.8 app/models/user_role.rb
smartkiosk-server-0.11.7 app/models/user_role.rb
smartkiosk-server-0.11.6 app/models/user_role.rb
smartkiosk-server-0.11.5 app/models/user_role.rb
smartkiosk-server-0.11.4 app/models/user_role.rb
smartkiosk-server-0.11.3 app/models/user_role.rb
smartkiosk-server-0.11.2 app/models/user_role.rb
smartkiosk-server-0.11.1 app/models/user_role.rb
smartkiosk-server-0.11 app/models/user_role.rb
smartkiosk-server-0.10.19 app/models/user_role.rb
smartkiosk-server-0.10.18 app/models/user_role.rb
smartkiosk-server-0.10.17 app/models/user_role.rb
smartkiosk-server-0.10.16 app/models/user_role.rb
smartkiosk-server-0.10.15 app/models/user_role.rb
smartkiosk-server-0.10.14 app/models/user_role.rb