Skip to content

Commit

Permalink
Further refactor ListCharacters to a single static method
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonroelofs committed Sep 17, 2012
1 parent 4499200 commit b4c0798
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 27 deletions.
3 changes: 1 addition & 2 deletions app/controllers/characters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ class CharactersController < ApplicationController
navigation :characters

def index
action = ListCharacters.new current_user
@characters_by_guild = CharactersByGuild.new action.run
@characters_by_guild = CharactersByGuild.new ListCharacters.all_for_user(current_user)

if @characters_by_guild.empty?
redirect_to action: "new"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/raids_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def show
@raid = find_raid params[:id]
@signups = ListSignups.for_raid(@raid)

@current_user_characters = ListCharacters.new(current_user).run
@current_user_characters = ListCharacters.all_for_user(current_user)

# Possibly move this logic elsewhere? Feels too much like implementation
# details in the controller.
Expand Down
10 changes: 2 additions & 8 deletions app/interactors/list_characters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@

class ListCharacters

attr_accessor :user

def initialize(user)
@user = user
end

def run
Repository.for(Character).find_all_for_user @user
def self.all_for_user(user)
Repository.for(Character).find_all_for_user user
end

end
4 changes: 2 additions & 2 deletions test/controllers/characters_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class CharactersControllerTest < ActionController::TestCase
Character.new(id: 3)
]

ListCharacters.any_instance.expects(:run).returns(characters)
ListCharacters.expects(:all_for_user).with(@user).returns(characters)

get :index

assigns(:characters_by_guild).wont_be_nil
end

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

get :index

Expand Down
6 changes: 3 additions & 3 deletions test/controllers/raids_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RaidsControllerTest < ActionController::TestCase

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

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

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

get :show, :id => 10

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

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

signups = ListSignups::Signups.new
signups.add_signup Signup.new(character: list[0])
Expand Down
16 changes: 5 additions & 11 deletions test/unit/interactors/list_characters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@

describe ListCharacters do

it "takes a user on construction" do
user = User.new
action = ListCharacters.new user
action.user.must_equal user
end

describe "#run" do
describe "#all" do
it "finds all characters for the given user" do
user = User.new
character = Character.new name: "Johnson", user: user
Repository.for(Character).save character
action = ListCharacters.new user

action.run.must_equal [character]
list = ListCharacters.all_for_user user
list.must_equal [character]
end

it "returns the empty list if no characters found" do
user = User.new
action = ListCharacters.new user

action.run.must_equal []
list = ListCharacters.all_for_user user
list.must_equal []
end
end

Expand Down

0 comments on commit b4c0798

Please sign in to comment.