Skip to content

Commit

Permalink
Fix coding style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aldesantis committed Nov 14, 2019
1 parent 974cf33 commit b6ae6d4
Show file tree
Hide file tree
Showing 29 changed files with 197 additions and 186 deletions.
2 changes: 1 addition & 1 deletion app/controllers/spree/feedback_reviews_controller.rb
Expand Up @@ -30,7 +30,7 @@ def create
protected

def load_review
@review ||= Spree::Review.find_by_id!(params[:review_id])
@review ||= Spree::Review.find_by!(id: params[:review_id])
end

def permitted_feedback_review_attributes
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/spree/reviews_controller.rb
Expand Up @@ -15,7 +15,7 @@ def new

# save if all ok
def create
params[:review][:rating].sub!(/\s*[^0-9]*\z/, '') unless params[:review][:rating].blank?
params[:review][:rating].sub!(/\s*[^0-9]*\z/, '') if params[:review][:rating].present?

@review = Spree::Review.new(review_params)
@review.product = @product
Expand Down
11 changes: 6 additions & 5 deletions app/decorators/models/solidus_reviews/spree/product_decorator.rb
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module SolidusReviews
module Spree
module ProductDecorator
Expand All @@ -16,11 +17,11 @@ def recalculate_rating
reviews_count = reviews.reload.default_approval_filter.count

self.reviews_count = reviews_count
if reviews_count > 0
self.avg_rating = '%.1f' % (reviews.default_approval_filter.sum(:rating).to_f / reviews_count)
else
self.avg_rating = 0
end
self.avg_rating = if reviews_count > 0
'%.1f' % (reviews.default_approval_filter.sum(:rating).to_f / reviews_count)
else
0
end
save
end

Expand Down
10 changes: 5 additions & 5 deletions app/helpers/spree/reviews_helper.rb
Expand Up @@ -18,10 +18,10 @@ def txt_stars(n, show_out_of = true)
def display_verified_purchaser?(review)
Spree::Reviews::Config[:show_verified_purchaser] && review.user &&
Spree::LineItem.joins(:order, :variant)
.where.not(spree_orders: { completed_at: nil })
.find_by(
spree_variants: { product_id: review.product_id },
spree_orders: { user_id: review.user_id }
).present?
.where.not(spree_orders: { completed_at: nil })
.find_by(
spree_variants: { product_id: review.product_id },
spree_orders: { user_id: review.user_id }
).present?
end
end
2 changes: 1 addition & 1 deletion app/models/spree/feedback_review.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class Spree::FeedbackReview < ActiveRecord::Base
class Spree::FeedbackReview < ApplicationRecord
belongs_to :user, class_name: Spree.user_class.to_s, optional: true

belongs_to :review, dependent: :destroy
Expand Down
17 changes: 9 additions & 8 deletions app/models/spree/review.rb
@@ -1,11 +1,11 @@
# frozen_string_literal: true

class Spree::Review < ActiveRecord::Base
class Spree::Review < ApplicationRecord
belongs_to :product, touch: true, optional: true
belongs_to :user, class_name: Spree.user_class.to_s, optional: true
has_many :feedback_reviews
has_many :images, -> { order(:position) }, as: :viewable,
dependent: :destroy, class_name: "Spree::Image"
dependent: :destroy, class_name: "Spree::Image"

before_create :verify_purchaser
after_save :recalculate_product_rating, if: :approved?
Expand All @@ -28,6 +28,7 @@ class Spree::Review < ActiveRecord::Base

def feedback_stars
return 0 if feedback_reviews.size <= 0

((feedback_reviews.sum(:rating) / feedback_reviews.size) + 0.5).floor
end

Expand All @@ -36,18 +37,18 @@ def recalculate_product_rating
end

def email
user.try!(:email)
user&.email
end

def verify_purchaser
return unless user_id && product_id

verified_purchase = Spree::LineItem.joins(:order, :variant)
.where.not(spree_orders: { completed_at: nil })
.find_by(
spree_variants: { product_id: product_id },
spree_orders: { user_id: user_id }
).present?
.where.not(spree_orders: { completed_at: nil })
.find_by(
spree_variants: { product_id: product_id },
spree_orders: { user_id: user_id }
).present?

self.verified_purchaser = verified_purchase
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/spree/reviews_ability.rb
Expand Up @@ -7,11 +7,11 @@ def initialize(user)
review_ability_class = self.class

can :create, Spree::Review do |_review|
review_ability_class.allow_anonymous_reviews? || !user.email.blank?
review_ability_class.allow_anonymous_reviews? || user.email.present?
end

can :create, Spree::FeedbackReview do |_review|
review_ability_class.allow_anonymous_reviews? || !user.email.blank?
review_ability_class.allow_anonymous_reviews? || user.email.present?
end

# You can read your own reviews, and everyone can read approved ones
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/api/reviews/_feedback_review.json.jbuilder
@@ -1,5 +1,5 @@
# frozen_string_literal: true

json.cache! [I18n.locale, feedback_review] do
json.(feedback_review, *feedback_review_attributes)
json.call(feedback_review, *feedback_review_attributes)
end
2 changes: 1 addition & 1 deletion app/views/spree/api/reviews/_review.json.jbuilder
@@ -1,7 +1,7 @@
# frozen_string_literal: true

