Skip to content

Instantly share code, notes, and snippets.

@HaiTo
Last active February 4, 2018 08:19
Show Gist options
  • Save HaiTo/90037fbf2a7333b47bf164b0ba4dd1d2 to your computer and use it in GitHub Desktop.
Save HaiTo/90037fbf2a7333b47bf164b0ba4dd1d2 to your computer and use it in GitHub Desktop.
何とも言えない設計
class FollowCommand
def initialize(user, params)
@user = user
@params = params
end
using UserCommand::Follow
def execute!
user = User.find(@params[:id])
User.transaction do
@user.follow!(user)
end
end
end
class FolloweesController < ApplicationController
using UserRepository::Follow
def create
following_user = User.find(params[:id])
current_user.follow!(following_user)
# using UserRepository::Follow {
# current_user.follow!(following_user)
# }
end
end
module UserRepository
module Follow
refine User do
def follow!(another_user)
relationship.create!(user: self, following: another_user)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment