We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
insoshi / app / models / photo.rb
100644 65 lines (57 sloc) 2.011 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# == Schema Information
# Schema version: 22
#
# Table name: photos
#
# id :integer(11) not null, primary key
# person_id :integer(11)
# parent_id :integer(11)
# content_type :string(255)
# filename :string(255)
# thumbnail :string(255)
# size :integer(11)
# width :integer(11)
# height :integer(11)
# primary :boolean(1)
# created_at :datetime
# updated_at :datetime
#
 
class Photo < ActiveRecord::Base
  include ActivityLogger
  UPLOAD_LIMIT = 5 # megabytes
  
  belongs_to :person
  has_attachment :content_type => :image,
                 :storage => :file_system,
                 :max_size => UPLOAD_LIMIT.megabytes,
                 :min_size => 1,
                 :resize_to => '240>',
                 :thumbnails => { :thumbnail => '72>',
                                  :icon => '36>',
                                  :bounded_icon => '36x36>' }
  
  has_many :activities, :foreign_key => "item_id", :dependent => :destroy
    
  after_save :log_activity
                 
  # Override the crappy default AttachmentFu error messages.
  def validate
    if filename.nil?
      errors.add_to_base("You must choose a file to upload")
    else
      # Images should only be GIF, JPEG, or PNG
      enum = attachment_options[:content_type]
      unless enum.nil? || enum.include?(send(:content_type))
        errors.add_to_base("You can only upload images (GIF, JPEG, or PNG)")
      end
      # Images should be less than UPLOAD_LIMIT MB.
      enum = attachment_options[:size]
      unless enum.nil? || enum.include?(send(:size))
        msg = "Images should be smaller than #{UPLOAD_LIMIT} MB"
        errors.add_to_base(msg)
      end
    end
  end
  
  def log_activity
    if self.primary?
      activity = Activity.create!(:item => self, :person => self.person)
      add_activities(:activity => activity, :person => self.person)
    end
  end
 
end