From 6427b28c60c6ed0dfd637307d1ab4ffe65c1144d Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Thu, 14 Nov 2024 15:47:43 +0000 Subject: Adds ResourceType model --- app/controllers/pdfresources_controller.rb | 17 ++--- app/controllers/resource_types_controller.rb | 78 ++++++++++++++++++++++ app/helpers/resource_types_helper.rb | 2 + app/models/pdfresource.rb | 1 + app/models/resource_type.rb | 3 + app/views/pdfresources/_form.html.erb | 4 ++ app/views/pdfresources/_pdfresource.html.erb | 6 +- app/views/resource_types/_form.html.erb | 22 ++++++ app/views/resource_types/_resource_type.html.erb | 7 ++ .../resource_types/_resource_type.json.jbuilder | 2 + app/views/resource_types/edit.html.erb | 8 +++ app/views/resource_types/index.html.erb | 21 ++++++ app/views/resource_types/index.json.jbuilder | 1 + app/views/resource_types/new.html.erb | 7 ++ app/views/resource_types/show.html.erb | 15 +++++ app/views/resource_types/show.json.jbuilder | 1 + 16 files changed, 186 insertions(+), 9 deletions(-) create mode 100644 app/controllers/resource_types_controller.rb create mode 100644 app/helpers/resource_types_helper.rb create mode 100644 app/models/resource_type.rb create mode 100644 app/views/resource_types/_form.html.erb create mode 100644 app/views/resource_types/_resource_type.html.erb create mode 100644 app/views/resource_types/_resource_type.json.jbuilder create mode 100644 app/views/resource_types/edit.html.erb create mode 100644 app/views/resource_types/index.html.erb create mode 100644 app/views/resource_types/index.json.jbuilder create mode 100644 app/views/resource_types/new.html.erb create mode 100644 app/views/resource_types/show.html.erb create mode 100644 app/views/resource_types/show.json.jbuilder (limited to 'app') diff --git a/app/controllers/pdfresources_controller.rb b/app/controllers/pdfresources_controller.rb index adacc64..a710404 100644 --- a/app/controllers/pdfresources_controller.rb +++ b/app/controllers/pdfresources_controller.rb @@ -69,16 +69,17 @@ class PdfresourcesController < ApplicationController # Only allow a list of trusted parameters through. def pdfresource_params params.require(:pdfresource).permit( - :name, - :stripe_product_id, - :price, - :age_range, - :curriculum, - :feature_slot, - :description, + :name, + :stripe_product_id, + :price, + :age_range, + :curriculum, + :resource_type_id, + :feature_slot, + :description, :card_description, :credits, - pdfs: [], + pdfs: [], thumbnails: [] ) end diff --git a/app/controllers/resource_types_controller.rb b/app/controllers/resource_types_controller.rb new file mode 100644 index 0000000..fdfb791 --- /dev/null +++ b/app/controllers/resource_types_controller.rb @@ -0,0 +1,78 @@ +class ResourceTypesController < ApplicationController + before_action :set_resource_type, only: %i[ show edit update destroy ] + before_action :require_admin, only: %i[ new create update destroy ] + + # GET /resource_types or /resource_types.json + def index + @resource_types = ResourceType.all + end + + # GET /resource_types/1 or /resource_types/1.json + def show + end + + # GET /resource_types/new + def new + @resource_type = ResourceType.new + end + + # GET /resource_types/1/edit + def edit + end + + # POST /resource_types or /resource_types.json + def create + @resource_type = ResourceType.new(resource_type_params) + + respond_to do |format| + if @resource_type.save + format.html { redirect_to @resource_type, notice: "Resource type was successfully created." } + format.json { render :show, status: :created, location: @resource_type } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @resource_type.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /resource_types/1 or /resource_types/1.json + def update + respond_to do |format| + if @resource_type.update(resource_type_params) + format.html { redirect_to @resource_type, notice: "Resource type was successfully updated." } + format.json { render :show, status: :ok, location: @resource_type } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @resource_type.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /resource_types/1 or /resource_types/1.json + def destroy + @resource_type.destroy! + + respond_to do |format| + format.html { redirect_to resource_types_path, status: :see_other, notice: "Resource type was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_resource_type + @resource_type = ResourceType.find(params.expect(:id)) + end + + # Only allow a list of trusted parameters through. + def resource_type_params + params.expect(resource_type: [ :name ]) + end + + # must be admin! + 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/resource_types_helper.rb b/app/helpers/resource_types_helper.rb new file mode 100644 index 0000000..0a02a03 --- /dev/null +++ b/app/helpers/resource_types_helper.rb @@ -0,0 +1,2 @@ +module ResourceTypesHelper +end diff --git a/app/models/pdfresource.rb b/app/models/pdfresource.rb index 86e600c..4fa2292 100644 --- a/app/models/pdfresource.rb +++ b/app/models/pdfresource.rb @@ -2,6 +2,7 @@ class Pdfresource < ApplicationRecord has_many_attached :pdfs has_many_attached :thumbnails has_many_attached :pdf_snapshots + belongs_to :resource_type validates :feature_slot, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 3, allow_nil: true }, allow_nil: true diff --git a/app/models/resource_type.rb b/app/models/resource_type.rb new file mode 100644 index 0000000..066a734 --- /dev/null +++ b/app/models/resource_type.rb @@ -0,0 +1,3 @@ +class ResourceType < ApplicationRecord + has_many :pdfresources +end diff --git a/app/views/pdfresources/_form.html.erb b/app/views/pdfresources/_form.html.erb index 1ad8fb1..a15bd67 100644 --- a/app/views/pdfresources/_form.html.erb +++ b/app/views/pdfresources/_form.html.erb @@ -90,6 +90,10 @@

