From 0276194015b9a94999519b010339c819c585d874 Mon Sep 17 00:00:00 2001 From: lorenzo farnararo Date: Sun, 11 Aug 2024 17:05:32 +0200 Subject: [PATCH] lot of work, better UI --- app/assets/images/dashboard.svg | 4 ++ app/assets/images/module.svg | 4 ++ app/assets/images/tags.svg | 4 ++ app/assets/images/trash.svg | 4 ++ app/controllers/base_controller.rb | 5 ++ app/controllers/clubs_controller.rb | 8 ++- app/controllers/events_controller.rb | 3 +- app/controllers/groups_controller.rb | 15 ++++- app/controllers/members_controller.rb | 15 +++++ app/controllers/tags_controller.rb | 7 +++ app/controllers/users_controller.rb | 62 +++++++++++++++++++ app/models/club.rb | 6 +- app/models/user.rb | 8 ++- app/views/application/_header.html.erb | 42 ++++++------- app/views/clubs/_form.html.erb | 21 ++++--- app/views/clubs/update.turbo_stream.erb | 5 ++ app/views/groups/edit.erb | 1 + app/views/groups/index.html.erb | 11 ++++ app/views/kaminari/_first_page.html.erb | 3 + app/views/kaminari/_gap.html.erb | 3 + app/views/kaminari/_last_page.html.erb | 3 + app/views/kaminari/_next_page.html.erb | 3 + app/views/kaminari/_page.html.erb | 9 +++ app/views/kaminari/_paginator.html.erb | 17 +++++ app/views/kaminari/_prev_page.html.erb | 3 + app/views/members/dashboard.html.erb | 33 ++++++++++ app/views/members/edit.erb | 3 + app/views/members/index.html.erb | 59 +++++++++--------- app/views/tags/_form.html.erb | 2 +- app/views/tags/index.html.erb | 10 +++ app/views/users/_form.html.erb | 17 +++++ app/views/users/create.turbo_stream.erb | 5 ++ app/views/users/edit.erb | 3 + app/views/users/index.html.erb | 30 +++++++++ app/views/users/new.html.erb | 3 + app/views/users/update.turbo_stream.erb | 3 + config/locales/it.yml | 24 +++++++ config/locales/simple_form.it.yml | 8 +++ config/routes.rb | 3 + db/migrate/20240724113600_add_clubs.rb | 2 - .../20240806122837_devise_create_users.rb | 5 ++ db/schema.rb | 6 +- db/seeds.rb | 17 +++-- 43 files changed, 419 insertions(+), 80 deletions(-) create mode 100644 app/assets/images/dashboard.svg create mode 100644 app/assets/images/module.svg create mode 100644 app/assets/images/tags.svg create mode 100644 app/assets/images/trash.svg create mode 100644 app/controllers/users_controller.rb create mode 100644 app/views/clubs/update.turbo_stream.erb create mode 100644 app/views/groups/edit.erb create mode 100644 app/views/kaminari/_first_page.html.erb create mode 100644 app/views/kaminari/_gap.html.erb create mode 100644 app/views/kaminari/_last_page.html.erb create mode 100644 app/views/kaminari/_next_page.html.erb create mode 100644 app/views/kaminari/_page.html.erb create mode 100644 app/views/kaminari/_paginator.html.erb create mode 100644 app/views/kaminari/_prev_page.html.erb create mode 100644 app/views/members/dashboard.html.erb create mode 100644 app/views/members/edit.erb create mode 100644 app/views/users/_form.html.erb create mode 100644 app/views/users/create.turbo_stream.erb create mode 100644 app/views/users/edit.erb create mode 100644 app/views/users/index.html.erb create mode 100644 app/views/users/new.html.erb create mode 100644 app/views/users/update.turbo_stream.erb diff --git a/app/assets/images/dashboard.svg b/app/assets/images/dashboard.svg new file mode 100644 index 0000000..f6e3e61 --- /dev/null +++ b/app/assets/images/dashboard.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/assets/images/module.svg b/app/assets/images/module.svg new file mode 100644 index 0000000..7ae53fc --- /dev/null +++ b/app/assets/images/module.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/assets/images/tags.svg b/app/assets/images/tags.svg new file mode 100644 index 0000000..ade5519 --- /dev/null +++ b/app/assets/images/tags.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/assets/images/trash.svg b/app/assets/images/trash.svg new file mode 100644 index 0000000..3020264 --- /dev/null +++ b/app/assets/images/trash.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index eef182e..f66ab10 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -1,8 +1,13 @@ # frozen_string_literal: true class BaseController < ApplicationController + before_action :redirect_to_root, unless: :user_signed_in? before_action :set_club + def redirect_to_root + redirect_to root_path + end + def set_club @club = current_user.club end diff --git a/app/controllers/clubs_controller.rb b/app/controllers/clubs_controller.rb index 98c7ff1..5a98cbc 100644 --- a/app/controllers/clubs_controller.rb +++ b/app/controllers/clubs_controller.rb @@ -10,10 +10,14 @@ def update if @club.update(club_params) respond_to do |format| - format.turbo_stream + format.html { redirect_to edit_club_url(@club), flash: { notice: I18n.t('club.updated') } } + format.turbo_stream { flash.now[:notice] = I18n.t('club.updated') } end else - render :new, status: :unprocessable_entity + respond_to do |format| + format.html { render :new, status: :unprocessable_entity } + format.turbo_stream + end end end diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 7c1f791..a30bed3 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -67,8 +67,9 @@ def update def destroy @event = @club.events.find(params[:id]) + @event.destroy - @event.discard + redirect_to club_events_url(@club), flash: { success: I18n.t('events.destroyed') } end private diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 03a93a4..4a1d7a2 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -2,13 +2,17 @@ class GroupsController < BaseController def index - @groups = @club.groups + @groups = @club.groups.page(params[:page]) end def new @group = @club.groups.build end + def edit + @group = @club.groups.find(params[:id]) + end + def create @group = @club.groups.build(group_params) @@ -26,7 +30,7 @@ def create end def update - @group = @club.members.find(params[:id]) + @group = @club.groups.find(params[:id]) if @group.update(group_params) respond_to do |format| @@ -41,6 +45,13 @@ def update end end + def destroy + @group = @club.groups.find(params[:id]) + @group.destroy + + redirect_to club_groups_url(@club), flash: { success: I18n.t('groups.destroyed') } + end + private def group_params diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index c0530d1..b4303e9 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -2,6 +2,10 @@ class MembersController < BaseController def index + @members = @club.members.page(params[:page]) + end + + def dashboard @group = params[:group_id] ? @club.groups.find(params[:group_id]) : @club.groups.first if @group.blank? @@ -15,6 +19,10 @@ def new @member = @club.members.build end + def edit + @member = @club.members.find(params[:id]) + end + def create @member = @club.members.build(member_params) @@ -43,6 +51,13 @@ def update end end + def destroy + @member = @club.members.find(params[:id]) + @member.discard + + redirect_to club_members_url(@club), flash: { success: I18n.t('members.destroyed') } + end + private def member_params diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 3cc3eae..32a22ba 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -45,6 +45,13 @@ def update end end + def destroy + @tag = @club.tags.find(params[:id]) + @tag.destroy + + redirect_to club_tags_url(@club), flash: { success: I18n.t('tags.destroyed') } + end + private def tag_params diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..1ad88a1 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +class UsersController < BaseController + def index + @users = @club.users.page(params[:page]) + end + + def new + @user = @club.users.build + end + + def edit + @user = @club.users.find(params[:id]) + end + + def create + @user = @club.users.build(user_params) + @user.role = :collaborator + @user.password = 'password' + + if @user.save + respond_to do |format| + format.html { redirect_to new_club_user_url(@club), flash: { notice: I18n.t('users.created') } } + format.turbo_stream { flash.now[:notice] = I18n.t('users.created') } + end + else + respond_to do |format| + format.html { render :new, status: :unprocessable_entity } + format.turbo_stream + end + end + end + + def update + @user = @club.users.find(params[:id]) + + if @user.update(user_params) + respond_to do |format| + format.html { redirect_to club_users_url(@club), flash: { notice: I18n.t('users.updated') } } + format.turbo_stream { flash.now[:notice] = I18n.t('users.updated') } + end + else + respond_to do |format| + format.html { render :edit, status: :unprocessable_entity } + format.turbo_stream + end + end + end + + def destroy + @user = @club.users.find(params[:id]) + @user.destroy + + redirect_to club_users_url(@club), flash: { success: I18n.t('users.destroyed') } + end + + private + + def user_params + params.require(:user).permit(:first_name, :last_name, :email, :picture) + end +end diff --git a/app/models/club.rb b/app/models/club.rb index c324ece..1aea6ca 100644 --- a/app/models/club.rb +++ b/app/models/club.rb @@ -6,7 +6,9 @@ class Club < ApplicationRecord has_one_attached :picture - belongs_to :user + has_many :users, dependent: :destroy + + alias collaborators users has_many :members, dependent: :destroy has_many :groups, dependent: :destroy @@ -14,5 +16,5 @@ class Club < ApplicationRecord has_many :events, dependent: :destroy has_many :payments, through: :members - validates :name, presence: true + validates :name, :email, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index 9123be2..0333d10 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,7 +7,11 @@ class User < ApplicationRecord :recoverable, :rememberable, :validatable, :confirmable, :lockable - enum role: { admin: 0, instructor: 1 } + has_one_attached :picture - has_one :club, dependent: :nullify + enum role: { admin: 0, collaborator: 1 } + + belongs_to :club + + validates :first_name, :last_name, :password, presence: true end diff --git a/app/views/application/_header.html.erb b/app/views/application/_header.html.erb index 024cc5f..ef3a20d 100644 --- a/app/views/application/_header.html.erb +++ b/app/views/application/_header.html.erb @@ -8,35 +8,29 @@