json.cache! [I18n.locale, review] do
json.(review, *review_attributes)
json.call(review, *review_attributes)
json.images(review.images) do |image|
json.partial!("spree/api/images/image", image: image)
end
Expand Down
3 changes: 1 addition & 2 deletions db/migrate/20120123141326_recalculate_ratings.rb
Expand Up @@ -16,6 +16,5 @@ def up
end
end

def down
end
def down; end
end
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class AddVerifiedPurchaserToReviews < SolidusSupport::Migration[4.2]
def change
add_column :spree_reviews, :verified_purchaser, :boolean, default: false
Expand Down
30 changes: 16 additions & 14 deletions lib/controllers/spree/api/reviews_controller.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Spree
module Api
class ReviewsController < Spree::Api::BaseController
Expand All @@ -9,11 +11,11 @@ class ReviewsController < Spree::Api::BaseController
before_action :prevent_multiple_reviews, only: [:create]

def index
if @product
@reviews = Spree::Review.default_approval_filter.where(product: @product)
else
@reviews = Spree::Review.where(user: @current_api_user)
end
@reviews = if @product
Spree::Review.default_approval_filter.where(product: @product)
else
Spree::Review.where(user: @current_api_user)
end

respond_with(@reviews)
end
Expand All @@ -34,7 +36,7 @@ def create

authorize! :create, @review
if @review.save
render json: @review, include: [:images, :feedback_reviews], status: 201
render json: @review, include: [:images, :feedback_reviews], status: :created
else
invalid_resource!(@review)
end
Expand All @@ -46,7 +48,7 @@ def update
attributes = review_params.merge(ip_address: request.remote_ip, approved: false)

if @review.update(attributes)
render json: @review, include: [:images, :feedback_reviews], status: 200
render json: @review, include: [:images, :feedback_reviews], status: :ok
else
invalid_resource!(@review)
end
Expand All @@ -56,7 +58,7 @@ def destroy
authorize! :destroy, @review

if @review.destroy
render json: @review, status: 200
render json: @review, status: :ok
else
invalid_resource!(@review)
end
Expand All @@ -74,11 +76,11 @@ def review_params

# Loads product from product id.
def load_product
if params[:product_id]
@product = Spree::Product.friendly.find(params[:product_id])
else
@product = @review&.product
end
@product = if params[:product_id]
Spree::Product.friendly.find(params[:product_id])
else
@review&.product
end
end

# Finds user based on api_key or by user_id if api_key belongs to an admin.
Expand All @@ -104,7 +106,7 @@ def prevent_multiple_reviews
# Converts rating strings like "5 units" to "5"
# Operates on params
def sanitize_rating
params[:rating].sub!(/\s*[^0-9]*\z/, '') unless params[:rating].blank?
params[:rating].sub!(/\s*[^0-9]*\z/, '') if params[:rating].present?
end
end
end
Expand Down
Expand Up @@ -11,7 +11,7 @@ def add_javascripts
end

def add_stylesheets
inject_into_file "vendor/assets/stylesheets/spree/frontend/all.css", " *= require spree/frontend/solidus_reviews\n", before: /\*\//, verbose: true
inject_into_file "vendor/assets/stylesheets/spree/frontend/all.css", " *= require spree/frontend/solidus_reviews\n", before: %r{\*/}, verbose: true
end

def add_migrations
Expand Down
Expand Up @@ -10,7 +10,7 @@
allow(controller).to receive(:spree_current_user).and_return(user)
end

context '#index' do
describe '#index' do
let!(:review) { create(:review) }
let!(:other_review) { create(:review) }

Expand Down
Expand Up @@ -10,7 +10,7 @@
allow(controller).to receive(:spree_current_user).and_return(user)
end

context '#update' do
describe '#update' do
it 'redirects to edit-review-settings page' do
put :update, params: { preferences: { preview_size: 4 } }
expect(response).to redirect_to spree.edit_admin_review_settings_path
Expand All @@ -22,7 +22,6 @@
feedback_rating: false,
require_login: true,
track_locale: true' do

it 'sets preferred_preview_size to 4' do
put :update, params: { preferences: { preview_size: 4 } }
expect(Spree::Reviews::Config.preferred_preview_size).to eq 4
Expand Down Expand Up @@ -50,8 +49,8 @@
end
end

context '#edit' do
it 'should render the edit template' do
describe '#edit' do
it 'renders the edit template' do
get :edit
expect(response).to render_template(:edit)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/spree/admin/reviews_controller_spec.rb
Expand Up @@ -13,7 +13,7 @@
allow(controller).to receive(:spree_current_user).and_return(user)
end

context '#index' do
describe '#index' do
it 'list reviews' do
reviews = [
create(:review, product: product),
Expand All @@ -24,7 +24,7 @@
end
end

context '#approve' do
describe '#approve' do
it 'show notice message when approved' do
review.update_attribute(:approved, true)
get :approve, params: { id: review.id }
Expand All @@ -39,7 +39,7 @@
end
end

context '#edit' do
describe '#edit' do
specify do
get :edit, params: { id: review.id }
expect(response.status).to eq(200)
Expand Down

0 comments on commit b6ae6d4

Please sign in to comment.