Number of credits required to purchase this resource.

+
+ <%= required_label_tag(form, :resource_type) %> + <%= form.collection_select :resource_type_id, ResourceType.all, :id, :name, {}, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> +
<%= form.label :stripe_product_id, "Stripe Product ID", class: "font-bold" %> diff --git a/app/views/pdfresources/_pdfresource.html.erb b/app/views/pdfresources/_pdfresource.html.erb index 12b2b58..8c96080 100644 --- a/app/views/pdfresources/_pdfresource.html.erb +++ b/app/views/pdfresources/_pdfresource.html.erb @@ -4,7 +4,7 @@

- <%= pdfresource.name %> + <%= link_to pdfresource.name, pdfresource, class: "text-blue-500 font-bold" %>

@@ -39,6 +39,10 @@ <% end %>
+

+ Resource type: + <%= pdfresource.resource_type&.name if pdfresource.respond_to?(:resource_type) %> +

Price: diff --git a/app/views/resource_types/_form.html.erb b/app/views/resource_types/_form.html.erb new file mode 100644 index 0000000..5675729 --- /dev/null +++ b/app/views/resource_types/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_with(model: resource_type, class: "contents") do |form| %> + <% if resource_type.errors.any? %> +

+

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

+ + +
+ <% end %> + +
+ <%= form.label :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.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/resource_types/_resource_type.html.erb b/app/views/resource_types/_resource_type.html.erb new file mode 100644 index 0000000..b144a58 --- /dev/null +++ b/app/views/resource_types/_resource_type.html.erb @@ -0,0 +1,7 @@ +
+

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

+ +
diff --git a/app/views/resource_types/_resource_type.json.jbuilder b/app/views/resource_types/_resource_type.json.jbuilder new file mode 100644 index 0000000..18f57c5 --- /dev/null +++ b/app/views/resource_types/_resource_type.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! resource_type, :id, :name, :created_at, :updated_at +json.url resource_type_url(resource_type, format: :json) diff --git a/app/views/resource_types/edit.html.erb b/app/views/resource_types/edit.html.erb new file mode 100644 index 0000000..fa2bc6b --- /dev/null +++ b/app/views/resource_types/edit.html.erb @@ -0,0 +1,8 @@ +
+

Editing resource type

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

<%= notice %>

+ <% end %> + + <% content_for :title, "Resource types" %> + +
+

Resource types

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

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

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

New resource type

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

<%= notice %>

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