module Polygallery module ActsAsPolyphoto extend ActiveSupport::Concern included do end module ClassMethods def acts_as_polyphoto(options={}) defaults = self.polygallery_settings settings = defaults.deep_merge(options) attr_accessor :photo_to_upload, :polygallery_options init_attachment settings[:paperclip] init_associations settings after_initialize do self.polygallery_options ||= self.polygallery_settings self.initialize_polyphoto end include ActsAsPolyphoto::LocalInstanceMethods end def polygallery_settings HasPolygallery::DEFAULTS end 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] before_validation :process_photo_to_upload end def init_associations(settings=HasPolygallery::DEFAULTS) belongs_to settings[:association_names][:gallery], :class_name => settings[:associations][:gallery][:class_name], :foreign_key => :gallery_id belongs_to :galleryable, :polymorphic => true end end module LocalInstanceMethods def include_polygallery_settings(settings) self.polygallery_options = settings self.initialize_polyphoto end def initialize_polyphoto self.class.init_associations(self.polygallery_settings) self.init_attachment end def polygallery_settings return self.polygallery_options if self.polygallery_options.present? if self.gallery_title.present? s = if self.galleryable.present? galleryable.class.send(:"#{self.gallery_title}_settings") elsif self.galleryable_type.present? Kernel.const_get(self.galleryable_type) .send(:"#{self.gallery_title}_settings") end return s if s.present? # elsif respond_to?(:gallery) && gallery.present? # return gallery.polygallery_settings # elsif self.gallery_id.present? # g = Kernel.const_get(self.galleryable_type).find(self.gallery_id) # if g.present? # self.gallery_title = g.title # return self.polygallery_settings # end end self.class.polygallery_settings # 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 if self.photo.present? && self.photo_to_upload.nil? if self.new_record? || self.photo_file_name_changed? self.photo_to_upload = self.photo.staged_path self.photo = nil end end 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