Skip to content

Instantly share code, notes, and snippets.

View cdimartino's full-sized avatar

Chris DiMartino cdimartino

View GitHub Profile
Panel 6: ✅ would move to workspace 124647 (General)
Panel 16: ✅ would move to workspace 85474 (General)
Panel 26: ✅ would move to workspace 96694 (General)
Panel 31: ✅ would move to workspace 154272 (General)
Panel 36: ✅ would move to workspace 85474 (General)
Panel 46: ✅ would move to workspace 106524 (General)
Panel 51: ✅ would move to workspace 106524 (General)
Panel 71: ✅ would move to workspace 85474 (General)
Panel 76: ✅ would move to workspace 85474 (General)
Panel 91: ✅ would move to workspace 96694 (General)
@cdimartino
cdimartino / pre-commit
Last active March 2, 2017 18:06
Yardoc git pre-commit hook
#!/bin/sh
# Regenerates documentaion and adds it to the commit
#
# This file must live in .git/hooks/pre-commit and be executable
#
# Setup:
#
# cp pre-commit .git/hooks/
# chmod +x .git/hooks/pre-commit
@cdimartino
cdimartino / -
Created December 14, 2016 02:50
#!/bin/sh
GIT=`which git`
$GIT clone https://github.com/kaplan-advance/backoffice-simple-setup
cd backoffice-simple-setup
bundle
bundle exec rake boss
@cdimartino
cdimartino / route_id_collisions.md
Created February 22, 2016 17:14
Sinatra routes - avoiding capture variable naming collisions

You MUST name your capture variables uniquely in your routes. The convention is that the object farthest to the right is the primary object being acted upon. Thus the implicit :id belongs to it, and all other :id type fields must be explicitly named:

Anti-Pattern

post '/posts/:id/comments/:id/upvote' do
end

Anti-Anti-Pattern

@cdimartino
cdimartino / habtm.md
Created February 12, 2016 22:47
Has and Belongs to Many

has_and_belongs_to_many and :through do not go together. Your Entry has_and_belongs_to_many :tags:

class Entry < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :entries
end
@cdimartino
cdimartino / database_constraints.md
Created February 12, 2016 22:44
Defense against the dark arts: Database Constraints

The database is our last level of defense against the dark evil data that Users try to feed us. Make sure you don't spoil your pristine data structures by making sure that constraints are present:

create_table :posts do |t|
  t.string :title, null: false
  t.string :content, null: false

  t.timestamps null: false
end
@cdimartino
cdimartino / form_fields_to_params.md
Last active February 12, 2016 22:38
Proper form input field naming conventions

If you make your form fields like:

<form method='PUT' action='/entries'>
  <input type='text' name='entry[title]' />
  <input type='text' name='entry[content'] />
</form>

Then your Ruby code becomes more straightforward:

@cdimartino
cdimartino / guard_clauses.md
Last active February 12, 2016 22:33
Guard clauses!

What happens if the update or create fails? We need to ensure that our user is notified in case of failure:

Anti-pattern

post '/posts' do
  @post = Post.new(params[:post)
  @post.save
  redirect "/posts"
end
@cdimartino
cdimartino / new_gist_file.md
Last active February 12, 2016 22:33
AR Associations - use the setters

The better way to make this association is to use the ActiveRecord setters provided by your belongs_to association.

Anti-pattern

post '/posts' do
  @post = Post.new(params[:post)
  @post.author_id = author.id
  if @post.save
    redirect "/posts"
 else
@cdimartino
cdimartino / 0_reuse_code.js
Created February 12, 2016 22:11
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console