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

- old
+ new

@@ -1,46 +1,89 @@ 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 + # 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={}) if title.is_a? Hash options = title title = 'photos' end - # raise 'BOOM' if options.keys.any? - settings = options + # 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 + accepts_nested_attributes_for title.to_sym, settings[:nested_attributes][:photos] # TODO: get the actual @settings somehow after_initialize do # raise galleryable_type if galleryable_type.present? send(title.to_sym).build( :galleryable_id => galleryable_id, :galleryable_type => galleryable_type, - :gallery_title => self.title + :"#{settings[:association_names][:gallery]}_title" => self.title ) unless send(title.to_sym).any? end + after_initialize :do_something include HasPolyphotos::LocalInstanceMethods end + + # def polygallery_settings + # @settings + # 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 + + def do_something + @polygallery_settings = Kernel.const_get(self.galleryable_type) + .send :"#{self.title}_settings" + end + end end end -ActiveRecord::Base.send :include, Polygallery::HasPolyphotos \ No newline at end of file +ActiveRecord::Base.send :include, Polygallery::HasPolyphotos