Last active
August 29, 2015 14:11
-
-
Save DemitryT/129e92c4296ded430076 to your computer and use it in GitHub Desktop.
theory.rb
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' | |
# You shouldn't add these credentials in the model or even check these into github. They should either | |
# be added via the Heroku toolbelt to the settings or at least stored in a separate settings file. | |
has_many :orders | |
has_many :packages, through: :orders | |
before_save :assign_referral_code, on: :create | |
after_create :schedule_welcome_email | |
validates_presence_of :email, :fname, :lname | |
# You should use the new Rails format 'validates ..., presence: true | |
# You should use more explicity names such as first_name and last_name | |
validates :referral_code, uniqueness: true | |
scope :recently_created, where("created_at <= #{Date.today - 2.days}") | |
# I prefer to have these in a class method instead of scopes. So you could do | |
# class << self;end within this class and define the class method there. | |
# I also don't think the comparison will work here as created_at is a DateTime | |
# and the second part will give you a Date object, would have to look into it. | |
def name | |
self.fname + ' ' + self.lname | |
end | |
# Again, you could put this into class << self;end so that you don't have to | |
# keep calling self | |
def formatted_name | |
"<strong>#{name}</strong> (<a href=\"mailto:#{email}\">#{email}</a>)" | |
# Horrible practice to output raw HTML from a class method. Need a helper | |
# or a decorator for your views instead of this. | |
end | |
def assign_referral_code | |
referral_code = SecureRandom.hex(6) | |
end | |
def schedule_welcome_email | |
Mailer.delay.welcome(self) # Queue email with DelayedJob | |
end | |
def has_orders | |
orders.any? | |
end | |
def most_recent_package_shipping_address | |
order.last.package.shipping_address | |
end | |
def can_manage? | |
(email = '[email protected]') or (email = '[email protected]') | |
# Might be better to store this in an authorization class for a gem you're using | |
# Also, use || instead of or | |
end | |
def order_product(product) | |
result = OrderProcessor.charge(self, product) | |
if result | |
Event.log_order_processing(self) | |
Mailer.order_confirmation(self, product).deliver | |
return true | |
# don't need to return true or false from these method, | |
# not a good Ruby convention | |
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