Skip to content

Instantly share code, notes, and snippets.

murdoch / description.markdown
Created December 31, 2016 17:31 — forked from runemadsen/description.markdown
Reverse polymorphic associations in Rails

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

murdoch / gist:7217644
Created October 29, 2013 16:09
JQuery scroll to anchor, nabbed from Chris Coyier at CSS Tricks
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
scrollTop: target.offset().top
}, 1000);
return false;
murdoch / gist:7207279
Created October 29, 2013 00:33
Devise embed signup form on home page or anywhere else
# Stick form in home.html.erb
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
murdoch / gist:7207237
Created October 29, 2013 00:28
Rspec "expect" snippets found on Stackoverflow
expect { thing.destroy }.to change(Thing, :count).from(1).to(0)
expect { = 5 }.to change { thing.total_price }.by(5)
expect {! }.to raise_error
expect { thing.symbolize_name }.to change { }.from(String).to(Symbol)
murdoch / gist:7205439
Created October 28, 2013 21:49
Devise allow unconfirmed access to site, but force confirmation for certain actions
Devise by default has a configuration named:
You can set it in your devise initializer and it will allow the user to access any page, any time, while still unconfirmed.
Then, for the pages you require confirmation, you can simply add a before filter:
before_filter :only_confirmed
murdoch / gist:7204126
Created October 28, 2013 20:34
Devise: Do something if it's the first login, there are much better ways to do this, but here's a quick one
def after_sign_in_path_for(resource_or_scope)
if first_login
def first_login
current_user.sign_in_count == 0
murdoch / gist:7204104
Created October 28, 2013 20:33
Override Devise inactive signup path
# if we have confirmable module turned on
def after_inactive_sign_up_path_for(resource)
murdoch / gist:7203932
Created October 28, 2013 20:23
Change signin path depending on url options
def stored_location_for(resource)
def after_sign_in_path_for(resource)
if condition_foo
redirect_to foo_url
elsif condition bar
class SelfMaker
def yet_what_is_self?
self.class.class_eval do
mirror(self, "Inside class_eval of SelfMaker#yet_what_is_self?")
instance_eval do
mirror(self, "Inside instance_eval of SelfMaker#yet_what_is_self?")
murdoch / README.markdown
Created August 10, 2012 12:54 — forked from greypants/README.markdown
RAILS 3: nav_link helper for adding 'selected' class to navigation elements

#Behold, the nav_link:

The nav_link helper works just like the standard Rails link_to helper, but adds a 'selected' class to your link (or its wrapper) if certain criteria are met. By default, if the link's destination url is the same url as the url of the current page, a default class of 'selected' is added to the link.

For full usage details, see:

Drop nav_link_helper.rb into app/helpers in your Rails 3.x app and enjoy.

#About this fork