forked from houdiniproject/houdini
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #772 from CommitChange/move-calculated-names-into-…
…a-concern Add calculated first and last names to User
- Loading branch information
Showing
7 changed files
with
120 additions
and
24 deletions.
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later | ||
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE | ||
module Model::CalculatedNames | ||
extend ActiveSupport::Concern | ||
included do | ||
def calculated_first_name | ||
name_parts = name&.strip&.split(' ')&.map(&:strip) | ||
case name_parts&.count || 0 | ||
when 0 | ||
nil | ||
when 1 | ||
name_parts[0] | ||
else | ||
name_parts[0..-2].join(" ") | ||
end | ||
end | ||
|
||
def calculated_last_name | ||
name_parts = name&.strip&.split(' ')&.map(&:strip) | ||
case name_parts&.count || 0 | ||
when 0 | ||
nil | ||
when 1 | ||
nil | ||
else | ||
name_parts[-1] | ||
end | ||
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
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later | ||
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE | ||
require 'rails_helper' | ||
|
||
RSpec.describe Model::CalculatedNames do | ||
subject do | ||
Class.new do | ||
include Model::CalculatedNames | ||
|
||
attr_accessor :name | ||
|
||
end.new | ||
end | ||
|
||
it_behaves_like 'a model with a calculated first and last name' | ||
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
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,65 @@ | ||
# frozen_string_literal: true | ||
|
||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later | ||
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE | ||
require 'rails_helper' | ||
# rubocop:disable RSpec/VerifiedDoubles, RSpec/MessageSpies regular doubles work fine in this use-case | ||
|
||
RSpec.shared_examples 'a model with a calculated first and last name' do | ||
|
||
let(:instance) { subject } | ||
|
||
describe "#calculated_first_name" do | ||
it "has nil name" do | ||
instance.name = nil | ||
expect(instance.calculated_first_name).to be_nil | ||
end | ||
|
||
it "has blank name" do | ||
instance.name = "" | ||
expect(instance.calculated_first_name).to be_nil | ||
end | ||
|
||
it "has one word name" do | ||
instance.name = "Penelope" | ||
expect(instance.calculated_first_name).to eq "Penelope" | ||
end | ||
|
||
it "has two word name" do | ||
instance.name = "Penelope Schultz" | ||
expect(instance.calculated_first_name).to eq "Penelope" | ||
end | ||
|
||
it "has three word name" do | ||
instance.name = "Penelope Rebecca Schultz" | ||
expect(instance.calculated_first_name).to eq "Penelope Rebecca" | ||
end | ||
end | ||
|
||
describe "#calculated_last_name" do | ||
it "has nil name" do | ||
instance.name = nil | ||
expect(instance.calculated_last_name).to be_nil | ||
end | ||
|
||
it "has blank name" do | ||
instance.name = "" | ||
expect(instance.calculated_last_name).to be_nil | ||
end | ||
|
||
it "has one word name" do | ||
instance.name = "Penelope" | ||
expect(instance.calculated_last_name).to be_nil | ||
end | ||
|
||
it "has two word name" do | ||
instance.name = "Penelope Schultz" | ||
expect(instance.calculated_last_name).to eq "Schultz" | ||
end | ||
|
||
it "has three word name" do | ||
instance.name = "Penelope Rebecca Schultz" | ||
expect(instance.calculated_last_name).to eq "Schultz" | ||
end | ||
end | ||
end |