class UserViewModel
def initialize(user:)
@user = user
end
def boards
@user.created_boards.readable(current_user).opened.page(1).per(5)
end
def view_name
# ActiveSupport::CurrentAttributes で登録済みの current_user を参照する
if CurrentAttributes.user == @user
'me'
elsif
# ...
end
end
def layout
'user'
end
end
def show
@user_vm = UserViewModel.new(
user: User.find_by_screen_name!(params[:screen_name])
)
render @user_vm.view_name, layout: @user_vm.layout
end
def show
ShowUserService.call(
controller: self, params: params, current_user: current_user
)
end
class ShowUserService < Yuba::Service
property :current_user
property :params
property :controller
delegate :render, to: :controller
delegate :myself?, :friends?, to: :view_model
def call
if myself?
render 'me', layout: 'user', view_model: view_model
elsif friends?
read_latest_condition
render 'friend', layout: 'user', view_model: view_model
else
render 'not_friend', layout: 'user', view_model: view_model
end
end
private
def view_model
@view_model ||= UserShowViewModel.new(
current_user: current_user, params: params
)
end
def read_latest_condition
Notification::ConditionReader.call(
reader: current_user, conditions: view_model.user.latest_condition
)
end
end
class UserShowViewModel < Yuba::ViewModel
property :current_user
property :params
def user
@user ||= User.find_by_screen_name!(params[:screen_name])
end
def boards
@boards ||= user.created_boards.readable(current_user).opened.page(1).per(5)
end
def myself?
current_user == user
end
def friends?
current_user.friends_with?(user)
end
# 友達ではないときにだけ使用される
def mutual_friends
@mutual_friends ||= MutualFriendsFinder.find(user, current_user, limit: 9)
end
end