module Polygallery::HasPolyphotos::LocalInstanceMethods

Public Instance Methods

build_first_photo() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 76
def build_first_photo
  photo_association = self.polygallery_photos
  photo_association.build(
    :polygallery_options => self.polygallery_settings,
    :galleryable => self.galleryable,
    :gallery_title => self.title ) unless photo_association.any?
end
build_polygallery_photos() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 111
def build_polygallery_photos
  # Convert attributes to array if in hash form
  # self.photos_attributes =
  #   self.photos_attributes.map{|_, v| v
  #     } if self.photos_attributes.is_a? Hash

  # Initialize a duplicate
  attrs_for_photos = if self.photos_attributes.is_a? Hash
    self.photos_attributes.values
  else
    ( self.photos_attributes || [] ).dup
  end
  return unless attrs_for_photos.any?
  return if self.photos_built?

  attrs_for_photos.each_with_index do |attrs_for_photo, index|
    attrs_for_photo[:polygallery_options] = self.polygallery_settings
    should_destroy = %w(true 1).include? attrs_for_photo.delete(:_destroy)
    if attrs_for_photo.key? :id
      existing_id = attrs_for_photo.delete(:id).to_i
      existing_photo = self.polygallery_photos_class.find existing_id
      if should_destroy
        # puts 'Destroying a photo'
        existing_photo.destroy
      else
        # puts 'Updating an existing photo'
        existing_photo.update_attributes attrs_for_photo end
    else
      # puts 'Building a new photo'
      self.polygallery_photos.build attrs_for_photo
    end
  end
  self.photos_built = true
end
first_photo() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 160
def first_photo
  # TODO: some kind of selection
  photos.first || Photo.new
end
include_polygallery_settings(settings) click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 66
def include_polygallery_settings(settings)
  self.polygallery_options = settings
  self.initialize_polygallery
end
initialize_polygallery() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 71
def initialize_polygallery
  settings = self.polygallery_settings
  self.class.init_associations(settings)
end
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/polygallery/has_polyphotos.rb, line 165
def method_missing(m, *args, &block)
  if m == :photos
    self.initialize_polygallery
    return send(m, *args, &block) if self.respond_to? m
    Photo.where('1 = 0')
  else super end
end
photos_built?() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 146
def photos_built?; self.photos_built.present? end
polygallery_photos() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 93
def polygallery_photos; photos end
polygallery_photos_attributes() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 94
def polygallery_photos_attributes; self.photos_attributes end
polygallery_photos_attributes=(v) click to toggle source

self.send :“#{polygallery_photos_name.to_s}_attributes” end

# File lib/polygallery/has_polyphotos.rb, line 96
def polygallery_photos_attributes=(v); self.photos_attributes = v end
polygallery_photos_class() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 100
def polygallery_photos_class
  Object.const_get polygallery_photos_classname end
polygallery_photos_classname() click to toggle source

self.send :“#{polygallery_photos_name.to_s}_attributes=”, v end

# File lib/polygallery/has_polyphotos.rb, line 98
def polygallery_photos_classname
  self.polygallery_settings[:associations][:photos][:class_name] end
polygallery_photos_name() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 91
def polygallery_photos_name
  self.polygallery_settings[:association_names][:photos] end
polygallery_settings() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 84
def polygallery_settings
  return self.polygallery_options if self.polygallery_options.present?
  return Kernel.const_get(self.galleryable_type)
           .send(:"#{self.title}_settings") if galleryable_type.present?
  self.class.polygallery_settings
end
prune_empty_photos() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 148
def prune_empty_photos
  # puts "#{title} is pruning empty photos..."
  polygallery_photos.each {|pp|
    if pp.new_record? && !pp.photo.file? && pp.photo_to_upload.nil?
      # puts 'Marking photo for destruction'
      pp.mark_for_destruction
      polygallery_photos.delete pp
    end }
end
remote_urls(style=nil) click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 158
def remote_urls(style=nil); photos.map{|p| p.remote_url style } || [] end
set_nest(photo) click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 56
def set_nest(photo)
  photo.galleryable_id ||= galleryable_id
  photo.galleryable_type ||= galleryable_type
  photo.galleryable ||= galleryable
  photo.gallery_title ||= title
  photo.polygallery_options ||= self.polygallery_settings
  photo.initialize_polyphoto
  photo.send(:"#{title}=", self) if photo.send(:"#{title}").nil?
end
thumb_url() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 159
def thumb_url; first_photo.thumb_url end