module Polygallery module ActsAsPolyphoto extend ActiveSupport::Concern included do attr_accessor :photo_to_upload, :polygallery_options, :galleryable_id, :galleryable_type, :gallery_title acts_as_polyphoto end module ClassMethods def acts_as_polyphoto(options={}) defaults = self.polygallery_settings settings = defaults.deep_merge(options) init_attachment settings[:paperclip] after_initialize do @polygallery_settings = settings if settings == HasPolygallery::DEFAULTS self.initialize_polyphoto end include ActsAsPolyphoto::LocalInstanceMethods end def polygallery_settings @polygallery_settings || HasPolygallery::DEFAULTS end def init_attachment(paperclip_settings) has_attached_file :photo, :styles => 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 end module LocalInstanceMethods def include_polygallery_settings(settings) @polygallery_settings = settings self.initialize_polyphoto end def initialize_polyphoto settings = self.polygallery_settings self.class.belongs_to settings[:association_names][:gallery], :class_name => settings[:associations][:gallery][:class_name], :foreign_key => :gallery_id self.init_attachment self.class.before_save :process_photo_to_upload end def polygallery_settings polygallery_options || @polygallery_settings || HasPolygallery::DEFAULTS # if gallery_title.present? && galleryable_type.present? # galleryable_class = Kernel.const_get(galleryable_type) # galleryable_class.send :"#{gallery_title}_settings" # elsif gallery_title.present? # self.send(gallery_title.to_sym).polygallery_settings # elsif self.gallery.present? && gallery.polygallery_settings.present? # self.gallery.polygallery_settings # else # HasPolygallery::DEFAULTS # end end def paperclip_settings self.polygallery_settings[:paperclip] end def process_photo_to_upload self.photo = File.open(photo_to_upload) if photo_to_upload.present? end def init_attachment self.class.init_attachment paperclip_settings end end end end