From 654da6a4e65acdc2e5427c5382eb558d0e59363c Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Fri, 7 Oct 2016 19:13:05 +0900 Subject: [PATCH 1/2] Add a failing test There needs more than 2 * events_to_show events to check if selection before limiting actually works. --- spec/models/agents/data_output_agent_spec.rb | 42 +++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/spec/models/agents/data_output_agent_spec.rb b/spec/models/agents/data_output_agent_spec.rb index bfce75bf..b4450066 100644 --- a/spec/models/agents/data_output_agent_spec.rb +++ b/spec/models/agents/data_output_agent_spec.rb @@ -242,13 +242,53 @@ describe Agents::DataOutputAgent do }) end + context 'with more events' do + let!(:event4) do + agents(:bob_website_agent).create_event payload: { + 'site_title' => 'XKCD', + 'url' => 'http://imgs.xkcd.com/comics/comic1.png', + 'title' => 'Comic 1', + 'date' => '', + 'hovertext' => 'Hovertext for Comic 1' + } + end + + let!(:event5) do + agents(:bob_website_agent).create_event payload: { + 'site_title' => 'XKCD', + 'url' => 'http://imgs.xkcd.com/comics/comic2.png', + 'title' => 'Comic 2', + 'date' => '', + 'hovertext' => 'Hovertext for Comic 2' + } + end + + let!(:event6) do + agents(:bob_website_agent).create_event payload: { + 'site_title' => 'XKCD', + 'url' => 'http://imgs.xkcd.com/comics/comic3.png', + 'title' => 'Comic 3', + 'date' => '', + 'hovertext' => 'Hovertext for Comic 3' + } + end + + describe 'limiting' do + it 'can select the last `events_to_show` events' do + agent.options['events_to_show'] = 2 + content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json') + expect(content['items'].map {|i| i["title"] }).to eq(["Comic 3", "Comic 2"]) + end + end + end + describe 'ordering' do before do agent.options['events_order'] = ['{{hovertext}}'] agent.options['events_list_order'] = ['{{title}}'] end - it 'can reorder the events_to_show last events based on a Liquid expression' do + it 'can reorder the last `events_to_show` events based on a Liquid expression' do agent.options['events_to_show'] = 2 asc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json') expect(asc_content['items'].map {|i| i["title"] }).to eq(["Evolving", "Evolving again"]) From 63c7ec5fddaf017c7b5182b8103b4efa3908fa25 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Fri, 7 Oct 2016 19:15:41 +0900 Subject: [PATCH 2/2] Reorder received_events by id asc The implementation of latest_events() assumed that while it wasn't actually the case; the association of received_events has a default scope of `order(id: :desc)`. --- app/models/agents/data_output_agent.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/agents/data_output_agent.rb b/app/models/agents/data_output_agent.rb index e7027b7e..242f9727 100644 --- a/app/models/agents/data_output_agent.rb +++ b/app/models/agents/data_output_agent.rb @@ -190,6 +190,8 @@ module Agents end def latest_events(reload = false) + received_events = received_events().reorder(id: :asc) + events = if (event_ids = memory[:event_ids]) && memory[:events_order] == events_order && @@ -207,8 +209,7 @@ module Agents new_events = if last_event_id = memory[:last_event_id] - received_events.where(Event.arel_table[:id].gt(last_event_id)). - order(id: :asc).to_a + received_events.where(Event.arel_table[:id].gt(last_event_id)).to_a else source_ids.flat_map { |source_id| # dig twice as many events as the number of