From 2022831d4960054aef904e27fd2934beb4e3c65e Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Thu, 14 Nov 2024 17:16:43 +0000 Subject: Adds category model but not wired up to resource yet --- app/controllers/categories_controller.rb | 77 ++++++++++++++++++++++++++++ app/helpers/categories_helper.rb | 2 + app/models/category.rb | 3 ++ app/models/resource_type.rb | 1 + app/views/categories/_category.html.erb | 17 ++++++ app/views/categories/_category.json.jbuilder | 2 + app/views/categories/_form.html.erb | 32 ++++++++++++ app/views/categories/edit.html.erb | 8 +++ app/views/categories/index.html.erb | 21 ++++++++ app/views/categories/index.json.jbuilder | 1 + app/views/categories/new.html.erb | 7 +++ app/views/categories/show.html.erb | 15 ++++++ app/views/categories/show.json.jbuilder | 1 + 13 files changed, 187 insertions(+) create mode 100644 app/controllers/categories_controller.rb create mode 100644 app/helpers/categories_helper.rb create mode 100644 app/models/category.rb create mode 100644 app/views/categories/_category.html.erb create mode 100644 app/views/categories/_category.json.jbuilder create mode 100644 app/views/categories/_form.html.erb create mode 100644 app/views/categories/edit.html.erb create mode 100644 app/views/categories/index.html.erb create mode 100644 app/views/categories/index.json.jbuilder create mode 100644 app/views/categories/new.html.erb create mode 100644 app/views/categories/show.html.erb create mode 100644 app/views/categories/show.json.jbuilder (limited to 'app') diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb new file mode 100644 index 0000000..5fc872b --- /dev/null +++ b/app/controllers/categories_controller.rb @@ -0,0 +1,77 @@ +class CategoriesController < ApplicationController + before_action :set_category, only: %i[ show edit update destroy ] + before_action :require_admin, only: %i[ new create update destroy ] + + # GET /categories or /categories.json + def index + @categories = Category.all + end + + # GET /categories/1 or /categories/1.json + def show + end + + # GET /categories/new + def new + @category = Category.new + end + + # GET /categories/1/edit + def edit + end + + # POST /categories or /categories.json + def create + @category = Category.new(category_params) + + respond_to do |format| + if @category.save + format.html { redirect_to @category, notice: "Category was successfully created." } + format.json { render :show, status: :created, location: @category } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @category.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /categories/1 or /categories/1.json + def update + respond_to do |format| + if @category.update(category_params) + format.html { redirect_to @category, notice: "Category was successfully updated." } + format.json { render :show, status: :ok, location: @category } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @category.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /categories/1 or /categories/1.json + def destroy + @category.destroy! + + respond_to do |format| + format.html { redirect_to categories_path, status: :see_other, notice: "Category was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_category + @category = Category.find(params.expect(:id)) + end + + # Only allow a list of trusted parameters through. + def category_params + params.expect(category: [ :name, :colour, :badge_foreground_colour ]) + end + + def require_admin + unless Current.session.user&.is_admin + redirect_to root_path, notice: "You must be an admin to perform this action." + end + end +end diff --git a/app/helpers/categories_helper.rb b/app/helpers/categories_helper.rb new file mode 100644 index 0000000..e06f315 --- /dev/null +++ b/app/helpers/categories_helper.rb @@ -0,0 +1,2 @@ +module CategoriesHelper +end diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 0000000..296e7d6 --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,3 @@ +class Category < ApplicationRecord + validates :name, presence: true, uniqueness: true +end diff --git a/app/models/resource_type.rb b/app/models/resource_type.rb index 066a734..414e71e 100644 --- a/app/models/resource_type.rb +++ b/app/models/resource_type.rb @@ -1,3 +1,4 @@ class ResourceType < ApplicationRecord has_many :pdfresources + validates :name, presence: true, uniqueness: true end diff --git a/app/views/categories/_category.html.erb b/app/views/categories/_category.html.erb new file mode 100644 index 0000000..3c51d8f --- /dev/null +++ b/app/views/categories/_category.html.erb @@ -0,0 +1,17 @@ +
+

+ Name: + <%= category.name %> +

+ +

+ Colour: + <%= category.colour %> +

+ +

+ Badge foreground colour: + <%= category.badge_foreground_colour %> +

+ +
diff --git a/app/views/categories/_category.json.jbuilder b/app/views/categories/_category.json.jbuilder new file mode 100644 index 0000000..c0950c8 --- /dev/null +++ b/app/views/categories/_category.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! category, :id, :name, :colour, :badge_foreground_colour, :created_at, :updated_at +json.url category_url(category, format: :json) diff --git a/app/views/categories/_form.html.erb b/app/views/categories/_form.html.erb new file mode 100644 index 0000000..fec6c3b --- /dev/null +++ b/app/views/categories/_form.html.erb @@ -0,0 +1,32 @@ +<%= form_with(model: category, class: "contents") do |form| %> + <% if category.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= required_label_tag(form, :name) %> + <%= form.text_field :name, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> +
+ +
+ <%= form.label :colour, class: "font-bold" %> + <%= form.text_field :colour, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> +
+ +
+ <%= form.label :badge_foreground_colour, class: "font-bold" %> + <%= form.text_field :badge_foreground_colour, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> +
+ +
+ <%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> +
+<% end %> diff --git a/app/views/categories/edit.html.erb b/app/views/categories/edit.html.erb new file mode 100644 index 0000000..57da660 --- /dev/null +++ b/app/views/categories/edit.html.erb @@ -0,0 +1,8 @@ +
+

Editing category

+ + <%= render "form", category: @category %> + + <%= link_to "Show this category", @category, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to "Back to categories", categories_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
diff --git a/app/views/categories/index.html.erb b/app/views/categories/index.html.erb new file mode 100644 index 0000000..68911e6 --- /dev/null +++ b/app/views/categories/index.html.erb @@ -0,0 +1,21 @@ +
+ <% if notice.present? %> +

<%= notice %>

+ <% end %> + + <% content_for :title, "Categories" %> + +
+

Categories

+ <%= link_to "New category", new_category_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> +
+ +
+ <% @categories.each do |category| %> + <%= render category %> +

+ <%= link_to "Show this category", category, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +

+ <% end %> +
+
diff --git a/app/views/categories/index.json.jbuilder b/app/views/categories/index.json.jbuilder new file mode 100644 index 0000000..aa5baad --- /dev/null +++ b/app/views/categories/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @categories, partial: "categories/category", as: :category diff --git a/app/views/categories/new.html.erb b/app/views/categories/new.html.erb new file mode 100644 index 0000000..fae8770 --- /dev/null +++ b/app/views/categories/new.html.erb @@ -0,0 +1,7 @@ +
+

New category

+ + <%= render "form", category: @category %> + + <%= link_to "Back to categories", categories_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
diff --git a/app/views/categories/show.html.erb b/app/views/categories/show.html.erb new file mode 100644 index 0000000..e38975f --- /dev/null +++ b/app/views/categories/show.html.erb @@ -0,0 +1,15 @@ +
+
+ <% if notice.present? %> +

<%= notice %>

+ <% end %> + + <%= render @category %> + + <%= link_to "Edit this category", edit_category_path(@category), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> + <%= link_to "Back to categories", categories_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> +
+ <%= button_to "Destroy this category", @category, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %> +
+
+
diff --git a/app/views/categories/show.json.jbuilder b/app/views/categories/show.json.jbuilder new file mode 100644 index 0000000..30e6b47 --- /dev/null +++ b/app/views/categories/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "categories/category", category: @category -- cgit v1.2.3