[176209168] Add jquery rating plugin with stars - #31
Open
mikhail-kilin wants to merge 5 commits into
Open
Conversation
mikhail-kilin
temporarily deployed
to
blog-add-stars-va0xcoq1dqjxgba
November 29, 2020 17:58
Inactive
mikhail-kilin
temporarily deployed
to
blog-add-stars-qwljibodc4e6wij
December 12, 2020 16:06
Inactive
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 | |||
Contributor
There was a problem hiding this comment.
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 | |||
|
|
||
| private | ||
|
|
||
| def rating_params |
Contributor
There was a problem hiding this comment.
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" |
Contributor
There was a problem hiding this comment.
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? |
Contributor
There was a problem hiding this comment.
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? |
| class Rating < ApplicationRecord | ||
| validates :rate, inclusion: { in: [1, 2, 3, 4, 5] } | ||
|
|
||
| belongs_to :user, required: true |
Contributor
There was a problem hiding this comment.
by this article belongs_to assoc required by defailt in rails 5
Suggested change
| belongs_to :user, required: true | |
| belongs_to :user |
mikhail-kilin
temporarily deployed
to
blog-add-stars-m9xgelsqkpxuzzd
December 17, 2020 16:32
Inactive
mikhail-kilin
temporarily deployed
to
blog-add-stars-evuo2vtyaxuumoz
December 17, 2020 16:56
Inactive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
.env.exampleand added to HerokuDeploy notes
None