Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/sum/model/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reset_spent_today!Object



55
56
57
58
59
60
61
# File 'lib/sum/model/user.rb', line 55

def reset_spent_today!
  conditions = [ 'send_at <= ?', Time.now.utc ]
  users = self.find(:all, :conditions => conditions)
  users.each do |user|
    user.reset_spent_today!
  end
end

.reset_users!Object



47
48
49
50
51
52
53
# File 'lib/sum/model/user.rb', line 47

def reset_users!
  conditions = [ 'reset_at <= ?', Time.now.utc ]
  users = self.find(:all, :conditions => conditions)
  users.each do |user|
    user.reset!
  end
end

.send_emails!(&block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sum/model/user.rb', line 63

def send_emails!(&block)
  conditions = [
    'send_now = 1 OR send_at <= ?',
    Time.now.utc
  ]
  users = self.find(:all, :conditions => conditions)
  users.each do |user|
    user.emails.each do |email|
      next unless email.active? && email.failures <= 5
      begin
        $mail.deliver(
          :from => '[email protected]',
          :to => email.email,
          :subject => "Today's budget",
          :body => yield(user)
        )
        email.sent!
      rescue Exception => e
        email.increment!(:failures)
      end
    end
    user.sent!
  end
end

Instance Method Details

#add_email!(address, send = false) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/sum/model/user.rb', line 105

def add_email!(address, send=false)
  UserEmail.create(:email => address, :user_id => self.id)
  if send
    self.flash = "Successfully added #{address} to your account."
    self.send_now = true
    self.save
  end
end

#beginning_of_monthObject

Beginning of fiscal month



115
116
117
# File 'lib/sum/model/user.rb', line 115

def beginning_of_month
  self.reset_at - 1.month
end

#days_in_monthObject

Days in this fiscal month



120
121
122
# File 'lib/sum/model/user.rb', line 120

def days_in_month
  (self.reset_at.to_date - self.beginning_of_month.to_date).to_i
end

#days_leftObject

Days left in this fiscal month



125
126
127
# File 'lib/sum/model/user.rb', line 125

def days_left
  to_local(self.reset_at).to_date - to_local(Time.now).to_date - 1
end

#days_left_including_todayObject

Days left in this fiscal month, including today



130
131
132
# File 'lib/sum/model/user.rb', line 130

def days_left_including_today
  self.days_left + 1
end

#days_passedObject

Days passed in this fiscal month



135
136
137
# File 'lib/sum/model/user.rb', line 135

def days_passed
  to_local(Time.now).to_date - to_local(self.beginning_of_month).to_date
end

#days_passed_including_todayObject

Days passed in this fiscal month, including today



140
141
142
# File 'lib/sum/model/user.rb', line 140

def days_passed_including_today
   (self.days_passed + 1).to_i
end

#reset!Object



149
150
151
152
153
154
155
156
157
# File 'lib/sum/model/user.rb', line 149

def reset!
  self.temporary_spending_cut = self.total_left * -1
  if self.temporary_spending_cut < 0
    self.temporary_spending_cut = 0
  end
  self.spent_this_month = 0
  update_reset_at
  self.save
end

#reset_spent_today!Object

Reset spent_today



145
146
147
# File 'lib/sum/model/user.rb', line 145

def reset_spent_today!
  self.update_attribute :spent_today, 0
end

#sent!Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/sum/model/user.rb', line 159

def sent!
  self.flash = nil
  self.send_now = false
  update_send_at unless self.send_now_changed?
  self.save
rescue
  # This fixes a really confusing error when running cucumber features:
  #   No response yet. Request a page first. (Rack::Test::Error)
  # Happens on User#save after using $mail.deliver.
end

#should_have_spentObject

How much the user should have spent in this period



171
172
173
# File 'lib/sum/model/user.rb', line 171

def should_have_spent
  self.spending_per_day * self.days_passed_including_today
end

#spend!(amount) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'lib/sum/model/user.rb', line 175

def spend!(amount)
  amount = amount.to_f if amount.respond_to?(:upcase)
  self.recent_transactions ||= []
  self.recent_transactions.unshift(amount)
  self.spent_this_month += amount
  self.spent_today += amount
  self.send_now = true
  self.save
end

#spending_goalObject

Spending goal for the month



186
187
188
# File 'lib/sum/model/user.rb', line 186

def spending_goal
  read_attribute(:spending_goal) - self.temporary_spending_cut
end

#spending_goal_todayObject

Today’s spending goal



191
192
193
# File 'lib/sum/model/user.rb', line 191

def spending_goal_today
  (self.surplus(:exclude_today) / self.days_left_including_today) - self.spent_today
end

#spending_goal_today_savingsObject

Today’s spending goal with savings included in money left to spend



196
197
198
# File 'lib/sum/model/user.rb', line 196

def spending_goal_today_savings
  (self.savings_goal + self.surplus(:exclude_today)) / self.days_left_including_today
end

#spending_per_dayObject

How much the user is spending per day (ideally)



201
202
203
# File 'lib/sum/model/user.rb', line 201

def spending_per_day
  self.spending_goal / self.days_in_month.to_f
end

#spent_this_month(option = nil) ⇒ Object

How much the user has spent this month



206
207
208
209
210
211
212
213
# File 'lib/sum/model/user.rb', line 206

def spent_this_month(option=nil)
  case option
  when :exclude_today
    read_attribute(:spent_this_month) - self.spent_today
  else
    read_attribute(:spent_this_month)
  end
end

#surplus(option = nil) ⇒ Object

Variance from budget based on savings_goal



216
217
218
# File 'lib/sum/model/user.rb', line 216

def surplus(option=nil)
  two_decimals(self.spending_goal - spent_this_month(option))
end

#surplus_for_period(option = nil) ⇒ Object

Variance from budget based on should_have_spent



221
222
223
# File 'lib/sum/model/user.rb', line 221

def surplus_for_period(option=nil)
  two_decimals(self.should_have_spent - spent_this_month(option))
end

#total_left(option = nil) ⇒ Object

Total remaining money for the month



226
227
228
# File 'lib/sum/model/user.rb', line 226

def total_left(option=nil)
  self.surplus(option) + self.savings_goal
end