Skip to content

Instantly share code, notes, and snippets.

@dhh
Created October 13, 2011 18:02
Show Gist options
  • Save dhh/1284958 to your computer and use it in GitHub Desktop.
Save dhh/1284958 to your computer and use it in GitHub Desktop.
def revoke(user)
proxy_association.owner.tap do |project|
# You can't remove the last user with access (someone has to have access to the project!)
if project.users.many?
if user.pending? && user.projects.one?
user.destroy
else
project.users.delete(user)
user.touch
end
project.touch :accesses_updated_at
end
end
end
@dhh
Copy link
Author

dhh commented Oct 13, 2011

Using tap to rename an unhandy variable for a fixed scope.

@nakajima
Copy link

Why not:

def revoke(user)
  project = proxy_association.owner
  # You can't remove the last user with access (someone has to have access to the project!)
  if project.users.many?
    if user.pending? && user.projects.one?
      user.destroy
    else
      project.users.delete(user)
      user.touch
    end

    project.touch :accesses_updated_at
  end
end

@leshill
Copy link

leshill commented Oct 13, 2011

Not sure if that was the intention, but the original will return the project.

@mkdynamic
Copy link

@nakajima For one, that is different. It doesn't return the project from the method.

@dhh
Copy link
Author

dhh commented Oct 13, 2011

nakajima, I much prefer to avoid local variables and I like how tap scopes the usage. As to say "in this block, we're going to be talking about proxy_association.owner as project".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment