Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visitor creates crank user #174

Merged
merged 2 commits into from
Mar 12, 2024
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
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
15 changes: 15 additions & 0 deletions app/controllers/crank_users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CrankUsersController < ApplicationController
skip_before_action :ensure_admin

expose(:crank_user, build: -> { CrankUser.new_with_code }, find_by: :code, id: :code)

def create
if crank_user.save
flash.notice = "Crank User created"
redirect_to crank_user_path(crank_user)
else
flash.alert = crank_user.errors.full_messages.to_sentence
redirect_to new_crank_user_path
end
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
20 changes: 20 additions & 0 deletions app/models/crank_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class CrankUser < ApplicationRecord
has_many :crank_counts
validates :code, presence: true, uniqueness: true

def self.generate_codes
3.times.lazy.map do
candidate = SecureRandom.hex(4)
candidate unless CrankUser.where(code: candidate).exists?
end.compact
end

def self.new_with_code
code = generate_codes.first
new(code: code)
end

def to_param
code
end
end
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}
6 changes: 6 additions & 0 deletions app/views/crank_users/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%h1 Create Crank User

%p Wow you seem like you can really crank - click the create button to get started!

= form_with model: crank_user do |f|
= f.button "create"
3 changes: 3 additions & 0 deletions app/views/crank_users/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%h1 Crank User #{crank_user.code}

%p OMG you did it!
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

resources :gift_ideas, only: %i[update]

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]
resources :csv_uploads
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240305034656_create_crank_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateCrankUsers < ActiveRecord::Migration[7.1]
def change
create_table :crank_users do |t|
t.string :code, null: false
t.timestamps
end

add_index :crank_users, :code, unique: true
end
end
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
5 changes: 5 additions & 0 deletions spec/factories/crank_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :crank_user do
code { "abcd1234" }
end
end
78 changes: 78 additions & 0 deletions spec/models/crank_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require "rails_helper"

describe CrankUser do
describe ".new_with_code" do
let!(:existing_crank_user) do
CrankUser.create(code: "existing code")
end

context "unique code on first try" do
let(:codes) do
[
"unique code"
]
end

it "is valid" do
expect(SecureRandom).to receive(:hex).with(4).exactly(1).and_return(*codes)
crank_user = CrankUser.new_with_code

expect(crank_user).to be_valid
expect(crank_user.code).to eq "unique code"
end
end

context "unique code on second try" do
let(:codes) do
[
"existing code",
"unique code"
]
end

it "is valid" do
expect(SecureRandom).to receive(:hex).with(4).exactly(2).and_return(*codes)
crank_user = CrankUser.new_with_code

expect(crank_user).to be_valid
expect(crank_user.code).to eq "unique code"
end
end

context "unique code on third try" do
let(:codes) do
[
"existing code",
"existing code",
"unique code"
]
end

it "is valid" do
expect(SecureRandom).to receive(:hex).with(4).exactly(3).and_return(*codes)
crank_user = CrankUser.new_with_code

expect(crank_user).to be_valid
expect(crank_user.code).to eq "unique code"
end
end

context "fails to find unique code" do
let(:codes) do
[
"existing code",
"existing code",
"existing code"
]
end

it "is not valid" do
expect(SecureRandom).to receive(:hex).with(4).exactly(3).and_return(*codes)
crank_user = CrankUser.new_with_code

expect(crank_user).to_not be_valid
expect(crank_user.code).to eq nil
end
end
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
12 changes: 12 additions & 0 deletions spec/system/crank_users/visitor_creates_crank_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "rails_helper"

describe "visitor creates crank user" do
scenario "create successfully" do
visit "/crank_users/new"
click_on "create"
expect(page).to have_content "Crank User created"
crank_user = CrankUser.last
expect(page).to have_content crank_user.code
expect(page.current_path).to eq crank_user_path(crank_user)
end
end
11 changes: 10 additions & 1 deletion 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,8 @@
expected_rows = [
"Artwork 0",
"Book 0",
"CrankCount 0",
"CrankUser 0",
"CsvUpload 0",
"FinancialAccount 0",
"FinancialStatement 0",
Expand Down Expand Up @@ -39,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)
Expand All @@ -63,6 +70,8 @@
expected_rows = [
"Artwork 1",
"Book 1",
"CrankCount 1",
"CrankUser 1",
"CsvUpload 1",
"FinancialAccount 1",
"FinancialStatement 1",
Expand All @@ -81,6 +90,6 @@

expect(actual_rows).to match_array(expected_rows)

expect(page.find("tfoot tr").text).to eq "Total 16"
expect(page.find("tfoot tr").text).to eq "Total 18"
end
end
Loading