Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
UserCim
Defined in:
app/models/user.rb

Overview

Schema Information

Table name: users

id                :integer(4)      not null, primary key
first_name        :string(255)
last_name         :string(255)
birth_date        :date
email             :string(255)
state             :string(255)
account_id        :integer(4)
customer_cim_id   :string(255)
password_salt     :string(255)
crypted_password  :string(255)
perishable_token  :string(255)
persistence_token :string(255)
access_token      :string(255)
comments_count    :integer(4)      default(0)
created_at        :datetime
updated_at        :datetime

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Array[User]) admin_grid(params = {})

paginated results from the admin User grid

Parameters:

  • (Optional params)

Returns:



353
354
355
356
357
358
359
360
# File 'app/models/user.rb', line 353

def self.admin_grid(params = {})
  grid = self
  grid = grid.includes(:roles)
  grid = grid.where("users.first_name LIKE ?", "%#{params[:first_name]}%") if params[:first_name].present?
  grid = grid.where("users.last_name LIKE ?",  "%#{params[:last_name]}%")  if params[:last_name].present?
  grid = grid.where("users.email LIKE ?",      "%#{params[:email]}%")      if params[:email].present?
  grid
end

+ (Arel) find_subscription_users

Find users that have signed up for the subscription

Returns:

  • (Arel)


337
338
339
# File 'app/models/user.rb', line 337

def self.find_subscription_users
  where('account_id NOT IN (?)', Account::FREE_ACCOUNT_IDS )
end

Instance Method Details

- (Boolean) active?

returns true or false if the user is active or not

Parameters:

  • (none)

Returns:

  • (Boolean)


173
174
175
# File 'app/models/user.rb', line 173

def active?
  !['canceled', 'inactive'].any? {|s| self.state == s }
end

- (Boolean) admin?

returns true or false if the user is an admin or not

Parameters:

  • (none)

Returns:

  • (Boolean)


197
198
199
# File 'app/models/user.rb', line 197

def admin?
  role?(:administrator) || role?(:super_administrator)
end

- (Address) billing_address

Returns the default billing address if it exists. otherwise returns the shipping address

Parameters:

  • (none)

Returns:



255
256
257
# File 'app/models/user.rb', line 255

def billing_address
  default_billing_address ? default_billing_address : shipping_address
end

- (Cart) current_cart

returns your last cart or nil

Parameters:

  • (none)

Returns:



214
215
216
# File 'app/models/user.rb', line 214

def current_cart
  carts.last
end

- (none) deliver_activation_instructions!

email activation instructions after a user signs up

Parameters:

  • (none)

Returns:

  • (none)


302
303
304
# File 'app/models/user.rb', line 302

def deliver_activation_instructions!
  Notifier.(self).deliver
end

- (Object) deliver_password_reset_instructions!



362
363
364
365
# File 'app/models/user.rb', line 362

def deliver_password_reset_instructions!
  self.reset_perishable_token!
  Notifier.password_reset_instructions(self).deliver
end

- (String) display_active

in plain english returns 'true' or 'false' if the user is active or not

Parameters:

  • (none)

Returns:

  • (String)


181
182
183
# File 'app/models/user.rb', line 181

def display_active
  active?.to_s
end

- (Object) display_birth_date



226
227
228
# File 'app/models/user.rb', line 226

def display_birth_date
  birth_date ? I18n.localize(birth_date, :format => :us_time) : 'N/A'
end

- (String) email_address_with_name

name and email string for the user ex. '"John Wayne" "jwayne@badboy.com"'

Parameters:

  • (none)

Returns:

  • (String)


311
312
313
# File 'app/models/user.rb', line 311

def email_address_with_name
  "\"#{name}\" <#{email}>"
end

- (none) form_birth_date

formats the String

Parameters:

  • formatted (String)

    in Euro-time

Returns:

  • (none)

    sets birth_date for the user



234
235
236
# File 'app/models/user.rb', line 234

def form_birth_date
  birth_date.present? ? birth_date.strftime("%m/%d/%Y") : nil
end

- (none) form_birth_date=(val)

formats the String

Parameters:

  • formatted (String)

    in Euro-time

Returns:

  • (none)

    sets birth_date for the user



241
242
243
# File 'app/models/user.rb', line 241

