<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 03:59:46 GMT</pubDate>
    <description>DZone Snippets: rails code</description>
    <item>
      <title>Simple cacher module for Rails</title>
      <link>http://snippets.dzone.com/posts/show/5297</link>
      <description>Illustration of a simple cacher module for Rails.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# lib/cacher.rb&lt;br /&gt;module Cacher&lt;br /&gt;  STORE = {}&lt;br /&gt;  &lt;br /&gt;  def cache(*args)&lt;br /&gt;    STORE[args.inspect] ||= yield&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def flush!&lt;br /&gt;    STORE.clear&lt;br /&gt;  end&lt;br /&gt;  module_function :flush!&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# app/models/person.rb&lt;br /&gt;class Person &lt; AR::Base&lt;br /&gt;  include Cacher&lt;br /&gt;&lt;br /&gt;  def finger&lt;br /&gt;    cache(:finger, email) do&lt;br /&gt;      `finger #{email}`&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# app/controller/application.rb&lt;br /&gt;class ApplicationController &lt; AC::Base&lt;br /&gt;  after_filter { Cacher.flush! }&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 01 Apr 2008 09:12:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5297</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>Persistent Rails cookie session</title>
      <link>http://snippets.dzone.com/posts/show/5173</link>
      <description>Session cookies, the Rails-2 kind, are transient because that's safer.  In some applications safety isn't important.  The following makes the session cookies persist for a year.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ApplicationController &lt; ActionController::Base&lt;br /&gt;  before_filter :update_session_expiration_date&lt;br /&gt;&lt;br /&gt;private&lt;br /&gt;  def update_session_expiration_date&lt;br /&gt;    unless ActionController::Base.session_options[:session_expires]&lt;br /&gt;      ActionController::Base.session_options[:session_expires] = 1.year.from_now&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 25 Feb 2008 12:23:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5173</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>load all fixtures</title>
      <link>http://snippets.dzone.com/posts/show/5007</link>
      <description>In some test cases I need all my fixtures to be loaded.  To make this easier, add the following to test/test_helper.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Test::Unit::TestCase&lt;br /&gt;  def self.all_fixtures&lt;br /&gt;    Dir[File.dirname(__FILE__) + "/fixtures/*.yml"].each do |f|&lt;br /&gt;      fixtures File.basename(f, '.yml')&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  ..&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and in your tests use it as follows:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class FooTest &lt; Test::Unit::TestCase&lt;br /&gt;  all_fixtures&lt;br /&gt;&lt;br /&gt;  ..&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Happy testing!</description>
      <pubDate>Fri, 18 Jan 2008 19:01:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5007</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>Disable protected attributes in ActiveRecordHelper forms</title>
      <link>http://snippets.dzone.com/posts/show/2489</link>
      <description>To make the form method in ActiveRecordHelper disable protected attribute, place the following in your application_help.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def attr_protected?(record, column)&lt;br /&gt;  o = instance_variable_get("@#{record}")&lt;br /&gt;  o &amp;&amp; o.class.protected_attributes &amp;&amp;&lt;br /&gt;      o.class.protected_attributes.include?(column.name.to_sym)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def default_input_block&lt;br /&gt;  Proc.new { |record, column|&lt;br /&gt;    options = attr_protected?(record, column) ? {:disabled =&gt; true} : {}&lt;br /&gt;    &lt;&lt;-"end_html" &lt;br /&gt;      &lt;p&gt;&lt;br /&gt;        &lt;label for="#{record}_#{column.name}"&gt;#{column.human_name}&lt;/label&gt;&lt;br /&gt;        &lt;br /&gt;&lt;br /&gt;        #{input(record, column.name, options)}&lt;br /&gt;      &lt;/p&gt;&lt;br /&gt;    end_html&lt;br /&gt;  }&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And mark some attributes protected, for example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Item &lt; ActiveRecord::Base&lt;br /&gt;  attr_protected :created_at&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The created_at attribute will be rendered but won't be editable.  This will work for "dynamic scaffolds" too!</description>
      <pubDate>Mon, 28 Aug 2006 20:48:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2489</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>give focus to first input field or textarea on page</title>
      <link>http://snippets.dzone.com/posts/show/2192</link>
      <description>Place the following in your application.js file and make sure your layout includes "javascript_include_tag :defaults".&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Event.observe(window, 'load', function() {&lt;br /&gt;  var e = $A(document.getElementsByTagName('*')).find(function(e) {&lt;br /&gt;    return (e.tagName.toUpperCase() == 'INPUT' &amp;&amp; (e.type == 'text' || e.type == 'password'))&lt;br /&gt;        || e.tagName.toUpperCase() == 'TEXTAREA' || e.tagName.toUpperCase() == 'SELECT';&lt;br /&gt;  });&lt;br /&gt;  if (e) e.focus();&lt;br /&gt;});&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 15 Jun 2006 12:05:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2192</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>non persistent ActiveRecord</title>
      <link>http://snippets.dzone.com/posts/show/1963</link>
      <description>&lt;code&gt;&lt;br /&gt;# = ActiveForm - non persistent ActiveRecord&lt;br /&gt;#&lt;br /&gt;# Simple base class to make AR objects without a corresponding database&lt;br /&gt;# table.  These objects can still use AR validations but can't be saved&lt;br /&gt;# to the database.  Use the +valid?+ method to validate.&lt;br /&gt;#&lt;br /&gt;# == Example&lt;br /&gt;#&lt;br /&gt;#   class FeedbackForm &lt; ActiveForm&lt;br /&gt;#     column :email&lt;br /&gt;#     column :message&lt;br /&gt;#     validates_presence_of :email, :message&lt;br /&gt;#   end&lt;br /&gt;#&lt;br /&gt;class ActiveForm &lt; ActiveRecord::Base&lt;br /&gt;  def self.columns() @columns ||= []; end # :nodoc:&lt;br /&gt;  &lt;br /&gt;  # Define an attribute, takes the same arguments as&lt;br /&gt;  # ActiveRecord::ConnectionAdapters::Column.new only in a&lt;br /&gt;  # slightly different order.&lt;br /&gt;  def self.column(name, sql_type = nil, default = nil, null = true)&lt;br /&gt;    columns &lt;&lt; ActiveRecord::ConnectionAdapters::Column.new(&lt;br /&gt;        name.to_s, default, sql_type.to_s, null)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 27 Apr 2006 11:52:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1963</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>url and xml encode to fool naive web spiders</title>
      <link>http://snippets.dzone.com/posts/show/1582</link>
      <description>Encode &lt;b&gt;all&lt;/b&gt; characters to XML entities:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def xml_encode(text)&lt;br /&gt;    text.unpack('c*').map{|c|"&amp;\##{c};"}.join&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Encode &lt;b&gt;all&lt;/b&gt; characters to %00%00.. url ecoding:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def url_encode(text)&lt;br /&gt;    text.split('').map{|c|"%#{c.unpack('H2')}"}.join&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The following:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  &lt;a href="&lt;%= xml_encode("mailto:" + url_encode("somebody@somewhere.net")) %&gt;"&gt;mail somebody&lt;/a&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;yields:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  &lt;a href="&amp;#109;&amp;#97;&amp;#105;&amp;#108;&amp;#116;&amp;#111;&amp;#58;&amp;#37;&amp;#55;&amp;#51;&amp;#37;&amp;#54;&amp;#102;&amp;#37;&amp;#54;&amp;#100;&amp;#37;&amp;#54;&amp;#53;&amp;#37;&amp;#54;&amp;#50;&amp;#37;&amp;#54;&amp;#102;&amp;#37;&amp;#54;&amp;#52;&amp;#37;&amp;#55;&amp;#57;&amp;#37;&amp;#52;&amp;#48;&amp;#37;&amp;#55;&amp;#51;&amp;#37;&amp;#54;&amp;#102;&amp;#37;&amp;#54;&amp;#100;&amp;#37;&amp;#54;&amp;#53;&amp;#37;&amp;#55;&amp;#55;&amp;#37;&amp;#54;&amp;#56;&amp;#37;&amp;#54;&amp;#53;&amp;#37;&amp;#55;&amp;#50;&amp;#37;&amp;#54;&amp;#53;&amp;#37;&amp;#50;&amp;#101;&amp;#37;&amp;#54;&amp;#101;&amp;#37;&amp;#54;&amp;#53;&amp;#37;&amp;#55;&amp;#52;"&gt;mail somebody&lt;/a&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;which works fine in a browser.</description>
      <pubDate>Sun, 26 Feb 2006 16:31:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1582</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
  </channel>
</rss>
