lib/polygallery/has_polyphotos.rb in polygallery-0.1.2 vs lib/polygallery/has_polyphotos.rb in polygallery-0.1.4

- old
+ new

@@ -1,88 +1,79 @@ module Polygallery module HasPolyphotos extend ActiveSupport::Concern - - DEFAULTS = { - :associations => { - :gallery => { - :class_name => 'Polygallery::Gallery', - :as => :galleryable, - :dependent => :destroy - }, - :photos => { - :class_name => 'Polygallery::Photo', - :before_add => :set_nest, - :dependent => :destroy - } - }, - :association_names => { - :gallery => :gallery, - :photos => :photos - }, - :nested_attributes => { - :gallery => {:reject_if => :all_blank}, - :photos => {:reject_if => lambda{|attributes| attributes['photo'].nil? }, :allow_destroy => true} - }, - :validates => {}, - :paperclip => { - :styles => {:medium => '300x300#', :thumb => '100x100#'}, - :default_url => ActionController::Base.helpers.asset_path('polygallery/thumbnail-missing.png') - } - } included do - # def defaults - # klass = self.class_name.present? ? Kernel.const_get(self.class_name) : Gallery - # klass::DEFAULTS - # end + attr_accessor :polygallery_options end module ClassMethods - def has_polyphotos(title='photos', options={}) + def has_polyphotos(title=nil, options={}) + defaults = self.polygallery_settings + association_names = (options[:association_names].present? ? + options : defaults)[:association_names] + title ||= association_names[:photos].to_s if title.is_a? Hash options = title - title = 'photos' + title = association_names[:photos].to_s end - # puts reflect_on_all_associations(:belongs_to).map{|a| a.table_name } - settings = HasPolyphotos::DEFAULTS.deep_merge(options) - cattr_accessor :polygallery_settings - send(:polygallery_settings=, settings) - puts HasPolyphotos::DEFAULTS - 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 + settings = defaults.deep_merge options + after_initialize do - # raise galleryable_type if galleryable_type.present? - send(title.to_sym).build( - :galleryable_id => galleryable_id, - :galleryable_type => galleryable_type, - :"#{settings[:association_names][:gallery]}_title" => self.title - ) unless send(title.to_sym).any? + @polygallery_settings = settings if settings == HasPolygallery::DEFAULTS + self.initialize_polygallery end - after_initialize :do_something + include HasPolyphotos::LocalInstanceMethods end - # def polygallery_settings - # @settings - # end + def polygallery_settings + @polygallery_settings || HasPolygallery::DEFAULTS + end end module LocalInstanceMethods def set_nest(photo) - photo.gallery ||= self + photo.send(:"#{title}=", self) if photo.send(:"#{title}").nil? photo.galleryable_id ||= galleryable_id photo.galleryable_type ||= galleryable_type photo.gallery_title ||= title end - def do_something - @polygallery_settings = Kernel.const_get(self.galleryable_type) - .send :"#{self.title}_settings" + # def polygallery_settings + # Kernel.const_get(self.galleryable_type).send :"#{self.title}_settings" + # end + + def include_polygallery_settings(settings) + @polygallery_settings = settings + self.initialize_polygallery end + def initialize_polygallery + settings = self.polygallery_settings + association_names = settings[:association_names] + self.class.has_many association_names[:photos], + settings[:associations][:photos].deep_merge({}) + self.class.accepts_nested_attributes_for association_names[:photos], + settings[:nested_attributes][:photos] + end + + def build_first_photo + settings = polygallery_settings + photo_association = send settings[:association_names][:photos] + photo_association.build( + :polygallery_options => settings, + :galleryable_id => galleryable_id, + :galleryable_type => galleryable_type, + :gallery_title => self.title + ) unless photo_association.any? + end + + def polygallery_settings + polygallery_options || @polygallery_settings || HasPolygallery::DEFAULTS + end end end end ActiveRecord::Base.send :include, Polygallery::HasPolyphotos