Archive for October, 2007

DRYing associations and behavior

module CommonBehaviour
  def self.included(base)
    base.class_eval <<-EOC
      belongs_to :foo
      has_many :bars
      delegate :baz, :to => :survey_execution
    EOC
    base.send :include, InstanceMethods
  end

  module InstanceMethods
    def bla
      p 'bla'
    end
  end
end

class OneOfCommon < ActiveRecord::Base
  include CommonBehaviour
end

sex spam

“Your penis will be the Moon when the other men’s penises will be just the stars.” — bartholomew caryn

“new” actions in nested rails resources

def new
@my_model = MyModel.new
end

# vs.

def new
@my_model = @my_parent_model.my_models.build
# gives you access to parent properties
# (useful e.g. in views)
end

howto initialize an empty edgerails app from scratch

mkdir -p some_name/vendor; cd some_name
svn co http://dev.rubyonrails.org/svn/rails/trunk vendor/rails
ruby vendor/rails/railties/bin/rails .

nested includes in find

# in your model

class << self

  INCLUDE_OPTIONS = [
    { :section_application => [
      { :chapter_application => [
        { :questionnaire_application => [
          { :survey_application => :survey },
          :questionnaire,
        ] },
        :chapter,
      ] },
      :section
    ] },
    :question,
    :input_type,
    :time_line,
    :unit
  ]

  def find_with_includes(*args)
    find_without_includes args.pop, :include => INCLUDE_OPTIONS
  end

  alias_method_chain :find, :includes

end

# in your controller

def index
  @models = Model.find_with_includes(:all)
end

ruby hash trick

Apparantly when you throw an even number of comma-separated items into a hash literal, ruby groups them into :key => :value pairs for you – what an amazing language it is! :-)

Taken from snippets.dzone

layout possibilities in rails

Layouts in rails can be accomplished in quite a few ways. Here are the most common:

  • layouts/application.html.erb #default for all controllers
  • layouts/my_controller.html.erb #layout for a controller with the same name
  • layout "my_layout" in controller # explicitly set layout in controller
  • layout :decide_on_layout # with ‘decide_on_layout being a method returning a layout name as string
  • render :layout => false # no layout at all
  • render :partial => ‘my_partial’, :layout => ‘my_other_partial’ # partials as layouts

clean page titles (from Ryan Bates’ screencasts)

# application_helper.rb

def title(page_title)
content_for :title { page_title }
end

# e.g. show.html.erb

<% title "overidden title" %>
...

# layouts/some_layout.html.erb

<%= yield(:title) %>

<%= yield %>

time formatting (from Ryan Bates’ screencasts)

# in views

<%= post.created_at.to_s(:my_cool_format) %>

# in environment.rb (or any rb file under 'config/initializers' if using rails 2.0)

Time::DATE_FORMATS[:my_cool_format] = "due at %B %d on %I:%M %p"

CUDdly design (as coined by Pivotal Labs)

Pivotal Labs coin the term CUDdly Models for refactoring public model actions with side effects, into proper ActiveRecord callbacks, thus leveraging the power of encapsulation, transactions, consistent interface, and (activerecord) lifecycle power.

See this for a more thorough explanation.

« Older Entries