Class: User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- User
- Defined in:
- lib/sum/model/user.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_email!(address, send = false) ⇒ Object
-
#beginning_of_month ⇒ Object
Beginning of fiscal month.
-
#days_in_month ⇒ Object
Days in this fiscal month.
-
#days_left ⇒ Object
Days left in this fiscal month.
-
#days_left_including_today ⇒ Object
Days left in this fiscal month, including today.
-
#days_passed ⇒ Object
Days passed in this fiscal month.
-
#days_passed_including_today ⇒ Object
Days passed in this fiscal month, including today.
- #reset! ⇒ Object
-
#reset_spent_today! ⇒ Object
Reset spent_today.
- #sent! ⇒ Object
-
#should_have_spent ⇒ Object
How much the user should have spent in this period.
- #spend!(amount) ⇒ Object
-
#spending_goal ⇒ Object
Spending goal for the month.
-
#spending_goal_today ⇒ Object
Today’s spending goal.
-
#spending_goal_today_savings ⇒ Object
Today’s spending goal with savings included in money left to spend.
-
#spending_per_day ⇒ Object
How much the user is spending per day (ideally).
-
#spent_this_month(option = nil) ⇒ Object
How much the user has spent this month.
-
#surplus(option = nil) ⇒ Object
Variance from budget based on savings_goal.
-
#surplus_for_period(option = nil) ⇒ Object
Variance from budget based on should_have_spent.
-
#total_left(option = nil) ⇒ Object
Total remaining money for the month.
Class Method Details
.reset_spent_today! ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/sum/model/user.rb', line 45 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
37 38 39 40 41 42 43 |
# File 'lib/sum/model/user.rb', line 37 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sum/model/user.rb', line 53 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
93 94 95 96 97 98 99 100 |
# File 'lib/sum/model/user.rb', line 93 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_month ⇒ Object
Beginning of fiscal month
103 104 105 |
# File 'lib/sum/model/user.rb', line 103 def beginning_of_month self.reset_at - 1.month end |
#days_in_month ⇒ Object
Days in this fiscal month
108 109 110 |
# File 'lib/sum/model/user.rb', line 108 def days_in_month self.reset_at.to_date - self.beginning_of_month.to_date end |
#days_left ⇒ Object
Days left in this fiscal month
113 114 115 |
# File 'lib/sum/model/user.rb', line 113 def days_left to_local(self.reset_at).to_date - to_local(Time.now).to_date - 1 end |
#days_left_including_today ⇒ Object
Days left in this fiscal month, including today
118 119 120 |
# File 'lib/sum/model/user.rb', line 118 def days_left_including_today self.days_left + 1 end |
#days_passed ⇒ Object
Days passed in this fiscal month
123 124 125 |
# File 'lib/sum/model/user.rb', line 123 def days_passed to_local(Time.now).to_date - to_local(self.beginning_of_month).to_date end |
#days_passed_including_today ⇒ Object
Days passed in this fiscal month, including today
128 129 130 |
# File 'lib/sum/model/user.rb', line 128 def days_passed_including_today self.days_passed + 1 end |
#reset! ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/sum/model/user.rb', line 137 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
133 134 135 |
# File 'lib/sum/model/user.rb', line 133 def reset_spent_today! self.update_attribute :spent_today, 0 end |
#sent! ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sum/model/user.rb', line 147 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_spent ⇒ Object
How much the user should have spent in this period
159 160 161 |
# File 'lib/sum/model/user.rb', line 159 def should_have_spent self.spending_per_day * self.days_passed_including_today end |
#spend!(amount) ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/sum/model/user.rb', line 163 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_goal ⇒ Object
Spending goal for the month
174 175 176 |
# File 'lib/sum/model/user.rb', line 174 def spending_goal read_attribute(:spending_goal) - self.temporary_spending_cut end |
#spending_goal_today ⇒ Object
Today’s spending goal
179 180 181 |
# File 'lib/sum/model/user.rb', line 179 def spending_goal_today (self.surplus(:exclude_today) / self.days_left_including_today) - self.spent_today end |
#spending_goal_today_savings ⇒ Object
Today’s spending goal with savings included in money left to spend
184 185 186 |
# File 'lib/sum/model/user.rb', line 184 def spending_goal_today_savings (self.savings_goal + self.surplus(:exclude_today)) / self.days_left_including_today end |
#spending_per_day ⇒ Object
How much the user is spending per day (ideally)
189 190 191 |
# File 'lib/sum/model/user.rb', line 189 def spending_per_day self.spending_goal / self.days_in_month end |
#spent_this_month(option = nil) ⇒ Object
How much the user has spent this month
194 195 196 197 198 199 200 201 |
# File 'lib/sum/model/user.rb', line 194 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
204 205 206 |
# File 'lib/sum/model/user.rb', line 204 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
209 210 211 |
# File 'lib/sum/model/user.rb', line 209 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
214 215 216 |
# File 'lib/sum/model/user.rb', line 214 def total_left(option=nil) self.surplus(option) + self.savings_goal end |