From d0149a50e2272cf4f1abbad7b994191b98c5e6e9 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 16 Feb 2024 17:22:09 -0600 Subject: [PATCH] Created the Forms model. Added styling elements. Still need to include a list of already known forms to add to system. Also locked forms and admin tab behind admin access. --- app/controllers/forms_controller.rb | 4 +++ app/models/user.rb | 8 ++++- app/views/devise/confirmations/new.html.erb | 2 +- app/views/devise/passwords/edit.html.erb | 2 +- app/views/devise/passwords/new.html.erb | 2 +- app/views/devise/registrations/edit.html.erb | 2 +- app/views/devise/registrations/new.html.erb | 2 +- app/views/forms/_form.html.erb | 36 +++++++++++++++++++ app/views/forms/edit.html.erb | 4 +-- app/views/forms/index.html.erb | 36 +++++++++++++++++-- app/views/forms/new.html.erb | 15 ++++++-- app/views/forms/show.html.erb | 21 +++++++++-- app/views/layouts/_navbar.html.erb | 19 +++++----- .../20240216230305_add_details_to_forms.rb | 8 +++++ db/schema.rb | 6 +++- 15 files changed, 143 insertions(+), 24 deletions(-) create mode 100644 app/views/forms/_form.html.erb create mode 100644 db/migrate/20240216230305_add_details_to_forms.rb diff --git a/app/controllers/forms_controller.rb b/app/controllers/forms_controller.rb index ce964d7..23f6352 100644 --- a/app/controllers/forms_controller.rb +++ b/app/controllers/forms_controller.rb @@ -47,4 +47,8 @@ class FormsController < ApplicationController def form_params params.require(:form).permit(:name) # Adjust attributes as needed end + + def check_admin + redirect_to(root_path, alert: "Not authorized") unless current_user.admin? + end end diff --git a/app/models/user.rb b/app/models/user.rb index 4756799..44eb338 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,10 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable -end + + + def admin? + self.admin + end + +end \ No newline at end of file diff --git a/app/views/devise/confirmations/new.html.erb b/app/views/devise/confirmations/new.html.erb index 240c457..71590fb 100644 --- a/app/views/devise/confirmations/new.html.erb +++ b/app/views/devise/confirmations/new.html.erb @@ -12,7 +12,7 @@
- <%= f.submit "Resend confirmation instructions", class: 'btn btn-primary' %> + <%= f.submit "Resend confirmation instructions", class: 'btn btn-dark' %>
<% end %> diff --git a/app/views/devise/passwords/edit.html.erb b/app/views/devise/passwords/edit.html.erb index a0279a8..eeec9d6 100644 --- a/app/views/devise/passwords/edit.html.erb +++ b/app/views/devise/passwords/edit.html.erb @@ -21,7 +21,7 @@
- <%= f.submit "Change my password", class: 'btn btn-primary' %> + <%= f.submit "Change my password", class: 'btn btn-dark' %>
<% end %> diff --git a/app/views/devise/passwords/new.html.erb b/app/views/devise/passwords/new.html.erb index 7ddfe65..8536f3a 100644 --- a/app/views/devise/passwords/new.html.erb +++ b/app/views/devise/passwords/new.html.erb @@ -12,7 +12,7 @@
- <%= f.submit "Send me reset password instructions", class: 'btn btn-primary' %> + <%= f.submit "Send me reset password instructions", class: 'btn btn-dark' %>
<% end %> diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 59af3f9..d9e03b5 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -34,7 +34,7 @@
- <%= f.submit "Update", class: 'btn btn-primary' %> + <%= f.submit "Update", class: 'btn btn-dark' %>
<% end %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index c021e11..49f5046 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -22,7 +22,7 @@
- <%= f.submit "Sign up", class: 'btn btn-primary' %> + <%= f.submit "Sign up", class: 'btn btn-dark' %>
<% end %> diff --git a/app/views/forms/_form.html.erb b/app/views/forms/_form.html.erb new file mode 100644 index 0000000..0c2abf3 --- /dev/null +++ b/app/views/forms/_form.html.erb @@ -0,0 +1,36 @@ +<%= form_with(model: form, local: true, html: { class: 'needs-validation', novalidate: true }) do |f| %> + <% if form.errors.any? %> +
+

<%= pluralize(form.errors.count, "error") %> prohibited this form from being saved:

