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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/polygallery/has_polygallery.rb', line 7
def has_polygallery(title='gallery', options={})
if title.is_a? Hash
options = title
title = 'gallery'
end
defaults = DEFAULT_SETTINGS
if options[:association_names].nil? && title.to_s != 'gallery'
options[:association_names] = {
:gallery => :"#{title}",
:photos => :"#{title.to_s.gsub('_gallery', '')}_photos" }
end
settings = defaults.deep_merge(options)
cattr_accessor "#{title}_settings".to_sym
send("#{title}_settings=".to_sym, settings)
attr_accessor :"#{title}_attributes", :galleries_built
has_one title.to_sym, -> { where(title: title.to_s) }, settings[:associations][:gallery]
has_many settings[:association_names][:photos],
:through => :"#{title}",
:source => :photos, :class_name => settings[:associations][:photos][:class_name]
accepts_nested_attributes_for settings[:association_names][:gallery],
settings[:nested_attributes][:gallery]
accepts_nested_attributes_for settings[:association_names][:photos],
settings[:nested_attributes][:photos]
after_initialize :if => :new_record? do
if self.gallery_attributes.nil?
build_gallery_associations
build_first_photos
end
end
before_validation do build_gallery_associations
prune_empty_photos
end
before_save :ensure_galleryable_set
include HasPolygallery::LocalInstanceMethods
end
|