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

Close Product Review #1383

Open
wants to merge 5 commits into
base: develop
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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ group :test do
gem 'webmock'
end

gem 'spree_reviews', github: 'bookmebus/spree_reviews', branch: 'spree-4-5-0-only-backend'

gemspec
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
GIT
remote: https://github.com/bookmebus/spree_reviews.git
revision: 60030182da9ef6c9c1fa7b859e19b8208f7a3b2a
branch: spree-4-5-0-only-backend
specs:
spree_reviews (4.0.0)
deface (~> 1.0)
spree_api (>= 4.5)
spree_backend (>= 4.5)
spree_core (>= 4.5)
spree_extension

GIT
remote: https://github.com/kimsrung/spree_dev_tools.git
revision: 83da7045d71138c9b12a2a3c118f8cd59f5cba83
Expand Down Expand Up @@ -877,6 +889,7 @@ DEPENDENCIES
shoulda-matchers (~> 5.0)
spree_cm_commissioner!
spree_dev_tools!
spree_reviews!
terminal-table (~> 3.0.1)
vcr
webmock
Expand Down
33 changes: 33 additions & 0 deletions app/controllers/spree/admin/taxon_star_ratings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Spree
module Admin
class TaxonStarRatingsController < Spree::Admin::ResourceController
before_action :load_resource

def create
context = SpreeCmCommissioner::StarRatingModifier.call(params: params, product: @product)
return unless context.success?

redirect_to admin_product_taxon_star_ratings_path(kind: params[:kind])
end

private

def model_class
SpreeCmCommissioner::TaxonStarRating
end

def object_name
'spree_cm_commissioner_taxon_star_rating'
end

def load_resource
@product ||= Spree::Product.find_by!(slug: params[:product_id])
@taxon_star_ratings ||= @product.taxon_star_ratings.where(kind: params[:kind]).all
@group_taxon_star_ratings ||= @taxon_star_ratings.group_by(&:star)
review_taxon = Spree::Taxon.where(kind: :review).first

@review_type_taxons ||= review_taxon ? review_taxon.children : []
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Spree
module Api
module V2
module Storefront
class FeedbackReviewsController < Spree::Api::V2::ResourceController
before_action :load_review

def create
feedback_review = @review.feedback_reviews.create(feedback_review_params)

if feedback_review.save
render_serialized_payload(201) { serialize_resource(feedback_review) }
else
render_error_payload(review, 400)
end
end

# override
def resource_serializer
SpreeCmCommissioner::V2::Storefront::FeedbackReviewSerializer
end

private

def load_review
@review ||= Spree::Review.find(params[:review_id])
end

def feedback_review_params
params.permit(:rating, :comment).merge(user: spree_current_user)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Spree
module Api
module V2
module Storefront
class ReviewImagesController < Spree::Api::V2::ResourceController
def create
context = SpreeCmCommissioner::ReviewImageCreator.call(
review: review,
url: params[:url]
)
if context.success?
render_serialized_payload { serialize_resource(context.result) }
else
render_error_payload(context.message)
end
end

private

def review
@review ||= Spree::Review.find(params[:user_review_id])
end

def resource_serializer
Spree::V2::Storefront::ReviewImageSerializer
end
end
end
end
end
end
36 changes: 36 additions & 0 deletions app/controllers/spree/api/v2/storefront/reviews_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Spree
module Api
module V2
module Storefront
class ReviewsController < Spree::Api::V2::ResourceController
def collection
vendor.reviews.approved
.where(params[:rating].present? ? { rating: params[:rating] } : {})
.page(params[:page])
.per(params[:per_page])
end

def show
review = Spree::Review.find(params[:id])
render_serialized_payload { serialize_resource(review) }
end

# override
def resource_serializer
SpreeCmCommissioner::V2::Storefront::ReviewSerializer
end

def collection_serializer
SpreeCmCommissioner::V2::Storefront::ReviewSerializer
end

