Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api integration #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions app/controllers/spree/api/v1/reviews_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Spree
module Api
module V1
class ReviewsController < Spree::Api::BaseController
before_action :find_product, only: [:index, :create]

def index
@reviews = Spree::Review.approved
.where(product: @product)
.page(params[:page])
.per(params[:per_page])
respond_with(@reviews)
end

def show
@review = Spree::Review.approved.find(params[:id])
respond_with(@review)
end

def create
@review = Spree::Review.new(review_params)
@review.product = @product
@review.user = spree_current_user if spree_user_signed_in?
@review.ip_address = request.remote_ip
@review.locale = I18n.locale.to_s if Spree::Reviews::Config[:track_locale]

authorize! :create, @review
if @review.save
respond_with(@review, status: 201, default_template: :show)
else
invalid_resource!(@review)
end
end

private
def find_product
@product = Spree::Product.find(params[:product_id])
authorize! :read, @product
end

def permitted_review_attributes
[:rating, :title, :review, :name, :show_identifier]
end

def review_params
params.require(:review).permit(permitted_review_attributes)
end
end
end
end
end
7 changes: 7 additions & 0 deletions app/views/spree/api/v1/reviews/index.v1.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object false
child(@reviews => :reviews) do
extends "spree/api/v1/reviews/show"
end
node(:count) { @reviews.count }
node(:current_page) { params[:page] ? params[:page].to_i : 1 }
node(:pages) { @reviews.total_pages }
9 changes: 9 additions & 0 deletions app/views/spree/api/v1/reviews/show.v1.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object @review
attributes :id, :rating, :title, :review, :show_identifier

node(:name) { |review| review.show_identifier ? review.name : Spree.t(:anonymous) }
node(:email) do |review|
if review.show_identifier && Spree::Reviews::Config[:show_email] && review.user.present?
review.user.email
end
end
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
resource :review_settings, only: [:edit, :update]
end

namespace :api, defaults: { format: 'json' } do
namespace :v1 do
resources :products do
resources :reviews, only: [:index, :show, :create]
end
end
end

resources :products, only: [] do
resources :reviews, only: [:index, :new, :create] do
end
Expand Down