module Polygallery::HasPolygallery::LocalInstanceMethods
Public Instance Methods
build_first_photos()
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 138 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
build_gallery_association(gallery_name=:gallery)
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 94 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
build_gallery_associations()
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 87 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_set()
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 155 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)
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 153 def first_photo(ga_name=nil); first_polyphoto(ga_name).photo end
first_polyphoto(ga_name=nil)
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 146 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?()
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 165 def galleries_built?; self.galleries_built.present? end
gallery_associations()
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 163 def gallery_associations self.class.polygalleries.map{|pg| send(pg) } end
has_polygallery?(gallery_title=nil)
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 82 def has_polygallery?(gallery_title=nil) return polygalleries.any? if gallery_title.nil? polygalleries.include?(gallery_title) end
polygalleries()
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 69 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_photos()
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 166 def prune_empty_photos gallery_associations.each(&:prune_empty_photos) end
remote_urls_for_polyphotos(style=nil)
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 169 def remote_urls_for_polyphotos(style=nil) gallery_associations.map{|ga| ga.remote_urls(style) }.flatten end
thumb_url()
click to toggle source
# File lib/polygallery/has_polygallery.rb, line 171 def thumb_url; first_polyphoto.thumb_url end