Skip to content

[176209168] Add jquery rating plugin with stars - #31

Open
mikhail-kilin wants to merge 5 commits into
masterfrom
add-stars
Open

[176209168] Add jquery rating plugin with stars#31
mikhail-kilin wants to merge 5 commits into
masterfrom
add-stars

Conversation

@mikhail-kilin

@mikhail-kilin mikhail-kilin commented Nov 29, 2020

Copy link
Copy Markdown
Owner

Summary

User can rate article with the help of stars js plugin

https://www.pivotaltracker.com/story/show/176209168

Test plan

None

Review notes

While reviewing pull-request (especially when it's your pull-request),
please make sure that:

  • you understand what problem is solved by PR and how is it solved
  • new tests are in place, no redundant tests
  • DB schema changes reflect new migrations
  • newly introduces DB fields have indexes and constraints
  • routes are RESTful, no useless routes
  • there are no missed files (migrations, view templates)
  • required ENV variables added and described in .env.example and added to Heroku
  • associated Heroku review app works correctly with introduced changes

Deploy notes

None

@mikhail-kilin
mikhail-kilin temporarily deployed to blog-add-stars-va0xcoq1dqjxgba November 29, 2020 17:58 Inactive
Comment thread app/controllers/articles_controller.rb Outdated
Comment thread app/decorators/article_decorator.rb Outdated
Comment thread app/controllers/articles_controller.rb Outdated
Comment thread app/models/article.rb Outdated
Comment thread app/models/rating.rb Outdated
Comment thread app/models/rating.rb
Comment thread app/policies/rating_policy.rb Outdated
Comment thread app/views/articles/_form.html.slim Outdated
Comment thread db/migrate/20201129151451_create_ratings.rb Outdated
Comment thread db/migrate/20201129151451_create_ratings.rb Outdated
Comment thread db/migrate/20201129151451_create_ratings.rb Outdated
Comment thread db/migrate/20201129151451_create_ratings.rb Outdated
Comment thread app/controllers/ratings_controller.rb
Comment thread app/controllers/ratings_controller.rb
Comment thread app/models/rating.rb Outdated
@mikhail-kilin
mikhail-kilin temporarily deployed to blog-add-stars-qwljibodc4e6wij December 12, 2020 16:06 Inactive
@mikhail-kilin
mikhail-kilin temporarily deployed to blog-add-stars-i486fykbaghvzhr December 12, 2020 16:10 Inactive
@@ -0,0 +1 @@
!function(t){var e={};function r(a){if(e[a])return e[a].exports;var s=e[a]={i:a,l:!1,exports:{}};return t[a].call(s.exports,s,s.exports,r),s.l=!0,s.exports}r.m=t,r.c=e,r.d=function(t,e,a){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:a})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var a=Object.create(null);if(r.r(a),Object.defineProperty(a,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)r.d(a,s,function(e){return t[e]}.bind(null,s));return a},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e){const r={value:Number($("#rating_rate").val()),stars:5,half:!1,emptyStar:"far fa-star",halfStar:"fas fa-star-half-alt",filledStar:"fas fa-star",color:"#fcd703",readonly:!1,click:function(t){console.error("No click callback provided!")}};jQuery.fn.extend({rating:function(t={}){return this.each((function(){$(this).attr("rating")&&$(this).empty(),this.stars=t.value?t.value:r.value,this.readonly=t.readonly?t.readonly:r.readonly,this.getStars=function(){return $(this).find($("i"))},$(this).css({color:t.color?t.color:r.color}).attr("rating",!0),this.readonly||($(this).off("mousemove").on("mousemove",(function(e){let a=t.half?t.half:r.half;if(this.getStars().index(e.target)>=0)if(a){$(this).find("i").attr("class",t.emptyStar?t.emptyStar:r.emptyStar);let a=.5;$(this).find("i").css({width:$(this).find("i").outerWidth()}),e.offsetX>$(e.target).outerWidth()/2&&(a=1);let s=this.getStars().index(e.target)+a;for(let e=0;e<this.getStars().length;e++)e+.5<s?$(this.getStars()[e]).attr("class",t.filledStar?t.filledStar:r.filledStar):e<s&&$(this.getStars()[e]).attr("class",t.halfStar?t.halfStar:r.halfStar)}else{$(this).find("i").attr("class",t.emptyStar?t.emptyStar:r.emptyStar);let a=this.getStars().index(e.target)+1;for(let e=0;e<this.getStars().length;e++)e<a&&$(this.getStars()[e]).attr("class",t.filledStar?t.filledStar:r.filledStar)}})),$(this).off("mouseout").on("mouseout",(function(t){this.printStars()})),$(this).off("click").on("click",(function(e){if(t.half?t.half:r.half){let t=.5;e.offsetX>$(e.target).outerWidth()/2&&(t=1),this.stars=this.getStars().index(e.target)+t}else this.stars=this.getStars().index(e.target)+1;(t.click?t.click:r.click)({stars:this.stars,event:e})})));const e=t.stars?t.stars:r.stars;for(let a=0;a<e;a++){let e=$("<i></i>").addClass(t.emptyStar?t.emptyStar:r.emptyStar).appendTo($(this));if(this.readonly||e.css({cursor:"pointer"}),a>1e3)return}if(this.printStars=function(){if(t.half?t.half:r.half){$(this).find("i").attr("class",t.emptyStar?t.emptyStar:r.emptyStar);for(let e=0;e<this.stars;e++)e<this.stars-.5?$(this.getStars()[e]).attr("class",t.filledStar?t.filledStar:r.filledStar):$(this.getStars()[e]).attr("class",t.halfStar?t.halfStar:r.halfStar)}else{$(this).find("i").attr("class",t.emptyStar?t.emptyStar:r.emptyStar);for(let e=0;e<this.stars;e++)$(this.getStars()[e]).attr("class",t.filledStar?t.filledStar:r.filledStar)}},this.stars>0){this.printStars();(t.click?t.click:r.click)({stars:this.stars})}}))}}),$((function(){$("[data-rating-stars]").each((function(){let t={},e=/^data-rating\-(.+)$/;$.each($(this).get(0).attributes,(function(r,a){if(e.test(a.nodeName)){let r=a.nodeName.match(e)[1];t[r]=a.nodeValue}})),null!=t.input&&(t.click=function(e){$(t.input).val(e.stars)}),$(this).rating(t)}))}))}]); No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like, this file is minified. for this reason, should we change filename to rating_file.min.js?

