Skip to content

Instantly share code, notes, and snippets.

@jonstorer
Created December 6, 2011 23:50
Show Gist options
  • Save jonstorer/1440675 to your computer and use it in GitHub Desktop.
Save jonstorer/1440675 to your computer and use it in GitHub Desktop.
Design testing
class Subscription
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :user
before :set_calculated_day_to_notify_on
FREQUENCY_UNITS = [ "daily", "weekly", "monthly", "yearly" ]
field :service, :type => String
field :amount, :type => Float
field :currency, :type => String, :default => "usd"
field :started, :type => Date
field :frequency, :type => String, :default => "monthly"
field :day_of_frequency, :type => Integer
field :number_of_days_to_notify_before_due_date, :type => Integer, :default => 2
field :calculated_day_to_notify_on, :type => Integer
attr_protected :calculated_day_to_notify_on
def self.send_monthly_email
where(:frequency => "monthly",
:calculated_day_to_notify_on => Date.today.day_of_month).each do |subscription|
puts subscription.service + ', ' + subscription.user.email
end
end
private
def set_calculated_day_to_notify_on
calculated_day = day_of_frequency - number_of_days_to_notify_before_due_date
#make sure we're not in last month
calculated_day = 31 - calculated_day if calculated_day < 1
#so we can handle end of month
calculated_day = 28 - calculated_day if calculated_day > 28
self.calculated_day_to_notify_on = calculated_day
end
end
class User
include Mongoid::Document
include Mongoid::Timestamps
field :username
field :password
field :email
has_many :subscriptions
attr_protected :password
def to_param
"#{username.parameterize}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment