Module: Saucy::Account::InstanceMethods

Defined in:
lib/saucy/account.rb

Instance Method Summary collapse

Instance Method Details

#can_change_plan_to?(new_plan) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
# File 'lib/saucy/account.rb', line 103

def can_change_plan_to?(new_plan)
  plan.limits.where(:value_type => :number).all? do |limit|
    new_plan.limit(limit.name).value >= send(limit.name).count
  end
end

#credit_cardObject



74
75
76
# File 'lib/saucy/account.rb', line 74

def credit_card
  customer.credit_cards[0] if customer && customer.credit_cards.any?
end

#customerObject



70
71
72
# File 'lib/saucy/account.rb', line 70

def customer
  Braintree::Customer.find(customer_token) if customer_token
end

#has_member?(user) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/saucy/account.rb', line 50

def has_member?(user)
  memberships.exists?(:user_id => user.id)
end

#memberships_by_nameObject



66
67
68
# File 'lib/saucy/account.rb', line 66

def memberships_by_name
  memberships.by_name
end

#past_due?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/saucy/account.rb', line 109

def past_due?
  subscription_status == Braintree::Subscription::Status::PastDue
end

#projects_by_nameObject



58
59
60
# File 'lib/saucy/account.rb', line 58

def projects_by_name
  projects.by_name
end

#projects_visible_to(user) ⇒ Object



62
63
64
# File 'lib/saucy/account.rb', line 62

def projects_visible_to(user)
  projects.visible_to(user)
end

#save_braintree!(attributes) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/saucy/account.rb', line 82

def save_braintree!(attributes)
  successful = true
  self.plan = ::Plan.find(attributes[:plan_id]) if attributes[:plan_id].present?
  if CUSTOMER_ATTRIBUTES.keys.any? { |attribute| attributes[attribute].present? }
    CUSTOMER_ATTRIBUTES.keys.each do |attribute|
      send("#{attribute}=", attributes[attribute]) if attributes[attribute].present?
    end
    result = Braintree::Customer.update(customer_token, customer_attributes)
    successful = result.success?
    handle_customer_result(result)
  end
  if successful && attributes[:plan_id].present?
    if subscription 
      Braintree::Subscription.update(subscription_token, :plan_id => attributes[:plan_id])
    else
      create_subscription
    end
  end
  successful && save
end

#subscriptionObject



78
79
80
# File 'lib/saucy/account.rb', line 78

def subscription
  Braintree::Subscription.find(subscription_token) if subscription_token
end

#to_paramObject



46
47
48
# File 'lib/saucy/account.rb', line 46

def to_param
  keyword
end

#users_by_nameObject



54
55
56
# File 'lib/saucy/account.rb', line 54

def users_by_name
  users.by_name
end