module Polygallery::HasPolyphotos::ClassMethods

Public Instance Methods

has_polyphotos(title=nil, options={}) click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 7
def has_polyphotos(title=nil, options={})
  attr_accessor :polygallery_options

  defaults = self.polygallery_settings
  association_names = (options[:association_names].present? ? 
                       options : defaults)[:association_names]
  title ||= association_names[:photos].to_s
  if title.is_a? Hash
    options = title
    title = association_names[:photos].to_s
  end
  attr_accessor :photos_attributes, :photos_built

  settings = defaults.deep_merge options
  # init_associations settings
  belongs_to :galleryable, polymorphic: true
  # photos_name = settings[:association_names][:photos]
  has_many :photos, settings[:associations][:photos].symbolize_keys

  after_initialize do
    self.polygallery_options ||= self.polygallery_settings
    self.initialize_polygallery
  end
  after_initialize  :build_polygallery_photos, :if => :new_record?
  before_validation :set_nested_attributes_to_correct_gallery
  before_validation :build_polygallery_photos, :unless => :photos_built? # , :on => :update
  before_validation :prune_empty_photos

  include HasPolyphotos::LocalInstanceMethods
end
init_associations(settings=DEFAULT_SETTINGS) click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 42
def init_associations(settings=DEFAULT_SETTINGS)
  belongs_to :galleryable, :polymorphic => true
  photos_name = settings[:association_names][:photos]
  has_many :photos, settings[:associations][:photos].symbolize_keys
  # has_many photos_name, settings[:associations][:photos].symbolize_keys
  accepts_nested_attributes_for :photos,
      settings[:nested_attributes][:photos].symbolize_keys
  attr_accessor :"#{photos_name.to_s}_attributes"
end
polygallery_settings() click to toggle source
# File lib/polygallery/has_polyphotos.rb, line 38
def polygallery_settings
  DEFAULT_SETTINGS
end