Skip to content

Commit

Permalink
Visitor creates crank count
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Mar 12, 2024
1 parent 4591a62 commit c928a5e
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 3 deletions.
22 changes: 22 additions & 0 deletions app/controllers/crank_counts_controller.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions app/models/crank_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CrankCount < ApplicationRecord
belongs_to :crank_user

validates :cranked_on, presence: true
validates :ticks, presence: true
end
1 change: 1 addition & 0 deletions app/models/crank_user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class CrankUser < ApplicationRecord
has_many :crank_counts
validates :code, presence: true, uniqueness: true

def self.generate_codes
Expand Down
7 changes: 7 additions & 0 deletions app/views/crank_counts/new.html.haml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions app/views/crank_counts/show.html.haml
Original file line number Diff line number Diff line change
@@ -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}
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240308221817_create_crank_counts.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/factories/crank_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :crank_count do
crank_user
ticks { 7 }
cranked_on { Date.today }
end
end
14 changes: 14 additions & 0 deletions spec/system/crank_counts/visitor_creates_crank_count_spec.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 8 additions & 2 deletions spec/system/model_counts/admin_views_model_counts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
expected_rows = [
"Artwork 0",
"Book 0",
"CrankCount 0",
"CrankUser 0",
"CsvUpload 0",
"FinancialAccount 0",
Expand All @@ -33,14 +34,18 @@

scenario "with some models" do
FactoryBot.create(:book)
FactoryBot.create(:crank_user)
FactoryBot.create(:csv_upload)
FactoryBot.create(:gift_idea)
FactoryBot.create(:killswitch)
FactoryBot.create(:post_bin_request)
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)
Expand All @@ -65,6 +70,7 @@
expected_rows = [
"Artwork 1",
"Book 1",
"CrankCount 1",
"CrankUser 1",
"CsvUpload 1",
"FinancialAccount 1",
Expand All @@ -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

0 comments on commit c928a5e

Please sign in to comment.