Skip to content
Permalink
Browse files

Improved History.list to only retrive history entries of last 2 days …

…(by generic condition as method argument) to save scheduler resources.
  • Loading branch information...
zammad-sync committed Oct 3, 2019
1 parent d01dfce commit 4182b0b56753b63d067be793e978aa85906be8c9
Showing with 44 additions and 2 deletions.
  1. +16 −1 app/models/history.rb
  2. +1 −1 app/models/transaction/notification.rb
  3. +27 −0 spec/models/history_spec.rb
@@ -146,9 +146,20 @@ def self.remove(requested_object, requested_object_id)
assets: assets,
}
return all history entries of an object and it's assets and extended with an condition (e. g. to only retrive new history entries)
history = History.list('Ticket', 123, nil, true, ['created_at > ?', [Time.zone.now - 2.days]])
returns
history = {
list: list,
assets: assets,
}
=end

def self.list(requested_object, requested_object_id, related_history_object = nil, assets = nil)
def self.list(requested_object, requested_object_id, related_history_object = nil, assets = nil, condition = nil)
histories = History.where(
history_object_id: object_lookup(requested_object).id,
o_id: requested_object_id
@@ -163,6 +174,10 @@ def self.list(requested_object, requested_object_id, related_history_object = ni
)
end

if condition.present?
histories = histories.where(condition)
end

histories = histories.order(:created_at)

list = histories.map(&:attributes).each do |data|
@@ -112,7 +112,7 @@ def perform
identifier = user.login
end
already_notified = false
History.list('Ticket', ticket.id).each do |history|
History.list('Ticket', ticket.id, nil, nil, ['created_at > ?', [Time.zone.now - 2.days]]).each do |history|
next if history['type'] != 'notification'
next if !history['value_to'].match?(/\(#{Regexp.escape(@item[:type])}:/)
next if !history['value_to'].match?(/#{Regexp.escape(identifier)}\(/)
@@ -188,5 +188,32 @@
end
end
end

context 'when given an object with histories' do
context 'and called without "condition" argument' do
let!(:object) { create(:user) }

before do
travel 3.days
object.update(email: 'foo@example.com')
end

context 'or "assets" flag' do
let(:list) { described_class.list(object.class.name, object.id, nil, nil, ['created_at > ?', [Time.zone.now - 2.days]]) }

it 'returns an array of attribute hashes for those histories' do
expect(list).to match_array(
[
hash_including(
'type' => 'updated',
'o_id' => object.id,
'value_to' => 'foo@example.com',
)
]
)
end
end
end
end
end
end

0 comments on commit 4182b0b

Please sign in to comment.
You can’t perform that action at this time.