Sha256: b71a24b574a11105a11bae54961a0e6452337128da74ce536df4aab4293a9261

Contents?: true

Size: 1.52 KB

Versions: 17

Compression:

Stored size: 1.52 KB

Contents

class AccountUser < ActiveRecord::Base
  include ActiveUUID::UUID

  has_many :roles, :class_name => "AccountRole", :dependent => :destroy
  belongs_to :user
  belongs_to :account

  after_create :add_default_roles

  validates_presence_of :user
  validates_presence_of :account

  attr_accessible :user

  alias :_destroy :destroy
  def destroy
    Delayed::Job.enqueue DestroyAccountUserJob.new(id.to_s), :queue => "account_user_#{id}_destroy", :run_at => DateTime.now + IuguSDK::delay_account_user_exclusion
  end

  def destruction_job
    Delayed::Job.find_by_queue("account_user_#{id}_destroy")
  end 

  def destroying?
    !!destruction_job
  end 

  def cancel_destruction
    destruction_job.try(:destroy) unless destruction_job.try(:locked_at)
  end 

  # Write tests for this
  def add_default_roles
    roles.create( { :name => APP_ROLES[ "owner_role" ] } )
    roles.create( { :name => APP_ROLES[ "admin_role" ] } ) if APP_ROLES[ "owner_role" ] != APP_ROLES[ "admin_role" ]
  end

  def set_roles(new_roles)
    if new_roles.nil?
      roles.destroy_all
    else
      valid = true
      new_roles.each do |new_role|
        valid = false unless APP_ROLES['roles'].include? new_role
      end
      return false unless valid 
      roles.destroy_all
      new_roles.each do |new_role|
        roles.create( :name => new_role )
      end
    end
    true
  end

  def is?(role)
    role = APP_ROLES[ "owner_role" ] if role.to_s == "owner"
    role = APP_ROLES[ "admin_role" ] if role.to_s == "admin"
    roles.exists?(:name => role)
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
iugusdk-1.0.0.alpha.25 app/models/account_user.rb
iugusdk-1.0.0.alpha.24 app/models/account_user.rb
iugusdk-1.0.0.alpha.23 app/models/account_user.rb
iugusdk-1.0.0.alpha.22 app/models/account_user.rb
iugusdk-1.0.0.alpha.21 app/models/account_user.rb
iugusdk-1.0.0.alpha.20 app/models/account_user.rb
iugusdk-1.0.0.alpha.19 app/models/account_user.rb
iugusdk-1.0.0.alpha.18 app/models/account_user.rb
iugusdk-1.0.0.alpha.17 app/models/account_user.rb
iugusdk-1.0.0.alpha.16 app/models/account_user.rb
iugusdk-1.0.0.alpha.14 app/models/account_user.rb
iugusdk-1.0.0.alpha.13 app/models/account_user.rb
iugusdk-1.0.0.alpha.12 app/models/account_user.rb
iugusdk-1.0.0.alpha.11 app/models/account_user.rb
iugusdk-1.0.0.alpha.10 app/models/account_user.rb
iugusdk-1.0.0.alpha.9 app/models/account_user.rb
iugusdk-1.0.0.alpha.7 app/models/account_user.rb