Module: Polygallery::HasPolygallery::ClassMethods

Defined in:
lib/polygallery/has_polygallery.rb

Instance Method Summary collapse

Instance Method Details

#has_polygallery(title = 'gallery', options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/polygallery/has_polygallery.rb', line 44

def has_polygallery(title='gallery', options={})
  if title.is_a? Hash
    options = title
    title = 'gallery'
  end

  # associations = options[:associations]
  # gallery_association = options[:associations][:gallery] if associations.present?
  # gallery_class_name =  gallery_association[:class_name] if gallery_association.present?
  defaults = HasPolygallery::DEFAULTS
  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)

  has_one title.to_sym, -> { where(:title => title.to_s) }, settings[:associations][:gallery]
  has_many settings[:association_names][:photos],
           :through => :"#{title}",
           :source => settings[:association_names][: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 :build_galleries
  before_save :ensure_galleryable_set

  include HasPolygallery::LocalInstanceMethods
end

#has_polygallery?(gallery_title = nil) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/polygallery/has_polygallery.rb', line 85

def has_polygallery?(gallery_title=nil)
  return polygalleries.any? if gallery_title.nil?
  polygalleries.include?(gallery_title)
end

#polygalleriesObject



80
81
82
83
# File 'lib/polygallery/has_polygallery.rb', line 80

def polygalleries
  self.reflect_on_all_associations(:has_one)
      .select{|a| a.foreign_key == 'galleryable_id'}.map(&:name)
end