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

Commit

Permalink
Use new enum on page service class
Browse files Browse the repository at this point in the history
  • Loading branch information
osahyoun committed Jul 25, 2016
1 parent 4da4c13 commit cacafc2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
1 change: 0 additions & 1 deletion app/models/page.rb
Expand Up @@ -19,7 +19,6 @@ class Page < ActiveRecord::Base
has_many :links, dependent: :destroy

scope :language, -> (code) { code ? joins(:language).where(languages: { code: code }) : all }
scope :published, -> { where(active: true) }
scope :featured, -> { where(featured: true) }

validates :title, presence: true
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/pages/index.json.jbuilder
@@ -1,4 +1,4 @@
json.array! @pages do |page|
json.extract! page, :id, :title, :slug, :content, :created_at, :updated_at, :active, :featured, :action_count
json.extract! page, :id, :title, :slug, :content, :created_at, :updated_at, :publish_status, :featured, :action_count
json.language page.language.code
end
4 changes: 2 additions & 2 deletions app/views/api/pages/show.json.jbuilder
@@ -1,2 +1,2 @@
json.extract! @page, :id, :title, :slug, :content, :created_at, :updated_at, :active, :featured, :action_count
json.language @page.language.code
json.extract! @page, :id, :title, :slug, :content, :created_at, :updated_at, :publish_status, :featured, :action_count
json.language @page.language.code
8 changes: 8 additions & 0 deletions spec/factories/pages.rb
Expand Up @@ -8,6 +8,14 @@
language
ak_petition_resource_uri "http://example.com/petition"
ak_donation_resource_uri "http://example.com/donation"

trait :published do
publish_status :published
end

trait :unpublished do
publish_status :unpublished
end
end
end

10 changes: 5 additions & 5 deletions spec/requests/api/pages_spec.rb
Expand Up @@ -7,7 +7,7 @@ def json

describe 'GET index' do
before do
create(:page, active: true, title: 'Foo', content: 'Bar')
create(:page, :published, title: 'Foo', content: 'Bar')
end

subject { JSON.parse(response.body) }
Expand All @@ -18,7 +18,7 @@ def json
expect(subject.size).to eq(1)

expect(subject.first.keys).to match(
%w{id title slug content created_at updated_at active featured action_count language}
%w{id title slug content created_at updated_at publish_status featured action_count language}
)

expect(subject.first.symbolize_keys).to include({
Expand All @@ -30,7 +30,7 @@ def json

describe 'GET featured' do
before do
create(:page, featured: true, active: true, title: 'Foo', content: 'Bar')
create(:page, :published, featured: true, title: 'Foo', content: 'Bar')
create(:page, featured: false)
end

Expand All @@ -42,7 +42,7 @@ def json
expect(subject.size).to eq(1)

expect(subject.first.keys).to match(
%w{id title slug content created_at updated_at active featured action_count language}
%w{id title slug content created_at updated_at publish_status featured action_count language}
)

expect(subject.first.symbolize_keys).to include({
Expand All @@ -61,7 +61,7 @@ def json

it 'returns page' do
expect(subject.keys).to match(
%w{id title slug content created_at updated_at active featured action_count language}
%w{id title slug content created_at updated_at publish_status featured action_count language}
)

expect(subject.symbolize_keys).to include({
Expand Down
14 changes: 7 additions & 7 deletions spec/services/page_service_spec.rb
Expand Up @@ -2,9 +2,9 @@

describe PageService do
describe '.list' do
let!(:en_page) { create(:page, active: true, language: create(:language, :english), created_at: 1.year.ago) }
let!(:en_unpublished) { create(:page, active: false, language: create(:language, :english)) }
let!(:fr_page) { create(:page, active: true, language: create(:language, :french)) }
let!(:en_page) { create(:page, :published, language: create(:language, :english), created_at: 1.year.ago) }
let!(:en_unpublished) { create(:page, :unpublished, language: create(:language, :english)) }
let!(:fr_page) { create(:page, :published, language: create(:language, :french)) }

it 'returns pages by language' do
expect(subject.list(language: 'fr')).to match_array([fr_page])
Expand All @@ -29,10 +29,10 @@
end

describe '.list_featured' do
let!(:en_page) { create(:page, active: true, featured: true, language: create(:language, :english), created_at: 1.year.ago) }
let!(:en_unpublished) { create(:page, active: false, language: create(:language, :english)) }
let!(:en_unfeatured) { create(:page, active: true, language: create(:language, :english)) }
let!(:fr_page) { create(:page, active: true, featured: true, language: create(:language, :french)) }
let!(:en_page) { create(:page, :published, featured: true, language: create(:language, :english), created_at: 1.year.ago) }
let!(:en_unpublished) { create(:page, :unpublished, language: create(:language, :english)) }
let!(:en_unfeatured) { create(:page, :published, language: create(:language, :english)) }
let!(:fr_page) { create(:page, :published, featured: true, language: create(:language, :french)) }

it 'returns featured pages by language' do
expect(subject.list_featured(language: 'en')).to match_array([en_page])
Expand Down

0 comments on commit cacafc2

Please sign in to comment.