diff --git a/app/models/page.rb b/app/models/page.rb index c0b5db578..a7de40f93 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -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 diff --git a/app/views/api/pages/index.json.jbuilder b/app/views/api/pages/index.json.jbuilder index 7bb162d90..07ede77a1 100644 --- a/app/views/api/pages/index.json.jbuilder +++ b/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) diff --git a/app/views/api/pages/show.json.jbuilder b/app/views/api/pages/show.json.jbuilder index 511e0686e..7ac1f9c18 100644 --- a/app/views/api/pages/show.json.jbuilder +++ b/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 diff --git a/spec/models/page_spec.rb b/spec/models/page_spec.rb index 8260d05d2..69837e7b5 100644 --- a/spec/models/page_spec.rb +++ b/spec/models/page_spec.rb @@ -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 } @@ -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) } diff --git a/spec/requests/api/pages_spec.rb b/spec/requests/api/pages_spec.rb index e05bb3971..c406fee0f 100644 --- a/spec/requests/api/pages_spec.rb +++ b/spec/requests/api/pages_spec.rb @@ -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') @@ -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') @@ -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') @@ -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)