<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>[kumo.it] &#187; rails</title>
	<atom:link href="http://www.kumo.it/articles/category/blog/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kumo.it</link>
	<description>iPhone software and Hiragana/Katakana quizzes</description>
	<lastBuildDate>Sat, 15 May 2010 14:35:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>acts_as_confirmable rails plugin</title>
		<link>http://www.kumo.it/articles/2008/06/11/acts_as_confirmable-plugin/</link>
		<comments>http://www.kumo.it/articles/2008/06/11/acts_as_confirmable-plugin/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 08:54:37 +0000</pubDate>
		<dc:creator>kumo</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kumo.it/?p=83</guid>
		<description><![CDATA[acts_as_confirmable is a Ruby on Rails plugin that is useful when you want to know who ticked a check box and when they did so. It can be found at http://github.com/kumo/acts_as_confirmable/tree/master. This plugin treats a datetime attribute and an integer attribute as a boolean. This boolean can then be used as normal attribute in a [...]]]></description>
			<content:encoded><![CDATA[<p><em>acts_as_confirmable</em> is a Ruby on Rails plugin that is useful when you want to know who ticked a check box and when they did so. It can be found at <a href="http://github.com/kumo/acts_as_confirmable/tree/master">http://github.com/kumo/acts_as_confirmable/tree/master</a>.</p>
<p>This plugin treats a datetime attribute and an integer attribute as a boolean. This boolean can then be used as normal attribute in a check box (or in any part of the program) and when it is checked, the datetime records the moment and the integer records the id of the current user.</p>
<p><em>It is assumed that there is a Users table and that the current user can be found at User.current_user. If the current user cannot be accessed then 1 is used as the id.</em></p>
<p>h3. Installation</p>
<pre lang="bash">
$ cd vendor/plugins
$ git checkout git://github.com/kumo/acts_as_confirmable/tree
</pre>
<p>h3. Usage</p>
<p>* On models where you want to be able to confirm X add a X_confirmed_at datetime and X_confirmed_by integer.<br />
* In the model put acts_as_confirmed :X<br />
* In the views add check boxes (check_box :X)</p>
<p>h3. Example</p>
<p>h4. create a new rails app</p>
<pre lang="bash">
$ rails confirm
$ cd confirm
</pre>
<p>h4. create users table and items table with 3 confirmable fields</p>
<pre lang="bash">
$ script/generate scaffold user name:string
$ script/generate scaffold item name:string \
      started_confirmed_by:integer started_confirmed_at:datetime \
      finished_confirmed_by:integer finished_confirmed_at:datetime \
      ready_confirmed_by:integer ready_confirmed_at:datetime
</pre>
<p>h4. add the plugin to the model</p>
<pre lang="Rails">
class Item < ActiveRecord::Base
  acts_as_confirmable :started, :finished, :ready
end
</pre>
<p>h4. add 3 check boxes in items/new.html.erb and items/edit.html.erb</p>
<pre lang="Rails">


  <%= f.label :started %>
  <%= f.check_box :started %>



  <%= f.label :finished %>
  <%= f.check_box :finished %>



  <%= f.label :ready %>
  <%= f.check_box :ready %>

</pre>
<p>h4. show the confirmation info in items/list.html.erb</p>
<pre lang="Rails">
<td>
  <%=h item.started? %>
  <%=h item.finished_confirmer.name if @item.finished? -%>
</td>
</pre>
<p>h4. example of assigning a user to current_user</p>
<pre lang="Rails">
class User < ActiveRecord::Base
  cattr_accessor :current_user
end

class ItemsController < ApplicationController
  before_filter :load_user

  def load_user
    User.current_user = User.find(:first)
  end
end
</pre>
<p>h3. Available fields</p>
<p>If a model has the attributes fields started_confirmed_at and started_confirmed_by, then the plugin provides:</p>
<p>*
<pre>started?</pre>
<p> -- true if it has been confirmed by someone on a specific date<br />
*
<pre>started</pre>
<p> -- same as above but is normally used by a check box tag<br />
*
<pre>started=</pre>
<p> -- the assignment method that is used by the check box when posting<br />
*
<pre>started_confirmer</pre>
<p> -- the user that confirmed it<br />
*
<pre>started_at</pre>
<p> -- when it was confirmed</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kumo.it/articles/2008/06/11/acts_as_confirmable-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The perils in trusting syntax highlighting</title>
		<link>http://www.kumo.it/articles/2008/02/14/the-perils-in-trusting-syntax-highlighting/</link>
		<comments>http://www.kumo.it/articles/2008/02/14/the-perils-in-trusting-syntax-highlighting/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 10:45:42 +0000</pubDate>
		<dc:creator>kumo</dc:creator>
				<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Stories]]></category>
		<category><![CDATA[notes]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.kumo.it/articles/2008/02/14/the-perils-in-trusting-syntax-highlighting/</guid>
		<description><![CDATA[Today after upgrading RSpec I ran a user story that I had just written and received a wonderful error: You have a nil object when you didn't expect it! (NoMethodError). The error occurred while evaluating nil.add_scenario. I didn&#8217;t even think to check my user story because the syntax highlighting looked fine and it made sense [...]]]></description>
			<content:encoded><![CDATA[<p>Today after upgrading <a href="http://rspec.info">RSpec</a> I ran a <a href="http://rspec.info/documentation/stories.html">user story</a> that I had just written and received a wonderful error: <code>You have a nil object when you didn't expect it! (NoMethodError). The error occurred while evaluating nil.add_scenario</code>. I didn&#8217;t even think to check my user story because the syntax highlighting looked fine and it made sense to me:</p>
<pre lang="Story">
  Scenario: areas have different types
    As an administrator
    I want to assign different types to the areas
    So that I can structure the home page

    Scenario: footer shows page links
      Given an area
      And area has 5 pages
      And area is of type footer
      When I view the home page
      Then I should see the home page
      And I should see the footer section
      And the list section should contain 0 areas
      And the footer section should contain 5 links
</pre>
<p>Of course, if I actually bothered to read it properly then I would have seen that the first line should have read:</p>
<pre lang="Story">
  Story: areas have different types
</pre>
<p>Moral of the story: even if the code that you have to write is small, always assume that you are capable of making silly mistakes!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kumo.it/articles/2008/02/14/the-perils-in-trusting-syntax-highlighting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>an assert_difference that works with arrays</title>
		<link>http://www.kumo.it/articles/2006/11/23/an-assert_difference-that-works-with-arrays/</link>
		<comments>http://www.kumo.it/articles/2006/11/23/an-assert_difference-that-works-with-arrays/#comments</comments>
		<pubDate>Thu, 23 Nov 2006 14:19:13 +0000</pubDate>
		<dc:creator>kumo</dc:creator>
				<category><![CDATA[RSpec]]></category>
		<category><![CDATA[notes]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.kumo.it/articles/2006/11/23/an-assert_difference-that-works-with-arrays/</guid>
		<description><![CDATA[I have started using the better assert_difference that was posted at blog.caboo.se to improve my rails tests. This allowed me to write tests such as: assert_difference Item, :count do Item.create(:name => "Monkey magic") end This replaces: item_count = Item.count Item.create(:name => "Monkey magic") assert_difference, item_count + 1, Item.count but I also wanted to be able [...]]]></description>
			<content:encoded><![CDATA[<p>I have started using the <i>better assert_difference</i> that was posted at <a href="http://blog.caboo.se/articles/2006/06/13/a-better-assert_difference">blog.caboo.se</a> to improve my rails tests. This allowed me to write tests such as:</p>
<pre lang="rails">
  assert_difference Item, :count do
    Item.create(:name => "Monkey magic")
  end
</pre>
<p>This replaces:</p>
<pre lang="rails">
  item_count = Item.count
  Item.create(:name => "Monkey magic")
  assert_difference, item_count + 1, Item.count
</pre>
<p>but I also wanted to be able to test if emails were sent by writing:</p>
<pre lang="rails">
  assert_difference ActionMailer::Base.deliveries, :size do
    SystemNotifier.deliver_important_message
  end
</pre>
<p>That didn&#8217;t work and it was noted on that website that the code didn&#8217;t work properly with arrays so I have modified the assert_difference method to check if the objects respond to the method (e.g. <i>size</i>) and act accordingly:</p>
<pre line="1" lang="rails">
  def assert_difference(objects, method = nil, difference = 1)
    initial_values = []
    if objects.respond_to? method
      initial_values = objects.send(method)
    else
      objects = [objects].flatten
      initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) }
    end

    yield

    if difference.nil?
      objects.each_with_index { |obj,i|
        assert_not_equal initial_values[i], obj.send(method) #, "#{obj}##{method}"
      }
    else
      if objects.respond_to? method
        assert_equal initial_values + difference, objects.send(method) #, "#{objects}##{method}"
      else
        objects.each_with_index { |obj,i|
          assert_equal initial_values[i] + difference, obj.send(method) #, "#{obj}##{method}"
        }
      end
    end
  end
</pre>
<p>I have tested it for an array such as <em>ActionMailer::Base.deliveries</em> but I haven't tested it in other situations so hopefully it won't create any problems.<br />
<hr />The rspec version of this is:</p>
<pre line="1" lang="rails">
  def assert_difference(objects, method = nil, difference = 1)
    initial_values = []
    if objects.respond_to? method
      initial_values = objects.send(method)
    else
      objects = [objects].flatten
      initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) }
    end

    yield

    if difference.nil?
      objects.each_with_index { |obj,i|
        initial_values[i].should_not_equal obj.send(method) #, "#{obj}##{method}"
      }
    else
      if objects.respond_to? method
        (initial_values + difference).should_eql objects.send(method) #, "#{objects}##{method}"
      else
        objects.each_with_index { |obj,i|
          (initial_values[i] + difference).should_eql obj.send(method) #, "#{obj}##{method}"
        }
      end
    end
  end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.kumo.it/articles/2006/11/23/an-assert_difference-that-works-with-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rubyonrails &#8211; beware of similarly named relationships</title>
		<link>http://www.kumo.it/articles/2006/04/29/habtm-clobbering/</link>
		<comments>http://www.kumo.it/articles/2006/04/29/habtm-clobbering/#comments</comments>
		<pubDate>Sat, 29 Apr 2006 10:26:00 +0000</pubDate>
		<dc:creator>kumo</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[habtm]]></category>
		<category><![CDATA[notes]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I recently developed a &#8220;rubyonrails&#8221;:http://www.rubyonrails.org application in which a user had a province (as part of their address) and also a collection of selected provinces. class Users belongs_to :province has_and_belongs_to_many :selected_provinces, :class_name => "Province" end I could very easily find out which provinces a user had selected and also which users had selected a specific [...]]]></description>
			<content:encoded><![CDATA[<p>I recently developed a &#8220;rubyonrails&#8221;:http://www.rubyonrails.org application in which a user had a province (as part of their address) and also a collection of selected provinces.</p>
<pre lang="rails">
class Users
  belongs_to :province
  has_and_belongs_to_many :selected_provinces, :class_name => "Province"
end
</pre>
<p>I could very easily find out which provinces a user had selected and also which users had selected a specific province. However I didn&#8217;t realise that if I accessed a user via the province then the <code>province_id</code> which previously pointed the user&#8217;s address province was replaced by the id of the selected province.</p>
<pre lang="rails">
province = Province.find_first
province.users.each do |user|
  # do something
  # however user.province_id now points to province
end
</pre>
<p>I assume the <code>province_id</code> is added by the habtm join so that we can understand which selected_province object this user object comes from however in my case it meant that I lost the original <code>province_id</code> and so if I saved that user then I would end up changing the address province.</p>
<p>This is probably not a bug as such but something that should be watched out for as it took me a while to discover this problem with my application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kumo.it/articles/2006/04/29/habtm-clobbering/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 8/14 queries in 0.029 seconds using disk

Served from: www.kumo.it @ 2010-07-29 16:34:12 -->