-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ab43f5
commit 4591a62
Showing
10 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
spec/system/crank_users/visitor_creates_crank_user_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters