Archive for October, 2007
DRYing associations and behavior
October 31st, 2007 •
tags: playground, rails, rails models
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
October 28th, 2007 •
tags: 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
October 28th, 2007 •
tags: best-practices, rails
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
October 23rd, 2007 •
tags: rails
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
October 19th, 2007 •
tags: rails, rails controller, rails models
# 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
October 19th, 2007 •
tags: ruby
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
October 17th, 2007 •
tags: rails, rails views
Layouts in rails can be accomplished in quite a few ways. Here are the most common:
layouts/application.html.erb#default for all controllerslayouts/my_controller.html.erb#layout for a controller with the same namelayout "my_layout"in controller # explicitly set layout in controllerlayout :decide_on_layout# with ‘decide_on_layout being a method returning a layout name as stringrender :layout => false# no layout at all-
render :partial => ‘my_partial’, :layout => ‘my_other_partial’# partials as layouts
clean page titles (from Ryan Bates’ screencasts)
October 16th, 2007 •
tags: rails, rails views
# 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)
October 16th, 2007 •
tags: rails, rails helper
# 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)
October 16th, 2007 •
tags: best-practices, rails, rails models
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.