Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Event < ActiveRecord::Base
- TITLE_SIZE = 1..30
- DESCRIPTION_SIZE = 0..5000
- DEFAULT_DATE = Date.today
- DEFAULTS = {
- :date => Date.today,
- :time => Time.parse("8:30PM")
- }
- acts_as_commentable
- @@total_pages = 1
- belongs_to :category
- validates_presence_of :category
- belongs_to :author, :class_name => 'User'
- validates_presence_of :author
- has_many :attendance, :class_name => 'EventAttendant'
- has_many :attenders, :through => :attendance,
- :source => :user
- has_attached_file :image,
- :styles => {:original => "300x300>", :small => "100x100>"}
- attr_protected :image_file_name, :image_content_type, :image_size
- acts_as_taggable_on :tags
- validates_size_of :title, :in => TITLE_SIZE
- validates_size_of :description, :in => DESCRIPTION_SIZE
- validates_numericality_of :cost
- validates_presence_of :date
- validates_date :date
- validates_presence_of :time
- validates_time :time
- before_save :add_author_as_attendant
- def self.new_with_defaults
- Event.new DEFAULTS
- end
- def free?
- cost == nil || cost == 0
- end
- def self.search(category_id, search, page)
- if category_id
- return Category.find_by_id(category_id).events.paginate :page => page
- end
- if search
- return paginate :page => page, :conditions => ['title like ?', "%#{search}%"],
- :order => 'title'
- end
- paginate :page => page, :order => 'created_at DESC'
- end
- protected
- def add_author_as_attendant
- attendance << author
- end
- end
- ## User model
- require 'digest/sha2'
- class User < ActiveRecord::Base
- NAME_SIZE = 1..64
- EMAIL_SIZE = 1..230
- PASSWORD_SIZE = 4..20
- GENDERS = { :male => 'm', :female => 'f' }
- # Create two virtual (in memory only) attributes to hold the password and its
- # confirmation.
- attr_accessor :new_password, :new_password_confirmation
- # We need to validate that the user has typed the same password twice
- # but we only want to do the validation if they've opted to change
- # their password.
- validates_confirmation_of :new_password,
- :if => :password_changed_or_new_record?
- validates_presence_of :new_password,
- :if => :password_changed_or_new_record?
- validates_size_of :new_password, :in => PASSWORD_SIZE,
- :if => :password_changed_or_new_record?
- validates_presence_of :email
- validates_uniqueness_of :email
- validates_size_of :email, :in => EMAIL_SIZE
- validates_format_of :email,
- :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
- validates_presence_of :name
- validates_size_of :name, :in => NAME_SIZE
- validates_inclusion_of :gender, :in => %w{m f}
- validates_presence_of :birthday
- validates_date :birthday, :before => Date.today
- has_and_belongs_to_many :events
- has_attached_file :avatar, :styles => {:original => "300x300>"}
- before_save :hash_new_password, :if => :password_changed_or_new_record?
- # By default the form_helpers will set new_password to "",
- # we don't want to go saving this as a password
- def password_changed_or_new_record?
- !@new_password.blank? or new_record?
- end
- def hash_new_password
- # First reset the salt to a new random string. You could choose a
- # longer string here but for a salt, 8 bytes of randomness is probably
- # fine. Note this uses SecureRandom which will use your platform's secure
- # random number generator.
- self.salt = ActiveSupport::SecureRandom.base64(8)
- # Now calculate the hash of the password, with the salt prepended, store
- # store that in the database
- self.hashed_password = Digest::SHA2.hexdigest(self.salt + @new_password)
- end
- # As is the 'standard' with rails apps we'll return the user record if the
- # password is correct and nil if it isn't.
- def self.authenticate(email, password)
- # Because we salt the passwords we can't do this query in one part, first
- # we need to fetch the potential user
- if user = find_by_email(email)
- # Then compare the provided password against the hashed one in the db.
- if user.hashed_password == Digest::SHA2.hexdigest(user.salt + password)
- # If they match we return the user
- return user
- end
- end
- # If we get here it means either there's no user with that email, or
- # the wrong password was provided. But we don't want to let an attacker
- # know which.
- return nil
- end
- def self.email_exists?(email)
- exists? :email => email
- end
- end
- ### Event Attendant
- class EventAttendant < ActiveRecord::Base
- set_table_name "event_attendance"
- belongs_to :event
- belongs_to :user
- validates_uniqueness_of :id, :scope => [:user_id, :event_id]
- validates_presence_of :user
- validates_presence_of :event
- end
Add Comment
Please, Sign In to add comment