Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
makes pages JSON API report campaign_action_count
Browse files Browse the repository at this point in the history
  • Loading branch information
NealJMD committed Nov 4, 2016
1 parent efe9cdc commit 4ba7711
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
8 changes: 8 additions & 0 deletions app/models/page.rb
Expand Up @@ -119,6 +119,14 @@ def slug_candidates
]
end

def campaign_action_count
@campaign_action_count ||= if campaign_id.present?
Page.where(campaign_id: campaign_id).sum(:action_count)
else
action_count
end
end

private

def switch_plugins
Expand Down
14 changes: 13 additions & 1 deletion app/views/api/pages/index.json.jbuilder
@@ -1,6 +1,18 @@
# frozen_string_literal: true
json.array! @pages do |page|
json.extract! page, :id, :title, :slug, :content, :created_at, :updated_at, :publish_status, :featured, :action_count
json.extract!(
page,
:id,
:title,
:slug,
:content,
:created_at,
:updated_at,
:publish_status,
:featured,
:action_count,
:campaign_action_count
)
json.language page.language.code
json.image image_url(page)
json.url member_facing_page_url(page)
Expand Down
14 changes: 13 additions & 1 deletion app/views/api/pages/show.json.jbuilder
@@ -1,3 +1,15 @@
# frozen_string_literal: true
json.extract! @page, :id, :title, :slug, :content, :created_at, :updated_at, :publish_status, :featured, :action_count
json.extract!(
@page,
:id,
:title,
:slug,
:content,
:created_at,
:updated_at,
:publish_status,
:featured,
:action_count,
:campaign_action_count
)
json.language @page.language.code
18 changes: 18 additions & 0 deletions spec/models/page_spec.rb
Expand Up @@ -64,6 +64,7 @@
it { is_expected.to respond_to :plugins }
it { is_expected.to respond_to :shares }
it { is_expected.to respond_to :action_count }
it { is_expected.to respond_to :campaign_action_count }
it { is_expected.to respond_to :tag_names }
it { is_expected.to respond_to :plugin_names }
it { is_expected.to respond_to :meta_tags }
Expand Down Expand Up @@ -331,6 +332,23 @@
end
end

describe 'campaign_action_count' do
it 'gives action count of just page if no associated campaigns' do
allow(page).to receive(:campaign_id).and_return(nil)
allow(page).to receive(:action_count).and_return(1234)
expect(page.campaign_action_count).to equal 1234
expect(page).to have_received(:campaign_id)
end

it 'gives action count of campaign if one is associated' do
campaign = create :campaign
page1 = create :page, action_count: 2000, campaign: campaign
page2 = create :page, action_count: 2500, campaign: campaign
expect(page1.campaign_action_count).to eq 4500
expect(page2.campaign_action_count).to eq 4500
end
end

describe '#dup' do
let(:image) { create(:image, page: page) }

Expand Down
41 changes: 33 additions & 8 deletions spec/requests/api/pages_spec.rb
Expand Up @@ -6,6 +6,24 @@ def json
JSON.parse(response.body)
end

let(:expected) do
%w(
id
title
slug
content
created_at
updated_at
publish_status
campaign_action_count
action_count
language
featured
image
url
)
end

describe 'GET index' do
before do
create(:page, :published, title: 'Foo', content: 'Bar')
Expand All @@ -18,9 +36,7 @@ def json
it 'returns list of pages' do
expect(subject.size).to eq(1)

expect(subject.first.keys).to match(
%w(id title slug content created_at updated_at publish_status featured action_count language image url)
)
expect(subject.first.keys).to match_array(expected)

expect(subject.first.symbolize_keys).to include(title: 'Foo',
content: 'Bar')
Expand All @@ -40,9 +56,7 @@ def json
it 'returns list of pages' do
expect(subject.size).to eq(1)

expect(subject.first.keys).to match(
%w(id title slug content created_at updated_at publish_status featured action_count language image url)
)
expect(subject.first.keys).to match_array(expected)

expect(subject.first.symbolize_keys).to include(title: 'Foo',
content: 'Bar')
Expand All @@ -57,9 +71,20 @@ def json
before { get(api_page_path(page, format: :json)) }

it 'returns page' do
expect(subject.keys).to match(
%w(id title slug content created_at updated_at publish_status featured action_count language)
expected = %w(
id
title
slug
content
created_at
updated_at
publish_status
featured
action_count
language
campaign_action_count
)
expect(subject.keys).to match_array(expected)

expect(subject.symbolize_keys).to include(title: 'Foo',
id: page.id)
Expand Down

0 comments on commit 4ba7711

Please sign in to comment.