lib/polygallery/has_polyphotos.rb in polygallery-0.0.8 vs lib/polygallery/has_polyphotos.rb in polygallery-0.0.9

- old
+ new

@@ -1,27 +1,44 @@ module Polygallery module HasPolyphotos extend ActiveSupport::Concern included do + def defaults + klass = self.class_name.present? ? Kernel.const_get(self.class_name) : Gallery + klass::DEFAULTS + end end module ClassMethods def has_polyphotos(title='photos', options={}) - has_many title.to_sym, :class_name => 'Polygallery::Photo', :before_add => :set_nest, :dependent => :destroy - accepts_nested_attributes_for title.to_sym, Gallery::DEFAULTS[:nested_attributes][:photo] # TODO: get the actual settings object somehow - attr_accessor :photos_to_upload + if title.is_a? Hash + options = title + title = 'photos' + end + + # raise 'BOOM' if options.keys.any? + settings = options + has_many title.to_sym, settings[:associations][:photos] + accepts_nested_attributes_for title.to_sym, settings[:nested_attributes][:photos] # TODO: get the actual settings somehow after_initialize do - send(title.to_sym).build unless send(title.to_sym).any? - # send(title.to_sym).each {|p| p.gallery_id ||= self.id } + # raise galleryable_type if galleryable_type.present? + send(title.to_sym).build( + :galleryable_id => galleryable_id, + :galleryable_type => galleryable_type, + :gallery_title => self.title + ) unless send(title.to_sym).any? end include HasPolyphotos::LocalInstanceMethods end end module LocalInstanceMethods def set_nest(photo) photo.gallery ||= self + photo.galleryable_id ||= galleryable_id + photo.galleryable_type ||= galleryable_type + photo.gallery_title ||= title end end end end \ No newline at end of file