+ +
+ <% end %> + +
+ <%= f.label :name, 'Form Name', class: 'form-label' %> + <%= f.text_field :name, class: 'form-control', placeholder: 'Enter form name' %> +
+ +
+ <%= f.label :required, 'Required', class: 'form-label' %> + <%= f.check_box :required, class: 'form-check-input' %> +
+ +
+ <%= f.label :program, 'Program', class: 'form-label' %> + <%= f.text_field :program, class: 'form-control', placeholder: 'Enter program name' %> +
+ +
+ <%= f.label :role, 'Who/Role', class: 'form-label' %> + <%= f.text_field :role, class: 'form-control', placeholder: 'Enter role' %> +
+ +
+ <%= f.submit 'Save', class: 'btn btn-dark' %> +
+<% end %> diff --git a/app/views/forms/edit.html.erb b/app/views/forms/edit.html.erb index 2a979c6..ea3f3fe 100644 --- a/app/views/forms/edit.html.erb +++ b/app/views/forms/edit.html.erb @@ -1,2 +1,2 @@ -

Forms#edit

-

Find me in app/views/forms/edit.html.erb

+

Edit Form

+<%= render 'form', form: @form %> \ No newline at end of file diff --git a/app/views/forms/index.html.erb b/app/views/forms/index.html.erb index 3964715..a2ad460 100644 --- a/app/views/forms/index.html.erb +++ b/app/views/forms/index.html.erb @@ -1,2 +1,34 @@ -

Forms Index

-

Placeholder for listing forms.

\ No newline at end of file +
+

Forms

+ +
+ <%= link_to 'New Form', new_form_path, class: 'btn btn-dark mb-3' %> +
+ + + + + + + + + + + + + <% @forms.each do |form| %> + + + + + + + + <% end %> + +
Form NameRequired/OptionalProgramWho/RoleActions
<%= form.name %><%= form.required ? 'Required' : 'Optional' %><%= form.program %><%= form.role %> + <%= link_to 'Edit', edit_form_path(form), class: 'btn btn-info btn-sm' %> + <%= link_to 'Delete', form, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger btn-sm' %> +
+
+ diff --git a/app/views/forms/new.html.erb b/app/views/forms/new.html.erb index e4a8cb0..3b1688d 100644 --- a/app/views/forms/new.html.erb +++ b/app/views/forms/new.html.erb @@ -1,2 +1,13 @@ -

Forms#new

-

Find me in app/views/forms/new.html.erb

+
+
+
+

New Form

+ + <%= render 'form', form: @form %> + +
+ <%= link_to 'Back to List', forms_path, class: "btn btn-secondary" %> +
+
+
+
diff --git a/app/views/forms/show.html.erb b/app/views/forms/show.html.erb index 19869da..c7297a7 100644 --- a/app/views/forms/show.html.erb +++ b/app/views/forms/show.html.erb @@ -1,2 +1,19 @@ -

Forms#show

-

Find me in app/views/forms/show.html.erb

+

<%= @form.name %>

+ +

+ Required/Optional: + <%= @form.required ? 'Required' : 'Optional' %> +

+ +

+ Program: + <%= @form.program %> +

+ +

+ Who/Role: + <%= @form.role %> +

+ +<%= link_to 'Edit', edit_form_path(@form), class: 'btn btn-info' %> | +<%= link_to 'Back to Forms', forms_path, class: 'btn btn-secondary' %> diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb index 0184543..39e195d 100644 --- a/app/views/layouts/_navbar.html.erb +++ b/app/views/layouts/_navbar.html.erb @@ -21,15 +21,16 @@ - + <% if user_signed_in? && current_user.admin? %> + + <% end %> + <% if user_signed_in? %> diff --git a/db/migrate/20240216230305_add_details_to_forms.rb b/db/migrate/20240216230305_add_details_to_forms.rb new file mode 100644 index 0000000..d21f99b --- /dev/null +++ b/db/migrate/20240216230305_add_details_to_forms.rb @@ -0,0 +1,8 @@ +class AddDetailsToForms < ActiveRecord::Migration[7.1] + def change + add_column :forms, :name, :string + add_column :forms, :required, :boolean + add_column :forms, :program, :string + add_column :forms, :role, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index de272d4..c238279 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[7.1].define(version: 2024_02_15_231142) do +ActiveRecord::Schema[7.1].define(version: 2024_02_16_230305) do create_table "bank_accounts", force: :cascade do |t| t.string "institution_name" t.string "account_type" @@ -75,6 +75,10 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_15_231142) do create_table "forms", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "name" + t.boolean "required" + t.string "program" + t.string "role" end create_table "participants", force: :cascade do |t|