Guest User

Untitled

a guest
Jul 29th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. class Event < ActiveRecord::Base
  2.  
  3. TITLE_SIZE = 1..30
  4.  
  5. DESCRIPTION_SIZE = 0..5000
  6.  
  7. DEFAULT_DATE = Date.today
  8.  
  9. DEFAULTS = {
  10. :date => Date.today,
  11. :time => Time.parse("8:30PM")
  12. }
  13.  
  14.  
  15. acts_as_commentable
  16.  
  17. @@total_pages = 1
  18.  
  19. belongs_to :category
  20.  
  21. validates_presence_of :category
  22.  
  23. belongs_to :author, :class_name => 'User'
  24.  
  25. validates_presence_of :author
  26.  
  27. has_many :attendance, :class_name => 'EventAttendant'
  28. has_many :attenders, :through => :attendance,
  29. :source => :user
  30.  
  31. has_attached_file :image,
  32. :styles => {:original => "300x300>", :small => "100x100>"}
  33.  
  34. attr_protected :image_file_name, :image_content_type, :image_size
  35.  
  36. acts_as_taggable_on :tags
  37.  
  38. validates_size_of :title, :in => TITLE_SIZE
  39.  
  40. validates_size_of :description, :in => DESCRIPTION_SIZE
  41.  
  42. validates_numericality_of :cost
  43. validates_presence_of :date
  44.  
  45. validates_date :date
  46.  
  47. validates_presence_of :time
  48.  
  49. validates_time :time
  50.  
  51. before_save :add_author_as_attendant
  52.  
  53. def self.new_with_defaults
  54. Event.new DEFAULTS
  55. end
  56.  
  57. def free?
  58. cost == nil || cost == 0
  59. end
  60.  
  61. def self.search(category_id, search, page)
  62. if category_id
  63. return Category.find_by_id(category_id).events.paginate :page => page
  64. end
  65. if search
  66. return paginate :page => page, :conditions => ['title like ?', "%#{search}%"],
  67. :order => 'title'
  68. end
  69. paginate :page => page, :order => 'created_at DESC'
  70. end
  71.  
  72. protected
  73. def add_author_as_attendant
  74. attendance << author
  75. end
  76.  
  77. end
  78.  
  79. ## User model
  80. require 'digest/sha2'
  81.  
  82. class User < ActiveRecord::Base
  83.  
  84. NAME_SIZE = 1..64
  85.  
  86. EMAIL_SIZE = 1..230
  87.  
  88. PASSWORD_SIZE = 4..20
  89.  
  90. GENDERS = { :male => 'm', :female => 'f' }
  91.  
  92. # Create two virtual (in memory only) attributes to hold the password and its
  93. # confirmation.
  94. attr_accessor :new_password, :new_password_confirmation
  95. # We need to validate that the user has typed the same password twice
  96. # but we only want to do the validation if they've opted to change
  97. # their password.
  98. validates_confirmation_of :new_password,
  99. :if => :password_changed_or_new_record?
  100.  
  101. validates_presence_of :new_password,
  102. :if => :password_changed_or_new_record?
  103.  
  104. validates_size_of :new_password, :in => PASSWORD_SIZE,
  105. :if => :password_changed_or_new_record?
  106.  
  107. validates_presence_of :email
  108.  
  109. validates_uniqueness_of :email
  110.  
  111. validates_size_of :email, :in => EMAIL_SIZE
  112.  
  113. validates_format_of :email,
  114. :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  115.  
  116. validates_presence_of :name
  117.  
  118. validates_size_of :name, :in => NAME_SIZE
  119.  
  120. validates_inclusion_of :gender, :in => %w{m f}
  121. validates_presence_of :birthday
  122.  
  123. validates_date :birthday, :before => Date.today
  124.  
  125. has_and_belongs_to_many :events
  126.  
  127. has_attached_file :avatar, :styles => {:original => "300x300>"}
  128.  
  129. before_save :hash_new_password, :if => :password_changed_or_new_record?
  130.  
  131. # By default the form_helpers will set new_password to "",
  132. # we don't want to go saving this as a password
  133. def password_changed_or_new_record?
  134. !@new_password.blank? or new_record?
  135. end
  136.  
  137. def hash_new_password
  138. # First reset the salt to a new random string. You could choose a
  139. # longer string here but for a salt, 8 bytes of randomness is probably
  140. # fine. Note this uses SecureRandom which will use your platform's secure
  141. # random number generator.
  142. self.salt = ActiveSupport::SecureRandom.base64(8)
  143. # Now calculate the hash of the password, with the salt prepended, store
  144. # store that in the database
  145. self.hashed_password = Digest::SHA2.hexdigest(self.salt + @new_password)
  146. end
  147.  
  148. # As is the 'standard' with rails apps we'll return the user record if the
  149. # password is correct and nil if it isn't.
  150. def self.authenticate(email, password)
  151. # Because we salt the passwords we can't do this query in one part, first
  152. # we need to fetch the potential user
  153. if user = find_by_email(email)
  154. # Then compare the provided password against the hashed one in the db.
  155. if user.hashed_password == Digest::SHA2.hexdigest(user.salt + password)
  156. # If they match we return the user
  157. return user
  158. end
  159. end
  160. # If we get here it means either there's no user with that email, or
  161. # the wrong password was provided. But we don't want to let an attacker
  162. # know which.
  163. return nil
  164. end
  165.  
  166. def self.email_exists?(email)
  167. exists? :email => email
  168. end
  169.  
  170. end
  171.  
  172. ### Event Attendant
  173. class EventAttendant < ActiveRecord::Base
  174. set_table_name "event_attendance"
  175.  
  176. belongs_to :event
  177. belongs_to :user
  178.  
  179. validates_uniqueness_of :id, :scope => [:user_id, :event_id]
  180.  
  181. validates_presence_of :user
  182.  
  183. validates_presence_of :event
  184. end
Add Comment
Please, Sign In to add comment