Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < ActiveRecord::Base
- acts_as_authentic
- has_many :residentships, :foreign_key => "resident_id"
- has_one :current_residentship,
- :class_name => "Residentship",
- :conditions => "moved_out_at IS NULL",
- :foreign_key => "resident_id"
- has_many :former_residentships,
- :class_name => "Residentship",
- :conditions => "moved_out_at IS NOT NULL",
- :foreign_key => "resident_id"
- named_scope :only_current,
- :include => :residentships,
- :conditions => "residentships.moved_out_at IS NULL"
- has_attached_file :avatar,
- :styles => {
- :large => "192x192>",
- :medium => "64x64>",
- :thumb => "50x50>" },
- :default_url => "/avatars/thumb/missing.gif"
- validates_presence_of :first_name
- validates_presence_of :last_name
- validates_presence_of :room_number
- validates_presence_of :moved_in_at
- delegate :hallway, :to => :room
- delegate :room, :to => :current_residentship, :allow_nil => true
- attr_accessor :avatar_width, :avatar_height, :avatar_x, :avatar_y
- attr_writer :room_number, :moved_in_at
- before_save :crop_avatar
- after_create :create_residentship
- def full_name
- "#{first_name} #{last_name}"
- end
- def room_number
- @room_number || (current_residentship && current_residentship.room_number)
- end
- def moved_out_at
- unless former_residentships.empty? or current_residentship
- former_residentships.first.moved_out_at
- end
- end
- def moved_in_at
- @moved_in_at || (current_residentship && current_residentship.moved_in_at)
- end
- def moved_out_at=(date)
- current_residentship.update_attribute(:moved_out_at, date)
- end
- def display_name
- full_name + (room ? " (#{room.number})" : "")
- end
- protected
- def crop_avatar
- if !avatar.dirty? && avatar_width
- image = MiniMagick::Image.from_file(avatar.path(:large))
- image.crop("#{avatar_width}x#{avatar_height}+#{avatar_x}+#{avatar_y}")
- image.write(avatar.path)
- self.avatar.reprocess!
- end
- end
- def create_residentship
- Residentship.create(:resident => self, :room_number => @room_number, :moved_in_at => @moved_in_at)
- end
- end
Add Comment
Please, Sign In to add comment