Module: Polygallery::ActsAsPolyphoto::ClassMethods

Defined in:
lib/polygallery/acts_as_polyphoto.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_polyphoto(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/polygallery/acts_as_polyphoto.rb', line 7

def acts_as_polyphoto(options={})
  defaults = self.polygallery_settings
  settings = defaults.deep_merge(options)

  attr_accessor :photo_to_upload, :polygallery_options,
                :polyphoto_initialized
  attr_reader   :photo_remote_url
  
  init_attachment settings[:paperclip]
  init_associations settings

  before_validation do
    # self.polygallery_options = nil
    # self.polygallery_options = self.polygallery_settings
    # self.class.init_attachment self.polygallery_options
    if gallery.present?
      self.gallery_title = gallery.title if gallery_title.blank?
      self.galleryable ||= gallery.galleryable
      initialize_polyphoto
    end
    process_photo_to_upload
  end
  # before_save {
  #   # puts "Photo changes: #{self.changes.inspect}"
  #   puts 'SAVING PHOTO!' }

  after_initialize do
    self.polygallery_options ||= self.polygallery_settings
    self.initialize_polyphoto
  end

  include ActsAsPolyphoto::LocalInstanceMethods
end

#init_associations(settings = DEFAULT_SETTINGS) ⇒ Object



56
57
58
59
60
61
# File 'lib/polygallery/acts_as_polyphoto.rb', line 56

def init_associations(settings=DEFAULT_SETTINGS)
  belongs_to settings[:association_names][:gallery],
    :class_name => settings[:associations][:gallery][:class_name],
    :foreign_key => :gallery_id
  belongs_to :galleryable, :polymorphic => true
end

#init_attachment(paperclip_settings) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/polygallery/acts_as_polyphoto.rb', line 45

def init_attachment(paperclip_settings)
  has_attached_file :photo,
    :styles => ->(a) {
        a.instance.paperclip_settings[:styles] },
    :default_url => paperclip_settings[:default_url]
  validations = polygallery_settings[:paperclip_validations]
  validates_attachment_content_type(:photo, :content_type =>
             validations[:content_type]) if validations[:content_type]
  validates_attachment_presence(:photo) if validations[:presence]
end

#polygallery_settingsObject



41
42
43
# File 'lib/polygallery/acts_as_polyphoto.rb', line 41

def polygallery_settings
  DEFAULT_SETTINGS
end