Skip to content

Commit

Permalink
Make DataOutputAgent use events_order for ordering events in the ou…
Browse files Browse the repository at this point in the history
…tput
  • Loading branch information
knu committed Jul 22, 2015
1 parent 098802a commit 50daec8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
@@ -1,5 +1,6 @@
# Changes

* Jul 22, 2015 - DataOutputAgent can configure the order of events in the output via `events_order`.
* Jul 20, 2015 - Control Links (used by the SchedularAgent) are correctly exported in Scenarios.
* Jul 20, 2015 - keep\_events\_for was moved from days to seconds; Scenarios have a schema verison.
* Jul 1, 2015 - DeDuplicationAgent properly handles destruction of memory.
Expand Down
8 changes: 6 additions & 2 deletions app/models/agents/data_output_agent.rb
Expand Up @@ -40,11 +40,15 @@ class DataOutputAgent < Agent
"_contents": "tag contents (can be an object for nesting)"
}
# Ordering events in the output
#{description_events_order('events in the output')}
# Liquid Templating
In Liquid templating, the following variable is available:
* `events`: An array of events being output, sorted in descending order up to `events_to_show` in number. For example, if source events contain a site title in the `site_title` key, you can refer to it in `template.title` by putting `{{events.first.site_title}}`.
* `events`: An array of events being output, sorted in the given order, up to `events_to_show` in number. For example, if source events contain a site title in the `site_title` key, you can refer to it in `template.title` by putting `{{events.first.site_title}}`.
MD
end
Expand Down Expand Up @@ -134,7 +138,7 @@ def receive_web_request(params, method, format)
end
end

source_events = received_events.order(id: :desc).limit(events_to_show).to_a
source_events = sort_events(received_events.order(id: :desc).limit(events_to_show).to_a)

interpolation_context.stack do
interpolation_context['events'] = source_events
Expand Down
16 changes: 16 additions & 0 deletions spec/models/agents/data_output_agent_spec.rb
Expand Up @@ -209,6 +209,22 @@
})
end

describe 'ordering' do
before do
agent.options['events_order'] = ['{{title}}']
end

it 'can reorder the events_to_show last events based on a Liquid expression' do
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", "Evolving yet again with a past date"])

agent.options['events_order'] = [['{{title}}', 'string', true]]

desc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
expect(desc_content['items']).to eq(asc_content['items'].reverse)
end
end

describe "interpolating \"events\"" do
before do
agent.options['template']['title'] = "XKCD comics as a feed{% if events.first.site_title %} ({{events.first.site_title}}){% endif %}"
Expand Down

0 comments on commit 50daec8

Please sign in to comment.