Skip to content

Commit

Permalink
Add format to book
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Dec 22, 2024
1 parent 5c24668 commit 9d87e16
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/crud/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 5 additions & 1 deletion app/models/book.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions app/views/crud/books/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions app/views/crud/books/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 11 additions & 0 deletions db/migrate/20241222195354_add_format_to_books.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/factories/book.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FactoryBot.define do
factory :book do
finished_on { Time.now }
format { "print" }
isbn { "123" }
pages { "100" }

Expand Down
4 changes: 3 additions & 1 deletion spec/system/crud/books/admin_creates_book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions spec/system/crud/books/admin_views_book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 9d87e16

Please sign in to comment.