Skip to content

Instantly share code, notes, and snippets.

View ericcf's full-sized avatar

Eric Carty-Fickes ericcf

  • Northwestern University
  • Chicago, IL
View GitHub Profile
$ rails _4.2.6_ new sti_object_label
...
$ cd sti_object_label
$ echo "gem 'rails_admin', '~> 0.8.1'" >> Gemfile
$ bundle
...
$ rails _4.2.6_ g rails_admin:install
...
<enter>
...
$ rails -v
Rails 4.2.6
$ rails new nested_update
...
$ cd nested_update
$ echo "gem 'rails_admin', '~> 0.8.1'" >> Gemfile
$ bundle
...
$ rails g rails_admin:install
...
@ericcf
ericcf / activity.rb
Created March 10, 2015 13:31
shareable item action labels
# in host app
class Activity
def action
status_label.downcase
end
end
@ericcf
ericcf / index.js.erb
Created December 19, 2011 14:52 — forked from ryanb/index.js.erb
Infinite scrolling solution covered in revised episode #114: http://railscasts.com/episodes/114-endless-page-revised
$('#products').append('<%= j render(@products) %>');
<% if @products.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@products) %>');
<% else %>
$('.pagination').remove();
<% end %>
@ericcf
ericcf / client_models.rb
Created June 22, 2011 16:05
example associations between physical and proxy models
class Ingredient < ActiveResource::Base
def recipes
# returns all recipes with this ingredient as a component
Recipe.joins(components).merge(components)
end
def components
Component.where :ingredient_id => id
end
@ericcf
ericcf / ingredient.rb
Created June 22, 2011 15:20
example serialization customization
class Ingredient < ActiveRecord::Base
def as_json(options = {})
super(options.merge(
:only => [:id, :name],
:methods => [:time_to_harvest],
:include => {
:substitutes => { :only => [:name] }
}
))
@ericcf
ericcf / serialization.rb
Created June 22, 2011 15:15
don't include root in json
# instead of { "my_model": { "id": 1, "title": "foo" } }
# to_json produces { "id": 1, "title": "foo" }
ActiveRecord::Base.include_root_in_json = false
@ericcf
ericcf / ingredients.rb
Created June 22, 2011 15:14
example fixture
[
{
"id" => 1,
"name" => "sassafras"
}
]
@ericcf
ericcf / init_fakeweb.rb
Created June 22, 2011 15:10
FakeWeb setup for testing
require 'fakeweb'
# this can also be set to a regular expression to whitelist URI's
FakeWeb.allow_net_connect = false
def load_fixture(name)
path = "#{Rails.root}/features/fixtures/#{name}.rb"
eval File.open(path).map { |line| line }.join("")
end
class IngredientsSweeper < ActionController::Caching::Sweeper
observe Ingredient
def after_save(record)
expire_action :controller => "ingredients",
:action => "index",
:format => :json
expire_action :controller => "ingredients",
:action => "show",