lib/reviewed/attachable.rb in reviewed-0.4.6 vs lib/reviewed/attachable.rb in reviewed-0.5.0
- old
+ new
@@ -1,21 +1,28 @@
module Reviewed
module Attachable
- def attachments tag, opts={}
- (@attachments ||= {})[tag] ||= fetch_attachments tag, opts
+ def attachments tag=nil, opts={}
+ if default_attachments.include?(tag.to_s)
+ return attributes['attachments'].select { |x| x.tags.include?(tag.to_s) }
+ else
+ fetch_attachments(opts.merge!(tags: tag)).to_a
+ end
end
def gallery tags=nil, num=8, page=1
- fetch_attachments tags, :gallery => true, :per_page => num, :page => page, :order => 'priority'
+ fetch_attachments tags: tags, :gallery => true, :per_page => num, :page => page, :order => 'priority'
end
private
- def fetch_attachments tag, opts={}
- params = opts.merge :tags => tag
- req = Request.new :resource => Attachment, :scope => self
- req.where params
+ def default_attachments
+ attributes['attachments'] ||= []
+ attributes['attachments'].map(&:tags).flatten.uniq.compact
end
+ def fetch_attachments opts={}
+ req = Request.new :resource => Attachment, :scope => self
+ req.where opts
+ end
end
end