Module: Polygallery::HasPolyphotos::ClassMethods

Defined in:
lib/polygallery/has_polyphotos.rb

Instance Method Summary collapse

Instance Method Details

#has_polyphotos(title = nil, 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
# File 'lib/polygallery/has_polyphotos.rb', line 7

def has_polyphotos(title=nil, options={})
  attr_accessor :polygallery_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 = association_names[:photos].to_s
  end
  attr_accessor :photos_attributes, :photos_built

  settings = defaults.deep_merge options
  # init_associations settings
  belongs_to :galleryable, polymorphic: true
  # photos_name = settings[:association_names][:photos]
  has_many :photos, settings[:associations][:photos].symbolize_keys

  after_initialize do
    self.polygallery_options ||= self.polygallery_settings
    self.initialize_polygallery
  end
  after_initialize  :build_polygallery_photos, :if => :new_record?
  before_validation :set_nested_attributes_to_correct_gallery
  before_validation :build_polygallery_photos, :unless => :photos_built? # , :on => :update
  before_validation :prune_empty_photos

  include HasPolyphotos::LocalInstanceMethods
end

#init_associations(settings = DEFAULT_SETTINGS) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/polygallery/has_polyphotos.rb', line 42

def init_associations(settings=DEFAULT_SETTINGS)
  belongs_to :galleryable, :polymorphic => true
  photos_name = settings[:association_names][:photos]
  has_many :photos, settings[:associations][:photos].symbolize_keys
  # has_many photos_name, settings[:associations][:photos].symbolize_keys
  accepts_nested_attributes_for :photos,
      settings[:nested_attributes][:photos].symbolize_keys
  attr_accessor :"#{photos_name.to_s}_attributes"
end

#polygallery_settingsObject



38
39
40
# File 'lib/polygallery/has_polyphotos.rb', line 38

def polygallery_settings
  DEFAULT_SETTINGS
end