aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-11-14 11:12:36 +0000
committerMatthew Lemon <y@yulqen.org>2024-11-14 11:12:36 +0000
commit1062c6328f60bf9cacba20c07f5cd33e55c153ee (patch)
treeb780312b4fd1427707cbaecdc45711ea827b9fb8
parente636e93c866b000ae4674605ea0543930c7e2e99 (diff)
Adds credits to resource and required asterisks on form
-rw-r--r--app/helpers/application_helper.rb8
-rw-r--r--app/helpers/pdfresources_helper.rb2
-rw-r--r--app/models/pdfresource.rb3
-rw-r--r--app/views/pdfresources/_form.html.erb17
-rw-r--r--db/migrate/20241114105702_add_credit_field_to_pdf_resources.rb5
-rw-r--r--db/schema.rb3
6 files changed, 29 insertions, 9 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index de6be79..b1c2b1e 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1,2 +1,10 @@
module ApplicationHelper
+ def required_label_tag(form, field, text = nil)
+ label_content = text || field.to_s.titleize
+ required = form.object.class.validators_on(field).any? { |v| v.is_a?(ActiveRecord::Validations::PresenceValidator) }
+
+ form.label field, class: "font-bold" do
+ required ? "#{label_content} <span class='text-red-500'>*</span>".html_safe : label_content
+ end
+ end
end
diff --git a/app/helpers/pdfresources_helper.rb b/app/helpers/pdfresources_helper.rb
deleted file mode 100644
index 1f400cc..0000000
--- a/app/helpers/pdfresources_helper.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-module PdfresourcesHelper
-end
diff --git a/app/models/pdfresource.rb b/app/models/pdfresource.rb
index a5ef56d..86e600c 100644
--- a/app/models/pdfresource.rb
+++ b/app/models/pdfresource.rb
@@ -6,7 +6,8 @@ class Pdfresource < ApplicationRecord
numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 3, allow_nil: true },
allow_nil: true
validate :validate_pdf_count
- validates :name, :thumbnails, presence: true
+ validates :name, :thumbnails, :description, :card_description, :pdfs, :credits, presence: true
+
def validate_pdf_count
if pdfs.size > 10
diff --git a/app/views/pdfresources/_form.html.erb b/app/views/pdfresources/_form.html.erb
index 0739bc9..8b439b0 100644
--- a/app/views/pdfresources/_form.html.erb
+++ b/app/views/pdfresources/_form.html.erb
@@ -12,25 +12,25 @@
<% end %>
<div class="my-5">
- <%= form.label :name, class: "font-bold" %>
+ <%= 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 :description, class: "font-bold" %>
+ <%= required_label_tag(form, :description) %>
<%= form.textarea :description, rows: 4, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
<p class="text-sm text-gray-700">Go to two with the description here. THIS WILL BE RICH TEXT.</p>
</div>
<div class="my-5">
- <%= form.label :card_description, class: "font-bold" %>
+ <%= required_label_tag(form, :card_description) %>
<%= form.textarea :card_description, rows: 4, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
<p class="text-sm text-gray-700">This is a much shorter description that is used inside the "card" boxes on the
listings page.</p>
</div>
<div class="my-5">
- <%= form.label :pdfs, "Resource PDFs", class: "font-bold" %>
+ <%= required_label_tag(form, :pdfs) %>
<%= form.file_field :pdfs, multiple: true, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
<p class="text-sm text-gray-700">You can upload up to 10 PDFs per resource. The original files were:</p>
<ul class="list-disc font-bold text-green-500">
@@ -41,7 +41,7 @@
</div>
<div class="my-5">
- <%= form.label :thumbnails, class: "font-bold" %>
+ <%= required_label_tag(form, :thumbnails) %>
<%= form.file_field :thumbnails, multiple: true, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
<p class="text-sm text-gray-700">You can upload up to 10 thumbnails per resource. The original files were:</p>
<ul class="list-disc font-bold text-green-500">
@@ -58,6 +58,13 @@
</div>
<div class="my-5">
+ <%= required_label_tag(form, :credits) %>
+ <%= form.number_field :credits, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
+ <p class="text-sm text-gray-700">Number of credits required to purchase this resource.</p>
+ </div>
+
+
+ <div class="my-5">
<%= form.label :stripe_product_id, "Stripe Product ID", class: "font-bold" %>
<%= form.text_field :stripe_product_id, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
diff --git a/db/migrate/20241114105702_add_credit_field_to_pdf_resources.rb b/db/migrate/20241114105702_add_credit_field_to_pdf_resources.rb
new file mode 100644
index 0000000..7f10ea3
--- /dev/null
+++ b/db/migrate/20241114105702_add_credit_field_to_pdf_resources.rb
@@ -0,0 +1,5 @@
+class AddCreditFieldToPdfResources < ActiveRecord::Migration[8.0]
+ def change
+ add_column :pdfresources, :credits, :integer
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 0e6e899..61a7f3a 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_13_163104) do
+ActiveRecord::Schema[8.0].define(version: 2024_11_14_105702) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -50,6 +50,7 @@ ActiveRecord::Schema[8.0].define(version: 2024_11_13_163104) do
t.text "card_description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.integer "credits"
end
create_table "sessions", force: :cascade do |t|