Module: Polygallery::HasPolygallery::LocalInstanceMethods

Defined in:
lib/polygallery/has_polygallery.rb

Instance Method Summary collapse

Instance Method Details

#build_first_photosObject



138
139
140
141
142
143
144
# File 'lib/polygallery/has_polygallery.rb', line 138

def build_first_photos
  return unless self.class.has_polygallery?
  # self.class.polygalleries.each{|pg|
  #   build_gallery_association pg
  #   send(pg.to_sym).build_first_photo }
  build_gallery_associations.map(&:build_first_photo)
end


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/polygallery/has_polygallery.rb', line 94

def build_gallery_association(gallery_name=:gallery)
  gallery_association = send gallery_name
  attrs_for_this_gallery = send(:"#{gallery_name}_attributes") || {}
  if gallery_association.present?
    # if attrs_for_this_gallery.empty?
    #   gallery_association.photos.each do |p|
    #     if p.photo_to_upload.present?
    #       p.photo = File.new p.photo_to_upload
    #       p.photo_to_upload = nil
    #     end
    #     relevant_changes = p.changed -
    #       %w(galleryable_type galleryable_id gallery_title)
    #     if relevant_changes.any?
    #       attrs_for_this_photo = Hash[*relevant_changes.map{|column|
    #         [ column, p.send(column) ] }.flatten]
    #       attrs_for_this_gallery[:photos_attributes] ||= {}
    #       attrs_for_this_gallery[:photos_attributes][(p.id ||
    #         attrs_for_this_gallery[:photos_attributes].length).to_s] = attrs_for_this_photo
    #     end
    #   end
    #   # return gallery_association
    # end
    if attrs_for_this_gallery.empty?
      # puts 'Returning due to empty attrs!'
      return gallery_association 
    end
    if gallery_association.persisted?
      # puts 'Updating an existing gallery!'
      gallery_association.update_attributes attrs_for_this_gallery
    else
      attrs_for_this_gallery.each{|k, v|
        gallery_association.send :"#{k}=", v }
    end
    return gallery_association
  end
  settings_for_this_gallery = send :"#{gallery_name}_settings"
  attrs_for_this_gallery[:polygallery_options] = settings_for_this_gallery
  # puts "Building a new gallery: #{gallery_name}"
  built_gallery = send :"build_#{gallery_name}", attrs_for_this_gallery
  gallery_association = send :"#{gallery_name}=", built_gallery
  gallery_association.galleryable ||= self
  gallery_association
end


87
88
89
90
91
92
93
# File 'lib/polygallery/has_polygallery.rb', line 87

def build_gallery_associations
  return [] unless self.class.has_polygallery?
  gallery_associations = self.class.polygalleries.map{|pg|
    self.build_gallery_association pg }
  self.galleries_built = true
  gallery_associations
end

#ensure_galleryable_setObject



155
156
157
158
159
160
161
# File 'lib/polygallery/has_polygallery.rb', line 155

def ensure_galleryable_set
  gallery_associations.each do |ga|
    ga.galleryable ||= self
    ga.polygallery_photos.each{|pp|
      pp.galleryable ||= self }
  end
end

#first_photo(ga_name = nil) ⇒ Object



153
# File 'lib/polygallery/has_polygallery.rb', line 153

def first_photo(ga_name=nil); first_polyphoto(ga_name).photo end

#first_polyphoto(ga_name = nil) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/polygallery/has_polygallery.rb', line 146

def first_polyphoto(ga_name=nil)
  return Photo.new unless gallery_associations.any?
  ga = if ga_name.present? then send(ga_name)
       else gallery_associations.to_a.find{|pg| pg.photos.any? } end
  return Photo.new if ga.nil?
  ga.first_photo
end

#galleries_built?Boolean

Returns:

  • (Boolean)


165
# File 'lib/polygallery/has_polygallery.rb', line 165

def galleries_built?; self.galleries_built.present? end


163
164
# File 'lib/polygallery/has_polygallery.rb', line 163

def gallery_associations
self.class.polygalleries.map{|pg| send(pg) } end

#has_polygallery?(gallery_title = nil) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/polygallery/has_polygallery.rb', line 82

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

#polygalleriesObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/polygallery/has_polygallery.rb', line 69

def polygalleries
  self.class.polygalleries.each do |pg_name|
    gallery_opts = self.send(:"#{pg_name}_settings")
    gallery_classname = gallery_opts[:associations][:gallery][:class_name]
    gallery_class = Object.const_get gallery_classname
    table_name = gallery_class.table_name
    gallery_class.select("#{table_name}.title")
      .where(:galleryable => self)
      .group("#{table_name}.title")
      .map(&:title)
  end
end

#prune_empty_photosObject



166
167
# File 'lib/polygallery/has_polygallery.rb', line 166

def prune_empty_photos
gallery_associations.each(&:prune_empty_photos) end

#remote_urls_for_polyphotos(style = nil) ⇒ Object



169
170
# File 'lib/polygallery/has_polygallery.rb', line 169

def remote_urls_for_polyphotos(style=nil)
gallery_associations.map{|ga| ga.remote_urls(style) }.flatten end

#thumb_urlObject



171
# File 'lib/polygallery/has_polygallery.rb', line 171

def thumb_url; first_polyphoto.thumb_url end