private

def vendor
@vendor ||= Spree::Vendor.find(params[:vendor_id])
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Spree
module Api
module V2
module Storefront
class TaxonStarRatingsController < Spree::Api::V2::ResourceController
def collection
product.taxon_star_ratings.where(star: params[:star]).all
end

def collection_serializer
SpreeCmCommissioner::V2::Storefront::TaxonStarRatingSerializer
end

private

def product
@product ||= Spree::Product.find(params[:product_id])
end
end
end
end
end
end
51 changes: 51 additions & 0 deletions app/controllers/spree/api/v2/storefront/user_reviews_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Spree
module Api
module V2
module Storefront
class UserReviewsController < Spree::Api::V2::ResourceController
before_action :require_spree_current_user

def collection
product.taxon_star_ratings.where(star: params[:star]).all
end

def show
product_id = params[:id]
user_review = spree_current_user.reviews.find_by(product_id: product_id)
render_serialized_payload { serialize_resource(user_review) }
end

def create
user_review = product.reviews.create(review_params)
if user_review.save
render_serialized_payload(201) { serialize_resource(user_review) }
else
render_error_payload(user_review, 400)
end
end

# override
def resource_serializer
SpreeCmCommissioner::V2::Storefront::ReviewSerializer
end

def collection_serializer
SpreeCmCommissioner::V2::Storefront::TaxonStarRatingSerializer
end

private

def product
@product ||= Spree::Product.find(params[:product_id])
end

def review_params
params.permit(:rating, :title, :review, :name, :show_identifier, :product_id,
taxon_reviews_attributes: %i[taxon_id rating]
).merge(user: spree_current_user)
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions app/interactors/spree_cm_commissioner/review_image_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module SpreeCmCommissioner
class ReviewImageCreator < BaseInteractor
def call
presigned_url = SpreeCmCommissioner::S3UrlGenerator.s3_presigned_url(context.url)
response = Faraday.get(presigned_url)

if response.success?
review = context.review
io = StringIO.new(response.body)
filename = File.basename(context.url)

review_image = SpreeCmCommissioner::ReviewImage.new(viewable: review)
review_image.attachment.attach(io: io, filename: filename)

if review_image.save
context.result = review_image

else
context.fail!(message: review_image.errors.full_messages.join(','))
end
end
rescue StandardError => e
context.fail!(message: "Error fetching the remote image: #{e.message}")
end
end
end
26 changes: 26 additions & 0 deletions app/interactors/spree_cm_commissioner/star_rating_modifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module SpreeCmCommissioner
class StarRatingModifier < BaseInteractor
delegate :params, :product, to: :context

def call
remove_unselected_taxon_star_ratings
modify_taxon_star_ratings
end

def modify_taxon_star_ratings
params[:taxon_star_ratings]&.each do |object|
taxon_id, star = object.split(',')
product.taxon_star_ratings.build(
taxon_id: taxon_id,
star: star,
kind: params[:kind]
)
end
product.save
end

def remove_unselected_taxon_star_ratings
product.taxon_star_ratings.where(kind: params[:kind]).destroy_all
end
end
end
2 changes: 1 addition & 1 deletion app/models/concerns/spree_cm_commissioner/taxon_kind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module TaxonKind
extend ActiveSupport::Concern

included do
enum kind: { category: 0, cms: 1, event: 2, occupation: 3, nationality: 4 }
enum kind: { category: 0, cms: 1, event: 2, occupation: 3, nationality: 4, review: 5 }
end
end
end
2 changes: 2 additions & 0 deletions app/models/spree_cm_commissioner/line_item_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def self.prepended(base)
base.has_many :guests, class_name: 'SpreeCmCommissioner::Guest', dependent: :destroy
base.has_many :pending_guests, pending_guests_query, class_name: 'SpreeCmCommissioner::Guest', dependent: :destroy
base.has_many :product_completion_steps, class_name: 'SpreeCmCommissioner::ProductCompletionStep', through: :product
base.has_many :taxon_star_ratings, class_name: 'SpreeCmCommissioner::TaxonStarRating', through: :product

