Skip to content

Commit

Permalink
Merge pull request #549 from ita-social-projects/fix/user_attributes_…
Browse files Browse the repository at this point in the history
…null

fix/user_attrbutes_null
  • Loading branch information
loqimean authored Nov 8, 2023
2 parents 23a95f5 + bc765d7 commit 8e762d7
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/controllers/account/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions app/views/account/users/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,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"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/uk/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ uk:
confirm_delete: "Ви справді хочете видалити цього користувача?"
table:
email_col: "Електронна адреса"
first_name: "Ім'я"
last_name: "Прізвище"
last_visit_col: "Час останнього візиту"
view_info_col: "Показати"
edit: "Відредагувати"
Expand Down
21 changes: 21 additions & 0 deletions db/migrate/20231019194527_change_user_attributes_null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ChangeUserAttributesNull < ActiveRecord::Migration[6.1]
def up
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
end

def down
change_column_null :users, :first_name, true
change_column_null :users, :last_name, true
end
end
9 changes: 5 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
17 changes: 14 additions & 3 deletions spec/lib/users_csv_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@

RSpec.describe UsersCsvGenerator do
let(:time_login) { Time.new(2020, 0o1, 0o1).utc }

let(:users) do
[create(:user, email: "[email protected]", last_sign_in_at: time_login)]
[create(:user, email: "[email protected]", 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", "last_sign_in_at"]))
.to eq("email,last_sign_in_at\n[email protected],#{time_login}\n")
fields: ["email", "first_name", "last_name", "last_sign_in_at"]))
.to eq(csv_content)
end
end
2 changes: 1 addition & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
16 changes: 14 additions & 2 deletions spec/requests/account/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e762d7

Please sign in to comment.