Skip to content

Instantly share code, notes, and snippets.

💭
Coding

Chris DiMartino cdimartino

💭
Coding
Block or report user

Report or block cdimartino

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:40d887108ad8df8cdfcecabcd62dcc21
CREATE TABLE `demographic_groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`gender` int(11) NOT NULL DEFAULT '0',
`country` int(11) NOT NULL DEFAULT '0',
`expertise` int(11) NOT NULL DEFAULT '0',
`operating_system` int(11) NOT NULL DEFAULT '0',
`gaming_genre` int(11) NOT NULL DEFAULT '0',
`social_network` int(11) NOT NULL DEFAULT '0',
`web_browser` int(11) NOT NULL DEFAULT '0',
`min_income` int(11) DEFAULT NULL,
View gist:d1e4a7483f7fb8048df9d2c8eecd1b75
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 Mar 2, 2017
Yardoc git pre-commit hook
View pre-commit
#!/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
View -
#!/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 Feb 22, 2016
Sinatra routes - avoiding capture variable naming collisions
View route_id_collisions.md

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 Feb 12, 2016
Has and Belongs to Many
View habtm.md

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 Feb 12, 2016
Defense against the dark arts: Database Constraints
View database_constraints.md

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 Feb 12, 2016
Proper form input field naming conventions
View form_fields_to_params.md

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:

View guard_clauses.md

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 Feb 12, 2016
AR Associations - use the setters
View new_gist_file.md

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
You can’t perform that action at this time.