Skip to content

Commit

Permalink
make these temporary controllers/UIs a little more useful
Browse files Browse the repository at this point in the history
  • Loading branch information
fermion committed Nov 27, 2023
1 parent d1f3e54 commit 5270a49
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 45 deletions.
3 changes: 2 additions & 1 deletion app/controllers/assignments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions app/controllers/work_weeks_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/models/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 deletions app/views/assignments/_assignment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
<%= assignment.project_id %>
</p>

<p class="my-5">
<strong class="block font-medium mb-1">Proposed:</strong>
<%= assignment.proposed %>
</p>

<p class="my-5">
<strong class="block font-medium mb-1">Status:</strong>
<%= assignment.status %>
Expand Down
11 changes: 3 additions & 8 deletions app/views/assignments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@

<div class="my-5">
<%= 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" %>
</div>

<div class="my-5">
<%= 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" %>
</div>

<div class="my-5">
<%= 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" %>
</div>

<div class="my-5">
<%= 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" %>
</div>

<div class="inline">
Expand Down
7 changes: 2 additions & 5 deletions app/views/clients/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@

<div class="my-5">
<%= 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" %>
</div>

<div class="my-5">
<%= 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" %>
</div>
<%= form.hidden_field :company_id, value: current_company_id %>

<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
Expand Down
16 changes: 3 additions & 13 deletions app/views/projects/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<div class="my-5">
<%= 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" %>
</div>

<div class="my-5">
Expand All @@ -23,17 +23,7 @@

<div class="my-5">
<%= 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" %>
</div>

<div class="my-5">
<%= 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" %>
</div>

<div class="my-5">
<%= 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" %>
</div>

<div class="my-5">
Expand All @@ -43,7 +33,7 @@

<div class="my-5">
<%= 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" %>
</div>

<div class="inline">
Expand Down
7 changes: 1 addition & 6 deletions app/views/projects/_project.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@

<p class="my-5">
<strong class="block font-medium mb-1">Company:</strong>
<%= project.company_id %>
</p>

<p class="my-5">
<strong class="block font-medium mb-1">Proposed:</strong>
<%= project.proposed %>
<%= project.company.id %>
</p>

<p class="my-5">
Expand Down
2 changes: 1 addition & 1 deletion app/views/work_weeks/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<div class="my-5">
<%= 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" %>
</div>

<div class="my-5">
Expand Down

0 comments on commit 5270a49

Please sign in to comment.