Skip to content

Instantly share code, notes, and snippets.

@waynerobinson
Last active August 29, 2015 14:03
Show Gist options
  • Save waynerobinson/463a50b479512cf63e12 to your computer and use it in GitHub Desktop.
Save waynerobinson/463a50b479512cf63e12 to your computer and use it in GitHub Desktop.
class ClubRegistration
def initialize(attributes = {})
@attributes = attributes
end
def club
@club ||= Club.new
end
def user
@user ||= User.new
end
def role
@role ||= Role.new
end
def first_member
@first_member ||= Member.new
end
def create
User.transaction {
@club = Club.create(@attributes.fetch(:club))
@user = User.create(@attributes.fetch(:user))
@role = Role.create(user: @user, club: @club, role: Role::OWNER)
@first_member = Member.create(
@attributes
.fetch(:user)
.slice(:first_name, :last_name, :etc)
.merge({lub: @club, user: @user)
)
}
end
def created?
@club.id && @user.id && @role.id && @first_member.id
end
end
# IN CONTROLLER
def new
@club_registration = ClubRegistration.new
# Render the form using the objects in @club_registration, e.g. @club_registration.user
end
def create
@club_registration = ClubRegistration.new(params).create
if @club_registration.create?
redirect_to "some-main-dashboard"
else
# Use the objects in @club_registration to get errors for display in the view.
# E.g. @club_registration.user.errors
render :new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment