This article describes how to use Backbone.Event to create a simple event aggregator. The author actually describes several alternate ways to implement this pattern, and then shows how to do it with the aggregator.
A few best practices regarding single table inheritance in Ruby on Rails.
scoping by a belongs_to association with decent_exposure
I’ve been using (and enjoying) decent_exposure for some of my new projects. You can read about how great it is on their site. I had an interesting requirement and ended up writing a custom strategy that I use with decent_exposure.
When a user registers for my site, they belong to an account. The account is the scope of most of the rest of the objects in the application (products, other users, etc) I needed my decent_exposures to respect this in all parts of the controller. Given a current_account method on the ApplicationController that returns the account of the currently logged-in User, I wrote this strategy:
class AccountStrategy < DecentExposure::ActiveRecordWithEagerAttributesStrategy
delegate :current_account, to: :controller
def collection_resource
super.where(:account_id => current_account.id)
end
def singular_resource
if id
scope.send(finder, id)
else
scope.new.tap { |m| m.account = current_account }
end
end
end
In the collection_resource override, we simply add a where clause to any query that will be executed.
In the singular_resource override, we can set the account to the current_account when creating a new model. The model will, of course, need to have a belongs_to :account association.
In the controller, we can use this like so:
class ProductsController < ApplicationController respond_to :html, :json, :xml, :js expose(:product, strategy: AccountStrategy) expose(:products, strategy: AccountStrategy) ...
A very good read on security in ruby on rails. Very thorough with a lot of great examples of what rails does for you.
I enjoyed this article about some of the cool things you can do with define_method, method_missing, and instance_eval in Ruby. The topics were presented in a very straightforward manner and easily digested.
disable layouts for ajax requests in rails
Use the following code snippet to disable rendering of layouts for ajax requests in Ruby on Rails. Put this in application_controller.rb in order for it work in all of your controllers.
application_controller.rb
layout proc {|c| c.request.xhr? ? false : 'application' }
The thing I don’t like about this is that it hard-codes the application layout, instead of using the default behavior of looking for a layout with the same name of the controller first. Does anyone know how to avoid this?
tumblrbot asked: WHAT MAKES YOU FEEL BETTER WHEN YOU ARE IN A BAD MOOD?
playing with my kids makes me feel better when i’m in a bad mood.
adding an autoload path to a railtie
when you want to add an autoload_path as part of your railtie, add the following initializer to the railtie
dir = File.expand_path('../../', __FILE__)
initializer "autoload_paths", :before => :set_autoload_paths do |app|
app.config.autoload_paths += [dir]
end
it is important to do this before :set_autoload_paths because it will freeze the array. dir is being set to the parent directory of the railtie in this example
(Source: stackoverflow.com)
asset precompile with capistrano and a relative application url
when deploying to an application server where the application isn’t hosted at the root domain name, but rather in a relative url, use the following to make sure all your assets are compiled correctly
Capfile
load 'deploy/assets'
deploy.rb
set :asset_env, "RAILS_GROUPS=assets RAILS_RELATIVE_URL_ROOT='/relative_url'"