Class: Gallery

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

Create the Gallery and all import for all picture in this directory



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/gallery.rb', line 74

def self.create_by_import_gallery(gallery_import, parent = nil)
  gallery = Gallery.new_empty
  gallery.name = gallery_import.name
  gallery.parent = parent
  gallery.define_name(gallery_import.name)
  gallery.save!
  gallery.insert_pictures(gallery_import.path)
  gallery_import.child.each do |gallery_child|
    gallery.children << Gallery.create_by_import_gallery(gallery_child, gallery)
  end
  gallery.save
  gallery
end

.create_from_directory(directory) ⇒ Object

Create a Gallery with the name of a directory. All directory in this directory are gallery too but with parent like the first directory if the directory is not a directory, return an empty array if the directory is not a directory



63
64
65
66
67
68
69
70
# File 'app/models/gallery.rb', line 63

def self.create_from_directory(directory)
  gallery_import = Pictrails::ImportSystem.search(directory)
  unless gallery_import.nil?
    Gallery.create_by_import_gallery(gallery_import)
  else
    nil
  end
end

.find_without(gallerie) ⇒ Object

Retrieve all Gallery without the gallery send by parameter



165
166
167
# File 'app/models/gallery.rb', line 165

def self.find_without(gallerie)
  find :all, :conditions => ['id <> ?', gallerie.id]
end

.new_emptyObject

Create a Gallery with description empty and status true



51
52
53
54
55
56
# File 'app/models/gallery.rb', line 51

def self.new_empty
  gallery = Gallery.new
  gallery.description = ''
  gallery.status = true
  gallery
end

.without_parentObject

get all Galleries who haven’t parent_id



170
171
172
# File 'app/models/gallery.rb', line 170

def self.without_parent
  find :all, :conditions => ['parent_id IS ?', nil]
end

Instance Method Details

Change the permalink if it’s denied



98
99
100
101
102
103
104
105
# File 'app/models/gallery.rb', line 98

def change_permalink(permalink)
  base_permalink = permalink
  i = 1
  while ensure_permalink_is_not_a_route || permalink_already_use
    self.permalink = base_permalink + "-#{i}"
    i += 1
  end
end

#define_name(name) ⇒ Object

Change the name if it’s already use in database



89
90
91
92
93
94
95
# File 'app/models/gallery.rb', line 89

def define_name(name)
  i = 1
  while not self.valid?
    self.name = name + "-#{i}"
    i += 1
  end
end

define the permalink with name in downcase



17
18
19
20
21
22
# File 'app/models/gallery.rb', line 17

def define_permalink
  unless self.name.nil?
    self.permalink = self.name.downcase.gsub(/[^a-z0-9]+/i, '-')
    change_permalink(self.permalink)
  end
end

#diff_paginateObject

get the number of element with not in pagination



149
150
151
# File 'app/models/gallery.rb', line 149

def diff_paginate
  children.count(:conditions => ['status = ?', true]) % Setting.default.pictures_pagination.to_i 
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



156
157
158
159
160
161
# File 'app/models/gallery.rb', line 156

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

#insert_pictures(directory) ⇒ Object

Insert in this Gallery all picture in the import table. The import model is use only for the save of all file there are in this directory All file are integrate in this gallery time after time If the directory finish by / it’s delete



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/gallery.rb', line 123

def insert_pictures(directory)
  list_import = []
  Dir.chdir(directory) do
    Dir.glob("*.{gif,png,jpg,bmp}", File::FNM_CASEFOLD) do |file|
      i = Import.new
      directory.chop! if directory[-1,1] == '/'
      i.path = "#{directory}/#{file}"
      i.gallery = self
      list_import << i
    end

    # Save the import with the total size for this import
    list_import.each { |i|
      i.total = list_import.size
      i.save!
    }
  end if File.directory? directory
end

#nb_contentObject

Get the number of content the content are Sub-gallery and Pictures inside



144
145
146
# File 'app/models/gallery.rb', line 144

def nb_content
  children.size + pictures.size
end

Say if the permalink is already use or not. If the permalink is already use see if the permalink is not use by our own object



109
110
111
112
113
114
115
# File 'app/models/gallery.rb', line 109

def permalink_already_use
  if self.id.nil?
    !Gallery.find_by_permalink(self.permalink).nil?
  else
    !Gallery.find_by_permalink(self.permalink, :conditions => ['id <> ?', self.id]).nil?
  end
end

#public_filename(type) ⇒ Object

Return the public_filename of this gallery If no pictures, in this gallery, return of no_picture.png



36
37
38
39
40
41
42
# File 'app/models/gallery.rb', line 36

def public_filename(type)
  unless pictures.empty?
    pictures.first.public_filename(type)
  else
    '/no_picture.png'
  end
end

#tag_countsObject

Return all Tag of this gallery



45
46
47
# File 'app/models/gallery.rb', line 45

def tag_counts
  Picture.tag_counts :conditions => ['pictures.gallery_id = ?', id]
end

#titleObject

Alias for name of Gallery



30
31
32
# File 'app/models/gallery.rb', line 30

def title
  name
end

#to_paramObject

define the param for permalink



25
26
27
# File 'app/models/gallery.rb', line 25

def to_param
  permalink
end