Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. ## the problem is with the Imt <> Lesson relationship.
  2. ## When asking for lesson.imt it errors out looking for imt_id in the table ( which doesn't exist as it's users id )
  3. ## So should I be using class_name, table_name, set_table_name or finder_sql or what.. ?
  4. ## and, it's possible to have different validations and assoications subclassed like this?
  5. ## e.g. all user types ( Student, Imt, etc ) validates_presence_of :first_name. But only Imt has_many :lessons..
  6.  
  7.  
  8. ## lesson.rb :
  9. class Lesson < ActiveRecord::Base
  10. validates_presence_of :instrument, :name, :cost, :status
  11. belongs_to :imt, :class_name => "User"
  12. has_many :bookings
  13. has_many :enrolments
  14. end
  15. ## user.rb
  16. class User < ActiveRecord::Base
  17. require 'digest/sha2'
  18. #validates_uniqueness_of :username
  19. attr_accessor :skip_password
  20. attr_accessor :tmp_password
  21. validates_confirmation_of :password, :on => :create #:if => :password_required?
  22. validates_length_of :password, :within => 5..40, :on => :create #:if => :password_required?
  23. validates_presence_of :password, :password_confirmation, :on => :create #:if => :password_required?
  24. validates_presence_of :first_name, :last_name
  25. def name
  26. first_name + " " + last_name
  27. end
  28. def self.getuser(id)
  29. find(id)
  30. end
  31. def self.authenticate(username, password)
  32. user = User.find(:first, :conditions => ['username = ?', username])
  33. if user.blank? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
  34. raise "Username or password invalid"
  35. end
  36. user
  37. end
  38. def password=(pass)
  39. self.tmp_password = pass
  40. unless pass.nil?
  41. salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
  42. self.password_salt, self.password_hash =
  43. salt, Digest::SHA256.hexdigest(pass + salt)
  44.  
  45. end
  46. end
  47. def password
  48. self.tmp_password
  49. end
  50. protected
  51. def password_required?
  52. @skip_password != true
  53. end
  54.  
  55. end
  56.  
  57. class Imt < User
  58. has_many :lessons
  59. end
  60.  
  61. class Student < User
  62. validates_presence_of :form_class, :student_id
  63. validates_uniqueness_of :student_id
  64. has_many :enrolments, :dependent => :destroy
  65. has_many :bookings, :dependent => :destroy
  66. def before_validation
  67. self.username = self.student_id
  68. end
  69. end
  70.  
  71. class Teacher < User
  72. end
  73.  
  74. class Admin < User
  75. end
Add Comment
Please, Sign In to add comment