diff --git a/app/models/concerns/archivable.rb b/app/models/concerns/archivable.rb index ed056a82..5af812ee 100644 --- a/app/models/concerns/archivable.rb +++ b/app/models/concerns/archivable.rb @@ -60,17 +60,19 @@ module Archivable io = StringIO.new(compressed_content) gz = Zlib::GzipReader.new(io) - result = nil - gz.each_line do |line| - data = JSON.parse(line) - if data['id'] == id - result = data['raw_data'] - break + begin + result = nil + gz.each_line do |line| + data = JSON.parse(line) + if data['id'] == id + result = data['raw_data'] + break + end end + result || {} + ensure + gz.close end - - gz.close - result || {} end def handle_archive_fetch_error(error) diff --git a/app/models/concerns/taggable.rb b/app/models/concerns/taggable.rb index 59f97c91..2bebef4c 100644 --- a/app/models/concerns/taggable.rb +++ b/app/models/concerns/taggable.rb @@ -8,18 +8,18 @@ module Taggable has_many :tags, through: :taggings scope :with_tags, ->(tag_ids) { joins(:taggings).where(taggings: { tag_id: tag_ids }).distinct } - scope :with_all_tags, ->(tag_ids) { - tag_ids = Array(tag_ids) + scope :with_all_tags, lambda { |tag_ids| + tag_ids = Array(tag_ids).uniq return none if tag_ids.empty? # For each tag, join and filter, then use HAVING to ensure all tags are present joins(:taggings) .where(taggings: { tag_id: tag_ids }) .group("#{table_name}.id") - .having("COUNT(DISTINCT taggings.tag_id) = ?", tag_ids.length) + .having('COUNT(DISTINCT taggings.tag_id) = ?', tag_ids.length) } scope :without_tags, -> { left_joins(:taggings).where(taggings: { id: nil }) } - scope :tagged_with, ->(tag_name, user) { + scope :tagged_with, lambda { |tag_name, user| joins(:tags).where(tags: { name: tag_name, user: user }).distinct } end