Skip to content

Commit

Permalink
Merge pull request #772 from CommitChange/move-calculated-names-into-…
Browse files Browse the repository at this point in the history
…a-concern

Add calculated first and last names to User
  • Loading branch information
wwahammy authored Sep 29, 2023
2 parents 1cbaca5 + b55092f commit f0aa1b1
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 24 deletions.
32 changes: 32 additions & 0 deletions app/models/concerns/model/calculated_names.rb
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
25 changes: 1 addition & 24 deletions app/models/supporter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
class Supporter < ActiveRecord::Base
include Model::Houidable
include Model::CalculatedNames
setup_houid :supp, :houid

ADDRESS_FIELDS = ['address', 'city', 'state_code', 'country', 'zip_code']
Expand Down Expand Up @@ -138,30 +139,6 @@ def update_member_on_all_lists
end
end

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

def profile_picture size=:normal
return unless self.profile
self.profile.get_profile_picture(size)
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
class User < ActiveRecord::Base
include Model::CalculatedNames

attr_accessible \
:email, # str: balidated with Devise
Expand Down
18 changes: 18 additions & 0 deletions spec/models/concerns/model/calculated_names_spec.rb
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
2 changes: 2 additions & 0 deletions spec/models/supporter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
require 'rails_helper'


RSpec.describe Supporter, type: :model do
it_behaves_like 'an houidable entity', :supp
it_behaves_like 'a model with a calculated first and last name'

it { is_expected.to have_many(:addresses).class_name("SupporterAddress")}
it { is_expected.to belong_to(:primary_address).class_name("SupporterAddress")}
Expand Down
1 change: 1 addition & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'rails_helper'

RSpec.describe User, :type => :model do
it_behaves_like 'a model with a calculated first and last name'

it {is_expected.to have_db_column(:locked_at).of_type(:datetime)}
it {is_expected.to have_db_column(:unlock_token).of_type(:string)}
Expand Down
65 changes: 65 additions & 0 deletions spec/support/contexts/calculated_names_context.rb
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

0 comments on commit f0aa1b1

Please sign in to comment.