Skip to content
Permalink
Browse files

Further refactor ListCharacters to a single static method

  • Loading branch information
jasonroelofs committed Sep 17, 2012
1 parent 4499200 commit b4c079816225270b946eaa12e9900067f2476a64
@@ -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"
@@ -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.
@@ -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
@@ -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

@@ -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

@@ -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

@@ -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])
@@ -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

0 comments on commit b4c0798

Please sign in to comment.
You can’t perform that action at this time.