From f88c66a992bdd7256afedae2179d2edda3c158c9 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Fri, 20 Oct 2023 00:06:32 +0300 Subject: [PATCH 1/4] changed users attributes bu;; to false, added columns to users table in admin menu --- app/controllers/account/users_controller.rb | 2 +- app/models/user.rb | 2 ++ app/views/account/users/index.html.slim | 4 ++++ config/locales/en/en.yml | 2 ++ config/locales/uk/uk.yml | 2 ++ .../20231019194527_change_user_attributes_null.rb | 14 ++++++++++++++ db/schema.rb | 6 +++--- spec/lib/users_csv_generator_spec.rb | 6 +++--- spec/models/user_spec.rb | 2 +- 9 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20231019194527_change_user_attributes_null.rb diff --git a/app/controllers/account/users_controller.rb b/app/controllers/account/users_controller.rb index 6b8f4f033..83d8577cf 100644 --- a/app/controllers/account/users_controller.rb +++ b/app/controllers/account/users_controller.rb @@ -17,7 +17,7 @@ def index format.html format.csv do UserReportJob.perform_later - send_data UsersCsvGenerator.call(@users, fields: ["email", "last_sign_in_at"]) + send_data UsersCsvGenerator.call(@users, fields: ["email", "first_name", "last_name", "last_sign_in_at"]) end end end diff --git a/app/models/user.rb b/app/models/user.rb index 2fe3b6e45..e6fffc60e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -41,6 +41,8 @@ class User < ApplicationRecord attr_accessor :current_password, :skip_password_validation, :send_credentials_email scope :ordered_by_email, -> { order(:email) } + scope :ordered_by_first_name, -> { order(:first_name) } + scope :ordered_by_last_name, -> { order(:last_name) } has_paper_trail ignore: [ :current_sign_in_at, :last_sign_in_at, :confirmation_token, diff --git a/app/views/account/users/index.html.slim b/app/views/account/users/index.html.slim index c1cf1d2fa..762c909ce 100644 --- a/app/views/account/users/index.html.slim +++ b/app/views/account/users/index.html.slim @@ -10,6 +10,8 @@ div class="container home" thead tr td = sort_link(@q, :email, t('.table.email_col'), default_order: :desc) + td = sort_link(@q, :first_name, t('.table.first_name'), default_order: :desc) + td = sort_link(@q, :last_name, t('.table.last_name'), default_order: :desc) td = sort_link(@q, :updated_at, t('.table.last_visit_col'), default_order: :desc) td = t '.table.view_info_col' td = t '.table.edit' @@ -19,6 +21,8 @@ div class="container home" - @users.each do |user| tr id="user-info-#{user.id}" td = user.email + td = user.first_name + td = user.last_name td = user.last_sign_in_at td = link_to icon('fa-solid', 'eye'), account_user_path(id: user.id) td = link_to icon('fa-solid', 'edit'), edit_account_user_path(id: user.id) diff --git a/config/locales/en/en.yml b/config/locales/en/en.yml index 6cd682c0f..7f48cf8ce 100644 --- a/config/locales/en/en.yml +++ b/config/locales/en/en.yml @@ -207,6 +207,8 @@ en: confirm_delete: "Are you sure you want to delete this user?" table: email_col: "Email" + first_name: "First name" + last_name: "Last name" last_visit_col: "Last visit" view_info_col: "Show" edit: "Edit" diff --git a/config/locales/uk/uk.yml b/config/locales/uk/uk.yml index 1f7084ac8..9b01155a4 100644 --- a/config/locales/uk/uk.yml +++ b/config/locales/uk/uk.yml @@ -189,6 +189,8 @@ uk: confirm_delete: "Ви справді хочете видалити цього користувача?" table: email_col: "Електронна адреса" + first_name: "Ім'я" + last_name: "Прізвище" last_visit_col: "Час останнього візиту" view_info_col: "Показати" edit: "Відредагувати" diff --git a/db/migrate/20231019194527_change_user_attributes_null.rb b/db/migrate/20231019194527_change_user_attributes_null.rb new file mode 100644 index 000000000..177f50379 --- /dev/null +++ b/db/migrate/20231019194527_change_user_attributes_null.rb @@ -0,0 +1,14 @@ +class ChangeUserAttributesNull < ActiveRecord::Migration[6.1] + def up + User.where(first_name: nil).update_all(first_name: "Firstname") # rubocop:disable Rails/SkipsModelValidations + User.where(last_name: nil).update_all(last_name: "Lastname") # rubocop:disable Rails/SkipsModelValidations + + change_column_null :users, :first_name, false + change_column_null :users, :last_name, false + end + + def down + change_column_null :users, :first_name, true + change_column_null :users, :last_name, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 39650f9e0..bc5365b3e 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: 2023_07_10_212500) do +ActiveRecord::Schema.define(version: 2023_10_19_194527) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -182,8 +182,8 @@ t.datetime "locked_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.string "first_name" - t.string "last_name" + t.string "first_name", null: false + t.string "last_name", null: false t.string "country" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" diff --git a/spec/lib/users_csv_generator_spec.rb b/spec/lib/users_csv_generator_spec.rb index 28e198a18..25d7ebc21 100644 --- a/spec/lib/users_csv_generator_spec.rb +++ b/spec/lib/users_csv_generator_spec.rb @@ -5,12 +5,12 @@ RSpec.describe UsersCsvGenerator do let(:time_login) { Time.new(2020, 0o1, 0o1).utc } let(:users) do - [create(:user, email: "test@gmail.com", last_sign_in_at: time_login)] + [create(:user, email: "test@gmail.com", first_name: "Han", last_name: "Solo", last_sign_in_at: time_login)] end it "export users" do expect(UsersCsvGenerator.call(users, - fields: ["email", "last_sign_in_at"])) - .to eq("email,last_sign_in_at\ntest@gmail.com,#{time_login}\n") + fields: ["email", "first_name", "last_name", "last_sign_in_at"])) + .to eq("email,first_name,last_name,last_sign_in_at\ntest@gmail.com,Han,Solo,#{time_login}\n") end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e35e596a2..fb143f9f1 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -38,7 +38,7 @@ require "rails_helper" RSpec.describe User, type: :model do - let!(:user) { build(:user) } + subject { build(:user) } describe "associations" do it { is_expected.to have_one_attached(:avatar) } From 5de79969f0b5effc5bd10cb9cbd7cb9cc1e73245 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Fri, 20 Oct 2023 20:35:30 +0300 Subject: [PATCH 2/4] fix updating methods in migration --- .../20231019194527_change_user_attributes_null.rb | 11 +++++++++-- db/schema.rb | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/db/migrate/20231019194527_change_user_attributes_null.rb b/db/migrate/20231019194527_change_user_attributes_null.rb index 177f50379..fd6c6d7cc 100644 --- a/db/migrate/20231019194527_change_user_attributes_null.rb +++ b/db/migrate/20231019194527_change_user_attributes_null.rb @@ -1,7 +1,14 @@ class ChangeUserAttributesNull < ActiveRecord::Migration[6.1] def up - User.where(first_name: nil).update_all(first_name: "Firstname") # rubocop:disable Rails/SkipsModelValidations - User.where(last_name: nil).update_all(last_name: "Lastname") # rubocop:disable Rails/SkipsModelValidations + User.where(first_name: nil).each do |user| + user.update(first_name: "Firstname") + user.save(validate: false) + end + + User.where(last_name: nil).each do |user| + user.update(last_name: "Lastname") + user.save(validate: false) + end change_column_null :users, :first_name, false change_column_null :users, :last_name, false diff --git a/db/schema.rb b/db/schema.rb index bc5365b3e..e445bbb28 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -200,7 +200,8 @@ end create_table "versions", force: :cascade do |t| - t.string "item_type", null: false + t.string "item_type" + t.string "{:null=>false}" t.bigint "item_id", null: false t.string "event", null: false t.string "whodunnit" From 08ae58ca6f429f890c2ae82edd079e7f2e819faa Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Fri, 20 Oct 2023 20:44:17 +0300 Subject: [PATCH 3/4] update csvgenerator test --- spec/lib/users_csv_generator_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/users_csv_generator_spec.rb b/spec/lib/users_csv_generator_spec.rb index 25d7ebc21..561304f06 100644 --- a/spec/lib/users_csv_generator_spec.rb +++ b/spec/lib/users_csv_generator_spec.rb @@ -11,6 +11,6 @@ it "export users" do expect(UsersCsvGenerator.call(users, fields: ["email", "first_name", "last_name", "last_sign_in_at"])) - .to eq("email,first_name,last_name,last_sign_in_at\ntest@gmail.com,Han,Solo,#{time_login}\n") + .to eq("email,first_name,last_name,last_sign_in_at\n#{user.email},#{user.first_name},#{user.last_name},#{time_login}\n") end end From bc765d7dba254e0d07d4ccb82c97a4c865803beb Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Fri, 20 Oct 2023 21:23:28 +0300 Subject: [PATCH 4/4] added test for csv response --- spec/lib/users_csv_generator_spec.rb | 13 ++++++++++++- spec/requests/account/users_spec.rb | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/spec/lib/users_csv_generator_spec.rb b/spec/lib/users_csv_generator_spec.rb index 561304f06..312f262e2 100644 --- a/spec/lib/users_csv_generator_spec.rb +++ b/spec/lib/users_csv_generator_spec.rb @@ -4,13 +4,24 @@ RSpec.describe UsersCsvGenerator do let(:time_login) { Time.new(2020, 0o1, 0o1).utc } + let(:users) do [create(:user, email: "test@gmail.com", first_name: "Han", last_name: "Solo", last_sign_in_at: time_login)] end + let(:csv_content) do + result = "email,first_name,last_name,last_sign_in_at\n" + + users.each do |user| + result += "#{users.first.email},#{users.first.first_name},#{users.first.last_name},#{time_login}\n" + end + + result + end + it "export users" do expect(UsersCsvGenerator.call(users, fields: ["email", "first_name", "last_name", "last_sign_in_at"])) - .to eq("email,first_name,last_name,last_sign_in_at\n#{user.email},#{user.first_name},#{user.last_name},#{time_login}\n") + .to eq(csv_content) end end diff --git a/spec/requests/account/users_spec.rb b/spec/requests/account/users_spec.rb index 6c575aa3a..912caa4a1 100644 --- a/spec/requests/account/users_spec.rb +++ b/spec/requests/account/users_spec.rb @@ -5,16 +5,28 @@ RSpec.describe "Account::UsersController", type: :request do include_context :authorize_admin - let!(:user) { create(:user) } + let!(:user) { create(:user, last_sign_in_at: Time.current) } describe "GET #index" do - it "returns a successful response" do + it "returns a successful html response" do get account_users_path expect(response).to be_successful expect(response).to render_template(:index) expect(response.body).to include(I18n.t("account.users.index.main_header")) end + + it "returns a successful csv response" do + get account_users_path(format: "csv") + + csv_content = response.instance_variable_get(:@stream).instance_variable_get(:@buf).join + + expect(response.header["Content-Type"]).to include "application/octet-stream" + expect(csv_content).to match(user.email) + expect(csv_content).to match(user.first_name) + expect(csv_content).to match(user.last_name) + expect(csv_content).to match(user.last_sign_in_at.to_s) + end end describe "GET #new" do