Skip to content

Instantly share code, notes, and snippets.

@murdoch
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
getting_started_path
else
super
end
end
def first_login
current_user.sign_in_count == 0
@murdoch
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:
config.allow_unconfirmed_access_for
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
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 { thing.tax = 5 }.to change { thing.total_price }.by(5)
expect { thing.save! }.to raise_error
expect { thing.symbolize_name }.to change { thing.name }.from(String).to(Symbol)
@murdoch
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
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) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
@murdoch
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 @article.media

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.