lib/polygallery/has_polyphotos.rb in polygallery-0.3.6 vs lib/polygallery/has_polyphotos.rb in polygallery-0.4.0

- old
+ new

@@ -1,12 +1,10 @@ +require 'polygallery' + 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 @@ -18,29 +16,32 @@ title = association_names[:photos].to_s end attr_accessor :photos_attributes, :photos_built settings = defaults.deep_merge options - init_associations settings + # 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? + 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 def polygallery_settings - HasPolygallery::DEFAULTS + DEFAULT_SETTINGS end - def init_associations(settings=HasPolygallery::DEFAULTS) + 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, @@ -75,12 +76,10 @@ def build_first_photo photo_association = self.polygallery_photos photo_association.build( :polygallery_options => self.polygallery_settings, :galleryable => self.galleryable, - :galleryable_id => self.galleryable_id, - :galleryable_type => self.galleryable_type, :gallery_title => self.title ) unless photo_association.any? end def polygallery_settings return self.polygallery_options if self.polygallery_options.present? @@ -100,15 +99,15 @@ self.polygallery_settings[:associations][:photos][:class_name] end def polygallery_photos_class Object.const_get polygallery_photos_classname end def set_nested_attributes_to_correct_gallery - # if self.photos_attributes.present? && - # self.polygallery_photos_name.to_s != 'photos' - # self.send :"#{polygallery_photos_name.to_s}_attributes=", self.photos_attributes - # self.photos_attributes = nil - # end + if self.photos_attributes.present? && + self.polygallery_photos_name.to_s != 'photos' + # self.send :"#{polygallery_photos_name.to_s}_attributes=", self.photos_attributes + # self.photos_attributes = nil + end end def build_polygallery_photos # Convert attributes to array if in hash form # self.photos_attributes = @@ -147,24 +146,31 @@ def photos_built?; self.photos_built.present? end def prune_empty_photos # puts "#{title} is pruning empty photos..." polygallery_photos.each {|pp| - if pp.new_record? && !pp.photo.file? - # TODO: find a way not to generate these photos in the first place! + if pp.new_record? && !pp.photo.file? && pp.photo_to_upload.nil? # puts 'Marking photo for destruction' pp.mark_for_destruction + polygallery_photos.delete pp end } end def remote_urls(style=nil); photos.map{|p| p.remote_url style } || [] end def thumb_url; first_photo.thumb_url end def first_photo # TODO: some kind of selection photos.first || Photo.new end + def method_missing(m, *args, &block) + if m == :photos + self.initialize_polygallery + return send(m, *args, &block) if self.respond_to? m + Photo.where('1 = 0') + else super end + end end end end -ActiveRecord::Base.send :include, Polygallery::HasPolyphotos +# ActiveRecord::Base.send :include, Polygallery::HasPolyphotos