Skip to content

Instantly share code, notes, and snippets.

View walshyb's full-sized avatar
hello world

Brandon Walsh walshyb

hello world
View GitHub Profile
@walshyb
walshyb / registrations_controller.rb
Created August 14, 2020 17:51
Updated RegistrationsController that uses Contact association
class RegistrationsController < Devise::RegistrationsController
protected
def sign_up_params
params.require(:user).permit(
:password,
:password_confirmation,
contact: [
:name,
email_addresses_attributes: [ :email ]
@walshyb
walshyb / new.html.erb
Created August 14, 2020 17:49
Updated devise new registration template that uses Contact association
# app/views/devise/registrations/new.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<%= f.fields_for :contact do |c| %>
<div class="field">
<%= c.label :name %><br />
<%= c.text_field :name %>
</div>
@walshyb
walshyb / user.rb
Created August 14, 2020 17:46
Updated user model that belongs to Contact, who has the EmailAddress association
# app/models/user.rb
class User
belongs_to :contact
# This is optional
has_many :email_addresses, through: :contact
# So we can create the contact association when creating a User
accepts_nested_attributes_for :contact
after_initialize :add_contact
@walshyb
walshyb / contact.rb
Created August 14, 2020 17:38
Example associations for Contact modal
# app/models/contact.rb
class Contact
has_one :user
has_many :email_addresses
accepts_nested_attributes_for :email_addresses
end
@walshyb
walshyb / user.rb
Created August 14, 2020 17:37
Helper method to get primary email in User model
# app/models/user.rb
def email
email_address = email_addresses.where(primary: true).first
return email_address.email if email_address
end
@walshyb
walshyb / user.rb
Last active August 14, 2020 17:35
Updated where clause that includes primary
where(conditions.to_h)
.includes(:email_addresses)
.where(email_addresses: {
email: email,
primary: true
}).first
# db/migrate/20200804153022_add_primary_to_email_addresses
class AddPrimaryToEmailAddresses < ActiveRecord::Migration[6.0]
def change
add_column :email_addresses, :primary, :boolean, default: false
add_index :email_addresses, :primary
end
end
@walshyb
walshyb / registrations_controller.rb
Last active August 14, 2020 17:22
Overriding devise's RegistrationController to permit more params
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
protected
def sign_up_params
params.require(:user).permit(
:name,
:password,
:password_confirmation,
email_addresses: [
@walshyb
walshyb / new.html.erb
Created August 14, 2020 17:17
Override for the devise new registration view
# app/views/devise/registrations/new.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<%= f.fields_for 'email_addresses_attributes[1]' do |e| %>
<div class="field">
<%= e.label :email %><br />
<%= e.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<% end %>
@walshyb
walshyb / user.rb
Last active August 17, 2020 17:45
Devise method overrides in User model
# app/models/user.rb
class User
has_many :email_addresses
accepts_nested_attributes_for :email_addresses
# ... association definitions and other methods
# Define our overrides: