Skip to content

Commit

Permalink
Merge pull request #165 from nickskalkin/update-users-using-interactor
Browse files Browse the repository at this point in the history
Updating users using interactor
  • Loading branch information
davydovanton authored Sep 23, 2017
2 parents 68842bf + 3a951b7 commit b8f4fc8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
16 changes: 3 additions & 13 deletions apps/admin/controllers/users/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,14 @@ class Update
end

def call(params)
# TODO: To operation
@user = repo.find(params[:id])
result = Interactors::Users::Update.new(params.valid?, params).call
@user = result.user

if @user && params.valid?
user_params = params[:user]
user_params[:admin] = user_params[:admin] == '1'

repo.update(@user.id, user_params)
if result.successful?
redirect_to routes.user_path(user.id)
else
redirect_to routes.edit_user_path(user.id)
end
end

private

def repo
@repo ||= UserRepository.new
end
end
end
36 changes: 36 additions & 0 deletions lib/ossboard/interactors/users/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'hanami/interactor'

module Interactors
module Users
class Update
include Hanami::Interactor

expose :user

def initialize(params_valid, params)
@params = params
@params_valid = params_valid
@user = repo.find(@params[:id])
end

def call
return error('No user found') unless @user
return error('Unprocessable entity') unless @params_valid

prepare_user_params
repo.update(@user.id, @params[:user])
end

private

def repo
UserRepository.new()
end

def prepare_user_params

@params[:user][:admin] = @params[:user][:admin] == '1'
end
end
end
end

0 comments on commit b8f4fc8

Please sign in to comment.