Skip to content

Instantly share code, notes, and snippets.

@eqbal
Last active June 1, 2017 18:06
Show Gist options
  • Save eqbal/3af8464c0f900258638e52caca20a3b9 to your computer and use it in GitHub Desktop.
Save eqbal/3af8464c0f900258638e52caca20a3b9 to your computer and use it in GitHub Desktop.
class Entry < ActiveRecord::Base
validates :status_weather, inclusion: {
in: EntryStatus::OPTIONS[:weather]
}
validates :status_landform, inclusion: {
in: EntryStatus::OPTIONS[:landform]
}
....
def status
@status ||= EntryStatus.new(status_weather, status_landform)
end
def status=(status)
self[:status_weather] = status.weather
self[:status_landform] = status.landform
@status = status
end
....
end
class Entry < ActiveRecord::Base
STATUS = {
weather: %w(Sunny Rainy Windy Dry),
landform: %w(Beach Cliff Desert Flat)
}
belongs_to :user
validates_presence_of :distance, :time_period, :date_time
validates :status_weather, inclusion: { in: STATUS[:weather] }
validates :status_landform, inclusion: { in: STATUS[:landform] }
validates_numericality_of :distance, :time_period
after_create :compare_speed_and_notify_user
def week
date = date_time.split(' ')[0]
Date.strptime(date, '%m/%d/%Y').strftime('%W')
end
def speed
distance / (time_period.to_f / 60)
end
private
def compare_speed_and_notify_user
entries_avg_speed = (Entry.all.sum(&:speed) / Entry.count).round(2)
if speed > entries_avg_speed
msg = 'You are doing great. Keep it up, superman. :)'
else
msg = 'Most of the users are faster than you. Try harder, dude! :('
end
NexmoClient.send_message(
from: 'Toptal',
to: user.mobile,
text: msg
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment