From 5270a4975977a04879ada25cf326a3d7b378c7f7 Mon Sep 17 00:00:00 2001
From: Rob Sterner
Date: Mon, 27 Nov 2023 17:04:25 -0500
Subject: [PATCH] make these temporary controllers/UIs a little more useful
---
app/controllers/assignments_controller.rb | 3 ++-
app/controllers/clients_controller.rb | 1 +
app/controllers/projects_controller.rb | 5 +++--
app/controllers/work_weeks_controller.rb | 1 +
app/models/assignment.rb | 4 +++-
app/models/client.rb | 7 ++++++-
app/models/project.rb | 7 +++++--
app/views/assignments/_assignment.html.erb | 5 -----
app/views/assignments/_form.html.erb | 11 +++--------
app/views/clients/_form.html.erb | 7 ++-----
app/views/projects/_form.html.erb | 16 +++-------------
app/views/projects/_project.html.erb | 7 +------
app/views/work_weeks/_form.html.erb | 2 +-
13 files changed, 31 insertions(+), 45 deletions(-)
diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb
index 48b2a5c4..57060720 100644
--- a/app/controllers/assignments_controller.rb
+++ b/app/controllers/assignments_controller.rb
@@ -1,4 +1,5 @@
class AssignmentsController < ApplicationController
+ before_action :require_user!
before_action :set_assignment, only: %i[ show edit update destroy ]
# GET /assignments or /assignments.json
@@ -65,6 +66,6 @@ def set_assignment
# Only allow a list of trusted parameters through.
def assignment_params
- params.require(:assignment).permit(:user_id, :project_id, :proposed, :status)
+ params.require(:assignment).permit(:user_id, :project_id, :status)
end
end
diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb
index d4780c6e..a0ec8e66 100644
--- a/app/controllers/clients_controller.rb
+++ b/app/controllers/clients_controller.rb
@@ -1,4 +1,5 @@
class ClientsController < ApplicationController
+ before_action :require_user!
before_action :set_client, only: %i[ show edit update destroy ]
# GET /clients or /clients.json
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index 5bed1361..942ee10c 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -1,9 +1,10 @@
class ProjectsController < ApplicationController
+ before_action :require_user!
before_action :set_project, only: %i[ show edit update destroy ]
# GET /projects or /projects.json
def index
- @projects = Project.all
+ @projects = Project.includes(:company). all
end
# GET /projects/1 or /projects/1.json
@@ -65,6 +66,6 @@ def set_project
# Only allow a list of trusted parameters through.
def project_params
- params.require(:project).permit(:client_id, :name, :status, :company_id, :proposed, :cost, :payment_frequency)
+ params.require(:project).permit(:client_id, :name, :status, :cost, :payment_frequency)
end
end
diff --git a/app/controllers/work_weeks_controller.rb b/app/controllers/work_weeks_controller.rb
index 9d471dd0..32f61050 100644
--- a/app/controllers/work_weeks_controller.rb
+++ b/app/controllers/work_weeks_controller.rb
@@ -1,4 +1,5 @@
class WorkWeeksController < ApplicationController
+ before_action :require_user!
before_action :set_work_week, only: %i[ show edit update destroy ]
# GET /work_weeks or /work_weeks.json
diff --git a/app/models/assignment.rb b/app/models/assignment.rb
index 4afc1577..36841273 100644
--- a/app/models/assignment.rb
+++ b/app/models/assignment.rb
@@ -3,7 +3,9 @@ class Assignment < ApplicationRecord
belongs_to :project
has_many :work_weeks, dependent: :destroy
+ VALID_STATUSES = %w(proposed active archived completed).freeze
+
validates :user_id, presence: true, uniqueness: { scope: :project_id }
validates :project_id, presence: true, uniqueness: { scope: :user_id }
- validates :status, presence: true, inclusion: { in: %w(proposed active archived completed) }
+ validates :status, presence: true, inclusion: { in: VALID_STATUSES }
end
diff --git a/app/models/client.rb b/app/models/client.rb
index e3476797..1a42ae53 100644
--- a/app/models/client.rb
+++ b/app/models/client.rb
@@ -2,7 +2,12 @@ class Client < ApplicationRecord
belongs_to :company
has_many :projects, dependent: :destroy
+ VALID_STATUSES = %w(active archived).freeze
+
validates :company_id, presence: true, uniqueness: { scope: :name }
validates :name, presence: true, uniqueness: { scope: :company_id }
- validates :status, presence: true, inclusion: { in: %w(active archived) }
+ validates :status, presence: true, inclusion: { in: VALID_STATUSES }
+
+ scope :active, -> { where(status: 'active') }
+ scope :archived, -> { where(status: 'archived') }
end
diff --git a/app/models/project.rb b/app/models/project.rb
index 439221ee..1921a6c8 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -5,9 +5,12 @@ class Project < ApplicationRecord
has_many :users, through: :assignments
has_many :work_weeks, through: :assignments, dependent: :destroy
+ VALID_STATUSES = %w(proposed active archived cancelled completed).freeze
+ VALID_PAYMENT_FREQUENCIES = %w(weekly monthly fortnightly quarterly annually).freeze
+
validates :client_id, presence: true
validates :name, presence: true, uniqueness: { case_sensitive: false }
- validates :status, presence: true, inclusion: { in: %w(proposed active archived cancelled completed) }
+ validates :status, presence: true, inclusion: { in: VALID_STATUSES }
validates :cost, presence: true, numericality: { greater_than_or_equal_to: 0.0 }
- validates :payment_frequency, presence: true, inclusion: { in: %w(weekly monthly fortnightly quarterly annually) }
+ validates :payment_frequency, presence: true, inclusion: { in: VALID_PAYMENT_FREQUENCIES }
end
diff --git a/app/views/assignments/_assignment.html.erb b/app/views/assignments/_assignment.html.erb
index 6da2f6d1..55b7ec96 100644
--- a/app/views/assignments/_assignment.html.erb
+++ b/app/views/assignments/_assignment.html.erb
@@ -9,11 +9,6 @@
<%= assignment.project_id %>
-
- Proposed:
- <%= assignment.proposed %>
-
-
Status:
<%= assignment.status %>
diff --git a/app/views/assignments/_form.html.erb b/app/views/assignments/_form.html.erb
index a80544a6..d43ea039 100644
--- a/app/views/assignments/_form.html.erb
+++ b/app/views/assignments/_form.html.erb
@@ -13,22 +13,17 @@
<%= form.label :user_id %>
- <%= form.number_field :user_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
+ <%= form.select :user_id, current_company.users.map { |u| [u.name, u.id] }, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
<%= form.label :project_id %>
- <%= form.number_field :project_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
-
-
-
- <%= form.label :proposed %>
- <%= form.check_box :proposed, class: "block mt-2 h-5 w-5" %>
+ <%= form.select :project_id, current_company.projects.map { |p| [p.name, p.id] }, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
<%= form.label :status %>
- <%= form.text_field :status, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
+ <%= form.select :status, Assignment::VALID_STATUSES, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
diff --git a/app/views/clients/_form.html.erb b/app/views/clients/_form.html.erb
index 342a794b..74595cb0 100644
--- a/app/views/clients/_form.html.erb
+++ b/app/views/clients/_form.html.erb
@@ -23,13 +23,10 @@
<%= form.label :status %>
- <%= form.text_field :status, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
+ <%= form.select :status, Client::VALID_STATUSES, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
-
- <%= form.label :company_id %>
- <%= form.number_field :company_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
-
+ <%= form.hidden_field :company_id, value: current_company_id %>
<%= 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/projects/_form.html.erb b/app/views/projects/_form.html.erb
index 7774798f..9e644839 100644
--- a/app/views/projects/_form.html.erb
+++ b/app/views/projects/_form.html.erb
@@ -13,7 +13,7 @@
<%= form.label :client_id %>
- <%= form.number_field :client_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
+ <%= form.select :client_id, current_company.clients.active.map { |c| [c.name, c.id] }, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
@@ -23,17 +23,7 @@
<%= form.label :status %>
- <%= form.text_field :status, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
-
-
-
- <%= form.label :company_id %>
- <%= form.number_field :company_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
-
-
-
- <%= form.label :proposed %>
- <%= form.check_box :proposed, class: "block mt-2 h-5 w-5" %>
+ <%= form.select :status, Project::VALID_STATUSES, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
@@ -43,7 +33,7 @@
<%= form.label :payment_frequency %>
- <%= form.text_field :payment_frequency, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
+ <%= form.select :payment_frequency, Project::VALID_PAYMENT_FREQUENCIES, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
diff --git a/app/views/projects/_project.html.erb b/app/views/projects/_project.html.erb
index 6c817899..f443b01c 100644
--- a/app/views/projects/_project.html.erb
+++ b/app/views/projects/_project.html.erb
@@ -16,12 +16,7 @@
Company:
- <%= project.company_id %>
-
-
-
- Proposed:
- <%= project.proposed %>
+ <%= project.company.id %>
diff --git a/app/views/work_weeks/_form.html.erb b/app/views/work_weeks/_form.html.erb
index d973b8b6..084539a2 100644
--- a/app/views/work_weeks/_form.html.erb
+++ b/app/views/work_weeks/_form.html.erb
@@ -33,7 +33,7 @@
<%= form.label :assignment_id %>
- <%= form.number_field :assignment_id, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
+ <%= form.select :assignment_id, current_company.projects.includes(assignments: :user).flat_map { |p| p.assignments.map { |a| ["[#{p.name}] #{a.user.name}", a.id] } }, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>