Class: PhotoCollection

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/models/photo_collection.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_title_cardObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/models/photo_collection.rb', line 44

def self.add_title_card
  if File.exists?(PhotoCollection.title_card_path)
    truncated_title_card_path = title_card_path[PhotoFolder.source_directory_location.length + 1,title_card_path.length]
    title_card = Photo.find_by_photo_collection_id_and_path(0,truncated_title_card_path) || Photo.create(
      :photo_collection_id => 0,
      :path => truncated_title_card_path
    )
    title_card.update_attribute :caption, Maruku.new(File.read(title_card_path)).to_html
  else
    false
  end
end

.cascade_meta_dataObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/models/photo_collection.rb', line 57

def self.
  PhotoCollection.find(:all).each do |collection|
    if collection.meta
      collection.descendants.each do |descendant|
        if(descendant.meta)
            collection_meta_as_object = JSON.parse(collection.meta)
            descendant_meta_as_object = JSON.parse(descendant.meta)
            merged_meta = collection_meta_as_object.merge(descendant_meta_as_object)
            descendant.update_attribute :meta, merged_meta.to_json
        else
          descendant.update_attribute :meta, collection.meta
        end
      end
    end
  end
end

.positionsObject



31
32
33
34
35
36
37
38
# File 'lib/models/photo_collection.rb', line 31

def self.positions
  return @positions if !@positions.nil?
  if File.exists?(self.positions_file_path)
    @positions = YAML.load(File.read(self.positions_file_path))
  else
    nil
  end
end

.positions_file_pathObject



27
28
29
# File 'lib/models/photo_collection.rb', line 27

def self.positions_file_path
  File.join(PhotoFolder.source_directory_location,'positions.yml')
end

.set_correct_positions(collections, is_callback = false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/models/photo_collection.rb', line 8

def self.set_correct_positions(collections,is_callback = false)
  collections.sort_by(&:name).each_with_index do |collection,i|
    collection.update_attribute :position, i
    set_correct_positions(PhotoCollection.find_all_by_parent_id(collection.id),true)
  end
  if self.positions && !is_callback
    self.positions.each_with_index do |file_name,i|
      photo_collection = PhotoCollection.find_by_path(file_name,{
        :conditions => {
          :parent_id => 0
        }
      })
      if photo_collection
        photo_collection.insert_at i + 1
      end
    end
  end
end

.title_card_pathObject



40
41
42
# File 'lib/models/photo_collection.rb', line 40

def self.title_card_path
  File.join(PhotoFolder.source_directory_location,'index.markdown')
end

Instance Method Details

#add_title_cardObject



130
131
132
133
134
135
136
137
138
139
# File 'lib/models/photo_collection.rb', line 130

def add_title_card
  if File.exists?(title_card_path)
    truncated_title_card_path = title_card_path[PhotoFolder.source_directory_location.length + 1,title_card_path.length]
    title_card = photos.find_by_path(truncated_title_card_path) || photos.create(:path => truncated_title_card_path)
    title_card.update_attribute :caption, Maruku.new(File.read(title_card_path)).to_html
    title_card.move_to_top
  else
    false
  end
end

#descendantsObject



74
75
76
# File 'lib/models/photo_collection.rb', line 74

def descendants
  children.empty? ? [] : children.map(&:descendants).flatten + children
end

#full_pathObject



99
100
101
# File 'lib/models/photo_collection.rb', line 99

def full_path
  File.join(PhotoFolder.source_directory_location,path)
end

#meta_file_pathObject



116
117
118
# File 'lib/models/photo_collection.rb', line 116

def meta_file_path
  File.join(full_path,'meta.json')
end

#parent_from_pathObject



95
96
97
# File 'lib/models/photo_collection.rb', line 95

def parent_from_path
  self.path.count('/') > 0 ? PhotoCollection.find_by_path(self.path.gsub(/\/[^\/]+$/,'')) : false 
end

#positionsObject



107
108
109
110
111
112
113
114
# File 'lib/models/photo_collection.rb', line 107

def positions
  return @positions if !@positions.nil?
  if File.exists?(positions_file_path)
    @positions = YAML.load(File.read(positions_file_path))
  else
    nil
  end
end

#positions_file_pathObject



103
104
105
# File 'lib/models/photo_collection.rb', line 103

def positions_file_path
  File.join(full_path,'positions.yml')
end

#set_children_parent_idObject



78
79
80
81
82
83
84
85
# File 'lib/models/photo_collection.rb', line 78

def set_children_parent_id
  PhotoFolder::all_paths_in_database.select{|path| self.path.underscore.gsub(/\s/,'_') == path.gsub(/\/[^\/]+$/,'').underscore.gsub(/\s/,'_')}.each do |path|
    Photo.update_all("photo_collection_id = #{self.id}","path = '#{path}'")
  end
  photos.each_with_index do |photo,i|
    photo.insert_at(i + 1)
  end
end

#set_correct_positionsObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/models/photo_collection.rb', line 141

def set_correct_positions
  positions.each_with_index do |file_name,i|
    photo = photos.find_by_path("#{path}/#{file_name}")
    photo_collection = children.find_by_path("#{path}/#{file_name}")
    if photo
      photo.insert_at i + 1
    elsif photo_collection
      photo_collection.insert_at i + 1
    end
  end
end

#set_metaObject



120
121
122
123
124
# File 'lib/models/photo_collection.rb', line 120

def set_meta
   = File.exists?(meta_file_path) ? File.read(meta_file_path) : nil
  update_attribute :meta, 
  
end

#set_nameObject



91
92
93
# File 'lib/models/photo_collection.rb', line 91

def set_name
  self.name = path.split('/').pop.gsub(/[\_\-]+/,' ').split(/\s+/).each(&:capitalize!).join(' ')
end

#set_newest_dateObject



157
158
159
# File 'lib/models/photo_collection.rb', line 157

def set_newest_date
  update_attribute :newest_date, photos.collect(&:date).reject(&:nil?).max || 0
end

#set_oldest_dateObject



153
154
155
# File 'lib/models/photo_collection.rb', line 153

def set_oldest_date
  update_attribute :oldest_date, photos.collect(&:date).reject(&:nil?).min || 0
end

#set_parent_idObject



87
88
89
# File 'lib/models/photo_collection.rb', line 87

def set_parent_id
  update_attribute :parent_id, parent_from_path ? parent_from_path.id : 0
end

#title_card_pathObject



126
127
128
# File 'lib/models/photo_collection.rb', line 126

def title_card_path
  File.join(full_path,'index.markdown')
end