Module: Reviewed::Attachable

Defined in:
lib/reviewed/attachable.rb

Instance Method Summary collapse

Instance Method Details

#attachments(opts = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/reviewed/attachable.rb', line 4

def attachments opts={}
  tags = opts.has_key?(:tags) ? [opts[:tags]].flatten : []
  attachments = []

  if tags.present?
    defaults = default_attachments & tags # attachments that already exist
    fetch = tags - defaults # attachments we need to fetch

    if defaults.present?
      tags.each do |tag|
        attachments <<  attributes['attachments'].select { |x| x.tags.include?(tag.to_s) }
      end
    end

    if fetch.present?
      attachments << fetch_attachments(opts.merge!(tags: fetch)).to_a
    end
  else
    attachments = fetch_attachments(opts).to_a
  end

  return attachments.flatten.uniq.compact
end


28
29
30
# File 'lib/reviewed/attachable.rb', line 28

def gallery tags=nil, num=8, page=1
  fetch_attachments tags: tags, :gallery => true, :per_page => num, :page => page, :order => 'priority'
end