app/models/concerns/polygallery/acts_as_polyphoto.rb in polygallery-0.1.2 vs app/models/concerns/polygallery/acts_as_polyphoto.rb in polygallery-0.1.4
- old
+ new
@@ -1,48 +1,80 @@
module Polygallery
module ActsAsPolyphoto
extend ActiveSupport::Concern
- DEFAULTS = {
- :paperclip => {
- :styles => {:medium => '300x300#', :thumb => '100x100#'},
- :default_url => '/images/:style/missing.png'
- }
- }
-
included do
- attr_accessor :photo_to_upload,
+ attr_accessor :photo_to_upload, :polygallery_options,
:galleryable_id, :galleryable_type, :gallery_title
-
- # has_attached_file :photo,
- # :styles => PAPERCLIP_SETTINGS[:styles], # ->(a) { if a.instance.class.name then raise a.instance.class.name else PAPERCLIP_SETTINGS[:styles] end },
- # :default_url => PAPERCLIP_SETTINGS[:default_url]
- # validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
- # validates_attachment_presence :photo # TODO: make this a setting
-
- belongs_to :gallery, :class_name => 'Polygallery::Gallery'
-
- after_initialize :init_attachment
- before_save :process_photo_to_upload
+ acts_as_polyphoto
end
module ClassMethods
- end
+ def acts_as_polyphoto(options={})
+ defaults = self.polygallery_settings
+ settings = defaults.deep_merge(options)
+
+ after_initialize do
+ @polygallery_settings = settings if settings == HasPolygallery::DEFAULTS
+ self.initialize_polyphoto
+ end
- def paperclip_settings
- return self.gallery.polygallery_settings[:paperclip] if self.gallery.present?
- DEFAULTS[:paperclip]
- end
+ include ActsAsPolyphoto::LocalInstanceMethods
+ end
- def process_photo_to_upload
- self.photo = File.open(photo_to_upload) if photo_to_upload.present?
+ def polygallery_settings
+ @polygallery_settings || HasPolygallery::DEFAULTS
+ end
end
- def init_attachment
- self.class.has_attached_file :photo,
- :styles => paperclip_settings[:styles],
- :default_url => paperclip_settings[:default_url]
- self.class.validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
- # self.class.validates_attachment_presence :photo # TODO: make this a setting
+ module LocalInstanceMethods
+ def include_polygallery_settings(settings)
+ @polygallery_settings = settings
+ self.initialize_polyphoto
+ end
+
+ def initialize_polyphoto
+ settings = self.polygallery_settings
+
+ self.class.belongs_to settings[:association_names][:gallery],
+ :class_name => settings[:associations][:gallery][:class_name],
+ :foreign_key => :gallery_id
+
+ self.init_attachment
+ self.class.before_save :process_photo_to_upload
+ end
+
+ def polygallery_settings
+ polygallery_options || @polygallery_settings || HasPolygallery::DEFAULTS
+ # if gallery_title.present? && galleryable_type.present?
+ # galleryable_class = Kernel.const_get(galleryable_type)
+ # galleryable_class.send :"#{gallery_title}_settings"
+ # elsif gallery_title.present?
+ # self.send(gallery_title.to_sym).polygallery_settings
+ # elsif self.gallery.present? && gallery.polygallery_settings.present?
+ # self.gallery.polygallery_settings
+ # else
+ # HasPolygallery::DEFAULTS
+ # end
+ end
+
+ def paperclip_settings
+ self.polygallery_settings[:paperclip]
+ end
+
+ def process_photo_to_upload
+ self.photo = File.open(photo_to_upload) if photo_to_upload.present?
+ end
+
+ def init_attachment
+ self.class.has_attached_file :photo,
+ :styles => paperclip_settings[:styles],
+ :default_url => paperclip_settings[:default_url]
+ validations = polygallery_settings[:paperclip_validations]
+ self.class.validates_attachment_content_type(:photo, :content_type =>
+ validations[:content_type]) if validations[:content_type]
+ self.class.validates_attachment_presence(:photo) if validations[:presence]
+ end
+
end
end
end