module Polygallery::ActsAsPolyphoto::ClassMethods

Public Instance Methods

acts_as_polyphoto(options={}) click to toggle source
# File lib/polygallery/acts_as_polyphoto.rb, line 7
def acts_as_polyphoto(options={})
  defaults = self.polygallery_settings
  settings = defaults.deep_merge(options)

  attr_accessor :photo_to_upload, :polygallery_options,
                :polyphoto_initialized
  attr_reader   :photo_remote_url
  
  init_attachment settings[:paperclip]
  init_associations settings

  before_validation do
    # self.polygallery_options = nil
    # self.polygallery_options = self.polygallery_settings
    # self.class.init_attachment self.polygallery_options
    if gallery.present?
      self.gallery_title = gallery.title if gallery_title.blank?
      self.galleryable ||= gallery.galleryable
      initialize_polyphoto
    end
    process_photo_to_upload
  end
  # before_save {
  #   # puts "Photo changes: #{self.changes.inspect}"
  #   puts 'SAVING PHOTO!' }

  after_initialize do
    self.polygallery_options ||= self.polygallery_settings
    self.initialize_polyphoto
  end

  include ActsAsPolyphoto::LocalInstanceMethods
end
init_associations(settings=DEFAULT_SETTINGS) click to toggle source
# File lib/polygallery/acts_as_polyphoto.rb, line 56
def init_associations(settings=DEFAULT_SETTINGS)
  belongs_to settings[:association_names][:gallery],
    :class_name => settings[:associations][:gallery][:class_name],
    :foreign_key => :gallery_id
  belongs_to :galleryable, :polymorphic => true
end
init_attachment(paperclip_settings) click to toggle source
# File lib/polygallery/acts_as_polyphoto.rb, line 45
def init_attachment(paperclip_settings)
  has_attached_file :photo,
    :styles => ->(a) {
        a.instance.paperclip_settings[:styles] },
    :default_url => paperclip_settings[:default_url]
  validations = polygallery_settings[:paperclip_validations]
  validates_attachment_content_type(:photo, :content_type =>
             validations[:content_type]) if validations[:content_type]
  validates_attachment_presence(:photo) if validations[:presence]
end
polygallery_settings() click to toggle source
# File lib/polygallery/acts_as_polyphoto.rb, line 41
def polygallery_settings
  DEFAULT_SETTINGS
end