From 80903e76931e71a3419f88691675f052c02e212a Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sun, 23 Jan 2022 09:52:41 +0100 Subject: [PATCH] ajout de User#admin --- app/controllers/users_controller.rb | 2 +- app/views/shared/_colored_pill.html.erb | 3 +++ app/views/users/_form.html.erb | 4 ++++ app/views/users/index.html.erb | 22 +++++++++++++++---- .../20220123074307_add_admin_to_users.rb | 6 +++++ db/schema.rb | 4 +++- 6 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 app/views/shared/_colored_pill.html.erb create mode 100644 db/migrate/20220123074307_add_admin_to_users.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index e44b64f..663842e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -68,6 +68,6 @@ class UsersController < ApplicationController # Only allow a list of trusted parameters through. def user_params - params.require(:user).permit(:name, :email) + params.require(:user).permit(:name, :email, :admin) end end diff --git a/app/views/shared/_colored_pill.html.erb b/app/views/shared/_colored_pill.html.erb new file mode 100644 index 0000000..cc87348 --- /dev/null +++ b/app/views/shared/_colored_pill.html.erb @@ -0,0 +1,3 @@ +-100 text-<%= (defined?(color) && color.presence) || "gray" %>-800"> + <%= yield %> + \ No newline at end of file diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb index 91d8585..f841873 100644 --- a/app/views/users/_form.html.erb +++ b/app/views/users/_form.html.erb @@ -28,6 +28,10 @@ <%= form.label :name %> <%= form.text_field :name %> +
+ <%= form.label :admin %> + <%= form.check_box :admin %> +
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 5d67122..41cdae3 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -22,6 +22,9 @@ Status + + Admin? + Edit @@ -50,13 +53,24 @@ <% if user.confirmed? %> - + <%= render "shared/colored_pill", { color: "green" } do %> Confirmed - + <% end %> <% else %> - + <%= render "shared/colored_pill", { color: "yellow" } do %> Unconfirmed - + <% end %> + <% end %> + + + <% if user.admin? %> + <%= render "shared/colored_pill", { color: "green" } do %> + Yes + <% end %> + <% else %> + <%= render "shared/colored_pill", { color: "yellow" } do %> + No + <% end %> <% end %> diff --git a/db/migrate/20220123074307_add_admin_to_users.rb b/db/migrate/20220123074307_add_admin_to_users.rb new file mode 100644 index 0000000..bb2e6e0 --- /dev/null +++ b/db/migrate/20220123074307_add_admin_to_users.rb @@ -0,0 +1,6 @@ +class AddAdminToUsers < ActiveRecord::Migration[7.0] + def change + add_column :users, :admin, :boolean, null: false, default: false + add_index :users, :admin + end +end diff --git a/db/schema.rb b/db/schema.rb index dbab0fd..a0a4b9e 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: 2021_12_26_105150) do +ActiveRecord::Schema.define(version: 2022_01_23_074307) do create_table "checks", force: :cascade do |t| t.string "name" @@ -32,6 +32,8 @@ ActiveRecord::Schema.define(version: 2021_12_26_105150) do t.datetime "confirmed_at", precision: 6 t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.boolean "admin", default: false, null: false + t.index ["admin"], name: "index_users_on_admin" t.index ["email"], name: "index_users_on_email", unique: true t.index ["remember_token"], name: "index_users_on_remember_token", unique: true t.index ["session_token"], name: "index_users_on_session_token", unique: true