lib/polygallery/has_polyphotos.rb in polygallery-0.2.5 vs lib/polygallery/has_polyphotos.rb in polygallery-0.3.0

- old
+ new

@@ -15,18 +15,24 @@ title ||= association_names[:photos].to_s if title.is_a? Hash options = title title = association_names[:photos].to_s end + attr_accessor :photos_attributes, :photos_built settings = defaults.deep_merge options init_associations settings after_initialize do self.polygallery_options ||= self.polygallery_settings self.initialize_polygallery + # self.build_polygallery_photos # if new_record? && !polygallery_photos_attributes.is_a?(Hash) end + 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 @@ -34,13 +40,15 @@ end def init_associations(settings=HasPolygallery::DEFAULTS) belongs_to :galleryable, :polymorphic => true photos_name = settings[:association_names][:photos] - has_many photos_name, settings[:associations][:photos] - accepts_nested_attributes_for photos_name, - settings[:nested_attributes][:photos] + has_many :photos, settings[:associations][:photos].symbolize_keys + # has_many photos_name, settings[:associations][:photos].symbolize_keys + accepts_nested_attributes_for :photos, + settings[:nested_attributes][:photos].symbolize_keys + attr_accessor :"#{photos_name.to_s}_attributes" end end @@ -80,11 +88,82 @@ return Kernel.const_get(self.galleryable_type) .send(:"#{self.title}_settings") if galleryable_type.present? self.class.polygallery_settings end - def polygallery_photos - self.send self.polygallery_settings[:association_names][:photos] + def polygallery_photos_name + self.polygallery_settings[:association_names][:photos] end + def polygallery_photos; photos end # send self.polygallery_photos_name end + def polygallery_photos_attributes; self.photos_attributes end + # self.send :"#{polygallery_photos_name.to_s}_attributes" end + def polygallery_photos_attributes=(v); self.photos_attributes = v end + # self.send :"#{polygallery_photos_name.to_s}_attributes=", v end + def polygallery_photos_classname + 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 + end + + def build_polygallery_photos + # puts "BUILDING PHOTOS for #{title}" + # puts "PHOTO ATTRS: #{self.photos_attributes.inspect}" + # Convert attributes to array if in hash form + # self.photos_attributes = + # self.photos_attributes.map{|_, v| v + # } if self.photos_attributes.is_a? Hash + + # Initialize a duplicate + attrs_for_photos = if self.photos_attributes.is_a? Hash + self.photos_attributes.values + else + ( self.photos_attributes || [] ).dup + end + return unless attrs_for_photos.any? + return if self.photos_built? + + attrs_for_photos.each_with_index do |attrs_for_photo, index| + attrs_for_photo[:polygallery_options] = self.polygallery_settings + should_destroy = attrs_for_photo.delete(:_destroy) == 'true' + if attrs_for_photo.key? :id + existing_id = attrs_for_photo.delete(:id).to_i + existing_photo = self.polygallery_photos_class.find existing_id + if should_destroy + # puts 'Destroying a photo' + existing_photo.destroy + else + # puts 'Updating an existing photo' + existing_photo.update_attributes attrs_for_photo end + # elsif attrs_for_photo.key? :built + # puts 'Photo already built' + else + # puts 'Building a new photo' + # puts attrs_for_photo.inspect + # raise 'WOO' if self.polygallery_photos + built_photo = self.polygallery_photos.build attrs_for_photo + set_nest built_photo + # self.polygallery_photos_attributes[index][:built] = true + end + end + self.photos_built = true + end + + 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! + # puts 'Marking photo for destruction' + pp.mark_for_destruction + end } end end end end