Sha256: e2d489654fd77c6036392e2a2017c81d9ff10718eda8a7f8c739c13270ab6f74

Contents?: true

Size: 898 Bytes

Versions: 6

Compression:

Stored size: 898 Bytes

Contents

class Role < AbstractAdapter
  belongs_to :user
  belongs_to :ownable, polymorphic: true

  validates_uniqueness_of :user_id, scope: [:ownable_type,:ownable_id], unless: "user_id.nil?", message: "user has already been assigned that role"
  validates_inclusion_of :ownable_type, in: ['SuperUser', 'Company', 'Project']

  delegate :email, to: :user,              allow_nil: true
  def attributes
    super.merge(email: email)
  end

  def ownable
    # return the SuperUser null object
    ownable_type == 'SuperUser' ? SuperUser.new : super
  end

  def self.ownable_assign_options(_model=nil)
    ([SuperUser.new] + Company.all + Project.all).map { |i| [ "#{i.class}: #{i.name}", "#{i.class}-#{i.id}"] }
  end

  def ownable_assign
    ownable.present? ? "#{ownable_type}-#{ownable_id}" : nil
  end

  def ownable_assign=(value)
    self.ownable_type,self.ownable_id = value.split('-')
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
introspective_grape-0.2.5 spec/dummy/app/models/role.rb
introspective_grape-0.2.4 spec/dummy/app/models/role.rb
introspective_grape-0.2.3 spec/dummy/app/models/role.rb
introspective_grape-0.2.2 spec/dummy/app/models/role.rb
introspective_grape-0.2.0 spec/dummy/app/models/role.rb
introspective_grape-0.1.9 spec/dummy/app/models/role.rb