Guest User

Untitled

a guest
Jul 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.45 KB | None | 0 0
  1. ## app/model/var.rb
  2. class Var < ActiveRecord::Base
  3.  
  4. named_scope :year_start_after, lambda {|day| {:conditions => ['previous_year_end <= ?',day]}}
  5. named_scope :year_end_before, lambda {|day| {:conditions => ['start_date >= ?',day]}}
  6. named_scope :current, :order => "year DESC"
  7.  
  8. end
  9.  
  10. ## app/model/user.rb
  11. require 'digest/sha1'
  12. class User < ActiveRecord::Base
  13. has_many :addresses, :dependent => :destroy
  14. has_many :phones, :dependent => :destroy
  15. has_many :order_transactions
  16. has_many :orders
  17. has_many :line_items, :through => :orders
  18. has_many :spaces
  19. has_many :previous_spaces, :class_name => "Space", :foreign_key => "previous_user_id"
  20.  
  21. # Virtual attribute for the unencrypted password
  22. attr_accessor :password, :fullname
  23.  
  24. validates_presence_of :login, :email
  25. validates_presence_of :password, :if => :password_required?
  26. validates_presence_of :password_confirmation, :if => :password_required?
  27. validates_length_of :password, :within => 4..40, :if => :password_required?
  28. validates_confirmation_of :password, :if => :password_required?
  29. validates_length_of :login, :within => 3..40
  30. validates_length_of :email, :within => 3..100
  31. validates_uniqueness_of :login, :case_sensitive => false
  32. validates_associated :addresses
  33. validates_associated :phones
  34. before_save :encrypt_password
  35. after_update :save_addresses, :save_phones
  36.  
  37. named_scope :by_id, :order => "users.id"
  38. named_scope :by_surname, :order => "surname"
  39. named_scope :by_given_name, :order => "given_name"
  40. named_scope :by_name, :order => "surname, given_name"
  41. named_scope :include_addresses, :include => :addresses
  42. named_scope :with_spaces, :include => :spaces
  43. named_scope :without_spaces, :include => :spaces,
  44. :conditions => ["spaces.user_id is null"]
  45. named_scope :with_previous_spaces, :joins => :previous_spaces
  46. ### logic is flawed, will find spaces reserved in previous years
  47. ### use :with_spaces instead
  48. named_scope :with_reservations, :include => :line_items,
  49. :conditions => ["line_items.billable_type = 'Space'"]
  50. named_scope :with_current_orders, :include => :orders,
  51. :conditions => ["orders.order_status = 'paid' or orders.order_status = 'confirmed'"]
  52. named_scope :awaiting_payment, :include => :orders,
  53. :conditions => ["orders.order_status = 'awaiting_payment'"]
  54. named_scope :with_archived_orders, :include => :orders,
  55. :conditions => ["orders.order_status = 'archived'"]
  56. named_scope :not_confirmed, :include => :orders,
  57. :conditions => ["orders.order_status <> 'confirmed'"]
  58. named_scope :orders_last_year, :include => :orders,
  59. :conditions => ["orders.checkout_at between ? and ?",Var.current.first.previous_year_start, Var.current.first.previous_year_end]
  60. named_scope :orders_this_year, :include => :orders,
  61. :conditions => ["orders.checkout_at >= ?", Var.current.first.previous_year_end]
  62. named_scope :no_orders_this_year, :joins => :orders,
  63. :conditions => ["orders.order_status <> 'archived'"]
  64. #:conditions => ["orders.checkout_at not between ? and ?", Var.current.first.previous_year_end, Var.current.first.end_date]
  65. named_scope :with_active_orders, :joins => :orders,
  66. :conditions => ["orders.order_status <> 'archived'"]
  67. named_scope :order_year, lambda { |*args| {:include => :orders, :conditions => ["orders.checkout_at between ? and ?", args.first, args.last]}}
  68. named_scope :sorting_order, lambda { |*args| {:order => args}}
  69.  
  70. # prevents a user from submitting a crafted form that bypasses activation
  71. # anything else you want your user to change should be added here.
  72. attr_accessible :login,
  73. :email,
  74. :password,
  75. :password_confirmation,
  76. :nickname,
  77. :url,
  78. :given_name,
  79. :middle_initial,
  80. :surname,
  81. :amcaid,
  82. :admin,
  83. :existing_address_attributes,
  84. :new_address_attributes,
  85. :address_attributes,
  86. :existing_phone_attributes,
  87. :new_phone_attributes,
  88. :phone_attributes,
  89. :company_name
  90.  
  91. def active_orders
  92. Order.user(self.id).neg_status("archived").size
  93. end
  94.  
  95. def new_address_attributes=(address_attributes)
  96. address_attributes.each do |attributes|
  97. addresses.build(attributes)
  98. end
  99. end
  100.  
  101. def existing_address_attributes=(address_attributes)
  102. addresses.reject(&:new_record?).each do |address|
  103. attributes = address_attributes[address.id.to_s]
  104. if attributes
  105. address.attributes = attributes
  106. else
  107. addresses.delete(address)
  108. end
  109. end
  110. end
  111.  
  112. def save_addresses
  113. addresses.each do |address|
  114. address.save(false)
  115. end
  116. end
  117.  
  118. def new_phone_attributes=(phone_attributes)
  119. phone_attributes.each do |attributes|
  120. phones.build(attributes)
  121. end
  122. end
  123.  
  124. def existing_phone_attributes=(phone_attributes)
  125. phones.reject(&:new_record?).each do |phone|
  126. attributes = phone_attributes[phone.id.to_s]
  127. if attributes
  128. phone.attributes = attributes
  129. else
  130. phone.delete(phone)
  131. end
  132. end
  133. end
  134.  
  135. def save_phones
  136. phones.each do |phone|
  137. phone.save(false)
  138. end
  139. end
  140.  
  141. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  142. def self.authenticate(login, password)
  143. u = find_by_login(login) # need to get the salt
  144. u && u.authenticated?(password) ? u : nil
  145. end
  146.  
  147. # Encrypts some data with the salt.
  148. def self.encrypt(password, salt)
  149. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  150. end
  151.  
  152. # Encrypts the password with the user salt
  153. def encrypt(password)
  154. self.class.encrypt(password, salt)
  155. end
  156.  
  157. def authenticated?(password)
  158. crypted_password == encrypt(password)
  159. end
  160.  
  161. def remember_token?
  162. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  163. end
  164.  
  165. # These create and unset the fields required for remembering users between browser closes
  166. def remember_me
  167. remember_me_for 2.weeks
  168. end
  169.  
  170. def remember_me_for(time)
  171. remember_me_until time.from_now.utc
  172. end
  173.  
  174. def remember_me_until(time)
  175. self.remember_token_expires_at = time
  176. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  177. save(false)
  178. end
  179.  
  180. def forget_me
  181. self.remember_token_expires_at = nil
  182. self.remember_token = nil
  183. save(false)
  184. end
  185.  
  186. def full_name
  187. self.fullname = self.middle_initial ? "#{self.given_name} #{self.middle_initial} #{self.surname}" : "#{self.given_name} #{self.surname}"
  188. self.fullname
  189. end
  190.  
  191. def name_with_amcaid
  192. "#{self.full_name} - #{self.amcaid}"
  193. end
  194.  
  195. # Returns true if the user has just been activated.
  196. def recently_activated?
  197. @activated
  198. end
  199.  
  200. protected
  201. # before filter
  202. def encrypt_password
  203. return if password.blank?
  204. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  205. self.crypted_password = encrypt(password)
  206. end
  207.  
  208. def password_required?
  209. crypted_password.blank? || !password.blank?
  210. end
  211.  
  212. end
Add Comment
Please, Sign In to add comment