Module: Polygallery::HasPolygallery::LocalInstanceMethods

Defined in:
lib/polygallery/has_polygallery.rb

Instance Method Summary collapse

Instance Method Details

#build_first_photosObject



177
178
179
180
181
182
183
# File 'lib/polygallery/has_polygallery.rb', line 177

def build_first_photos
  return unless self.class.has_polygallery?
  # self.class.polygalleries.each{|pg|
  #   build_gallery_association pg
  #   send(pg.to_sym).build_first_photo }
  build_gallery_associations.map(&:build_first_photo)
end


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/polygallery/has_polygallery.rb', line 133

def build_gallery_association(gallery_name=:gallery)
  gallery_association = send gallery_name
  attrs_for_this_gallery = send(:"#{gallery_name}_attributes") || {}
  if gallery_association.present?
    # if attrs_for_this_gallery.empty?
    #   gallery_association.photos.each do |p|
    #     if p.photo_to_upload.present?
    #       p.photo = File.new p.photo_to_upload
    #       p.photo_to_upload = nil
    #     end
    #     relevant_changes = p.changed -
    #       %w(galleryable_type galleryable_id gallery_title)
    #     if relevant_changes.any?
    #       attrs_for_this_photo = Hash[*relevant_changes.map{|column|
    #         [ column, p.send(column) ] }.flatten]
    #       attrs_for_this_gallery[:photos_attributes] ||= {}
    #       attrs_for_this_gallery[:photos_attributes][(p.id ||
    #         attrs_for_this_gallery[:photos_attributes].length).to_s] = attrs_for_this_photo
    #     end
    #   end
    #   # return gallery_association
    # end
    if attrs_for_this_gallery.empty?
      # puts 'Returning due to empty attrs!'
      return gallery_association 
    end
    if gallery_association.persisted?
      # puts 'Updating an existing gallery!'
      gallery_association.update_attributes attrs_for_this_gallery
    else
      attrs_for_this_gallery.each{|k, v|
        gallery_association.send :"#{k}=", v }
    end
    return gallery_association
  end
  settings_for_this_gallery = send :"#{gallery_name}_settings"
  attrs_for_this_gallery[:polygallery_options] = settings_for_this_gallery
  # puts "Building a new gallery: #{gallery_name}"
  built_gallery = send :"build_#{gallery_name}", attrs_for_this_gallery
  gallery_association = send :"#{gallery_name}=", built_gallery
  gallery_association.galleryable ||= self
  gallery_association
end


126
127
128
129
130
131
132
# File 'lib/polygallery/has_polygallery.rb', line 126

def build_gallery_associations
  return [] unless self.class.has_polygallery?
  gallery_associations = self.class.polygalleries.map{|pg|
    self.build_gallery_association pg }
  self.galleries_built = true
  gallery_associations
end

#ensure_galleryable_setObject



194
195
196
197
198
199
200
# File 'lib/polygallery/has_polygallery.rb', line 194

def ensure_galleryable_set
  gallery_associations.each do |ga|
    ga.galleryable ||= self
    ga.polygallery_photos.each{|pp|
      pp.galleryable ||= self }
  end
end

#first_photo(ga_name = nil) ⇒ Object



192
# File 'lib/polygallery/has_polygallery.rb', line 192

def first_photo(ga_name=nil); first_polyphoto(ga_name).photo end

#first_polyphoto(ga_name = nil) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/polygallery/has_polygallery.rb', line 185

def first_polyphoto(ga_name=nil)
  return Photo.new unless gallery_associations.any?
  ga = if ga_name.present? then send(ga_name)
       else gallery_associations.to_a.find{|pg| pg.photos.any? } end
  return Photo.new if ga.nil?
  ga.first_photo
end

#galleries_built?Boolean

Returns:

  • (Boolean)


204
# File 'lib/polygallery/has_polygallery.rb', line 204

def galleries_built?; self.galleries_built.present? end


202
203
# File 'lib/polygallery/has_polygallery.rb', line 202

def gallery_associations
self.class.polygalleries.map{|pg| send(pg) } end

#has_polygallery?(gallery_title = nil) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/polygallery/has_polygallery.rb', line 121

def has_polygallery?(gallery_title=nil)
  return polygalleries.any? if gallery_title.nil?
  polygalleries.include?(gallery_title)
end

#polygalleriesObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/polygallery/has_polygallery.rb', line 108

def polygalleries
  self.class.polygalleries.each do |pg_name|
    gallery_opts = self.send(:"#{pg_name}_settings")
    gallery_classname = gallery_opts[:associations][:gallery][:class_name]
    gallery_class = Object.const_get gallery_classname
    table_name = gallery_class.table_name
    gallery_class.select("#{table_name}.title")
      .where(:galleryable => self)
      .group("#{table_name}.title")
      .map(&:title)
  end
end

#prune_empty_photosObject



205
206
# File 'lib/polygallery/has_polygallery.rb', line 205

def prune_empty_photos
gallery_associations.each(&:prune_empty_photos) end

#remote_urls_for_polyphotos(style = nil) ⇒ Object



208
209
# File 'lib/polygallery/has_polygallery.rb', line 208

def remote_urls_for_polyphotos(style=nil)
gallery_associations.map{|ga| ga.remote_urls(style) }.flatten end

#thumb_urlObject



210
# File 'lib/polygallery/has_polygallery.rb', line 210

def thumb_url; first_polyphoto.thumb_url end