Sha256: a66865d896b1ea352eba2f25429e3afae7dd5027acc1202430a1f1d1db49acb0

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

class Account < ActiveRecord::Base
  # Validators

  has_many :account_users, :dependent => :destroy, :include => [:roles,:account]
  has_many :account_domains, :dependent => :destroy
  has_many :users, :through => :account_users
  has_many :tokens, :as => :tokenable, :class_name => "ApiToken"
  handle_asynchronously :destroy, :queue => Proc.new { |p| "account_#{p.id}_destroy" },
                        :run_at => Proc.new { DateTime.now + IuguSDK::delay_account_exclusion }

  validates :subdomain, :uniqueness => true, :unless => Proc.new { |a| a.subdomain.blank? }
  validates :api_token, :uniqueness => true, :if => :api_token?
  validate :subdomain_blacklist, :name

  attr_accessible :subdomain, :name 

  after_create :set_first_subdomain, :unless => :subdomain?

  def self.get_from_domain(domain)
    AccountDomain.verified.find_by_url(domain).try(:account) || Account.find_by_subdomain(domain.gsub(".#{IuguSDK::application_main_host}",""))
  end
  

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

  def destroying?
    !!destruction_job
  end

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

  def valid_user_for_account?( user )
    user = user.try(:id) if user.is_a? Object
    users.exists? user
  end

  def is?(role, user)
    account_users.find_by_user_id(user.id).is?(role.to_s)
  end

  def name
    (super.blank? ? "#{I18n.t('iugu.account')} ##{id}" : super)
  end
  
  private

  def set_first_subdomain
    self.update_attribute(:subdomain, "#{IuguSDK::account_alias_prefix}#{id}")
  end

  def subdomain_blacklist
    if subdomain
      IuguSDK::custom_domain_invalid_prefixes.each do |invalid_prefix|
        errors.add(:subdomain, "Subdomain blacklisted") if subdomain == invalid_prefix
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iugusdk-1.0.0.alpha.5 app/models/account.rb
iugusdk-1.0.0.alpha.4 app/models/account.rb