Last active
December 23, 2015 22:49
-
-
Save dpmccabe/6706273 to your computer and use it in GitHub Desktop.
a really bad model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
MAILCHIMP_API_KEY ='m23lm092m3' | |
MAILCHIMP_LIST_ID ='h439cds' | |
has_many :orders | |
before_save :assign_referral_code | |
after_create :schedule_welcome_email | |
validates :referral_code, uniqueness: true | |
scope :recently_created, where("created_at <= #{Date.today - 2.days}") | |
def name | |
self.fname + ' ' + self.lname | |
end | |
def formatted_name | |
"<strong>#{name}</strong> (<a href=\"mailto:#{email}\">#{email}</a>)" | |
end | |
def assign_referral_code | |
referral_code = SecureRandom.hex(6) | |
end | |
def schedule_welcome_email | |
Mailer.delay_until(Time.now + 30.minutes).welcome(self) # Queue with DelayedJob + Redis | |
end | |
def update_email_address(new_email) | |
email = new_email | |
self.save! | |
end | |
def has_orders | |
orders.any? | |
end | |
def order_product(product) | |
result = OrderProcessor.charge(self, product) | |
if result == true | |
Event.log_order_processing(self) | |
Mailer.order_confirmation(self, product).deliver | |
return true | |
else | |
Event.log_failed_order_processing(self) | |
return false | |
end | |
end | |
def self.delete_user(email) | |
begin | |
user = User.find_by_email(email) | |
user.destroy! | |
rescue Exception => e # email not found | |
Rails.logger.error("Could not delete user with email #{email}") | |
end | |
end | |
end | |
# == Schema Information | |
# | |
# Table name: users | |
# | |
# id :integer not null, primary key | |
# email :string(255) default(""), not null | |
# fname :string(255) default(""), not null | |
# lname :string(255) default(""), not null | |
# referral_code :string(255) | |
# created_at :datetime | |
# updated_at :datetime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment