Class: Picture

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/picture.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_picture_by_import(import) ⇒ Object

A picture model is create by an import model In this import model, the Gallery_id and path is usefull All information enough.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/picture.rb', line 50

def self.create_picture_by_import(import)
  pic = Picture.new
  filename = import.path.gsub(File.dirname(import.path), '')
  pic.title = filename[1,(filename.rindex(/\./)-1)]
  pic.description = ''
  pic.status = true
  pic.content_type = File.mime_type? import.path
  pic.filename = import.path
  pic.temp_data = File.new(import.path).read
  pic.gallery_id = import.gallery.id
  pic.save!
  pic
end

Instance Method Details

Define the permalink. Test if this permalink is already use. if it already use add a -#index after



31
32
33
34
35
36
37
38
39
40
# File 'app/models/picture.rb', line 31

def define_permalink
  self.permalink = title.downcase.gsub(/[^a-z0-9]+/i, '-')
  permalink_insert = self.permalink
  i = 1
  while !Picture.find_by_permalink(permalink_insert).nil? || ensure_permalink_is_not_a_route(permalink_insert) do
    permalink_insert = "#{self.permalink}-#{i}"
    i = i + 1
  end
  self.permalink = permalink_insert
end

Check if the permalink is possible for the URL /galleries/#permalink a permalinks static variable is use for performance, because the collect of routing is longer It the permalink is a route return true



68
69
70
71
72
73
# File 'app/models/picture.rb', line 68

def ensure_permalink_is_not_a_route(permalink_test)
  @@permalinks ||= ActionController::Routing::Routes.routes.collect {|r|
    r.generation_structure.match(/"\/pictures\/([\w]+)/)[1] rescue nil
  }.uniq.compact
  @@permalinks.include?(permalink_test)
end

#nextObject



100
101
102
103
# File 'app/models/picture.rb', line 100

def next
  Picture.find(:first, :conditions => ['gallery_id = ? AND created_at >= ? AND id > ?', gallery.id, created_at, id],
                :order => 'created_at ASC, id ASC')
end

#old_tagObject

Return old tag save if there are difference with the tag_list work after save too



78
79
80
81
# File 'app/models/picture.rb', line 78

def old_tag
  return [] unless @tag_list
  tags.reject { |tag| @tag_list.include?(tag.name) }
end

#previousObject



95
96
97
98
# File 'app/models/picture.rb', line 95

def previous
  Picture.find(:first, :conditions => ['gallery_id = ? AND created_at <= ? AND id < ?', gallery.id, created_at, id],
                :order => 'created_at DESC, id DESC')
end

#to_paramObject



42
43
44
# File 'app/models/picture.rb', line 42

def to_param
  permalink
end

#update_thumbObject



83
84
85
86
87
# File 'app/models/picture.rb', line 83

def update_thumb
  tmp = self.create_temp_file
  self.create_or_update_thumbnail(tmp, 'thumb', self.attachment_options[:thumbnails][:thumb])
  tmp.delete
end

#update_viewObject



89
90
91
92
93
# File 'app/models/picture.rb', line 89

def update_view
  tmp = self.create_temp_file
  self.create_or_update_thumbnail(tmp, 'view', self.attachment_options[:thumbnails][:view])
  tmp.delete
end