From 9d87e16ae2ce27de6609a589e6a1d4745149eeda Mon Sep 17 00:00:00 2001 From: Jon Allured Date: Sun, 22 Dec 2024 13:55:40 -0700 Subject: [PATCH] Add format to book --- app/controllers/crud/books_controller.rb | 2 +- app/helpers/application_helper.rb | 10 ++++++++++ app/models/book.rb | 6 +++++- app/views/crud/books/edit.html.haml | 1 + app/views/crud/books/new.html.haml | 1 + db/migrate/20241222195354_add_format_to_books.rb | 11 +++++++++++ spec/factories/book.rb | 1 + spec/system/crud/books/admin_creates_book_spec.rb | 4 +++- spec/system/crud/books/admin_views_book_spec.rb | 1 + 9 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20241222195354_add_format_to_books.rb diff --git a/app/controllers/crud/books_controller.rb b/app/controllers/crud/books_controller.rb index a725927..0439f83 100644 --- a/app/controllers/crud/books_controller.rb +++ b/app/controllers/crud/books_controller.rb @@ -38,6 +38,6 @@ def destroy private def book_params - params.require(:book).permit(:finished_on, :isbn, :pages, :title) + params.require(:book).permit(:finished_on, :format, :isbn, :pages, :title) end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b146ee0..030c74b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,14 @@ module ApplicationHelper + def book_format_options + placeholder_option = [["please select", ""]] + + format_options = Book::FORMATS.map do |format| + [format, format] + end + + placeholder_option + format_options + end + def last_week_path(work_week) target_date = work_week.target_date - 1.week target = TargetSlug.for(target_date) diff --git a/app/models/book.rb b/app/models/book.rb index edf0ce8..5a1325f 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -1,6 +1,9 @@ class Book < ApplicationRecord - validates :isbn, presence: true + FORMATS = %w[print audio kindle] + validates :finished_on, presence: true + validates :format, inclusion: {in: FORMATS} + validates :isbn, presence: true has_object :enhancer @@ -9,6 +12,7 @@ def table_attrs ["ISBN", isbn], ["Title", title], ["Pages", pages], + ["Format", format], ["Finished On", finished_on.to_fs], ["Created At", created_at.to_fs], ["Updated At", updated_at.to_fs] diff --git a/app/views/crud/books/edit.html.haml b/app/views/crud/books/edit.html.haml index 9f52310..b41d85f 100644 --- a/app/views/crud/books/edit.html.haml +++ b/app/views/crud/books/edit.html.haml @@ -6,5 +6,6 @@ = form.text_field :isbn, placeholder: "isbn" = form.text_field :title, placeholder: "title" = form.text_field :pages, placeholder: "pages" + = form.select :format, book_format_options, selected: book.format = form.date_field :finished_on, placeholder: "finished on" = form.button "update" diff --git a/app/views/crud/books/new.html.haml b/app/views/crud/books/new.html.haml index 6fa20ed..bfeaf9d 100644 --- a/app/views/crud/books/new.html.haml +++ b/app/views/crud/books/new.html.haml @@ -4,5 +4,6 @@ = form_with model: [:crud, book] do |form| = form.text_field :isbn, placeholder: "isbn" + = form.select :format, book_format_options, selected: book.format = form.date_field :finished_on, placeholder: "finished on" = form.button "create" diff --git a/db/migrate/20241222195354_add_format_to_books.rb b/db/migrate/20241222195354_add_format_to_books.rb new file mode 100644 index 0000000..f5e85a2 --- /dev/null +++ b/db/migrate/20241222195354_add_format_to_books.rb @@ -0,0 +1,11 @@ +class AddFormatToBooks < ActiveRecord::Migration[8.0] + def up + add_column :books, :format, :string + Book.update_all(format: "print") + change_column_null :books, :format, false + end + + def down + remove_column :books, :format + end +end diff --git a/spec/factories/book.rb b/spec/factories/book.rb index 62181d7..5f352c7 100644 --- a/spec/factories/book.rb +++ b/spec/factories/book.rb @@ -1,6 +1,7 @@ FactoryBot.define do factory :book do finished_on { Time.now } + format { "print" } isbn { "123" } pages { "100" } diff --git a/spec/system/crud/books/admin_creates_book_spec.rb b/spec/system/crud/books/admin_creates_book_spec.rb index c4ab8c1..939b4df 100644 --- a/spec/system/crud/books/admin_creates_book_spec.rb +++ b/spec/system/crud/books/admin_creates_book_spec.rb @@ -14,13 +14,14 @@ scenario "create with errors" do visit "/crud/books/new" click_on "create" - expect(page).to have_css ".alert", text: "Isbn can't be blank and Finished on can't be blank" + expect(page).to have_css ".alert", text: "Finished on can't be blank, Format is not included in the list, and Isbn can't be blank" expect(page).to have_current_path new_crud_book_path end scenario "create successfully" do visit "/crud/books/new" fill_in "isbn", with: "abc-123" + select "print", from: "book_format" fill_in "finished on", with: "01/01/2000" click_on "create" @@ -40,6 +41,7 @@ ["ISBN", "abc-123"], ["Title", ""], ["Pages", ""], + ["Format", "print"], ["Finished On", "01/01/2000"], ["Created At", book.created_at.to_fs], ["Updated At", book.updated_at.to_fs] diff --git a/spec/system/crud/books/admin_views_book_spec.rb b/spec/system/crud/books/admin_views_book_spec.rb index 304f3e9..4448b35 100644 --- a/spec/system/crud/books/admin_views_book_spec.rb +++ b/spec/system/crud/books/admin_views_book_spec.rb @@ -32,6 +32,7 @@ ["ISBN", "abc-123"], ["Title", "Whittling And You"], ["Pages", "777"], + ["Format", "print"], ["Finished On", book.finished_on.to_fs], ["Created At", book.created_at.to_fs], ["Updated At", book.updated_at.to_fs]