Sha256: 1f7c6c1dfadd2e383aa1bdaa8c563ccafce332d89a6b45c558e73e24bc8f3069

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

class AccountUser < ActiveRecord::Base
  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

  handle_asynchronously :destroy, :queue => Proc.new { |p| "account_user_#{p.id}_destroy" },
                        :run_at => Proc.new { DateTime.now + IuguSDK::delay_account_user_exclusion }


  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

3 entries across 3 versions & 1 rubygems

Version Path
iugusdk-1.0.0.alpha.2 app/models/account_user.rb
iugusdk-1.0.0.alpha.1 app/models/account_user.rb
iugusdk-1.0.0.alpha.0 app/models/account_user.rb