Skip to content

Instantly share code, notes, and snippets.

@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.

@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 / 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: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: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: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: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)
"http://google.com"
end
@murdoch
murdoch / gist:7203932
Created October 28, 2013 20:23
Change signin path depending on url options
protected
def stored_location_for(resource)
nil
end
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?")
end
instance_eval do
mirror(self, "Inside instance_eval of SelfMaker#yet_what_is_self?")
end
@murdoch
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: http://viget.com/extend/rails-selected-nav-link-helper

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

#About this fork