<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in 'Write Your Own!' | Pragmatic Forums</title>
    <link>http://forums.pragprog.com/forums/8/topics/2</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <description></description>
    <item>
      <title>Write Your Own! posted by Fora User @ Sun, 05 Aug 2007 16:34:38 -0000</title>
      <description>&lt;p&gt;Great post, Maxim.&lt;/p&gt;


	&lt;p&gt;To take it even further, you can use any partial to act as a layout:&lt;/p&gt;


&lt;strong&gt;application_helper.rb&lt;/strong&gt;
&lt;pre&gt;&lt;code&gt;
def inside(partial, AMPERSANDblock)
  instance_variable_set(:@content_for_layout, capture(AMPERSANDblock))
  concat(render(:partial =&amp;gt; partial), block.binding)
end
&lt;/code&gt;&lt;/pre&gt;&lt;br/&gt;

&lt;strong&gt;_wrapper.rhtml&lt;/strong&gt; &lt;small&gt;(the outer layout partial)&lt;/small&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;div id="wrapper"&amp;gt;
  &amp;lt;%= yield %&amp;gt;
&amp;lt;div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br/&gt;

&lt;strong&gt;content.rhtml&lt;/strong&gt; &lt;small&gt;(the actual template)&lt;/small&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;% inside "wrapper" do -%&amp;gt;
  Inside the wrapper
&amp;lt;% end -%&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br/&gt;

&lt;strong&gt;Result:&lt;/strong&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;div id="wrapper"&amp;gt;
  Inside the wrapper
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br/&gt;

	&lt;p&gt;This is completely compatible with the standard Rails layouts. They&amp;#8217;ll work like they did before.&lt;/p&gt;


	&lt;p&gt;However, I don&amp;#8217;t know if this plays well with page caching. Testing this is left as an exercise for the reader :)&lt;/p&gt;


	&lt;p&gt;&amp;#8212;Andy&lt;/p&gt;</description>
      <pubDate>Sun, 05 Aug 2007 16:34:38 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1125</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Tue, 13 Mar 2007 15:40:38 -0000</title>
      <description>&lt;p&gt;Thanks for the post, helped a lot.&lt;/p&gt;


	&lt;p&gt;A little tough to follow though, here is the result that worked for me:&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://pastie.caboo.se/46627"&gt;http://pastie.caboo.se/46627&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 13 Mar 2007 15:40:38 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1309</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Fri, 02 Mar 2007 06:55:43 -0000</title>
      <description>&lt;p&gt;Hi all I am new(beginner) to rails&amp;#8230;.  where should i start from&amp;#8230; please guide me&amp;#8230;.
suggest me some books&amp;#8230;. thank u&lt;/p&gt;</description>
      <pubDate>Fri, 02 Mar 2007 06:55:43 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1176</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Thu, 08 Feb 2007 21:14:21 -0000</title>
      <description>&lt;p&gt;Very useful post!  A good addition to help newbies like me would be to say that the skeleton for the plugin can be generated through&lt;/p&gt;


	&lt;p&gt;./script/generate plugin validates_write_once_of&lt;/p&gt;


	&lt;p&gt;Then you can paste into these skeleton files the parts of this recipe&lt;/p&gt;


	&lt;p&gt;I added a quick note on this on my site &lt;a href="http://jcandkimmita.info/jc/"&gt;http://jcandkimmita.info/jc/&lt;/a&gt;?p=63&lt;/p&gt;</description>
      <pubDate>Thu, 08 Feb 2007 21:14:21 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1068</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Wed, 31 Jan 2007 16:40:19 -0000</title>
      <description>&lt;p&gt;Hello i&amp;#8217;m new on rails, i will post here anyways think will help some one. the comments above i did this 
