Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit b4c0798

Browse files
committed
Further refactor ListCharacters to a single static method
1 parent 4499200 commit b4c0798

File tree

6 files changed

+14
-27
lines changed

6 files changed

+14
-27
lines changed

app/controllers/characters_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ class CharactersController < ApplicationController
44
navigation :characters
55

66
def index
7-
action = ListCharacters.new current_user
8-
@characters_by_guild = CharactersByGuild.new action.run
7+
@characters_by_guild = CharactersByGuild.new ListCharacters.all_for_user(current_user)
98

109
if @characters_by_guild.empty?
1110
redirect_to action: "new"

app/controllers/raids_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def show
1212
@raid = find_raid params[:id]
1313
@signups = ListSignups.for_raid(@raid)
1414

15-
@current_user_characters = ListCharacters.new(current_user).run
15+
@current_user_characters = ListCharacters.all_for_user(current_user)
1616

1717
# Possibly move this logic elsewhere? Feels too much like implementation
1818
# details in the controller.

app/interactors/list_characters.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33

44
class ListCharacters
55

6-
attr_accessor :user
7-
8-
def initialize(user)
9-
@user = user
10-
end
11-
12-
def run
13-
Repository.for(Character).find_all_for_user @user
6+
def self.all_for_user(user)
7+
Repository.for(Character).find_all_for_user user
148
end
159

1610
end

test/controllers/characters_controller_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ class CharactersControllerTest < ActionController::TestCase
2828
Character.new(id: 3)
2929
]
3030

31-
ListCharacters.any_instance.expects(:run).returns(characters)
31+
ListCharacters.expects(:all_for_user).with(@user).returns(characters)
3232

3333
get :index
3434

3535
assigns(:characters_by_guild).wont_be_nil
3636
end
3737

3838
it "redirects to #new if there are no characters" do
39-
ListCharacters.any_instance.expects(:run).returns([])
39+
ListCharacters.expects(:all_for_user).with(@user).returns([])
4040

4141
get :index
4242

test/controllers/raids_controller_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class RaidsControllerTest < ActionController::TestCase
3838

3939
@raid = Raid.new id: 10, when: Date.today, start_at: Time.now, invite_at: Time.now
4040
FindRaid.expects(:by_id).with(10).returns(@raid)
41-
ListCharacters.any_instance.stubs(:run).returns([])
41+
ListCharacters.stubs(:all_for_user).returns([])
4242
ListSignups.stubs(:for_raid).returns ListSignups::Signups.new
4343
end
4444

@@ -60,7 +60,7 @@ class RaidsControllerTest < ActionController::TestCase
6060

6161
it "grabs the current user's list of characters" do
6262
list = [Character.new]
63-
ListCharacters.any_instance.expects(:run).returns(list)
63+
ListCharacters.expects(:all_for_user).with(@user).returns(list)
6464

6565
get :show, :id => 10
6666

@@ -70,7 +70,7 @@ class RaidsControllerTest < ActionController::TestCase
7070

7171
it "removes characters already signed up from the current user's list" do
7272
list = [Character.new]
73-
ListCharacters.any_instance.stubs(:run).returns(list)
73+
ListCharacters.stubs(:all_for_user).with(@user).returns(list)
7474

7575
signups = ListSignups::Signups.new
7676
signups.add_signup Signup.new(character: list[0])

test/unit/interactors/list_characters_test.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,21 @@
66

77
describe ListCharacters do
88

9-
it "takes a user on construction" do
10-
user = User.new
11-
action = ListCharacters.new user
12-
action.user.must_equal user
13-
end
14-
15-
describe "#run" do
9+
describe "#all" do
1610
it "finds all characters for the given user" do
1711
user = User.new
1812
character = Character.new name: "Johnson", user: user
1913
Repository.for(Character).save character
20-
action = ListCharacters.new user
2114

22-
action.run.must_equal [character]
15+
list = ListCharacters.all_for_user user
16+
list.must_equal [character]
2317
end
2418

2519
it "returns the empty list if no characters found" do
2620
user = User.new
27-
action = ListCharacters.new user
2821

29-
action.run.must_equal []
22+
list = ListCharacters.all_for_user user
23+
list.must_equal []
3024
end
3125
end
3226

0 commit comments

Comments
 (0)