diff --git a/app/controllers/crank_counts_controller.rb b/app/controllers/crank_counts_controller.rb new file mode 100644 index 0000000..0c5257c --- /dev/null +++ b/app/controllers/crank_counts_controller.rb @@ -0,0 +1,22 @@ +class CrankCountsController < ApplicationController + skip_before_action :ensure_admin + + expose(:crank_user, find_by: :code, id: :crank_user_code) + expose(:crank_count, parent: :crank_user) + + def create + if crank_count.save + flash.notice = "Crank Count created" + redirect_to crank_user_crank_count_path(crank_user, crank_count) + else + flash.alert = crank_count.errors.full_messages.to_sentence + redirect_to new_crank_user_crank_count_path(crank_user) + end + end + + private + + def crank_count_params + params.require(:crank_count).permit(:ticks).merge(cranked_on: Date.today) + end +end diff --git a/app/models/crank_count.rb b/app/models/crank_count.rb new file mode 100644 index 0000000..70150be --- /dev/null +++ b/app/models/crank_count.rb @@ -0,0 +1,6 @@ +class CrankCount < ApplicationRecord + belongs_to :crank_user + + validates :cranked_on, presence: true + validates :ticks, presence: true +end diff --git a/app/models/crank_user.rb b/app/models/crank_user.rb index a53c476..ab2a7ac 100644 --- a/app/models/crank_user.rb +++ b/app/models/crank_user.rb @@ -1,4 +1,5 @@ class CrankUser < ApplicationRecord + has_many :crank_counts validates :code, presence: true, uniqueness: true def self.generate_codes diff --git a/app/views/crank_counts/new.html.haml b/app/views/crank_counts/new.html.haml new file mode 100644 index 0000000..f95b1b0 --- /dev/null +++ b/app/views/crank_counts/new.html.haml @@ -0,0 +1,7 @@ +%h1 Create Crank Count for Crank User #{crank_user.code} + +%p Had enough for today? Go ahead and click create to log #{params[:ticks]} ticks! + += form_with model: [crank_user, crank_count] do |f| + = f.hidden_field :ticks, value: params[:ticks] + = f.button "create" diff --git a/app/views/crank_counts/show.html.haml b/app/views/crank_counts/show.html.haml new file mode 100644 index 0000000..7000c39 --- /dev/null +++ b/app/views/crank_counts/show.html.haml @@ -0,0 +1,4 @@ +%h1 Crank Count for Crank User #{crank_user.code} + +%p #{crank_count.ticks} ticks +%p cranked on #{crank_count.cranked_on} diff --git a/config/routes.rb b/config/routes.rb index 385ef1e..b89bc6c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,7 +24,9 @@ resources :gift_ideas, only: %i[update] - resources :crank_users, only: %i[create new show], param: :code + resources :crank_users, only: %i[create new show], param: :code do + resources :crank_counts, only: %i[create new show] + end namespace :crud do resources :books, only: %i[create edit new update] diff --git a/db/migrate/20240308221817_create_crank_counts.rb b/db/migrate/20240308221817_create_crank_counts.rb new file mode 100644 index 0000000..2058aa4 --- /dev/null +++ b/db/migrate/20240308221817_create_crank_counts.rb @@ -0,0 +1,10 @@ +class CreateCrankCounts < ActiveRecord::Migration[7.1] + def change + create_table :crank_counts do |t| + t.belongs_to :crank_user + t.integer :ticks, null: false + t.date :cranked_on, null: false + t.timestamps + end + end +end diff --git a/spec/factories/crank_count.rb b/spec/factories/crank_count.rb new file mode 100644 index 0000000..c01ccfe --- /dev/null +++ b/spec/factories/crank_count.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :crank_count do + crank_user + ticks { 7 } + cranked_on { Date.today } + end +end diff --git a/spec/system/crank_counts/visitor_creates_crank_count_spec.rb b/spec/system/crank_counts/visitor_creates_crank_count_spec.rb new file mode 100644 index 0000000..5deec95 --- /dev/null +++ b/spec/system/crank_counts/visitor_creates_crank_count_spec.rb @@ -0,0 +1,14 @@ +require "rails_helper" + +describe "visitor creates crank count" do + scenario "create successfully" do + crank_user = FactoryBot.create(:crank_user) + visit "/crank_users/#{crank_user.code}/crank_counts/new?ticks=77" + click_on "create" + expect(page).to have_content "Crank Count created" + crank_count = CrankCount.last + expect(page).to have_content crank_user.code + expect(page.current_path).to eq crank_user_crank_count_path(crank_user, crank_count) + expect(page).to have_content "77 ticks" + end +end diff --git a/spec/system/model_counts/admin_views_model_counts_spec.rb b/spec/system/model_counts/admin_views_model_counts_spec.rb index aaf470f..e5a5495 100644 --- a/spec/system/model_counts/admin_views_model_counts_spec.rb +++ b/spec/system/model_counts/admin_views_model_counts_spec.rb @@ -11,6 +11,7 @@ expected_rows = [ "Artwork 0", "Book 0", + "CrankCount 0", "CrankUser 0", "CsvUpload 0", "FinancialAccount 0", @@ -33,7 +34,6 @@ scenario "with some models" do FactoryBot.create(:book) - FactoryBot.create(:crank_user) FactoryBot.create(:csv_upload) FactoryBot.create(:gift_idea) FactoryBot.create(:killswitch) @@ -41,6 +41,11 @@ FactoryBot.create(:project) FactoryBot.create(:work_day) + FactoryBot.create( + :crank_count, + crank_user: FactoryBot.create(:crank_user) + ) + financial_account = FactoryBot.create(:financial_account) FactoryBot.create(:financial_statement, financial_account: financial_account) FactoryBot.create(:financial_transaction, financial_account: financial_account) @@ -65,6 +70,7 @@ expected_rows = [ "Artwork 1", "Book 1", + "CrankCount 1", "CrankUser 1", "CsvUpload 1", "FinancialAccount 1", @@ -84,6 +90,6 @@ expect(actual_rows).to match_array(expected_rows) - expect(page.find("tfoot tr").text).to eq "Total 17" + expect(page.find("tfoot tr").text).to eq "Total 18" end end