-
Notifications
You must be signed in to change notification settings - Fork 0
[176209168] Add jquery rating plugin with stars #31
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| if ($("#review").length > 0) { | ||
|
|
||
| $("#review").rating({ | ||
| "click":function (e) { | ||
| if (e.stars > 0 && e.stars <= 5) { | ||
| if ($("#new_rating").length > 0) { | ||
| $("#rating_rate").val(e.stars); | ||
| $("#new_rating").submit(); | ||
| } else { | ||
| $("#rating_rate").val(e.stars); | ||
| $(".edit_rating").submit(); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class RatingsController < ApplicationController | ||
| expose :rating | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redundant? |
||
| before_action :authenticate_user! | ||
| expose_decorated :article | ||
| expose :rating, build: ->(rating_params) { Rating.new(rating_params) } | ||
|
|
||
|
mikhail-kilin marked this conversation as resolved.
|
||
| def create | ||
| redirect_to article_path(article) unless rating.save | ||
| end | ||
|
|
||
| def update | ||
| redirect_to article_path(article) unless rating.update rating_params | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def rating_params | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| { | ||
| user: current_user, | ||
| article_id: params[:article_id], | ||
| rate: params[:rating][:rate] | ||
| } | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,4 +13,10 @@ def author | |||||
| def sorted_comments | ||||||
| object.comments.sorted_by_created_at | ||||||
| end | ||||||
|
|
||||||
| def rating | ||||||
| return "Nobody has rated on this article yet" unless object.ratings.any? | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, using locales is good practice
Suggested change
|
||||||
|
|
||||||
| "#{object.ratings.average(:rate).round(2)}/5" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
def average_rating
@average_rating ||= object.ratings.average(:rate).round(2)
end |
||||||
| end | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,6 @@ | ||||||
| class Rating < ApplicationRecord | ||||||
| validates :rate, inclusion: { in: [1, 2, 3, 4, 5] } | ||||||
|
mikhail-kilin marked this conversation as resolved.
|
||||||
|
|
||||||
| belongs_to :user, required: true | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :article, required: true | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .row(id="rating_data") | ||
| .medium-6.columns | ||
| = "Rating: #{article.rating}" | ||
| = simple_form_for [article, rating], remote: true do |f| | ||
| = f.input :rate, as: :hidden |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| $("#rating_data").html(""); | ||
|
|
||
| $("#rating_data").append("<%= escape_javascript(render :partial => 'articles/form') %>"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| $("#rating_data").html(""); | ||
|
|
||
| $("#rating_data").append("<%= escape_javascript(render :partial => 'articles/form') %>"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class CreateRatings < ActiveRecord::Migration[5.2] | ||
| def change | ||
| create_table :ratings do |t| | ||
| t.references :article, foreign_key: true, null: false | ||
| t.references :user, foreign_key: true, null: false | ||
| t.integer :rate, default: 0, null: false | ||
| end | ||
|
|
||
| add_index :ratings, [:article_id, :user_id], unique: true | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| FactoryBot.define do | ||
| factory :rating do | ||
| user | ||
| article | ||
| rate { 5 } | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require "rails_helper" | ||
|
|
||
| feature "Create Rating" do | ||
| include_context "current user signed in" | ||
| let(:company) { create :company, owner: current_user } | ||
| let(:article) { create :article, :company, user: current_user, company: company } | ||
| let(:another_user) { create :user } | ||
|
|
||
| background do | ||
| create :rating, user: another_user, article: article, rate: 5 | ||
| visit article_path(article) | ||
| end | ||
|
|
||
| scenario "User choose 1 point", js: true do | ||
| expect(page).to have_content("5.0/5") | ||
| star = find(:xpath, "//i[1]") | ||
| star.click | ||
| expect(page).to have_content("3.0/5") | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| require "rails_helper" | ||
|
|
||
| feature "Update Rating" do | ||
| include_context "current user signed in" | ||
| let(:company) { create :company, owner: current_user } | ||
| let(:article) { create :article, :company, user: current_user, company: company } | ||
|
|
||
| background do | ||
| create :rating, user: current_user, article: article, rate: 4 | ||
| visit article_path(article) | ||
| end | ||
|
|
||
| scenario "User choose 3 points", js: true do | ||
| expect(page).to have_content("4.0/5") | ||
| find(:xpath, "//i[3]").click | ||
| expect(page).to have_content("3.0/5") | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.