def form_birth_date=(val)
  self.birth_date = Date.strptime(val, "%m/%d/%Y").to_s(:db) if val.present?
end

- (none) format_birth_date(b_date)

formats the String

Parameters:

  • formatted (String)

    in Euro-time

Returns:

  • (none)

    sets birth_date for the user



222
223
224
# File 'app/models/user.rb', line 222

def format_birth_date(b_date)
  self.birth_date = Date.strptime(b_date, "%m/%d/%Y").to_s(:db) if b_date.present?
end

- (String) get_cim_profile

place holder method for creating cim profiles for recurring billing

Parameters:

  • (none)

Returns:

  • (String)

    CIM id returned from the gateway



319
320
321
322
323
# File 'app/models/user.rb', line 319

def get_cim_profile
  return customer_cim_id if customer_cim_id
  create_cim_profile
  customer_cim_id
end

- (Arel) include_default_addresses

include addresses in Find

Returns:

  • (Arel)


345
346
347
# File 'app/models/user.rb', line 345

def include_default_addresses
  includes([:default_billing_address, :default_shipping_address, :account])
end

- (String) merchant_description

name and first line of address (used by credit card gateway to descript the merchant)

Parameters:

  • (none)

Returns:

  • (String)

    name and first line of address



329
330
331
# File 'app/models/user.rb', line 329

def merchant_description
  [name, default_shipping_address.try(:address_lines)].compact.join(', ')
end

- (Object) might_be_interested_in_these_products

This method will one day grow into the products a user most likely likes.

Storing a list of product ids vs cron each night might be the most efficent mode for this method to work.


247
248
249
# File 'app/models/user.rb', line 247

def might_be_interested_in_these_products
  Product.limit(4).all
end

- (String) name

gives the user's first and last name if available, otherwise returns the users email

Parameters:

  • (none)

Returns:

  • (String)


279
280
281
# File 'app/models/user.rb', line 279

def name
  (first_name? && last_name?) ? [first_name.capitalize, last_name.capitalize ].join(" ") : email
end

- (Object) number_of_finished_orders



367
368
369
# File 'app/models/user.rb', line 367

def number_of_finished_orders
  finished_orders.count
end

- (Object) number_of_finished_orders_at(at)



371
372
373
# File 'app/models/user.rb', line 371

def number_of_finished_orders_at(at)
  finished_orders.select{|o| o.completed_at < at }.size
end

- (Boolean) registered_user?

returns true or false if the user is a registered user or not

Parameters:

  • (none)

Returns:

  • (Boolean)


271
272
273
# File 'app/models/user.rb', line 271

def registered_user?
  registered? || registered_with_credit?
end

- (Boolean) role?(role_name)

returns true or false if the user has a role or not

Parameters:

  • role (String)

    name the user should have

Returns:

  • (Boolean)


189
190
191
# File 'app/models/user.rb', line 189

def role?(role_name)
  roles.any? {|r| r.name == role_name.to_s}
end

- (none) sanitize_data

sanitizes the saving of data. removes white space and assigns a free account type if one doesn't exist

Parameters:

  • (none)

Returns:

  • (none)


287
288
289
290
291
292
293
294
295
296
# File 'app/models/user.rb', line 287

def sanitize_data
  self.email      = self.email.strip.downcase       unless email.blank?
  self.first_name = self.first_name.strip.capitalize  unless first_name.nil?
  self.last_name  = self.last_name.strip.capitalize   unless last_name.nil?

  ## CHANGE THIS IF YOU HAVE DIFFERENT ACCOUNT TYPES
  unless 
    self. = Account.first
  end
end

- (Address) shipping_address

Returns the default shipping address if it exists. otherwise returns the first shipping address

Parameters:

  • (none)

Returns:



263
264
265
# File 'app/models/user.rb', line 263

def shipping_address
  default_shipping_address ? default_shipping_address : shipping_addresses.first
end

- (Boolean) super_admin?

returns true or false if the user is a super admin or not FYI your IT staff might be an admin but your CTO and IT director is a super admin

Parameters:

  • (none)

Returns:

  • (Boolean)


206
207
208
# File 'app/models/user.rb', line 206

def super_admin?
  role?(:super_administrator)
end