Skip to content
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
1 change: 1 addition & 0 deletions app/assets/javascripts/rate/rating_file.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions app/assets/javascripts/rate/rating_init.js
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();
}
}
}
});
}
11 changes: 8 additions & 3 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ class ArticlesController < ApplicationController
expose_decorated :article, -> { set_article }
expose :comment, -> { set_comment }
expose_decorated :company, -> { article.company }
expose :rating, -> { set_rating }

layout "company"

def show
authorize article
end

private

def set_article
Article.includes(:comments).find_by(id: params["id"])
Article.includes(:comments, :ratings).find_by(id: params["id"])
end

private

def set_comment
article.comments.new
end

def set_rating
Rating.find_or_initialize_by(article: article, user: current_user)
end
end
24 changes: 24 additions & 0 deletions app/controllers/ratings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class RatingsController < ApplicationController
Comment thread
mikhail-kilin marked this conversation as resolved.
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?

before_action :authenticate_user!
expose_decorated :article
expose :rating, build: ->(rating_params) { Rating.new(rating_params) }

Comment thread
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

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

{
user: current_user,
article_id: params[:article_id],
rate: params[:rating][:rate]
}
end
end
6 changes: 6 additions & 0 deletions app/decorators/article_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

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?


"#{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
end
1 change: 1 addition & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Article < ApplicationRecord
belongs_to :user
belongs_to :company
has_many :comments, dependent: :destroy
has_many :ratings, dependent: :destroy

paginates_per 5

Expand Down
6 changes: 6 additions & 0 deletions app/models/rating.rb
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] }
Comment thread
mikhail-kilin marked this conversation as resolved.

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

belongs_to :article, required: true
end
5 changes: 5 additions & 0 deletions app/views/articles/_form.html.slim
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
3 changes: 3 additions & 0 deletions app/views/articles/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ article.showed
= "Published #{distance_of_time_in_words_to_now(article.updated_at)} ago"
div
= "#{article.author}"
.medium-2.columns
div(id="review")
= render "form"

= render article.sorted_comments

Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/company.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ html class="no-js" lang="en"
= csrf_meta_tags

= stylesheet_link_tag :application
= stylesheet_link_tag "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css"

= javascript_tag "window.App = {}"

Expand Down
3 changes: 3 additions & 0 deletions app/views/ratings/create.js.erb
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') %>");
3 changes: 3 additions & 0 deletions app/views/ratings/update.js.erb
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') %>");
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

resources :articles, only: :show do
resources :comments, except: %i[index new]
resources :ratings, only: %i[create update]
end

namespace :admin_scope do
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20201129151451_create_ratings.rb
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
11 changes: 11 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
t.index ["company_id"], name: "index_companies_users_on_company_id"
end

create_table "ratings", force: :cascade do |t|
t.bigint "article_id", null: false
t.bigint "user_id", null: false
t.integer "rate", default: 0, null: false
t.index ["article_id", "user_id"], name: "index_ratings_on_article_id_and_user_id", unique: true
t.index ["article_id"], name: "index_ratings_on_article_id"
t.index ["user_id"], name: "index_ratings_on_user_id"
end

create_table "static_pages", force: :cascade do |t|
t.string "title"
t.text "content"
Expand Down Expand Up @@ -101,4 +110,6 @@
t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
end

add_foreign_key "ratings", "articles"
add_foreign_key "ratings", "users"
end
7 changes: 7 additions & 0 deletions spec/factories/ratings.rb
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
20 changes: 20 additions & 0 deletions spec/features/user/ratings/create_spec.rb
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
18 changes: 18 additions & 0 deletions spec/features/user/ratings/update_spec.rb
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