module Polygallery module HasPolyphotos extend ActiveSupport::Concern included do end module ClassMethods 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 settings = defaults.deep_merge options has_many association_names[:photos], settings[:associations][:photos].deep_merge({}) accepts_nested_attributes_for association_names[:photos], settings[:nested_attributes][:photos] after_initialize do @polygallery_settings = settings if settings == HasPolygallery::DEFAULTS self.initialize_polygallery end include HasPolyphotos::LocalInstanceMethods end def polygallery_settings @polygallery_settings || HasPolygallery::DEFAULTS end end module LocalInstanceMethods def set_nest(photo) photo.send(:"#{title}=", self) if photo.send(:"#{title}").nil? photo.galleryable_id ||= galleryable_id photo.galleryable_type ||= galleryable_type photo.gallery_title ||= title end # def polygallery_settings # Kernel.const_get(self.galleryable_type).send :"#{self.title}_settings" # end def include_polygallery_settings(settings) @polygallery_settings = self.polygallery_options = settings self.initialize_polygallery end def initialize_polygallery settings = self.polygallery_settings association_names = settings[:association_names] # self.class.has_many association_names[:photos], # settings[:associations][:photos].deep_merge({}) # self.class.accepts_nested_attributes_for association_names[:photos], # settings[:nested_attributes][:photos] end def build_first_photo settings = polygallery_settings photo_association = send settings[:association_names][:photos] photo_association.build( :polygallery_options => settings, :galleryable_id => galleryable_id, :galleryable_type => galleryable_type, :gallery_title => self.title ) unless photo_association.any? end def polygallery_settings polygallery_options || @polygallery_settings || HasPolygallery::DEFAULTS end end end end ActiveRecord::Base.send :include, Polygallery::HasPolyphotos