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 / sendIP.py
Created August 29, 2015 01:38
Simple script that checks if there's internet connection and if there is, then use the Mandrill API to send the client's current IP address to a specified email address.
import mandrill
import socket
REMOTE_SERVER = "www.google.com"
#checks if there is internet connection
def is_connected():
try:
# see if we can resolve the host name -- tells us if there is
# a DNS listening
@walshyb
walshyb / upload_svg_if_admin.php
Last active May 25, 2017 19:55
In WordPress, allow uploading of svgs through the Admin Panel to the media library if the current user is an administrator.
<?php
function enable_svg_upload_if_admin($mimes) {
$current_user_roles = wp_get_current_user()->roles; // get roles of current user
// check if user is administrator and user is in the admin panel (not in the frontend)
if(in_array('administrator', $current_user_roles) && is_admin()) {
$mimes['svg'] = 'image/svg+xml';
return $mimes; // if administrator, enable svg upload
}
@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:
@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 / 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: [
# 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 / 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
@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 / 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: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