Created
April 11, 2020 10:16
-
-
Save alex-lairan/1aaff258593192c83ad410c6f040220f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class UserFetcher | |
abstract def call : User? | |
class FromEmail < UserFetcher | |
@email : String? | |
def initialize(@email : String?, @query = UserQuery.new) | |
end | |
def call : User? | |
@email.try { |email| @query.email(email).first? } | |
end | |
end | |
class FromInstance < UserFetcher | |
def initialize(@user : User?) | |
end | |
def call : User? | |
@user | |
end | |
end | |
end | |
class Workspaces::AssignUser < UserWorkspace::SaveOperation | |
param_key :membership | |
permit_columns workspace_id | |
needs user_fetcher : UserFetcher | |
before_save do | |
validate_required workspace_id | |
assign_user_id | |
validate_not_already_member | |
end | |
private def assign_user_id | |
@user_fetcher.call.try { |user| user_id.value = user.id } | |
end | |
private def validate_not_already_member | |
workspace_id.value.try do |id| | |
validate_uniqueness_of user_id, | |
query: UserWorkspaceQuery.new.workspace_id(id).user_id, | |
message: "already member" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment