Skip to content

Commit

Permalink
Visitor creates crank user
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Mar 5, 2024
1 parent 9ab43f5 commit 4591a62
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 1 deletion.
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
19 changes: 19 additions & 0 deletions app/models/crank_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class CrankUser < ApplicationRecord
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
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!
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

resources :gift_ideas, only: %i[update]

resources :crank_users, only: %i[create new show], param: :code

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
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
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
5 changes: 4 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,7 @@
expected_rows = [
"Artwork 0",
"Book 0",
"CrankUser 0",
"CsvUpload 0",
"FinancialAccount 0",
"FinancialStatement 0",
Expand All @@ -32,6 +33,7 @@

scenario "with some models" do
FactoryBot.create(:book)
FactoryBot.create(:crank_user)
FactoryBot.create(:csv_upload)
FactoryBot.create(:gift_idea)
FactoryBot.create(:killswitch)
Expand Down Expand Up @@ -63,6 +65,7 @@
expected_rows = [
"Artwork 1",
"Book 1",
"CrankUser 1",
"CsvUpload 1",
"FinancialAccount 1",
"FinancialStatement 1",
Expand All @@ -81,6 +84,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 17"
end
end

0 comments on commit 4591a62

Please sign in to comment.