summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2015-01-11 09:39:08 -0700
committermo khan <mo@mokhan.ca>2015-01-11 09:39:08 -0700
commit2b450d0c49da229768467953bbdba9687fc40ee5 (patch)
treec7683a5d7f56cc40969c8d6176b1fe90d35faec4
parent227bdd2d5eb7532c5a5af7136f488fb410e1eabe (diff)
add tests for searching cakes.
-rw-r--r--app/controllers/api/v2/cakes_controller.rb2
-rw-r--r--app/controllers/concerns/pageable.rb4
-rw-r--r--app/models/creation.rb2
-rw-r--r--spec/controllers/api/v2/cakes_controller_spec.rb26
-rw-r--r--spec/factories.rb3
5 files changed, 32 insertions, 5 deletions
diff --git a/app/controllers/api/v2/cakes_controller.rb b/app/controllers/api/v2/cakes_controller.rb
index ec38e753..4c85fd90 100644
--- a/app/controllers/api/v2/cakes_controller.rb
+++ b/app/controllers/api/v2/cakes_controller.rb
@@ -7,7 +7,7 @@ module Api
end
def index
- @cakes = @repository.search_with(params).includes(:category).page(page).per(per_page)
+ @cakes = paginate(@repository.search_with(params).includes(:category))
end
def show(id = params[:id])
diff --git a/app/controllers/concerns/pageable.rb b/app/controllers/concerns/pageable.rb
index 606d7612..3b2c5bdd 100644
--- a/app/controllers/concerns/pageable.rb
+++ b/app/controllers/concerns/pageable.rb
@@ -9,4 +9,8 @@ module Pageable
def per_page
params[:per_page] || DEFAULT_PER_PAGE
end
+
+ def paginate(items)
+ items.page(page).per(per_page)
+ end
end
diff --git a/app/models/creation.rb b/app/models/creation.rb
index 901e2db1..d576803d 100644
--- a/app/models/creation.rb
+++ b/app/models/creation.rb
@@ -1,7 +1,7 @@
class Creation < ActiveRecord::Base
validates :name, presence: true
validates :category_id, presence: true
- belongs_to :user, :counter_cache => true
+ belongs_to :user, counter_cache: true
belongs_to :category
has_many :photos, -> { order :created_at }, dependent: :destroy, as: :imageable
has_many :favorites, :dependent => :destroy
diff --git a/spec/controllers/api/v2/cakes_controller_spec.rb b/spec/controllers/api/v2/cakes_controller_spec.rb
index f313285e..93ba4beb 100644
--- a/spec/controllers/api/v2/cakes_controller_spec.rb
+++ b/spec/controllers/api/v2/cakes_controller_spec.rb
@@ -6,10 +6,32 @@ module Api
render_views
describe "#index" do
- let!(:cake) { create(:published_cake) }
+ let!(:cakes) { create(:category, slug: 'cakes') }
+ let!(:cookies) { create(:category, slug: 'cookies') }
+ let!(:cake) { create(:published_cake, name: 'cake', category: cakes) }
+ let!(:cookie) { create(:published_cake, name: 'cookie', category: cookies) }
+ let!(:unpublished_cake) { create(:cake, name: 'unpublished', category: cakes) }
- it 'returns all cakes in the page' do
+ it 'returns all published cakes' do
xhr :get, :index
+ expect(assigns(:cakes)).to match_array([cake, cookie])
+ end
+
+ it 'returns all cakes in the category' do
+ xhr :get, :index, category: cookie.category.slug
+ expect(assigns(:cakes)).to match_array([cookie])
+ end
+
+ it 'returns all cakes matching the search query' do
+ xhr :get, :index, q: cake.name[0..2]
+ expect(assigns(:cakes)).to match_array([cake])
+ end
+
+ it 'returns all cakes tagged with the tag' do
+ cake.tag_list = 'cakes'
+ cake.save!
+
+ xhr :get, :index, tags: 'cakes'
expect(assigns(:cakes)).to match_array([cake])
end
end
diff --git a/spec/factories.rb b/spec/factories.rb
index 64309da3..8cdc18f3 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -15,6 +15,7 @@ FactoryGirl.define do
association :user
association :category
factory :published_cake do
+ photos_count 1
after(:create) do |cake, evaluator|
cake.photos << create(:photo, image: 'spec/fixtures/images/example.png')
end
@@ -81,4 +82,4 @@ FactoryGirl.define do
name { Faker::Name.name }
asin { SecureRandom.uuid }
end
-end \ No newline at end of file
+end