aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-11-14 17:16:43 +0000
committerMatthew Lemon <y@yulqen.org>2024-11-14 17:16:43 +0000
commit2022831d4960054aef904e27fd2934beb4e3c65e (patch)
tree77f11b9c629d66b9782d7dc6c77105bb178116d8
parent9292aae8fe1876fe98c90ae0216fb4ea8917b38a (diff)
Adds category model but not wired up to resource yet
-rw-r--r--app/controllers/categories_controller.rb77
-rw-r--r--app/helpers/categories_helper.rb2
-rw-r--r--app/models/category.rb3
-rw-r--r--app/models/resource_type.rb1
-rw-r--r--app/views/categories/_category.html.erb17
-rw-r--r--app/views/categories/_category.json.jbuilder2
-rw-r--r--app/views/categories/_form.html.erb32
-rw-r--r--app/views/categories/edit.html.erb8
-rw-r--r--app/views/categories/index.html.erb21
-rw-r--r--app/views/categories/index.json.jbuilder1
-rw-r--r--app/views/categories/new.html.erb7
-rw-r--r--app/views/categories/show.html.erb15
-rw-r--r--app/views/categories/show.json.jbuilder1
-rw-r--r--config/routes.rb1
-rw-r--r--db/migrate/20241114170349_create_categories.rb11
-rw-r--r--db/schema.rb10
-rw-r--r--test/controllers/categories_controller_test.rb48
-rw-r--r--test/fixtures/categories.yml11
-rw-r--r--test/models/category_test.rb7
-rw-r--r--test/system/categories_test.rb45
20 files changed, 319 insertions, 1 deletions
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 @@
+<div id="<%= dom_id category %>">
+ <p class="my-5">
+ <strong class="block font-medium mb-1">Name:</strong>
+ <%= category.name %>
+ </p>
+
+ <p class="my-5">
+ <strong class="block font-medium mb-1">Colour:</strong>
+ <%= category.colour %>
+ </p>
+
+ <p class="my-5">
+ <strong class="block font-medium mb-1">Badge foreground colour:</strong>
+ <%= category.badge_foreground_colour %>
+ </p>
+
+</div>
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? %>
+ <div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
+ <h2><%= pluralize(category.errors.count, "error") %> prohibited this category from being saved:</h2>
+
+ <ul>
+ <% category.errors.each do |error| %>
+ <li><%= error.full_message %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="my-5">
+ <%= 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" %>
+ </div>
+
+ <div class="my-5">
+ <%= 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" %>
+ </div>
+
+ <div class="my-5">
+ <%= 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" %>
+ </div>
+
+ <div class="inline">
+ <%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
+ </div>
+<% 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 @@
+<div class="mx-auto md:w-2/3 w-full">
+ <h1 class="font-bold text-4xl">Editing category</h1>
+
+ <%= 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" %>
+</div>
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 @@
+<div class="w-full">
+ <% if notice.present? %>
+ <p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
+ <% end %>
+
+ <% content_for :title, "Categories" %>
+
+ <div class="flex justify-between items-center">
+ <h1 class="font-bold text-4xl">Categories</h1>
+ <%= link_to "New category", new_category_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
+ </div>
+
+ <div id="categories" class="min-w-full">
+ <% @categories.each do |category| %>
+ <%= render category %>
+ <p>
+ <%= link_to "Show this category", category, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
+ </p>
+ <% end %>
+ </div>
+</div>
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 @@
+<div class="mx-auto md:w-2/3 w-full">
+ <h1 class="font-bold text-4xl">New category</h1>
+
+ <%= 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" %>
+</div>
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 @@
+<div class="mx-auto md:w-2/3 w-full flex">
+ <div class="mx-auto">
+ <% if notice.present? %>
+ <p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
+ <% 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" %>
+ <div class="inline-block ml-2">
+ <%= button_to "Destroy this category", @category, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
+ </div>
+ </div>
+</div>
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
diff --git a/config/routes.rb b/config/routes.rb
index 1e8b325..3455716 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
+ resources :categories
resources :resource_types
resources :pdfresources
resources :users
diff --git a/db/migrate/20241114170349_create_categories.rb b/db/migrate/20241114170349_create_categories.rb
new file mode 100644
index 0000000..983f85c
--- /dev/null
+++ b/db/migrate/20241114170349_create_categories.rb
@@ -0,0 +1,11 @@
+class CreateCategories < ActiveRecord::Migration[8.0]
+ def change
+ create_table :categories do |t|
+ t.string :name
+ t.string :colour
+ t.string :badge_foreground_colour
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index e09d8c2..8c534b7 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[8.0].define(version: 2024_11_14_152810) do
+ActiveRecord::Schema[8.0].define(version: 2024_11_14_170349) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -39,6 +39,14 @@ ActiveRecord::Schema[8.0].define(version: 2024_11_14_152810) do
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end
+ create_table "categories", force: :cascade do |t|
+ t.string "name"
+ t.string "colour"
+ t.string "badge_foreground_colour"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
create_table "pdfresources", force: :cascade do |t|
t.string "name"
t.string "stripe_product_id"
diff --git a/test/controllers/categories_controller_test.rb b/test/controllers/categories_controller_test.rb
new file mode 100644
index 0000000..a21af3b
--- /dev/null
+++ b/test/controllers/categories_controller_test.rb
@@ -0,0 +1,48 @@
+require "test_helper"
+
+class CategoriesControllerTest < ActionDispatch::IntegrationTest
+ setup do
+ @category = categories(:one)
+ end
+
+ test "should get index" do
+ get categories_url
+ assert_response :success
+ end
+
+ test "should get new" do
+ get new_category_url
+ assert_response :success
+ end
+
+ test "should create category" do
+ assert_difference("Category.count") do
+ post categories_url, params: { category: { badge_foreground_colour: @category.badge_foreground_colour, colour: @category.colour, name: @category.name } }
+ end
+
+ assert_redirected_to category_url(Category.last)
+ end
+
+ test "should show category" do
+ get category_url(@category)
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get edit_category_url(@category)
+ assert_response :success
+ end
+
+ test "should update category" do
+ patch category_url(@category), params: { category: { badge_foreground_colour: @category.badge_foreground_colour, colour: @category.colour, name: @category.name } }
+ assert_redirected_to category_url(@category)
+ end
+
+ test "should destroy category" do
+ assert_difference("Category.count", -1) do
+ delete category_url(@category)
+ end
+
+ assert_redirected_to categories_url
+ end
+end
diff --git a/test/fixtures/categories.yml b/test/fixtures/categories.yml
new file mode 100644
index 0000000..9074eff
--- /dev/null
+++ b/test/fixtures/categories.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ name: MyString
+ colour: MyString
+ badge_foreground_colour: MyString
+
+two:
+ name: MyString
+ colour: MyString
+ badge_foreground_colour: MyString
diff --git a/test/models/category_test.rb b/test/models/category_test.rb
new file mode 100644
index 0000000..869357c
--- /dev/null
+++ b/test/models/category_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class CategoryTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/test/system/categories_test.rb b/test/system/categories_test.rb
new file mode 100644
index 0000000..acff020
--- /dev/null
+++ b/test/system/categories_test.rb
@@ -0,0 +1,45 @@
+require "application_system_test_case"
+
+class CategoriesTest < ApplicationSystemTestCase
+ setup do
+ @category = categories(:one)
+ end
+
+ test "visiting the index" do
+ visit categories_url
+ assert_selector "h1", text: "Categories"
+ end
+
+ test "should create category" do
+ visit categories_url
+ click_on "New category"
+
+ fill_in "Badge foreground colour", with: @category.badge_foreground_colour
+ fill_in "Colour", with: @category.colour
+ fill_in "Name", with: @category.name
+ click_on "Create Category"
+
+ assert_text "Category was successfully created"
+ click_on "Back"
+ end
+
+ test "should update Category" do
+ visit category_url(@category)
+ click_on "Edit this category", match: :first
+
+ fill_in "Badge foreground colour", with: @category.badge_foreground_colour
+ fill_in "Colour", with: @category.colour
+ fill_in "Name", with: @category.name
+ click_on "Update Category"
+
+ assert_text "Category was successfully updated"
+ click_on "Back"
+ end
+
+ test "should destroy Category" do
+ visit category_url(@category)
+ click_on "Destroy this category", match: :first
+
+ assert_text "Category was successfully destroyed"
+ end
+end