module Polygallery module HasPolyphotos extend ActiveSupport::Concern included do 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 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 } end include HasPolyphotos::LocalInstanceMethods end end module LocalInstanceMethods def set_nest(photo) photo.gallery ||= self end end end end ActiveRecord::Base.send :include, Polygallery::HasPolyphotos