Created
September 16, 2021 17:58
-
-
Save muyiwaoyeniyi/8b532c88d3004805fba803f2729af8f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # == 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 | |
| # TIME ALLOWED: 15mins | |
| class User < ActiveRecord::Base | |
| MAILCHIMP_API_KEY = 'm23lm092m3' | |
| 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 | |
| validates :referral_code, uniqueness: true | |
| scope :recently_created, where("created_at >= #{Date.today - 2.days}") | |
| def name | |
| fname + ' ' + 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.welcome(self) # Queue email with DelayedJob | |
| end | |
| def has_orders | |
| orders.any? | |
| end | |
| def most_recent_package_shipping_address | |
| orders.last.package.shipping_address | |
| end | |
| def can_manage? | |
| (email = 'manager@example.com') or (email = 'admin@example.com') | |
| end | |
| def recent_count | |
| recently_created.length | |
| end | |
| def order_product(product) | |
| result = OrderProcessor.charge(self, product) | |
| if result | |
| Event.log_order_processing(self) | |
| Mailer.order_confirmation(self, product).deliver | |
| true | |
| else | |
| Event.log_failed_order_processing(self) | |
| false | |
| end | |
| end | |
| def self.delete_user(email) | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment