Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions app/controllers/crud/chores_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Crud::ChoresController < ApplicationController
expose(:chore)
expose(:chores) do
Chore.order(created_at: :desc).page(params[:page])
end

def show
redirect_to crud_chore_path(Chore.random) if random_id?
end

def create
if chore.save
flash.notice = "Chore created"
redirect_to crud_chore_path(chore)
else
flash.alert = chore.errors.full_messages.to_sentence
redirect_to new_crud_chore_path
end
end

def update
if chore.update(chore_params)
flash.notice = "Chore updated"
redirect_to crud_chore_path(chore)
else
flash.alert = chore.errors.full_messages.to_sentence
redirect_to edit_crud_chore_path(chore)
end
end

def destroy
chore.destroy
flash.notice = "Chore deleted"
redirect_to crud_chores_path
end

private

def chore_params
permitted_params = params.require(:chore).permit(Chore.permitted_params)
due_days = Chore.day_names_to_numbers(params[:chore][:due_days])
permitted_params[:due_days] = due_days
permitted_params
end
end
41 changes: 41 additions & 0 deletions app/models/chore.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Chore < ApplicationRecord
DAY_NAMES = Date::DAYNAMES.map(&:downcase)

enum :assignee, {jon: 0, jess: 1, jack: 2}, validate: true

validates :due_days, presence: true
validates :title, presence: true

def self.day_names_to_numbers(names)
return [] if names.nil?
return (0..6).to_a if names.include?("all")

names.map { |name| DAY_NAMES.index(name) }
end

def self.permitted_params
[
:assignee,
:title
]
end

def daily?
due_days.count == 7
end

def due_on?(name)
name_index = DAY_NAMES.index(name)
due_days.include?(name_index)
end

def table_attrs
[
["Title", title],
["Assignee", assignee],
["Due Days", due_days.join(" ")],
["Created At", created_at],
["Updated At", updated_at]
]
end
end
99 changes: 99 additions & 0 deletions app/models/chore/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
class Chore
class List
include Prawn::View

def self.build_and_save(target = :disk)
list = new
list.build

if target == :disk
list.save_to_disk
elsif target == :s3
list.save_to_s3
else
raise ArgumentError
end

list
end

def build
stroke_color "000000"

pages.each_with_index do |page, index|
page.each do |section|
draw(section)
end

start_new_page unless index + 1 == pages.size
end
end

def save_to_disk
local_path = "tmp/chore-list.pdf"
save_as(local_path)
end

def save_to_s3
s3_key = "chore-list.pdf"
pdf_data = document.render
S3Api.write(s3_key, pdf_data)
end

private

def pages
[
[
{assignee: "jess", due_day: 0, offset: 720},
{assignee: "jess", due_day: 1, offset: 480},
{assignee: "jess", due_day: 2, offset: 240}
],
[
{assignee: "jess", due_day: 3, offset: 720},
{assignee: "jess", due_day: 4, offset: 480},
{assignee: "jess", due_day: 5, offset: 240}
],
[
{assignee: "jess", due_day: 6, offset: 720},
{assignee: "jack", due_day: 0, offset: 480},
{assignee: "jack", due_day: 1, offset: 240}
],
[
{assignee: "jack", due_day: 2, offset: 720},
{assignee: "jack", due_day: 3, offset: 480},
{assignee: "jack", due_day: 4, offset: 240}
],
[
{assignee: "jack", due_day: 5, offset: 720},
{assignee: "jack", due_day: 6, offset: 480}
]
]
end

def draw(section)
move_cursor_to section[:offset]

text "Chore List".upcase, style: :bold_italic, size: 42

move_up 48

label = "#{section[:assignee].titlecase}\n#{Date::DAYNAMES[section[:due_day]]}"
text label, align: :right, size: 14

move_down 3

stroke do
horizontal_rule
end

move_down 20

chores = Chore.where(assignee: section[:assignee]).where("? = ANY(due_days)", section[:due_day])

chores.each do |chore|
text chore.title, size: 14
end
end
end
end
42 changes: 5 additions & 37 deletions app/models/daily_packet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,12 @@ def reading_list_phrase
end

def chore_list
chores = []
chores << "unload dishwasher"
chores << "collect laundry" if built_on_weekend?
chores << "collect and wash towels" if built_on_wednesday?
chores << "defrost meat"
chores = Chore
.where(assignee: "jon")
.where("? = ANY(due_days)", built_on.wday)
.order(created_at: :asc)

if built_on_weekend? && built_during_summer?
chores << "poop patrol"
chores << "mow front"
chores << "mow back"
chores << "mow way back"
end

chores << "put out garbage cans" if built_on_sunday?
chores << "refill soap dispensers"
chores << "do hand wash"
chores << "wipe down kitchen"
chores << "wash dog bowls" if built_on_tuesday?
chores << "wash bathroom cups" if built_on_tuesday?
chores << "run dishwasher"

chores
chores.pluck(:title).map(&:downcase)
end

