Skip to content

Commit

Permalink
Merge pull request #46 from ericsaupe/api-fixes
Browse files Browse the repository at this point in the history
Adds missing API features to the index and show endpoints
  • Loading branch information
ericsaupe authored Aug 15, 2019
2 parents 3287105 + ccd9d32 commit 0c4500b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/helpers/spree/api/api_helpers_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
Spree::Api::ApiHelpers.module_eval do
@@review_attributes = [
:id, :product_id, :name, :location, :rating, :title, :review, :approved,
:created_at, :updated_at, :user_id, :ip_address, :locale, :show_identifier
:created_at, :updated_at, :user_id, :ip_address, :locale, :show_identifier,
:verified_purchaser
]

@@feedback_review_attributes = [
:id, :user_id, :review_id, :rating, :comment, :created_at, :updated_at, :locale
]

def review_attributes
@@review_attributes
end

def feedback_review_attributes
@@feedback_review_attributes
end
end
5 changes: 5 additions & 0 deletions app/views/spree/api/reviews/_feedback_review.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

json.cache! [I18n.locale, feedback_review] do
json.(feedback_review, *feedback_review_attributes)
end
3 changes: 3 additions & 0 deletions app/views/spree/api/reviews/_review.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ json.cache! [I18n.locale, review] do
json.images(review.images) do |image|
json.partial!("spree/api/images/image", image: image)
end
json.feedback_reviews(review.feedback_reviews) do |feedback_review|
json.partial!("spree/api/reviews/feedback_review", feedback_review: feedback_review)
end
end
3 changes: 3 additions & 0 deletions spec/controllers/spree/api/reviews_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
it 'returns all approved reviews for the product' do
review.update(approved: true)
review.images << create(:image)
review.feedback_reviews << create(:feedback_review, review: review)
expect(Spree::Review.count).to be >= 2
expect(subject.size).to eq(2)
expect(subject["reviews"][0]["id"]).to eq(review.id)
expect(subject["reviews"][0]["images"].count).to eq(1)
expect(subject["reviews"][0]["feedback_reviews"].count).to eq(1)
expect(subject["reviews"][0]["verified_purchaser"]).to eq(false)
expect(subject["avg_rating"]).to eq("5.0")
end
end
Expand Down

0 comments on commit 0c4500b

Please sign in to comment.