inf helper/application_helper.rb i added this
module ApplicationHelper
in_place_collection_editor_field(object,method,container)
  tag = ::ActionView::Helpers::InstanceTag.new(object, method, self)
  url = url_for( :action =&gt; &amp;#8220;set_#{object}_#{method}&amp;#8221;, :id =&gt; tag.object.id )
  function =  &amp;#8220;new Ajax.InPlaceCollectionEditor(&amp;#8221; 
  function &lt;&lt; &amp;#8221;&amp;#8217;#{method}&amp;#8217;,&amp;#8221; 
  function &lt;&lt; &amp;#8221;&amp;#8217;#{url}&amp;#8217;,&amp;#8221; 
  collection = container.inject([]) do |options, element|
   options &lt;&lt; &amp;#8220;[ &amp;#8217;#{html_escape(element.last.to_s)}&amp;#8217;, &amp;#8217;#{html_escape(element.first.to_s)}&amp;#8217;]&amp;#8221; 
  end
  function &lt;&lt; &amp;#8220;{collection: [#{collection.join(&amp;#8217;,&amp;#8217;)}]});&amp;#8221; 
  javascript_tag(function)
 end
on the view i added  like this and that works for me
&lt;span id="sex"&gt;&lt;&lt;span&gt;=@user.sex &lt;/span&gt;&gt;&lt;/span&gt;&lt;%= in_place_collection_editor_field :user,&amp;#8217;sex&amp;#8217;,
                           Sex.find_all.collect{|x| [x.id,x.english]}  -%&gt;&lt;br/&gt;&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;def&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;where Sex is a object storage on the db so
Also there is a catch that i figured out later from the main solution text you will find this line function &lt;&lt; &amp;#8220;{collection: [#{collection.join(&amp;#8217;,&amp;#8217;)}],}); i have to change it to function &lt;&lt; &amp;#8220;{collection: [#{collection.join(&amp;#8217;,&amp;#8217;)}]}); so it works with IE now&lt;/p&gt;</description>
      <pubDate>Wed, 31 Jan 2007 16:40:19 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1516</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Mon, 04 Dec 2006 21:19:19 -0000</title>
      <description>&lt;p&gt;Is there a reason why you would use both Bluecloth and RedCloth? Would just one or the other suffice?&lt;/p&gt;</description>
      <pubDate>Mon, 04 Dec 2006 21:19:19 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1263</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Tue, 07 Nov 2006 22:41:14 -0000</title>
      <description>&lt;p&gt;I&amp;#8217;m totally lost as to what the complete code is supposed to look like or in which files each of your code snippets live?  Can you possibly provide a little more detail?   It sounds exactly like what I need. ie.values along with the option tags.&lt;/p&gt;</description>
      <pubDate>Tue, 07 Nov 2006 22:41:14 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1619</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sun, 08 Oct 2006 16:49:01 -0000</title>
      <description>&lt;p&gt;I&amp;#8217;ve written an update to this recipe that allows you to safely filter attributes/tags and define &amp;#8220;profiles&amp;#8221; that let you create different levels of filtering for different users.&lt;/p&gt;


	&lt;p&gt;Details here:&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.kookdujour.com/blog/details/11"&gt;http://www.kookdujour.com/blog/details/11&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sun, 08 Oct 2006 16:49:01 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1467</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sun, 10 Sep 2006 14:15:56 -0000</title>
      <description>&lt;p&gt;One big problem with this recipe:&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;It doesn&amp;#8217;t do anything about tag attributes.&lt;/strong&gt; A false sense of security is worse than none at all.&lt;/p&gt;


	&lt;p&gt;This means I can submit javascript handlers on allowed tags, a pretty big no-no. Other pre-santizing factors come into play as well.&lt;/p&gt;


	&lt;p&gt;The way around this is simple, but annoyingly so because you can&amp;#8217;t write to the attributes attribute of an &lt;span class="caps"&gt;HTML&lt;/span&gt;::Tag node.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
# stuff . . .
 if ALLOWED_TAGS.include?(node.name)
                             new_node=HTML::Tag.new(node.parent, node.line, node.position, node.name, [], node.closing)
                             new_node.to_s
                           else
# stuff . . .
&lt;/code&gt;
&lt;/pre&gt;</description>
      <pubDate>Sun, 10 Sep 2006 14:15:56 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1333</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sun, 27 Aug 2006 06:48:20 -0000</title>
      <description>&lt;p&gt;hello,&lt;/p&gt;


	&lt;p&gt;i am not new to rails but i am new to ajax.  when i try to run the in_place_editor_field recipe i get a weird error.&lt;/p&gt;


	&lt;p&gt;this is what it says.&lt;/p&gt;


	&lt;p&gt;Error communicating with the server: &lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;Action Controller: Exception caught&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;body { background-color: #fff; color: #333; }&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;body, p, ol, ul, td {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size:   13px;
  line-height: 18px;
}&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;pre {
  background-color: #eee;
  padding: 10px;
  font-size: 11px;
}&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;Unknown action
No action responded to set_client_contact_first_name&lt;/p&gt;


	&lt;p&gt;i generated my app with scaffolding, and include the default javascript files.  this error comes up after i make a change and press OK.  then the area where i was editing the field says saving, then the error pops up in a javascript alert window.&lt;/p&gt;


	&lt;p&gt;any help would be greatly appreciated.&lt;/p&gt;


	&lt;p&gt;thanks.&lt;/p&gt;</description>
      <pubDate>Sun, 27 Aug 2006 06:48:20 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1706</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Tue, 20 Jun 2006 16:36:57 -0000</title>
      <description>&lt;p&gt;The last method above, load_from_yaml, doesn&amp;#8217;t work anymore as written with ruby 1.8.4 .  I&amp;#8217;ve patched the &lt;a href="http://www.apptrain.com/restore.rake"&gt;restore.rake&lt;/a&gt; file. The link above also reflects the changes.&lt;/p&gt;</description>
      <pubDate>Tue, 20 Jun 2006 16:36:57 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1839</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Fri, 09 Jun 2006 20:49:31 -0000</title>
      <description>&lt;h2&gt;Problem&lt;/h2&gt;


	&lt;p&gt;You want to share instance variables across methods, or controllers.&lt;/p&gt;


	&lt;h2&gt;Extra Ingredients&lt;/h2&gt;


	&lt;p&gt;None&lt;/p&gt;


	&lt;h2&gt;Credits&lt;/h2&gt;


	&lt;p&gt;Taken from a post by Ezra Zygmuntowicz on the rails mailing list.&lt;/p&gt;


	&lt;h2&gt;Solution&lt;/h2&gt;


	&lt;p&gt;Put this in the top of your controller where you want to have a
shared var. Assuming the var is @foo&lt;/p&gt;


	&lt;p&gt;MyController &lt; ApplicationController&lt;/p&gt;
	&lt;pre&gt;&lt;code&gt;before_filter :setup_foo
 attr_reader :foo&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;def setup_foo
    @foo = "Whatever you want @foo to contain" 
end&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;end&lt;/p&gt;


	&lt;p&gt;Now @foo will be available to every action method in your controller
and also in your views for that controller. If you want @foo to be
available in all your controllers and view, put that code in your
application.rb ApplicationController.&lt;/p&gt;


	&lt;h2&gt;Discussion&lt;/h2&gt;


	&lt;p&gt;You can also use a &lt;span class="caps"&gt;CONSTANT&lt;/span&gt; of course, defined in application.rb, for a variable whose contents will not change, and which will be available to the entire application.&lt;/p&gt;</description>
      <pubDate>Fri, 09 Jun 2006 20:49:31 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1796</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Wed, 07 Jun 2006 00:47:26 -0000</title>
      <description>&lt;p&gt;I understand where you&amp;#8217;re coming from, resty, but that&amp;#8217;s not what &amp;#8220;recipe&amp;#8221; books are about, in general. It&amp;#8217;s an established genre, and it&amp;#8217;s pretty much what it sounds like&amp;#8212;how to make a specific thing, be it chocolate chip cookies or in-place editing.&lt;/p&gt;


	&lt;p&gt;To carry the analogy, if you&amp;#8217;re holding your recipe book in one hand, and looking worryingly at what you&amp;#8217;ve been assured is an &amp;#8220;oven,&amp;#8221; the recipe book is best perused after you read a book of the &amp;#8220;How To Cook&amp;#8221; genre.&lt;/p&gt;


	&lt;p&gt;I would suggest Agile Web Development with Rails, by Dave Thomas and &lt;span class="caps"&gt;DHH&lt;/span&gt;, which is available from Pragmatic Bookshelf. Once you&amp;#8217;ve got your head wrapped around that, try Ruby For Rails, by David Black, from Manning Press.&lt;/p&gt;</description>
      <pubDate>Wed, 07 Jun 2006 00:47:26 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1745</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sun, 28 May 2006 03:52:03 -0000</title>
      <description>&lt;h2&gt;Problem&lt;/h2&gt;


	&lt;p&gt;You&amp;#8217;ve got your software running.  You&amp;#8217;re using Capistrano to deploy it.  But there are several people working on the project who can deploy to production.  You want all of the developers to be notified when your software gets deployed to production, including knowing which version of the software was deployed.&lt;/p&gt;


	&lt;h2&gt;Extra Ingredients&lt;/h2&gt;


	&lt;p&gt;Because this hooks into the deployment, this assumes you&amp;#8217;ll already be including &lt;a href="http://manuals.rubyonrails.com/read/book/17"&gt;Capistrano&lt;/a&gt;&lt;/p&gt;


	&lt;h2&gt;Solution&lt;/h2&gt;


Generate a mailer : 
&lt;pre&gt;
&lt;code&gt;
 ruby script/generate mailer Notifier deployed
      exists  app/models/
      create  app/views/notifier
      exists  test/unit/
      create  test/fixtures/notifier
      create  app/models/notifier.rb
      create  test/unit/notifier_test.rb
      create  app/views/notifier/deployed.rhtml
      create  test/fixtures/notifier/deployed 
&lt;/code&gt;
&lt;/pre&gt;

In the capistrano deploy recipe (config/deploy.rb), create an &amp;#8220;after_deploy&amp;#8221; task : 
&lt;pre&gt;&lt;code&gt;
desc "After the deploy is done, let people know about it" 
task :after_deploy do
  #  The mailer needs the environment configured for it - otherwise it doesn't 
  #  find the templates, and it doesn't know how to send email.
  require File.join(File.dirname(__FILE__), 'environment')
  Notifier.deliver_deployed("#{revision}")
end  

task :after_deploy_with_migrations do
  #  The same stuff that we want to do after a "regular" deploy
  after_deploy
end
&lt;/code&gt;&lt;/pre&gt;

Let the Notifier model handle the information sent to it : 
&lt;pre&gt;&lt;code&gt;
  def deployed(version)
    @subject    = "Software Deployed!" 
    body(:version =&amp;gt; version)
    @recipients = '&lt;a href="mailto:geeks@example.com"&gt;geeks@example.com&lt;/a&gt;'
    @from       = '&lt;a href="mailto:deployer@example.com"&gt;deployer@example.com&lt;/a&gt;'
    @sent_at    = Time.now
    @sent_on    = Time.now
  end  
&lt;/code&gt;&lt;/pre&gt;

Finally, modify the view (app/views/notifier/deployed.rhtml) to have some important information.
&lt;pre&gt;&lt;code&gt;
Hello Geeks,

We just deployed to production with version &amp;lt;%= @version %&amp;gt; of the baseline.

It is now &amp;lt;%= Time.now %&amp;gt;.  Have a nice day.

Thank you, 

"The Deployer" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, whenever someone deploys your software to production, you will receive a nice email letting you know about it.&lt;/p&gt;


	&lt;h2&gt;Discussion&lt;/h2&gt;


	&lt;p&gt;This example uses the version that was deployed to production, but there are several variables that the deployment has available to it.  Maybe including the user who did the deployment would be important to you, or the machines that were included in the deployment.  See information in the deploy.rb along with documentation on Capistrano.&lt;/p&gt;


	&lt;p&gt;Including the &amp;#8220;environment&amp;#8221; in the require means that you will have access to all of the information in your application (and all of the database connections).  That means you could also do different things than just sending an email &amp;#8211; you could update a column in one of your models, insert information into your database, or just list all the users in your system.&lt;/p&gt;</description>
      <pubDate>Sun, 28 May 2006 03:52:03 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1682</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Thu, 04 May 2006 15:48:38 -0000</title>
      <description>&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;A model requires that one or more of its attributes be write once only.
In other words, these protected attributes may only be set when the model is first save dto the database and are immutable thereafter.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Credits&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;In order to write my unit tests for this code I referred to Ezra Zygmuntowicz&amp;#8217;s &lt;a href="http://OpenSVN.csie.org/ezra/rails/plugins/ez_where"&gt;ez_where plugin&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;


The code that you are going to have to write needs to do the following :
	&lt;ul&gt;
	&lt;li&gt;Only run on an update. This validation doesn&amp;#8217;t apply to record creations.&lt;/li&gt;
		&lt;li&gt;Retrieve a copy of the model form the database.&lt;/li&gt;
		&lt;li&gt;Compare each protected attribute of the current model to that of the copy.&lt;/li&gt;
		&lt;li&gt;Add an error to the model&amp;#8217;s errors collection for each protected attribute that differs.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;You could situate this code within the model&amp;#8217;s &lt;em&gt;validate_on_update&lt;/em&gt; method. But if you created  your own validation as a plugin, &lt;em&gt;validates_write_once_of&lt;/em&gt;, you could reuse this validation on other models and you&amp;#8217;d gain the benefit of making your code more descriptive.
Rails makes this easy (of course) &amp;#8211; we&amp;#8217;ll just model the new vaidation on an existing one.&lt;/p&gt;


	&lt;p&gt;Validations are located in lib/active_record/validations.rb&lt;/p&gt;


Examining the method validates_presence_of for example, shows that it takes the following options
	&lt;ul&gt;
	&lt;li&gt;message &amp;#8211; a cusom error message (default is &amp;#8220;can&amp;#8217;t be blank&amp;#8221;)&lt;/li&gt;
		&lt;li&gt;on &amp;#8211; Specifies when this validation is active (default is :save, other options are :create, :update)&lt;/li&gt;
		&lt;li&gt;if &amp;#8211; Specifies a method, proc or string to call to determine if the validation should occur&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;The new validation will take all those options except for &amp;#8216;on&amp;#8217;. It only makes sense for this validation to be active on an update. The default message will be &amp;#8220;can&amp;#8217;t be changed&amp;#8221;.&lt;/p&gt;


Here&amp;#8217;s the code for &lt;em&gt;write_once_of.rb&lt;/em&gt;
&lt;pre&gt;
&lt;code&gt;
module WriteOnceOf
  def validates_write_once_of(*attr_names)
    configuration = { :message =&amp;gt; "can't be changed" }
    configuration.merge!(attr_names.pop) if attr_names.last.is_a?(Hash)
    send( validation_method(:update) ) do |record|
      unless configuration[:if] and not evaluate_condition(configuration[:if], record)
        previous = self.find record.id
        attr_names.each do |attr_name|
          record.errors.add( attr_name, configuration[:message] ) if record.respond_to?(attr_name) and previous.send(attr_name) != record.send(attr_name)
          # replace the 'and' above with a double ampersand 
        end
      end
    end
  end
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;To get the validation to work as a plugin package it as follows&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;validates_write_once_of/
  init.rb
  lib/
    write_once_of.rb&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;The code for init.rb is &lt;pre&gt;
&lt;code&gt;
require 'write_once_of'
ActiveRecord::Base.extend WriteOnceOf
&lt;/pre&gt;
&lt;/code&gt;
The validates_write_once_of folder should be placed in the /vendor/plugins folder of your rails application.&lt;/p&gt;


Use as follows&lt;pre&gt;
&lt;code&gt;
class SomeClass &amp;lt; ActiveRecord::Base
  validates_write_once_of :some_attribute, { :message =&amp;gt; "can't be changed ... ever" }
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;Discussion&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Rails ships with a very full and flexible set of validations.
But being able to create your own validations enables you to write code that is very declarative and specific to your application domain. Its certainly worth doing so given how easily it can be done.&lt;/p&gt;</description>
      <pubDate>Thu, 04 May 2006 15:48:38 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1156</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sat, 29 Apr 2006 09:13:10 -0000</title>
      <description>&lt;p&gt;&lt;strong&gt;Bold phrase&lt;/strong&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 29 Apr 2006 09:13:10 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1114</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sat, 22 Apr 2006 20:35:53 -0000</title>
      <description>&lt;p&gt;Hello,&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m afraid the very first recipe, InPlaceEditing, stumped me. How do I &amp;#8216;cook&amp;#8217; this recipe? Don&amp;#8217;t I have to create a database first? What&amp;#8217;s the name of the database to create? I know I have to run some scripts sometime in the process.&lt;/p&gt;


	&lt;p&gt;Could we, at least for the first few recipes, be as detailed as can be, with one recipe that starts from the very, very beginning?&lt;/p&gt;


	&lt;p&gt;Thanks!
frustrated&lt;/p&gt;</description>
      <pubDate>Sat, 22 Apr 2006 20:35:53 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1024</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sun, 16 Apr 2006 09:07:01 -0000</title>
      <description>&lt;p&gt;That&amp;#8217;s right!  Thanks :)&lt;/p&gt;</description>
      <pubDate>Sun, 16 Apr 2006 09:07:01 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1816</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sat, 15 Apr 2006 19:20:18 -0000</title>
      <description>&lt;p&gt;This is almost certainly not the correct place to post this, so apologies in advance. 
&lt;em&gt;Update&lt;/em&gt; Its &lt;em&gt;definately&lt;/em&gt; the wrong place :-(.&lt;/p&gt;


	&lt;p&gt;Paragraph four in &amp;#8220;The Console is Your Friend&amp;#8221; ends with the following line:&lt;/p&gt;


	&lt;p&gt;&amp;#8220;Who needs and &lt;span class="caps"&gt;IDE&lt;/span&gt;?!&amp;#8221;&lt;/p&gt;


	&lt;p&gt;My guess is that this was meant to be &amp;#8220;Who needs &lt;strong&gt;an&lt;/strong&gt; &lt;span class="caps"&gt;IDE&lt;/span&gt;?!&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Keith&lt;/p&gt;</description>
      <pubDate>Sat, 15 Apr 2006 19:20:18 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1803</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sat, 08 Apr 2006 18:49:29 -0000</title>
      <description>&lt;h1&gt;Problem&lt;/h1&gt;


	&lt;p&gt;You want to use &lt;span class="caps"&gt;AJAX&lt;/span&gt; on a field that isn&amp;#8217;t free text, but is a list of things that your user can choose.&lt;/p&gt;


	&lt;h1&gt;Extra Ingredients&lt;/h1&gt;


	&lt;p&gt;Rails 1.1&lt;/p&gt;


	&lt;h1&gt;Solution&lt;/h1&gt;


	&lt;p&gt;As of script.aculo.us version 1.5.3, a control called Ajax.InPlaceCollectionEditor deals with precisely this issue. This function works identically to Ajax.InPlaceEditor, except that it takes an array of arrays consisting of option names and values.&lt;/p&gt;


	&lt;p&gt;A Rails-oriented solution, however, should work similarly to the way select options are generated. Define a function in your &lt;strong&gt;application_helper.rb&lt;/strong&gt; file:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;def in_place_collection_editor_field(object,method,container)&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;The final parameter, &lt;strong&gt;container&lt;/strong&gt;, contains the options for the drop-down box. First, resolve a tag and create the submit &lt;span class="caps"&gt;URL&lt;/span&gt;:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;tag = ::ActionView::Helpers::InstanceTag.new(object, method, self)
url = url_for( :action =&amp;gt; "set_#{object}_#{method}", :id =&amp;gt; tag.object.id )&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;On the controller side, you will need to define a method &lt;strong&gt;set_object_method&lt;/strong&gt; to handle the input. Next, start the definition of the function:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;function =  "new Ajax.InPlaceCollectionEditor(" 
function &amp;lt;&amp;lt; "'#{method}'," 
function &amp;lt;&amp;lt; "'#{url}',"&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;Then, process the options:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;collection = container.inject([]) do |options, element|
  options &amp;lt;&amp;lt; "[ '#{html_escape(element.last.to_s)}', '#{html_escape(element.first.to_s)}']" 
end&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;Finally, add the collection to the final Javascript:&lt;/p&gt;
	&lt;pre&gt;&lt;code&gt;function &amp;lt;&amp;lt; "{collection: [#{collection.join(',')}]," 
  function &amp;lt;&amp;lt; "});" 
end&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;javascript_tag(function)&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;For example, in a &lt;strong&gt;list.rhtml&lt;/strong&gt; file, the following defines an editor for a field called &lt;strong&gt;custom_field&lt;/strong&gt; that has a number of options taken from the &lt;strong&gt;FieldType&lt;/strong&gt; model:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;&amp;lt;%= in_place_collection_editor_field 'custom_field', 'field_type', 
                           FieldType.find_all.collect{|x| [x.name,x.id]} %&amp;gt;&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;The &lt;strong&gt;x.name&lt;/strong&gt; is the plaintext description, to be displayed to the user, while the &lt;strong&gt;x.id&lt;/strong&gt; is the internal id. Given that the selected object id will be passed to the controller, along with the id of the object to process, the controller is simply defined as:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;def set_custom_field_field_type
  @i = CustomField.find(params[:id])
  f  = FieldType.find(params[:value])
  @i.update_attribute( :field_type, f )
  render :text =&amp;gt; f.name
end&lt;/code&gt;&lt;/pre&gt;


	&lt;h1&gt;Discussion&lt;/h1&gt;


	&lt;p&gt;While this example also by-passes model validation, this can easily be built into the controller action. Logical extensions to this approach might include the other approaches used in ActionView::Helpers::FormOptionsHelper.&lt;/p&gt;


	&lt;h1&gt;Further Reading&lt;/h1&gt;


	&lt;p&gt;See the &lt;a href="http://wiki.script.aculo.us/scriptaculous/show/Ajax.In+Place+Collection+Editor"&gt;script.aculo.us documentation&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 08 Apr 2006 18:49:29 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1720</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Mon, 03 Apr 2006 17:33:31 -0000</title>
      <description>&lt;p&gt;There doesn&amp;#8217;t seem to be any actual source code in the Authentication folder (or any other folder)?&lt;/p&gt;</description>
      <pubDate>Mon, 03 Apr 2006 17:33:31 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1687</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Tue, 28 Mar 2006 08:13:36 -0000</title>
      <description>&lt;p&gt;Can&amp;#8217;t wait for the book fellas&amp;#8230;another great Rails resource.  I made you famous by adding your topics &lt;span class="caps"&gt;RSS&lt;/span&gt; to &lt;a href="http://www.rails-a-rama.com/links/"&gt;Rails-a-Rama!&lt;/a&gt;.  Yes I am pimping it&amp;#8230;because its a good thing like Martha Stewart said!  I think&amp;#8230; ;-)&lt;/p&gt;</description>
      <pubDate>Tue, 28 Mar 2006 08:13:36 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1214</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Tue, 21 Mar 2006 16:58:51 -0000</title>
      <description>&lt;p&gt;Note that to be able to use fixtures, we also need to define the reset_pk_sequence! for our MysqlAdapter. This function is called when all the fixtures are loaded to set the value of the sequence table accordingly (otherwise it will have a perhaps inconsistent value).&lt;/p&gt;


	&lt;p&gt;In our case, we have to get the last &lt;code&gt;id&lt;/code&gt; value of our table, and set or update the &lt;code&gt;id&lt;/code&gt; value of our sequence table accordingly:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
      def reset_pk_sequence!( table, pk = nil, sequence = nil )                                                                                                                      
        sequence_name = table + "_sequence"                                                                                
        max_pk = select_value("SELECT MAX(id) FROM #{table}").to_i + 1                                                    
        sql = "INSERT INTO #{sequence_name} (`id`) VALUES (#{max_pk})"                                                         
        begin                                                                                                                  
          insert( sql )                                                                                                        
        rescue ActiveRecord::StatementInvalid                                                                                  
          sql = "UPDATE #{sequence_name} SET id=#{max_pk}"                                                                     
          nb = update( sql ,"Update Sequence")                                                                                 
        end                                                                                                                    
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Tue, 21 Mar 2006 16:58:51 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1101</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Sun, 19 Mar 2006 10:29:40 -0000</title>
      <description>&lt;h2&gt;Problem&lt;/h2&gt;


	&lt;p&gt;You have a rails application with lots of controllers / views. All your pages have common structure elements (like header, footer etc.) through usage of layouts.&lt;/p&gt;


	&lt;p&gt;Rails allows one level of layouts. Most of the time it is sufficient, but sometimes you feel uncofortable with having pages sharing common elements. For example, all pages of a particular controller should have a common &amp;#8220;submenu&amp;#8221;. You end up using the same particles on every controller view.&lt;/p&gt;


	&lt;h2&gt;Solution&lt;/h2&gt;


	&lt;p&gt;Most of modern frameworks have an ability to embed pages in a layout, and then embed the result in another layout and so on. With a little trick you can do the equal thing in Rails.&lt;/p&gt;


	&lt;p&gt;First, add the following method into your ApplicationHelper:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
def inside_layout(layout, AMPERSANDblock)
  @template.instance_variable_set("@content_for_layout", capture(AMPERSANDblock))

  layout = layout.include?("/") ? layout : "layouts/#{layout}" if layout
  buffer = eval("_erbout", block.binding)
  buffer.concat(@template.render_file(layout, true))
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Then, design your views to use e.g. &amp;#8216;inner_layout&amp;#8217;.&lt;/p&gt;


	&lt;p&gt;In &amp;#8216;layouts/inner_layout.rhtml&amp;#8217; write:&lt;/p&gt;


&lt;pre&gt;
&amp;lt;% inside_layout 'outer_layout' do %&amp;gt;

  &amp;lt;div id="common_elements"&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;%= @content_for_layout %&amp;gt;

&amp;lt;% end %&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;The &amp;#8216;outer_layout.rhtml&amp;#8217; will go as ussual layout (unless it needs to be nested in other higher level layout)&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;NOTE: Due to a limitation in textile (used in this forum) I had to remove the ampersands from the code above, so you&#8217;ll need to re-insert those before &amp;#8220;block&amp;#8221; variable. So the code should look like &amp;#8220;capture(AMPERSANDblock) where &lt;span class="caps"&gt;AMPERSAND&lt;/span&gt; is replaced by the ampersand character.&lt;/strong&gt;&lt;/p&gt;</description>
      <pubDate>Sun, 19 Mar 2006 10:29:40 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1052</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
    <item>
      <title>Write Your Own! posted by Fora User @ Fri, 17 Mar 2006 19:31:32 -0000</title>
      <description>&lt;h3&gt;Problem&lt;/h3&gt;


	&lt;p&gt;You want to allow your users to use certain &lt;span class="caps"&gt;HTML&lt;/span&gt; tags in their input&lt;br /&gt;while restricting all other &lt;span class="caps"&gt;HTML&lt;/span&gt; tags.&lt;/p&gt;


	&lt;h3&gt;Extra Ingredients&lt;/h3&gt;


	&lt;p&gt;None&lt;/p&gt;


	&lt;h3&gt;Credits&lt;/h3&gt;


	&lt;p&gt;The idea for this recipe comes from Koen Eijsvogels and his post in&lt;br /&gt;the Request a&lt;br /&gt;Recipe forum.&lt;/p&gt;


	&lt;h3&gt;Solution&lt;/h3&gt;


	&lt;p&gt;Modify config/environment.rb by adding the following line at the end:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
require_dependency 'rails_patch/text_helper'
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Add a new subdirectory &lt;em&gt;rails_patch&lt;/em&gt; to your application &lt;em&gt;lib&lt;/em&gt; directory.&lt;/p&gt;


	&lt;p&gt;Add &lt;em&gt;text_helper.rb&lt;/em&gt; to your &lt;em&gt;lib/rails_patch&lt;/em&gt; directory and ensure it&lt;br /&gt;looks as follows:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
module ActionView
  module Helpers
    module TextHelper
      ALLOWED_TAGS = %w(a img) unless defined?(ALLOWED_TAGS)

      def whitelist(html)
        # only do this if absolutely necessary
        if html.index("&amp;lt;")
          tokenizer = HTML::Tokenizer.new(html)
          new_text = "" 

          while token = tokenizer.next
            node = HTML::Node.parse(nil, 0, 0, token, false)
            new_text &amp;lt;&amp;lt; case node
                        when HTML::Tag
                          if ALLOWED_TAGS.include?(node.name)
                            node.to_s
                          else
                            node.to_s.gsub(/&amp;lt;/, "lt")
                          end
                        else
                          node.to_s.gsub(/&amp;lt;/, "lt")
                        end
          end

          html = new_text
        end

        html
      end
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Then in your views you can simply do the following:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;
  &amp;lt;%= whitelist(@post.body) %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;: Due to a limitation in textile (used in this forum) I had to remove the ampersands and semi-colons from the code above, so you&amp;#8217;ll need to re-insert those in the gsub calls.  So the code should look like &lt;em&gt;node.to_s.gsub(/LESS-THAN/, &amp;#8220;AMPERSANDltSEMI-COLON&amp;#8221;)&lt;/em&gt; where &lt;span class="caps"&gt;LESS&lt;/span&gt;-THAN, &lt;span class="caps"&gt;AMPERSAND&lt;/span&gt; and &lt;span class="caps"&gt;SEMI&lt;/span&gt;-COLON are replaced by their respective characters.&lt;/strong&gt;&lt;/p&gt;


	&lt;h3&gt;Discussion&lt;/h3&gt;


	&lt;p&gt;Obviously we are taking advantage of the fact that classes (and&lt;br /&gt;modules, in this case) in Ruby are open by patching our own method&lt;br /&gt;into Rail&amp;#8217;s TextHelper module.&lt;/p&gt;


	&lt;p&gt;If you want to allow more tags than just anchors and images, add &lt;br /&gt;the additional tags to the &lt;strong&gt;%w()&lt;/strong&gt; in following line:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;
ALLOWED_TAGS = %w(a img) unless defined?(ALLOWED_TAGS)
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;


	&lt;h3&gt;Further Reading&lt;/h3&gt;


	&lt;p&gt;You can see examples of this in the source code for the &lt;a href="&lt;a"&gt;Typo&lt;/a&gt;&lt;br /&gt;weblog where it patches ActiveRecord::Base and also&lt;br /&gt;ActionController::Components.&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;href="http://www.typosphere.org"&amp;gt;http://www.typosphere.org&lt;/a&gt;&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Fri, 17 Mar 2006 19:31:32 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:8:2:1844</guid>
      <author>Fora User</author>
      <link>http://forums.pragprog.com/forums/8/topics/2</link>
    </item>
  </channel>
</rss>
