Class: Account

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveUUID::UUID
Defined in:
app/models/account.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveUUID::UUID

#fix_uuid_strings, included

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



22
23
24
# File 'app/models/account.rb', line 22

def currency
  @currency
end

#emailObject

Returns the value of attribute email.



22
23
24
# File 'app/models/account.rb', line 22

def email
  @email
end

#plan_identifierObject

Returns the value of attribute plan_identifier.



22
23
24
# File 'app/models/account.rb', line 22

def plan_identifier
  @plan_identifier
end

Class Method Details

.get_from_domain(domain) ⇒ Object



30
31
32
# File 'app/models/account.rb', line 30

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

Instance Method Details

#_destroyObject



13
# File 'app/models/account.rb', line 13

alias :_destroy :destroy

#cached_subscription_active?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/account.rb', line 64

def cached_subscription_active?
  key = [self, "subscription_active?"]
  if Rails.cache.exist? key
    Rails.cache.read key
  else
    active = subscription_active?
    active ? time = 1.day : time = 5.minutes
    Rails.cache.write key, active, expires_in: time
    active
  end
end

#cached_subscription_featuresObject



80
81
82
# File 'app/models/account.rb', line 80

def cached_subscription_features
  Rails.cache.fetch([self, "subscription_features"], expires_in: 1.day) { JSON.parse(subscription_features.to_json) || {} }
end

#cancel_destructionObject



42
43
44
# File 'app/models/account.rb', line 42

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

#change_plan(identifier) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'app/models/account.rb', line 117

def change_plan(identifier)
  sub = subscription
  return false unless sub
  if sub.change_plan(identifier)
    clear_cached_subscription_features
    return true
  end
  false
end

#clear_cached_subscription_activeObject



76
77
78
# File 'app/models/account.rb', line 76

def clear_cached_subscription_active
  Rails.cache.delete [self, "subscription_active?"]
end

#clear_cached_subscription_featuresObject



84
85
86
# File 'app/models/account.rb', line 84

def clear_cached_subscription_features
  Rails.cache.delete [self, "subscription_features"]
end

#destroyObject



14
15
16
# File 'app/models/account.rb', line 14

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

#destroying?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/account.rb', line 38

def destroying?
  !!destruction_job
end

#destruction_jobObject



34
35
36
# File 'app/models/account.rb', line 34

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

#is?(role, user) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/models/account.rb', line 51

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

#nameObject



55
56
57
# File 'app/models/account.rb', line 55

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

#owner_account_userObject



96
97
98
99
# File 'app/models/account.rb', line 96

def 
  .each { |au| return au if au.is? :owner }
  nil
end

#subscriptionObject



59
60
61
62
# File 'app/models/account.rb', line 59

def subscription
  return nil unless subscription_id
  Iugu::Api::Subscription.find subscription_id.to_s.to_uuid.to_s
end

#subscription_active?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/models/account.rb', line 88

def subscription_active?
  subscription.try :active 
end

#subscription_featuresObject



92
93
94
# File 'app/models/account.rb', line 92

def subscription_features
  subscription.try :features
end

#transfer_ownership(user) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/account.rb', line 101

def transfer_ownership(user)
  old_owner = 
  au = .find_by_user_id user.id  
  return unless au
  au.set_owner
  old_owner.roles.find_by_name("owner").try :destroy if old_owner

  if au.is? :owner
    subscription = Iugu::Api::Subscription.find subscription_id.to_s.to_uuid.to_s
    customer = Iugu::Api::Customer.find subscription.customer_id
    customer.email = user.email
    customer.name = user.name
    customer.save
  end
end

#valid_user_for_account?(user) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'app/models/account.rb', line 46

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