Permalink
Browse files

Refactored UseCases and Controller

  • Loading branch information...
smakagon committed Mar 27, 2017
1 parent 1f2ddef commit a68ece205b4424e2a8e5dd9aaf49340e28213b3a
@@ -11,18 +11,24 @@ def new
def create
success = ->(user) { redirect_to user_path(user), notice: 'User has been created' }
- error = ->(user) { @user = user; render :new }
+ error = lambda do |user|
+ @user = user
+ render :new
+ end
- UseCase::User::SignUp.new(repo).call(user_params, success: success, failure: error)
+ UseCase::User::SignUp.call(user_params, success: success, failure: error)
end
def edit; end
def update
success = ->(user) { redirect_to user_path(user), notice: 'User has been updated' }
- error = ->(user) { @user = user; render :edit }
+ error = lambda do |user|
+ @user = user
+ render :edit
+ end
- UseCase::User::UpdateProfile.new(repo).call(@user, user_params, success: success, failure: error)
+ UseCase::User::UpdateProfile.call(@user, user_params, success: success, failure: error)
end
def show; end
@@ -31,7 +37,7 @@ def destroy
success = -> { redirect_to users_path, notice: 'User has been removed' }
error = -> { redirect_to users_path, alert: 'Couldn\'t remove user' }
- UseCase::User::DeleteProfile.new(repo).call(@user, success: success, failure: error)
+ UseCase::User::DeleteProfile.call(@user, success: success, failure: error)
end
private
@@ -1,9 +0,0 @@
-module UseCase
- class Base
- attr_reader :repository
-
- def initialize(repo)
- @repository = repo
- end
- end
-end
@@ -1,9 +1,23 @@
module UseCase
module User
- class DeleteProfile < UseCase::Base
- def call(user, callbacks)
+ class DeleteProfile
+ def self.call(user, callbacks, repository = UserRepository.new)
+ new(user, callbacks, repository).call
+ end
+
+ def call
repository.delete(user) ? callbacks[:success].call : callbacks[:failure].call
end
+
+ private
+
+ def initialize(user, callbacks, repository)
+ @user = user
+ @callbacks = callbacks
+ @repository = repository
+ end
+
+ attr_reader :user, :callbacks, :repository
end
end
end
@@ -1,10 +1,24 @@
module UseCase
module User
- class SignUp < UseCase::Base
- def call(attrs, callbacks)
+ class SignUp
+ def self.call(attrs, callbacks, repository = UserRepository.new)
+ new(attrs, callbacks, repository).call
+ end
+
+ def call
user = repository.new_entity(attrs)
repository.save(user) ? callbacks[:success].call(user) : callbacks[:failure].call(user)
end
+
+ private
+
+ def initialize(attrs, callbacks, repository)
+ @attrs = attrs
+ @callbacks = callbacks
+ @repository = repository
+ end
+
+ attr_reader :repository, :callbacks, :attrs
end
end
end
@@ -1,11 +1,26 @@
module UseCase
module User
- class UpdateProfile < UseCase::Base
- def call(user, attrs, callbacks)
+ class UpdateProfile
+ def self.call(user, attrs, callbacks, repository = UserRepository.new)
+ new(user, attrs, callbacks, repository).call
+ end
+
+ def call
user.assign_attributes(attrs)
repository.save(user) ? callbacks[:success].call(user) : callbacks[:failure].call(user)
end
+
+ private
+
+ def initialize(user, attrs, callbacks, repository)
+ @user = user
+ @attrs = attrs
+ @callbacks = callbacks
+ @repository = repository
+ end
+
+ attr_reader :user, :attrs, :callbacks, :repository
end
end
end

0 comments on commit a68ece2

Please sign in to comment.