Skip to content

Instantly share code, notes, and snippets.

View msmithstubbs's full-sized avatar

Matt Stubbs msmithstubbs

View GitHub Profile
class Book < ApplicationRecord
validates :title, presence: true
end
book = Book.new
> <Book id: nil, title: nil, author_id: nil, created_at: nil, updated_at: nil, genre: nil>
book.valid?
> false

A multi-column unique index

This example walks through enforcing a unique constraint on multiple columns.

As an example, let’s look at an appointment application for a vet surgery. Each customer has one or more pets. To ensure we don’t enter a customer’s pet twice we want to make sure a pet’s name is unique for that customer. This means the customer Jill can have a pet named Sparkles. Another customer, Bob, can also have a pet named Sparkles, but a customer cannot have more than one pet named Sparkles: Bob cannot have two cats named Sparkles.

Here’s an example schema:

Customers

@msmithstubbs
msmithstubbs / ssl_puma.sh
Created December 3, 2017 00:13 — forked from tadast/ssl_puma.sh
localhost SSL with puma
# 1) Create your private key (any password will do, we remove it below)
$ cd ~/.ssh
$ openssl genrsa -des3 -out server.orig.key 2048
# 2) Remove the password
$ openssl rsa -in server.orig.key -out server.key
@msmithstubbs
msmithstubbs / webhook_verification.rb
Created June 8, 2016 04:22
Verifying a Back in Stock webhook
SHARED_SECRET = 'your_back_in_stock_api_key'
def verify_webhook(data, signature)
digest = OpenSSL::Digest::Digest.new('sha256')
calculated_hmac = Base64.encode64(OpenSSL::HMAC.digest(digest, SHARED_SECRET, data)).strip
calculated_hmac == signature
end
signature = request.headers["X-BIS-Signature"]
data = request.body
@msmithstubbs
msmithstubbs / webhook.json
Last active June 8, 2016 04:15
Back in Stock webhook example
{
"topic": "notification/sent",
"timestamp": "2016-06-08 03:58:20 UTC",
"customer": {
"email": "[email protected]"
},
"product": {
"product_id": 12345678,
"variant_id": 98765432,
"sku": "aliens-02"
@msmithstubbs
msmithstubbs / gist:855cfb2cf668d97f8852
Last active August 29, 2015 14:17
Back in Stock: Default form CSS
html, body, ul, ol, li, form, fieldset, legend {
margin: 0;
padding: 0;
}
h1, h2, h3, h4, h5, h6, p { margin-top: 0; }
fieldset,img { border: 0; }
legend { color: #000; }
@msmithstubbs
msmithstubbs / gist:6f2a9d1f8a3d601c56ae
Created March 3, 2015 11:41
Set customer email address for Back in Stock modal
{% if customer.email %}
<script>
$(document).load(function() { // wait for app JS to load
BISPopover.ready.then(function() { // wait for Back in Stock to create the iframe
var iframe = $('#BIS_frame').get(0); // find the iframe
if (iframe) {
$(iframe.contentDocument).find("#email_address").val('{{customer.email}}'); // set the email address field in the iframe
}
})
@msmithstubbs
msmithstubbs / back-in-stock.liquid
Created January 10, 2015 14:39
Back in Stock form for Supply theme (Shopify)
<div class="BIS_form">
<label for="BIS_email">Email me when restocked</label>
<input id="BIS_email" name="email" type="email" data-product-id="{{product.id}}" placeholder="[email protected]" value="{{customer.email}}">
<button class="btn" id="BIS_button">Notify Me</button>
<p class="BIS_response"> </p>
</div>
<style>
.BIS_form {
background: #f5f5f5;
@msmithstubbs
msmithstubbs / back-in-stock.liquid
Created January 5, 2015 15:12
Back in Stock form for Pacific theme (Shopify)
<style>
.BIS_form {
text-align: center;
margin-top: 20px;
display: none;
}
.BIS_form label {
display: block;
}
@msmithstubbs
msmithstubbs / back-in-stock.liquid
Created December 3, 2014 10:07
Back In Stock Product page form with mailing list checkbox
<div>
<label for="bis-email">Email me when available</label>
<input type="email" id="bis-email">
<label>
<input type="checkbox" id="bis-accepts-marketing">
Add me to the store mailing list
</label>
<button id="bis-submit">Email when available</button>
</div>