This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 316
/
photo.rb
executable file
·85 lines (69 loc) · 3.45 KB
/
photo.rb
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Photo < ActiveRecord::Base
acts_as_commentable
belongs_to :album
has_attachment prepare_options_for_attachment_fu(configatron.photo.attachment_fu_options.to_hash)
# attr_accessor :cropped_size
# before_thumbnail_saved do |thumbnail|
# raise thumbnail.inspect
# # thumbnail.send(:'attributes=', {:thumbnail_resize_options => cropped_size}, false) if thumbnail.parent.cropped_size
# if thumbnail.parent.cropped_size[:x1]
# # img = Magick::Image::read(@photo.public_filename).first
# thumbnail.crop!(::Magick::CenterGravity, parent.cropped_size[:x1].to_i, parent.cropped_size[:y1].to_i, parent.cropped_size[:width].to_i, parent.cropped_size[:height].to_i, true)
# raise thumbnail.inspect
# # size = configatron.photo['attachment_fu_options']['thumbnails']['medium']
# # dimensions = size[1..size.size].split("x")
# # img.crop_resized!(dimensions[0].to_i, dimensions[1].to_i)
# # img.write @settings.header_image_file
# end
# end
acts_as_taggable
acts_as_activity :user, :if => Proc.new{|record| record.parent.nil? && record.album_id.nil?}
validates_presence_of :size
validates_presence_of :content_type
validates_presence_of :filename
validates_presence_of :user, :if => Proc.new{|record| record.parent.nil? }
validates_inclusion_of :content_type, :in => attachment_options[:content_type], :message => "is not allowed", :allow_nil => true
validates_inclusion_of :size, :in => attachment_options[:size], :message => " is too large", :allow_nil => true
belongs_to :user
has_one :user_as_avatar, :class_name => "User", :foreign_key => "avatar_id"
#Named scopes
scope :recent, :order => "photos.created_at DESC", :conditions => ["photos.parent_id IS NULL"]
scope :new_this_week, :order => "photos.created_at DESC", :conditions => ["photos.created_at > ? AND photos.parent_id IS NULL", 7.days.ago.to_s(:db)]
scope :tagged_with, lambda {|tag_name|
{:conditions => ["tags.name = ?", tag_name], :include => :tags}
}
attr_accessible :name, :description
def display_name
(self.name && self.name.length>0) ? self.name : "#{:created_at.l.downcase}: #{I18n.l(self.created_at, :format => :published_date)}"
end
def description_for_rss
"<a href='#{self.link_for_rss}' title='#{self.name}'><img src='#{self.public_filename(:large)}' alt='#{self.name}' /><br />#{self.description}</a>"
end
def owner
self.user
end
def previous_photo
self.user.photos.find(:first, :conditions => ['created_at < ?', created_at], :order => 'created_at DESC')
end
def next_photo
self.user.photos.find(:first, :conditions => ['created_at > ?', created_at], :order => 'created_at ASC')
end
def previous_in_album
return nil unless self.album
self.user.photos.find(:first, :conditions => ['created_at < ? and album_id = ?', created_at, self.album.id], :order => 'created_at DESC')
end
def next_in_album
return nil unless self.album
self.user.photos.find(:first, :conditions => ['created_at > ? and album_id = ?', created_at, self.album_id], :order => 'created_at ASC')
end
def self.find_recent(options = {:limit => 3})
self.new_this_week.find(:all, :limit => options[:limit])
end
def self.find_related_to(photo, options = {})
merged_options = options.merge({:limit => 8,
:order => 'created_at DESC',
:conditions => ['photos.id != ?', photo.id]
})
photo = find_tagged_with(photo.tags.collect{|t| t.name }, merged_options).uniq
end
end