@@ -0,0 +1,24 @@
class RatingsController < ApplicationController
expose :rating

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant?


private

def rating_params

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expose :rating, build: ->(rating_params, _scope) { current_user.ratings.new(rating_params) }

private 

def rating_params
  params.require(:rating).permit(:rate).to_h.merge(article: article)
end

def rating
return "Nobody has rated on this article yet" unless object.ratings.any?

"#{object.ratings.average(:rate).round(2)}/5"

@sergeyantonov1 sergeyantonov1 Dec 16, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"#{object.ratings.average(:rate).round(2)}/5"
"#{average_rating}/5"
def average_rating
  @average_rating ||= object.ratings.average(:rate).round(2)
end

end

def rating
return "Nobody has rated on this article yet" unless object.ratings.any?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, using locales is good practice

Suggested change
return "Nobody has rated on this article yet" unless object.ratings.any?
return I18n.t("some.locale") unless object.ratings.any?

Comment thread app/models/rating.rb
class Rating < ApplicationRecord
validates :rate, inclusion: { in: [1, 2, 3, 4, 5] }

belongs_to :user, required: true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by this article belongs_to assoc required by defailt in rails 5

Suggested change
belongs_to :user, required: true
belongs_to :user

@mikhail-kilin
mikhail-kilin temporarily deployed to blog-add-stars-m9xgelsqkpxuzzd December 17, 2020 16:32 Inactive
@mikhail-kilin mikhail-kilin changed the title Added stars jquery plugin [176209168] Add jquery rating plugin with stars Dec 17, 2020
@mikhail-kilin
mikhail-kilin temporarily deployed to blog-add-stars-evuo2vtyaxuumoz December 17, 2020 16:56 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants