Guest User

Untitled

a guest
Jun 13th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2. acts_as_authentic
  3. has_many :residentships, :foreign_key => "resident_id"
  4.  
  5. has_one :current_residentship,
  6. :class_name => "Residentship",
  7. :conditions => "moved_out_at IS NULL",
  8. :foreign_key => "resident_id"
  9.  
  10. has_many :former_residentships,
  11. :class_name => "Residentship",
  12. :conditions => "moved_out_at IS NOT NULL",
  13. :foreign_key => "resident_id"
  14.  
  15. named_scope :only_current,
  16. :include => :residentships,
  17. :conditions => "residentships.moved_out_at IS NULL"
  18.  
  19. has_attached_file :avatar,
  20. :styles => {
  21. :large => "192x192>",
  22. :medium => "64x64>",
  23. :thumb => "50x50>" },
  24. :default_url => "/avatars/thumb/missing.gif"
  25.  
  26. validates_presence_of :first_name
  27. validates_presence_of :last_name
  28. validates_presence_of :room_number
  29. validates_presence_of :moved_in_at
  30.  
  31. delegate :hallway, :to => :room
  32. delegate :room, :to => :current_residentship, :allow_nil => true
  33.  
  34. attr_accessor :avatar_width, :avatar_height, :avatar_x, :avatar_y
  35.  
  36. attr_writer :room_number, :moved_in_at
  37.  
  38. before_save :crop_avatar
  39. after_create :create_residentship
  40.  
  41. def full_name
  42. "#{first_name} #{last_name}"
  43. end
  44.  
  45. def room_number
  46. @room_number || (current_residentship && current_residentship.room_number)
  47. end
  48.  
  49. def moved_out_at
  50. unless former_residentships.empty? or current_residentship
  51. former_residentships.first.moved_out_at
  52. end
  53. end
  54.  
  55. def moved_in_at
  56. @moved_in_at || (current_residentship && current_residentship.moved_in_at)
  57. end
  58.  
  59. def moved_out_at=(date)
  60. current_residentship.update_attribute(:moved_out_at, date)
  61. end
  62.  
  63. def display_name
  64. full_name + (room ? " (#{room.number})" : "")
  65. end
  66.  
  67. protected
  68.  
  69. def crop_avatar
  70. if !avatar.dirty? && avatar_width
  71. image = MiniMagick::Image.from_file(avatar.path(:large))
  72. image.crop("#{avatar_width}x#{avatar_height}+#{avatar_x}+#{avatar_y}")
  73. image.write(avatar.path)
  74. self.avatar.reprocess!
  75. end
  76. end
  77.  
  78. def create_residentship
  79. Residentship.create(:resident => self, :room_number => @room_number, :moved_in_at => @moved_in_at)
  80. end
  81. end
Add Comment
Please, Sign In to add comment