Rails Form Models
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
# app/forms/base_form.rb | |
class BaseForm | |
include ActiveModel::Validations | |
include ActiveModel::Conversion | |
extend ActiveModel::Naming | |
def initialize(attributes = {}) | |
attributes.each do |name, value| | |
send("#{name}=", value) | |
end | |
end | |
def persisted? | |
false | |
end | |
end |
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
<%= form_for @signup, url: signup_path do |f| %> | |
<% if @signup.errors.any? %> | |
<div id="errorExplanation"> | |
<h2><%= pluralize(@signup.errors.count, 'error') %> prohibited your signup:</h2> | |
<ul> | |
<% @signup.errors.full_messages.each do |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
... you know how to do forms ... | |
<% end %> |
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
# app/forms/signup.rb | |
class Signup < BaseForm | |
attr_accessor :company_name, | |
:subdomain, | |
:email, | |
:password, | |
:password_confirmation, | |
:first_name, | |
:last_name | |
validates :company_name, presence: true | |
validates :subdomain, presence: true, subdomain: true | |
validates :email, presence: true | |
validates :first_name, :last_name, presence: true | |
validates :password, presence: true, confirmation: true | |
attr_reader :user, :company | |
def save! | |
return false unless valid? | |
@company = Company.new( | |
name: company_name, | |
subdomain: subdomain | |
) | |
# bail out before creating schema so the user can correct any missing fields | |
unless company.valid? | |
company.errors.each do |attr, message| | |
errors.add attr, message | |
end | |
return false | |
end | |
if company.save! | |
# create a new schema for the company | |
Apartment::Database.create(subdomain) | |
Apartment::Database.switch(subdomain) | |
@user = User.new( | |
email: email, | |
password: password, | |
first_name: first_name, | |
last_name: last_name | |
) | |
user.enabled = true | |
user.build_profile # make a blank profile | |
# create the admin user | |
user.role = Role.where(name: 'System Administrator').first | |
user.save! | |
# load our post-create seeds into our schema | |
load_post_create_seeds! | |
user # return user to sign them in | |
end | |
end | |
private | |
def load_post_create_seeds! | |
silence_stream(STDOUT){ load_or_abort("#{Rails.root}/db/post-create-seeds.rb") } | |
end | |
def load_or_abort(file) | |
if File.exists?(file) | |
load(file) | |
else | |
abort %{#{file} doesn't exist yet} | |
end | |
end | |
end |
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
# app/controllers/signups_controller.rb | |
class SignupsController < ApplicationController | |
respond_to :html | |
def new | |
@signup = Signup.new | |
respond_with @signup | |
end | |
def create | |
@signup = Signup.new(params[:signup]) | |
if user = @signup.save! | |
sign_in user | |
end | |
respond_with @signup, location: root_url(subdomain: @signup.subdomain) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment