Skip to content

Commit

Permalink
Merge pull request #30 from leouofa/add-book
Browse files Browse the repository at this point in the history
Adding a book model
  • Loading branch information
leouofa authored Sep 21, 2024
2 parents 80ac7c7 + 83fcb61 commit 7bfa779
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 2 deletions.
13 changes: 13 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class BooksController < MetaController
def component_name
'Books'
end

def component_class
'Book'.constantize
end

def component_params
params.require(@computer_name.to_sym).permit(:title, :writing_style_id)
end
end
9 changes: 9 additions & 0 deletions app/frontend/stylesheets/admin.sass
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
label
font-size: 1.1em
input[type="text"]
font-family: $body-font-family !important
@apply border-4 focus:border-blue-400 !important
select
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e") !important
background-position: right 0.5rem center !important
background-repeat: no-repeat !important
background-size: 1.5em 1.5em !important
appearance: none !important
padding: 0.5em 0.9em !important
@apply h-12 border-4 focus:border-blue-400 !important

@layer
.btn-primary
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/books_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module BooksHelper
end
15 changes: 15 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# == Schema Information
#
# Table name: books
#
# id :bigint not null, primary key
# title :text
# writing_style_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Book < ApplicationRecord
belongs_to :writing_style

validates :title, presence: true
end
2 changes: 2 additions & 0 deletions app/models/writing_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class WritingStyle < ApplicationRecord
serialize :prompt, coder: JSON

has_many :texts, dependent: :destroy
has_many :books, dependent: :nullify

validates :name, presence: true

has_paper_trail ignore: [:name, :pending], versions: {
Expand Down
15 changes: 15 additions & 0 deletions app/views/books/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- if @component.errors.any?
=render 'partials/form_errors'

=r ux.component_segment
= form_with model: @component, class: 'ui form', local: true do |form|
=r ux.component_field
= form.label :title
= form.text_field :title, value: @component.title

= r ux.component_field
= form.label :writing_style_id, "Writing Style"
= form.collection_select :writing_style_id, WritingStyle.all, :id, :name, { prompt: "Select a Writing Style" }, class: "ui dropdown"

=r ux.component_submit_field
= form.submit class: 'btn-primary'
9 changes: 9 additions & 0 deletions app/views/books/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=r ux.container
=r ux.row
=r ux.column width: 16
= turbo_frame_tag 'crud_frame'
=r ux.component_container
=r ux.component_header text: "Edit #{@component_name.singularize}"
=r ux.component_return_action text: '< Back', url: send(@component_list_path)
=r ux.component_divider
=render 'form'
31 changes: 31 additions & 0 deletions app/views/books/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=r ux.container
=r ux.row
=r ux.column width: 16
= turbo_frame_tag 'crud_frame'
=r ux.component_container
=r ux.component_header
=@component_name.pluralize
=r ux.component_action text: "New #{@component_name}", url: send(@component_new_path)
=r ux.component_divider

=r ux.div 'text-center'
= paginate @component_list

=r ux.table class: 'relaxed celled striped structured'
=r ux.thead
=r ux.tr
=r ux.th text: 'Book Name'
th.center.aligned colspan="2" Actions
- @component_list.each do |component|
=r ux.tr
=r ux.td
=link_to component.title, book_path(component), class: 'item text-sm', data: {'turbo-action': 'replace'}
= r ux.td 'collapsing'
= link_to 'Edit', send(@component_edit_path, component), class: 'btn-list-edit'
= r ux.td 'collapsing'
= button_to 'Delete', send(@component_delete_path, id: component.id), method: :delete,
data: { turbo_confirm: "Are you sure you want to delete this #{@component_name.downcase}?" },
class: 'btn-list-delete'

=r ux.div 'text-center'
= paginate @component_list
9 changes: 9 additions & 0 deletions app/views/books/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=r ux.container
=r ux.row
=r ux.column width: 16
= turbo_frame_tag 'crud_frame'
=r ux.component_container
=r ux.component_header text: "New #{@component_name.singularize}"
=r ux.component_return_action text: '< Back', url: send(@component_list_path)
=r ux.component_divider
=render 'form'
7 changes: 6 additions & 1 deletion app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ html lang="en"
=r ux.item url: root_path, class: "logo-item #{menu_active?(root_path)}}"
=r ux.header text: 'Book Rhino', class: 'inverted logo !font-mono'

= link_to writing_styles_path, class: "item #{menu_active?(writing_styles_path)}"
= link_to books_path, class: "item #{menu_active?(books_path)}"
=r ux.icon 'book'
| Books

= link_to writing_styles_path, class: "item #{menu_active?(writing_styles_path)}"
=r ux.icon 'vest patches'
| Writing Styles


/
/
/ =r ux.item class: "dropdown #{dropdown_active?(stories_path)}", ui: :on
Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
devise_for :users

root "page#index"

resources :writing_styles do
resources :texts

member do
post :iterate
end
Expand All @@ -24,6 +26,10 @@
end
end

resources :books do

end

resources :unauthorized, only: %i[index]
resources :settings, only: [:index, :edit]
resource :settings, only: [:update]
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240921193059_create_books.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateBooks < ActiveRecord::Migration[7.2]
def change
create_table :books do |t|
t.text :title
t.references :writing_style, null: false, foreign_key: true

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7bfa779

Please sign in to comment.