Created
February 12, 2014 16:59
-
-
Save jeremyf/8959710 to your computer and use it in GitHub Desktop.
Thinking about how to model connecting a Group to a User
This file contains 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
class Hydramata::Connection | |
include Virtus.model | |
include ActiveModel::Validations | |
extend ActiveModel::Naming | |
attribute :current_user, User | |
attribute :group_id, String | |
attribute :user_id, String | |
attribute :role, Sting | |
validates :role, inclusion: { ['manager', 'editor', 'soon?']} | |
def group | |
@group ||= Group.find(group_id) | |
end | |
def user | |
@user ||= User.find(user_id) | |
end | |
def save | |
valid? ? persist : false | |
end | |
private | |
def persist | |
group.add_member(user) && user.add_group(group) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment