From 4103861f9088d5d5131ec4961edbd1d00ca595cf Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 27 Aug 2018 12:59:22 +0200 Subject: [PATCH 1/5] Add the rating column to feedbacks --- app/models/feedback.rb | 6 ++++++ db/migrate/20180827102828_add_rating_to_feedbacks.rb | 5 +++++ db/schema.rb | 3 ++- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180827102828_add_rating_to_feedbacks.rb diff --git a/app/models/feedback.rb b/app/models/feedback.rb index 63affddde..d697541d9 100644 --- a/app/models/feedback.rb +++ b/app/models/feedback.rb @@ -1,3 +1,9 @@ class Feedback < ApplicationRecord belongs_to :user + + enum rating: { + happy: 'happy', + neutral: 'neutral', + unhappy: 'unhappy' + } end diff --git a/db/migrate/20180827102828_add_rating_to_feedbacks.rb b/db/migrate/20180827102828_add_rating_to_feedbacks.rb new file mode 100644 index 000000000..cc3c0c97d --- /dev/null +++ b/db/migrate/20180827102828_add_rating_to_feedbacks.rb @@ -0,0 +1,5 @@ +class AddRatingToFeedbacks < ActiveRecord::Migration[5.2] + def change + add_column :feedbacks, :rating, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 492fe9f51..f3ed94862 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_08_22_162952) do +ActiveRecord::Schema.define(version: 2018_08_27_102828) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -323,6 +323,7 @@ ActiveRecord::Schema.define(version: 2018_08_22_162952) do t.integer "mark" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "rating" t.index ["user_id"], name: "index_feedbacks_on_user_id" end From e9a262947fda3705162fc766064f069444294bd7 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 27 Aug 2018 13:06:04 +0200 Subject: [PATCH 2/5] Migrate marks to ratings on feedbacks --- lib/tasks/2018_08_27_migrate_feedbacks.rake | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 lib/tasks/2018_08_27_migrate_feedbacks.rake diff --git a/lib/tasks/2018_08_27_migrate_feedbacks.rake b/lib/tasks/2018_08_27_migrate_feedbacks.rake new file mode 100644 index 000000000..f57a3bd3a --- /dev/null +++ b/lib/tasks/2018_08_27_migrate_feedbacks.rake @@ -0,0 +1,19 @@ +require Rails.root.join("lib", "tasks", "task_helper") + +namespace :'2018_08_27_migrate_feedbacks' do + task run: :environment do + MAPPING = { + 0 => Feedback.ratings.fetch(:unhappy), + 1 => Feedback.ratings.fetch(:neutral), + 2 => Feedback.ratings.fetch(:happy) + } + + MAPPING.keys.each do |mark| + rating = MAPPING[mark] + + Feedback + .where(mark: mark) + .update_all(rating: rating) + end + end +end From 3ba4ce0d378a1d16a9b89593fabe95f0cc21e467 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 27 Aug 2018 13:06:23 +0200 Subject: [PATCH 3/5] Create feedbacks with ratings instead of marks --- app/controllers/new_user/feedbacks_controller.rb | 2 +- app/views/new_user/dossiers/index.html.haml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/new_user/feedbacks_controller.rb b/app/controllers/new_user/feedbacks_controller.rb index e3b952b91..d7db41189 100644 --- a/app/controllers/new_user/feedbacks_controller.rb +++ b/app/controllers/new_user/feedbacks_controller.rb @@ -1,6 +1,6 @@ class NewUser::FeedbacksController < ApplicationController def create - current_user.feedbacks.create!(mark: params[:mark]) + current_user.feedbacks.create!(rating: params[:rating]) flash.notice = "Merci de votre retour, si vous souhaitez nous en dire plus, n'hésitez pas à nous contacter par email." end end diff --git a/app/views/new_user/dossiers/index.html.haml b/app/views/new_user/dossiers/index.html.haml index dcd145289..aecf834f0 100644 --- a/app/views/new_user/dossiers/index.html.haml +++ b/app/views/new_user/dossiers/index.html.haml @@ -58,11 +58,11 @@ #user-satisfaction %h3 Que pensez-vous de la facilité d'utilisation de ce service ? .icons - = link_to feedback_path(mark: 0), data: { remote: true, method: :post } do + = link_to feedback_path(rating: Feedback.ratings.fetch(:unhappy)), data: { remote: true, method: :post } do %span.icon.frown - = link_to feedback_path(mark: 1), data: { remote: true, method: :post } do + = link_to feedback_path(rating: Feedback.ratings.fetch(:neutral)), data: { remote: true, method: :post } do %span.icon.meh - = link_to feedback_path(mark: 2), data: { remote: true, method: :post } do + = link_to feedback_path(rating: Feedback.ratings.fetch(:happy)), data: { remote: true, method: :post } do %span.icon.smile - else From 18624ff8723e954a6142d6141ed9b6164dd71f9b Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 27 Aug 2018 14:12:38 +0200 Subject: [PATCH 4/5] Update the stats code to use ratings instead of marks --- app/controllers/stats_controller.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 11de1eaa5..7b5605e0e 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -89,16 +89,16 @@ class StatsController < ApplicationController def satisfaction_usagers legend = { - "0" => "Mécontents", - "1" => "Neutres", - "2" => "Satisfaits" + Feedback.ratings.fetch(:unhappy) => "Mécontents", + Feedback.ratings.fetch(:neutral) => "Neutres", + Feedback.ratings.fetch(:happy) => "Satisfaits" } totals = Feedback.where(created_at: 5.weeks.ago..Time.now).group_by_week(:created_at).count - (0..2).map do |mark| + Feedback::rating.values.map do |rating| data = Feedback - .where(created_at: 5.weeks.ago..Time.now, mark: mark) + .where(created_at: 5.weeks.ago..Time.now, rating: rating) .group_by_week(:created_at) .count .map do |week, count| @@ -112,7 +112,7 @@ class StatsController < ApplicationController end.to_h { - name: legend[mark.to_s], + name: legend[rating], data: data } end From d6748bde20b36b3afde4b2bb1a9fa290221bd748 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 27 Aug 2018 15:04:20 +0200 Subject: [PATCH 5/5] Update a factory so that it uses rating instead of mark --- spec/factories/feedback.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/feedback.rb b/spec/factories/feedback.rb index 231989f34..d332d2eae 100644 --- a/spec/factories/feedback.rb +++ b/spec/factories/feedback.rb @@ -1,5 +1,5 @@ FactoryBot.define do factory :feedback do - mark 3 + rating Feedback.ratings.fetch(:happy) end end