base.before_save :update_vendor_id
base.before_create :add_due_date, if: :subscription?

Expand Down
2 changes: 2 additions & 0 deletions app/models/spree_cm_commissioner/product_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def self.prepended(base)

base.has_many :complete_line_items, through: :classifications, source: :line_items

base.has_many :taxon_star_ratings, class_name: 'SpreeCmCommissioner::TaxonStarRating', dependent: :destroy

base.scope :min_price, lambda { |vendor|
joins(:prices_including_master)
.where(vendor_id: vendor.id, product_type: vendor.primary_product_type)
Expand Down
11 changes: 11 additions & 0 deletions app/models/spree_cm_commissioner/review_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SpreeCmCommissioner
module ReviewDecorator
def self.prepended(base)
base.has_many :images, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::ReviewImage'
base.has_many :taxon_reviews, class_name: 'SpreeCmCommissioner::TaxonReview', dependent: :destroy
base.accepts_nested_attributes_for :taxon_reviews, allow_destroy: true
end
end
end

Spree::Review.prepend(SpreeCmCommissioner::ReviewDecorator) unless Spree::Review.included_modules.include?(SpreeCmCommissioner::ReviewDecorator)
12 changes: 12 additions & 0 deletions app/models/spree_cm_commissioner/review_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module SpreeCmCommissioner
class ReviewImage < Asset
protected

def asset_styles
{
thumb: '60x60>',
small: '180x180>'
}
end
end
end
6 changes: 6 additions & 0 deletions app/models/spree_cm_commissioner/taxon_review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module SpreeCmCommissioner
class TaxonReview < Base
belongs_to :review, class_name: 'Spree::Review'
belongs_to :taxon, class_name: 'Spree::Taxon'
end
end
8 changes: 8 additions & 0 deletions app/models/spree_cm_commissioner/taxon_star_rating.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module SpreeCmCommissioner
class TaxonStarRating < Base
validates :star, presence: true
enum kind: { sub_rating: 0, selection: 1 }
belongs_to :taxon, class_name: 'Spree::Taxon'
belongs_to :product, class_name: 'Spree::Product'
end
end
1 change: 1 addition & 0 deletions app/models/spree_cm_commissioner/user_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.prepended(base)
base.has_many :check_ins, foreign_key: 'check_in_by_id', class_name: 'SpreeCmCommissioner::CheckIn'
base.has_many :user_events, class_name: 'SpreeCmCommissioner::UserEvent'
base.has_many :events, through: :user_events, class_name: 'Spree::Taxon', source: 'taxon'
base.has_many :reviews, foreign_key: :user_id, class_name: 'Spree::Review', dependent: :nullify

base.has_many :wished_items, class_name: 'Spree::WishedItem', through: :wishlists
base.has_many :promotions, through: :promotion_rules, class_name: 'Spree::Promotion'
Expand Down
2 changes: 2 additions & 0 deletions app/models/spree_cm_commissioner/vendor_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def self.prepended(base)
base.has_many :places,
through: :nearby_places, source: :place, class_name: 'SpreeCmCommissioner::Place'

base.has_many :reviews, through: :products

base.has_one :logo, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::VendorLogo'
base.has_one :payment_qrcode, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::VendorPaymentQrcode'
base.has_one :web_promotion_banner, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::VendorWebPromotionBanner'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- insert_after "erb[silent]:contains('Spree::Digital')" -->

<%= content_tag :li, class: 'nav-item' do %>
<%= link_to_with_icon 'star.svg',
Spree.t(:star_rating),
admin_product_taxon_star_ratings_url(@product, kind: :sub_rating),
class: "nav-link #{'active' if current == :star_rating}" %>
<% end %>
Loading
Loading