def start_list
Expand All @@ -78,23 +62,7 @@ def stop_list
]
end

def built_on_tuesday?
built_on.tuesday?
end

def built_on_wednesday?
built_on.wednesday?
end

def built_on_sunday?
built_on.sunday?
end

def built_on_weekend?
built_on.saturday? || built_on.sunday?
end

def built_during_summer?
(4..10).cover?(built_on.month)
end
end
15 changes: 15 additions & 0 deletions app/views/crud/chores/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
= form_with model: [:crud, chore] do |form|
= form.text_field :title, placeholder: "title"

= form.select :assignee, ["jon", "jess", "jack"]

= form.label :due_days, value: "all" do
= form.checkbox :due_days, { checked: chore.daily?, include_hidden: false, multiple: true}, "all"
all

- Chore::DAY_NAMES.each do |day_name|
= form.label :due_days, value: day_name do
= form.checkbox :due_days, { checked: !chore.daily? && chore.due_on?(day_name), include_hidden: false, multiple: true}, day_name
#{day_name}

= form.button button_label
5 changes: 5 additions & 0 deletions app/views/crud/chores/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h1 Edit Chore #{chore.id}

%p= link_to "Show Chore", crud_chore_path(chore)

= render "form", chore: chore, button_label: "update"
18 changes: 18 additions & 0 deletions app/views/crud/chores/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%h1 Chores

%p= link_to "New Chore", new_crud_chore_path

%p= link_to "Random Chore", crud_chore_path("random")

%table
%thead
%tr
%th ID
%th.text-right Created At
%tbody
- chores.each do |chore|
%tr
%td= link_to chore.id, crud_chore_path(chore)
%td.text-right= in_tz chore.created_at

= paginate chores
5 changes: 5 additions & 0 deletions app/views/crud/chores/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h1 New Chore

%p= link_to "Chore List", crud_chores_path

= render "form", chore: chore, button_label: "create"
9 changes: 9 additions & 0 deletions app/views/crud/chores/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%h1 Chore #{chore.id}

%p= link_to "Chore List", crud_chores_path

%p= link_to "Edit Chore", edit_crud_chore_path(chore)

%p= link_to "Delete Chore", crud_chore_path(chore), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" }

= render partial: "attrs_table", locals: { attrs: chore.table_attrs }
5 changes: 3 additions & 2 deletions app/views/dashboard/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

%h2 CRUD Pages

%p= link_to "Boops", crud_boops_path
%p= link_to "Apache Log Items", crud_apache_log_items_path
%p= link_to "Apache Log Files", crud_apache_log_files_path
%p= link_to "Apache Log Items", crud_apache_log_items_path
%p= link_to "Books", crud_books_path
%p= link_to "Boops", crud_boops_path
%p= link_to "CSV Uploads", crud_csv_uploads_path
%p= link_to "Chores", crud_chores_path
%p= link_to "Financial Accounts", crud_financial_accounts_path
%p= link_to "Gift Ideas", crud_gift_ideas_path
%p= link_to "Post Bin Requests", crud_post_bin_requests_path
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
resources :apache_log_items
resources :books
resources :boops
resources :chores
resources :csv_uploads
resources :financial_accounts do
resources :financial_statements
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20260222224919_create_chores.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateChores < ActiveRecord::Migration[8.0]
def change
create_table :chores do |t|
t.string :title, null: false
t.integer :assignee, null: false
t.integer :due_days, array: true, default: []

t.timestamps
end
end
end
7 changes: 7 additions & 0 deletions spec/factories/chore.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :chore do
assignee { "jon" }
title { "Clean Up" }
due_days { [0] }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
%h2 CRUD Pages

%p= link_to "Things", crud_things_path
%p= link_to "Boops", crud_boops_path
%p= link_to "Apache Log Items", crud_apache_log_items_path
%p= link_to "Apache Log Files", crud_apache_log_files_path
%p= link_to "Apache Log Items", crud_apache_log_items_path
%p= link_to "Books", crud_books_path
%p= link_to "Boops", crud_boops_path
%p= link_to "CSV Uploads", crud_csv_uploads_path
%p= link_to "Chores", crud_chores_path
%p= link_to "Financial Accounts", crud_financial_accounts_path
%p= link_to "Gift Ideas", crud_gift_ideas_path
%p= link_to "Post Bin Requests", crud_post_bin_requests_path
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/generators/crud_pages/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
resources :apache_log_items
resources :books
resources :boops
resources :chores
resources :csv_uploads
resources :financial_accounts do
resources :financial_statements
Expand Down
19 changes: 19 additions & 0 deletions spec/models/chore_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "rails_helper"

describe Chore do
describe "validation" do
context "without required attrs" do
it "is invalid" do
chore = Chore.new
expect(chore).to_not be_valid
end
end

context "with required attrs" do
it "is valid" do
chore = Chore.new(assignee: "jon", due_days: [0], title: "Clean Up")
expect(chore).to be_valid
end
end
end
end
Loading