<?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>Programming is FUN! &#187; Ruby on Rails</title>
	<atom:link href="http://kresimirbojcic.com/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://kresimirbojcic.com</link>
	<description>At least that's what I've been telling everyone</description>
	<lastBuildDate>Thu, 03 Jun 2010 23:22:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Gruff &#8211; good looking charts in Rails</title>
		<link>http://kresimirbojcic.com/2010/05/gruff-good-looking-charts-in-rails/</link>
		<comments>http://kresimirbojcic.com/2010/05/gruff-good-looking-charts-in-rails/#comments</comments>
		<pubDate>Sun, 23 May 2010 00:19:00 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[gruff]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[special characters]]></category>
		<category><![CDATA[utf-8]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=609</guid>
		<description><![CDATA[Gruff is plugin for creating pretty decent charts in Rails. It has nice straight forward API and couple of built-in themes. 

To render it I&#8217;ve used(Haml)
 %img{:src =&#62;'/izvjestaji/ucestalost_po_rasvjetnom_mjestu_graf' ,
:style =&#62;'border:1px solid #aabcca;'}
and in controller:

  def ucestalost_po_rasvjetnom_mjestu_graf
    g = create_pie
    ucestalost_po_rasvjetnom_mjestu.each do &#124;item&#124;
      g.data [...]]]></description>
			<content:encoded><![CDATA[<p>Gruff is plugin for creating pretty decent charts in Rails. It has nice straight forward API and couple of built-in themes. </p>
<p><img src="http://kresimirbojcic.com/images/gruff_pie.jpeg" alt="Gruff in action" /></p>
<p>To render it I&#8217;ve used(Haml)</p>
<pre class="brush: text;"> %img{:src =&gt;'/izvjestaji/ucestalost_po_rasvjetnom_mjestu_graf' ,
:style =&gt;'border:1px solid #aabcca;'}</pre>
<p>and in controller:</p>
<pre class="brush: ruby;">
  def ucestalost_po_rasvjetnom_mjestu_graf
    g = create_pie
    ucestalost_po_rasvjetnom_mjestu.each do |item|
      g.data item.oznaka.to_s, item.broj_kvarova.to_i
    end
    send_data(g.to_blob(), :disposition =&gt; 'inline', :type =&gt; 'image/png',
:filename =&gt; &quot;ucestalost_po_rasvjetnom_mjestu&quot;)
  end
</pre>
<pre class="brush: ruby;">
  def create_pie
    g = Gruff::Pie.new('948x450')
    g.theme_pastel
    g.font = &quot;#{Prawn::BASEDIR}/data/fonts/times.ttf&quot;
    g.legend_font_size = 10
    g.hide_title
    g.title_margin = 0
    g.title_font_size = 0
    return g
  end
</pre>
<p>My biggest issue with Gruff was having to install  RMagick. Please read <a href="http://http://rmagick.rubyforge.org/install-faq.html">RMagick FAQ</a>. You&#8217;ll have to compile it, and make sure you have all the libraries it needs.</p>
<p>Major grief were Croatian characters(ŠĐŽĆČ) but it applies to all utf-8. Worst part was that compile went OK, but result was all but OK <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To save some time make sure you:</p>
<p>1. Use true type font that has chars that you want<br />
2. Install RMagick with all the -dev libraries that it needs (otherwise special characters are screwed up)<br />
3. FreeType library is a must &#8211; make sure you have it before compilation</p>
<p>When I look at the result I wish it had 3D pie, but never the less it&#8217;s worth to keep an eye on.</p>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2010/05/gruff-good-looking-charts-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Haml &#8211; yay or nay?</title>
		<link>http://kresimirbojcic.com/2010/05/haml-yay-or-nay/</link>
		<comments>http://kresimirbojcic.com/2010/05/haml-yay-or-nay/#comments</comments>
		<pubDate>Sun, 16 May 2010 18:14:44 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[haml]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=594</guid>
		<description><![CDATA[I finally decided to give it another shot. First time I&#8217;ve checked I was not very impressed with Haml. I did like the logo, and the theory&#8230; but in practice it just didn&#8217;t feel quite right. Boy am I glad that I tried it out for the second time. This time it is a definitive [...]]]></description>
			<content:encoded><![CDATA[<p>I finally decided to give it another shot. First time I&#8217;ve checked I was not very impressed with Haml. I did like the logo, and the theory&#8230; but in practice it just didn&#8217;t feel quite right. Boy am I glad that I tried it out for the second time. This time it is a definitive <strong>yay</strong>.</p>
<p><img src="http://kresimirbojcic.com/images/before_and_after.png" alt="Before and after Haml" /></p>
<p>What I like about it?</p>
<p>1. You get smaller template<br />
2. Nice ruby and .css integration<br />
3. Haml validations that makes you align everything just right<br />
4. Not having to close tags &#8211; this always seemed too mechanical</p>
<p>My advice would be &#8211; definitely try it out if you are not already using it. Convert a couple of templates and decide for yourself. <strong>BUT DO</strong> try it out yourself on real example!</p>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2010/05/haml-yay-or-nay/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rake stats &#8211; adding a custom folder</title>
		<link>http://kresimirbojcic.com/2009/11/rake-stats-adding-a-custom-folder/</link>
		<comments>http://kresimirbojcic.com/2009/11/rake-stats-adding-a-custom-folder/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 06:08:17 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[adding folder to stats]]></category>
		<category><![CDATA[custom folder]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[stats]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=561</guid>
		<description><![CDATA[Rake stats is useful when you need to know the statistics of your project. However if you try to add custom folder to be listed &#8211; documentation is somewhat sparse.
Question is why would you need a custom folder? After all isn&#8217;t the Rails prescribed way to go&#8230; Well not everything is a model or a [...]]]></description>
			<content:encoded><![CDATA[<p>Rake stats is useful when you need to know the <strong>statistics</strong> of your project. However if you try to add <strong>custom folder</strong> to be listed &#8211; documentation is somewhat sparse.</p>
<p>Question is why would you need a custom folder? After all isn&#8217;t the Rails prescribed way to go&#8230; Well not everything is a model or a a lib. You can have complex domain models in Ruby that have nothing to do with the database or the general library utility classes. Those poor classes<strong> just scream</strong> for the folder of their own.</p>
<p>If you add code to new folder named FancyNewFolder &#8220;rake stats&#8221; is not picking it up.<br />
<img src="http://kresimirbojcic.com/images/before_custom_folder_stats.jpeg" alt="Stats before adding custom folder" /><br />
Here is what I did.
<pre class="brush: text;">gvim lib/tasks/AppTasks.rake</pre>
<p>And then I&#8217;ve added<code>
<pre class="brush: ruby;">STATS_DIRECTORIES = [
  %w(Controllers        app/controllers),
  %w(Helpers            app/helpers),
  %w(Models             app/models),
  %w(Libraries          lib/),
  %w(FancyNewFolder     app/fancy_new_folder),
  %w(Integration\ tests test/integration),
  %w(Functional\ tests  test/functional),
  %w(Unit\ tests        test/unit)

].collect {|name, dir| [name, &quot;#{RAILS_ROOT}/#{dir}&quot;]}.select {|name, dir| File.directory?(dir)}

desc &quot;Report code statistics (KLOCs, etc) from the application&quot;
task :stats do
  require 'code_statistics'
  CodeStatistics.new(*STATS_DIRECTORIES).to_s
end</pre>
<p></code><br />
 Voilà there is your custom folder, I&#8217;ll bet the stats are not so good as they used to be, but at least now you know the truth <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<img src="http://kresimirbojcic.com/images/after_custom_folder_stats.jpeg" alt="Stats after adding custom folder" /></p>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2009/11/rake-stats-adding-a-custom-folder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Redgreen plugin not working with Ruby 1.9.1</title>
		<link>http://kresimirbojcic.com/2009/09/redgreen-plugin-not-working-with-ruby-1-9-1/</link>
		<comments>http://kresimirbojcic.com/2009/09/redgreen-plugin-not-working-with-ruby-1-9-1/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 03:39:28 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[not working]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[redgreen]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=550</guid>
		<description><![CDATA[
You have upgraded to Ruby 1.9.1 and now (after manually changing &#038; compiling mongrel to work) you noticed that redgreen plugin is not working. Whenever you start test you get something similar to this:
`require': no such file to load -- test/unit/ui/console/testrunner (MissingSourceFile)
Just type this in:
sudo gem install test-unit -v 1.2.3
If you are one of those [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://kresimirbojcic.com/images/redgreen.jpg" alt="Redgreen in action" /><br />
You have upgraded to Ruby 1.9.1 and now (after manually changing &#038; compiling <a href="http://rubynyc.wordpress.com/ruby-1-9-1/installing-mongrel-1-1-5-on-ruby-1-9-1/">mongrel</a> to work) you noticed that <a href="http://on-ruby.blogspot.com/2006/05/red-and-green-for-ruby.html">redgreen</a> plugin is not working. Whenever you start test you get something similar to this:
<pre class="brush: text;">`require': no such file to load -- test/unit/ui/console/testrunner (MissingSourceFile)</pre>
<p>Just type this in:
<pre class="brush: text;">sudo gem install test-unit -v 1.2.3</pre>
<p>If you are one of those &#8220;problematic&#8221; people that want to know why is this working&#8230; truth is much simpler than I would like it to be considering that I lost 2 hours over this <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Ruby 1.9.1 replaced test-unit framework with <a href="http://blog.zenspider.com/minitest/">minitest</a> (because it&#8217;s supposed to be better). Redgreen plugin depends on console-runner from <a href="http://rubyforge.org/projects/test-unit/">test-unit</a> so you need to install it as u gem. (Older version, because the new 2.0.3 changed API just enough so redgreen is not working)</p>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2009/09/redgreen-plugin-not-working-with-ruby-1-9-1/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Ruby refactoring fun</title>
		<link>http://kresimirbojcic.com/2009/08/ruby-refactoring-fun/</link>
		<comments>http://kresimirbojcic.com/2009/08/ruby-refactoring-fun/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 04:19:46 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=529</guid>
		<description><![CDATA[I my last post I&#8217;ve said that I don&#8217;t care if code for method_missing implementation is ugly. Well I&#8217;ve lied.
This is starting point

def method_missing(method, *params)
     method = method.to_s
     type = :get
     if (method.index('store_').nil? == false) #if you have name conflict use store_ prefix,
 [...]]]></description>
			<content:encoded><![CDATA[<p>I my last post I&#8217;ve said that I don&#8217;t care if code for method_missing implementation is ugly. Well I&#8217;ve lied.<br />
This is starting point</p>
<pre class="brush: ruby;">
def method_missing(method, *params)
     method = method.to_s
     type = :get
     if (method.index('store_').nil? == false) #if you have name conflict use store_ prefix,
                                                             #or rename hash key or class method
         method.gsub!('store_','')
     elsif (method.index('exists_').nil? == false)
         method = method.gsub!('exsists_','')
         type = :check_if_exists
     elsif (method.index('set_').nil? == false &amp;&amp; params.count == 1)
         method = method.gsub!('set_','')
         type = :set
     end
     method = method.to_sym
     if type == :get
         return @store[method] if @store.keys.collect(&amp;:to_sym).include?(method)
     elsif type == :check_if_exists
        return @store.keys.collect(&amp;:to_sym).include?(method)
     else
        return @store[method] = params[0]
     end
     return nil  # return nil if key does not exist
  end
</pre>
<p></code><br />
If you want to see what kid of result did I get read on<span id="more-529"></span><br />
<code></p>
<pre class="brush: ruby;">
  def method_missing(method, *params)
     command = Command.new(method, { :store =&gt; :get, :exists =&gt; :check_if_exists,  :set =&gt; :set },
 :get )
     case command.name
       when :get then
          return @store[command.method] if @store.keys.collect(&amp;:to_sym).include?(command.method)
       when :check_if_exists then
          return @store.keys.collect(&amp;:to_sym).include?(command.method)
       when :set then
          @store[command.method] = params[0]
       else return nil
     end
  end 
</pre>
<p></code></p>
<p>All this is possible with the help of brand-new-and-shiny <strong>Command</strong> class<br />
<code></p>
<pre class="brush: ruby;">
class Command 

attr_reader :name
attr_reader :method

def initialize(method, command_map, default_value = nil)
  @name = default_value
  @method = method
  parse(command_map)
end

private

def parse(command_map)
    method = @method.to_s
    command_map.each_pair do |key, value|
       parse_string = &quot;#{key}_&quot;
       if method.index(parse_string).nil? == false
         @name = value.to_sym
         @method = method.gsub(parse_string,'').to_sym
         break
       end
    end
end
end
</pre>
<p></code><br />
To be honest, my real life code was a bit more brutal, so Command class really payed of there.<br />
I really like how much Ruby is <strong>NOT</strong> getting in my way. Language is really elegant and has much more appeal to me that ROR itself. (ROR drew me to the Ruby, just like Compiz drew me to the Ubuntu, and than Ubuntu to Linux, and then Linux to Arch <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</p>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2009/08/ruby-refactoring-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Ruby Metaprogramming to Increase READABILITY of Code</title>
		<link>http://kresimirbojcic.com/2009/07/using-ruby-metaprogramming-to-increase-readability-of-code/</link>
		<comments>http://kresimirbojcic.com/2009/07/using-ruby-metaprogramming-to-increase-readability-of-code/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 12:09:24 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[metaprogamming]]></category>
		<category><![CDATA[method_missing]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[user friendly hash]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=465</guid>
		<description><![CDATA[Let&#8217;s assume you have a class that has some sort of hash where it stores it&#8217;s data. We want to calculate amount that we need to pay, but only if number of items that we want to sell is entered. We&#8217;ll also assume that price is already present so we don&#8217;t need to check for [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s assume you have a class that has some sort of hash where it stores it&#8217;s data. We want to calculate amount that we need to pay, but only if number of items that we want to sell is entered. We&#8217;ll also assume that price is already present so we don&#8217;t need to check for that. It&#8217;s easy:<code>
<pre class="brush: ruby;">@store[:total] = @store[:price] * @store[:amount] if @store.has_key?(:amount)</pre>
<p></code>A bit more readable would be:<code>
<pre class="brush: ruby;">set_total(price * amount) if amount_exists</pre>
<p></code><span id="more-465"></span>This is possible by using <strong>method_missing</strong> that is cleverly embedded in Ruby method invocation chain. Ruby first tries to find method in calling class, then in it&#8217;s super classes and mixins. If all of that fails then it looks into method_missing. Basic usage is covered <a href="http://www.intridea.com/2007/8/7/bending-ruby-i-user-friendly-hashes-using-method_missing?blog=company">here</a>. To make upper code possible, original example must be upgraded a little:<code>
<pre class="brush: ruby;">
def method_missing(method, *params)
     method = method.to_s
     type = :get
     if (method.index('store_').nil? == false) #if you have name conflict use store_ prefix,
                                                             #or rename hash key or class method
         method.gsub!('store_','')
     elsif (method.index('_exists').nil? == false)
         method = method.gsub!('_exsists','')
         type = :check_if_exists
     elsif (method.index('set_').nil? == false &amp;&amp; params.count == 1)
         method = method.gsub!('set_','')
         type = :set
     end
     method = method.to_sym
     if type == :get
         return @store[method] if @store.keys.collect(&amp;:to_sym).include?(method)
     elsif type == :check_if_exists
        return @store.keys.collect(&amp;:to_sym).include?(method)
     else
        return @store[method] = params[0]
     end
     return nil  # return nil if key does not exist
  end
</pre>
<p></code>The code is ugly and un-refactored, but it&#8217;ll work and it will make my class more user friendly. Hopefully I&#8217;ll never look again in method_missing method,<del datetime="2009-08-12T04:03:13+00:00"> so who cares how it looks&#8230; for now</del> <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I care so I refactored it <a href="http://kresimirbojcic.com/ruby-refactoring-fun">here</a>.</p>
<blockquote><p>
<strong>Caveats : </strong><br />
1. Performance &#8211; obviously there is a performance penalty, I like not to dwell over it too much until it bites me. My experience has taught me that I am too stupid to catch performance issues (except the really obvious one) in advance. Knowing my limits I sit and wait for bottlenecks to introduce them self in a don&#8217;t-call-us-we&#8217;ll-call-you-manner (those guys really like to show off, don&#8217;t they)<br />
2. Naming &#8211; if there is a hash item named same as existing method &#8211; you may not always get what you want. You can add prefix like this: store_price, store_amount&#8230; or you can just rename conflicting method<br />
3. Encapsulation is broken &#8211; all of a sudden all hash elements are public even if a hash is a private member &#8211; on the bright side this can be fixed fairly easy
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2009/07/using-ruby-metaprogramming-to-increase-readability-of-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tweak CalendarDateSelect plugin to work with European datetime format</title>
		<link>http://kresimirbojcic.com/2009/06/tweak-calendardateselect-plugin-to-work-with-european-datetime-format/</link>
		<comments>http://kresimirbojcic.com/2009/06/tweak-calendardateselect-plugin-to-work-with-european-datetime-format/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 06:51:04 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[calendardateselect]]></category>
		<category><![CDATA[dd.mm.yyyy]]></category>
		<category><![CDATA[european datetime]]></category>
		<category><![CDATA[Heroku]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=434</guid>
		<description><![CDATA[
If you want to use calendar_date_select plug-in for date time picker, and you would like to use dd.mm.yyyy format read on!
 I wrote this post after many wrong turns and many tutorials that just didn&#8217;t work. This informations applies for RAILS 2.3.2, and Calendar Date Select 1.15. I you are anything like me you want [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-width: 0px;" src="http://kresimirbojcic.com/images/calendardateselect.jpg" alt="Calendar date select in action" />
<p>If you want to use <a href="http://code.google.com/p/calendardateselect/">calendar_date_select</a> plug-in for date time picker, and you would like to use <strong>dd.mm.yyyy</strong> format read on!</p>
<p> I wrote this post after many wrong turns and many tutorials that just didn&#8217;t work. This informations applies for <strong>RAILS 2.3.2</strong>, and <strong>Calendar Date Select 1.15</strong>. I you are anything like me you want proof that my <strong>tutorial is working? </strong><span id="more-434"></span>OK, go <a href=" http://gosjar2.heroku.com/events/new">here</a><strong>BUT</strong> you should have trusted me in the first place <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  After you installed the <a href="http://code.google.com/p/calendardateselect/">plug-in</a>, unpack it so you can use it on shared hosting (I am using Heroku).</p>
<pre><code>rake gems:unpack</code></pre>
<p>In file:
<pre><code>/vendor/gems/calendar_date_select-1.15/lib/calendar_date_select/calendar_date_select.rb</code></pre>
<p>change self.format to:<code>
<pre class="brush: ruby;">
 def self.format
     @format ||= FORMATS[:finnish]
 end</pre>
<p></code></p>
<blockquote><p>Note: Alternative is to use
<pre><code><% CalendarDateSelect.format=(:finnish )%></code></pre>
<p> in head tag of your layout page, but for some reason it&#8217;s not working on first page load on Heroku.</p></blockquote>
<p>Almost there <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  now in you need to add file date_formats.rb to folder
<pre><code>/config/initializers</code></pre>
<p> with this content:<code>
<pre class="brush: ruby;">
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!
(:default =&gt; &quot;%d.%m.%Y&quot;)
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!
(:default =&gt; &quot;%d.%m.%Y %H:%M&quot;)
</pre>
<p></code>Initializers get called by default, so you don&#8217;t have to do anything else with them. This is used for formating date in wanted format when using to_s. This works perfecly on my development machine that has dd.mm.yyyy as a system date format. If that&#8217;s case with you, then you probably didn&#8217;t need this tutorial at all &#8211; <strong>go surf some place else</strong> <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>However on Heroku your <strong>day and month will switch places</strong> everytime you save model to a DB. It turns out that Rails is using heuristic to determine date time<br />
format. Idea is not bad, but format is highly ambigous. Some suggest that you should change default <a href=" http://source.mihelac.org/2006/9/13/parsing-european-date-format-in-ruby-rails">Date._parse method</a> &#8211; I did not want to do that. Some say that you need to change <a href=" http://blog.nominet.org.uk/tech/2007/06/14/date-and-time-formating-issues-in-ruby-on-rails/">ActiveRecord::ConnectionAdapters::Column:string_to_date</a> method. The problem with that approact is that by the time you get into ActiveRecord::ConnectionAdapters::Column:string_to_date , the model date is already <strong>&#8220;switched&#8221;</strong> &#8211; I think this was not a fact when that tutorial was written. At the end and after much head-banging I decided to help the default Date._parse method to do the<strong> right thing</strong>.</p>
<p>I knew that if you have 06/23/2009 it will get interpreted as American format (mm/dd/yyyy) and that if you have 06-23-2009 it will get interpreted as European format (dd-mm-yyyy). Format that I wanted to use was dd.mm.yyyy &#8211; it was correctly interpreted on my machine but not on Heroku hosting. Let&#8217;s say that you have model <strong>:event</strong> and datetime filed <strong>:event_date</strong>.</p>
<p>The right time for &#8220;strike&#8221; is before you send <strong>parms[:event]</strong> in you controller. I didn&#8217;t want to write call to that for create or update so I used before_filter.<code>
<pre class="brush: ruby;">
class EventsController &lt; ApplicationController
before_filter :localize_params,  : only =&gt; [:update, :create ]

def localize_params
   params[:event][:event_date].gsub!('.','-')
end
</pre>
<p></code></p>
<p>Easy-peasy&#8230;. Now it should work as expected.</p>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2009/06/tweak-calendardateselect-plugin-to-work-with-european-datetime-format/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RAILS auto_complete With Additional FILTER</title>
		<link>http://kresimirbojcic.com/2009/05/rails_auto_complete-with-additional-filter/</link>
		<comments>http://kresimirbojcic.com/2009/05/rails_auto_complete-with-additional-filter/#comments</comments>
		<pubDate>Fri, 29 May 2009 03:34:49 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[auto_complete]]></category>
		<category><![CDATA[filter auto complete selection]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=397</guid>
		<description><![CDATA[
The best tutorial about auto complete control by far was one from Ryan Bates. It worked like a charm, and he made it look so easy. Only problem I had with it was that I wanted another control on the form to filter the auto-complete control.
The tutorial didn&#8217;t cover that, so I got stranded in [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-width: 0px;" src="http://kresimirbojcic.com/images/auto_complete_filter.jpg" alt="Auto complete with additional filter" />
<p>The best tutorial about auto complete control by far was one from <a href="http://railscasts.com/episodes/102-auto-complete-association">Ryan Bates</a>. It worked like a charm, and he made it look so easy. Only problem I had with it was that I wanted another control on the form<strong> to filter the auto-complete control</strong>.<br />
The tutorial didn&#8217;t cover that, so I got stranded in a strange place. The solution was pretty simple, but it took me a while to figure it out. If you are just beginning Rails like I am &#8211; this is rigth tutorial for you.</p>
<p><span id="more-397"></span>I have &#8216;City&#8217; and &#8216;Street&#8217; on my form. &#8216;City&#8217; is regular drop down list since there are only few cities to select from. I wanted the &#8216;Street&#8217; control to be an autocomplete text box that get&#8217;s filtered based on the city that is selected. You need to do everything that Ryan says you should &#8211; when you finish his tutorial come back for adding &#8220;filter&#8221; functionality <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I&#8217;ll won&#8217;t add exactly the same fields, but you&#8217;ll get the point&#8230; Oh, you are back already&#8230; ok. I am assuming that you&#8217;ll change model names based on the model that you are using. I did everything in Ryan tutorial but with my model.<br />
You need to pass additional parameter that you want to be a filter. In my case that is currently selected city id. I&#8217;ll put it in template &#8216;new.html.erb&#8217; of view that is rendering both controls<code>
<pre class="brush: ruby;">&lt;%= f.select  :settlement_id, @settlements.map {|u| [u.name,u.id]} %&gt;
 &lt;%= f.text_field_with_auto_complete :event, :street_name , {
 :url =&gt; formatted_streets_path(:js), :method =&gt; :get,
 :with =&gt;&quot;'search=' + element.value + '&amp;settlement_id=' + $('event_settlement_id').value&quot;  } %&gt;</pre>
<p></code></p>
<p><strong>Step 1)</strong></p>
<p>Pay attention to :with<code>
<pre class="brush: ruby;">:with =&gt;&quot;'search=' + element.value + '&amp;settlement_id=' + $('event_settlement_id').value&quot;:</pre>
<p></code></p>
<p>You are basicly saying : Append to url search=#value_of_my_control&#038;settlement_id=#value_of_control_named_event_settlement_id</p>
<p><strong>Step 2)</strong></p>
<p>Change the street controller to know how to accept additional parameters and filter the results<code>
<pre class="brush: ruby;">
def index

    if params[:settlement_id].blank?
      @streets = Street.find(:all,
                          :conditions =&gt; ['name LIKE ?', &quot;%#{params[:search]}%&quot;])
    else
      @streets = Street.find(:all,
                   :conditions =&gt; ['name LIKE ? and settlement_id = ?', &quot;%#{params[:search]}%&quot;, &quot;#{params[:settlement_id]}&quot; ])
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml =&gt; @streets }
      format.js
    end
  end
 </pre>
<p></code></p>
<p>You can check it out just by typing
<pre><code>http://localhost:3000/streets.js?search=mac&#038;settlement_id=1 </code></pre>
<p> to see if data fetching is working. After that the autocomplete should &#8220;just work&#8221; </p>
<p>That&#8217;s all for now&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2009/05/rails_auto_complete-with-additional-filter/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Ruby BLOCKS Explained 101</title>
		<link>http://kresimirbojcic.com/2009/05/ruby-blocks-explained-101/</link>
		<comments>http://kresimirbojcic.com/2009/05/ruby-blocks-explained-101/#comments</comments>
		<pubDate>Sat, 16 May 2009 20:20:55 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[blocks]]></category>
		<category><![CDATA[explained]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=363</guid>
		<description><![CDATA[
Block is basically anonymous method that gets called from another method. If you have some code that executes passed in method like this:
def execute_method_in_block(named_method)
    named_method.call
    named_method.call
end

execute_method_in_block Proc.new {puts &#34;Hello, blocks are cool!&#34;}

You can write it like this using blocks:
def execute_method_in_block
    yield
    yield
end

execute_method_in_block  [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-width: 0px;" src="http://kresimirbojcic.com/images/rubyblocks/blocks.jpg" alt="Blocks" />
<p><strong>Block </strong>is basically <strong>anonymous method</strong> that gets called from another method. If you have some code that executes passed in method like this:<code>
<pre class="brush: ruby;">def execute_method_in_block(named_method)
    named_method.call
    named_method.call
end

execute_method_in_block Proc.new {puts &quot;Hello, blocks are cool!&quot;}
</pre>
<p></code>You can write it like this using blocks:<code>
<pre class="brush: ruby;">def execute_method_in_block
    yield
    yield
end

execute_method_in_block  {puts &quot;Hello, blocks are cool!&quot;}
</pre>
<p></code></p>
<p>We&#8217;ve lost &#8220;Proc.new&#8221; and a method name, we are just saying </p>
<blockquote><p>Whenever you find yield, execute block that is defined within braces.</p></blockquote>
<p>You might be wondering what&#8217;s the fuss all about,<span id="more-363"></span> this code is obviously not better than:<code>
<pre class="brush: ruby;">puts &quot;Hello, blocks are cool!&quot;
puts &quot;Hello, blocks are cool!&quot;
</pre>
<p></code>It turns out that blocks are useful for at least two things: <strong>iteration </strong>and <strong>comparison</strong>.You don&#8217;t loose any flexibility because you can send a block of code to execute at predefined placeholder position. That means that you can implement iteration only once, no matter how complicated it might be. Then you can &#8220;send&#8221; your custom logic and execute it for the each item in collection.
<p>In the next example we have a collection that has iteration. Nothing fancy, it just goes through the whole collection, the &#8220;yield&#8221; command is really interesting<code>
<pre class="brush: ruby;">def each
  #bla bla loop, fetch item one by one
   yield item  #placeholed for a block, share &quot;item&quot; with block
  #bla bla loop again
end
</pre>
<p></code>The &#8220;yield&#8221; command is a <strong>placeholder </strong>that is telling us where we can insert our block. We can do whatever we want with collection items and if we just want to print them we can do something like this:<code>
<pre class="brush: ruby;">[1,2,3,4].each do |x|
  puts x
 end
</pre>
<p></code>We are using |x|. Although the name is not important it means that we can use &#8220;x&#8221; in our block, and that it is corresponding with &#8220;item&#8221; that is present after yield command.</p>
<p>It&#8217;s custom that you use braces for one-line-blocks, so this will also work: <code>
<pre class="brush: ruby;">[1,2,3,4].each { |x|  puts x }
</pre>
<p></code>In the runtime this will happen:<code>
<pre class="brush: ruby;">def each
  #bla bla loop, fetch item one by one
  puts item  # block
  #bla bla loop again
end
</pre>
<p></code>Point is that it can be <strong>anything </strong>you want so you don&#8217;t have to duplicate iteration logic.
<p>That&#8217;s all for now, next time we&#8217;ll go through using block for comparison. Stay tuned&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2009/05/ruby-blocks-explained-101/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Install TabNav Plugin?</title>
		<link>http://kresimirbojcic.com/2009/04/how-to-install-tabnav-plugin/</link>
		<comments>http://kresimirbojcic.com/2009/04/how-to-install-tabnav-plugin/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 04:33:43 +0000</pubDate>
		<dc:creator>Kresimir Bojcic</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[ROR]]></category>
		<category><![CDATA[tabnav]]></category>
		<category><![CDATA[widgets]]></category>

		<guid isPermaLink="false">http://kresimirbojcic.com/?p=232</guid>
		<description><![CDATA[
If you need tabbed navigation on your pages (well who doesn&#8217;t?   ) please read on about cool plugin for Rails. It&#8217;s work of Italian guy named Paolo Dona. Documentation is a bit scarce, especially for me. But Paolo helped me himself to get it working. 
 Lately plugin is a part of this [...]]]></description>
			<content:encoded><![CDATA[<p><img  style="border-width: 0px; margin: 20px 0px 20px 0px;" src="http://kresimirbojcic.com/images/tabnavplugin/tabnav.jpg" alt="Tabnav in action" />
<p>If you need tabbed navigation on your pages (well who doesn&#8217;t? <img src='http://kresimirbojcic.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) please read on about cool plugin for Rails. It&#8217;s work of Italian guy named <a href="http://www.paolodona.com">Paolo Dona</a>. Documentation is a bit scarce, especially for me. But Paolo <strong>helped me himself</strong> to get it working. </p>
<p><span id="more-232"></span> Lately plugin is a part of this <a href="http://blog.seesaw.it/articles/2007/08/18/tabnav-retires-enter-widgets">widgets collection</a>. It appears as if you have three choices when installing this plugin.</p>
<p>1. SVN<br />
2. Git<br />
3. Manual plugin install</p>
<p>Well, you don&#8217;t, and it costed me a couple of hours to figure out that SVN is <strong>not working</strong> since it&#8217;s not supported by Paolo anymore. For Git you need to do this:
<pre><code>$ cd <yourapp>
$ cd vendor/plugins
$ git clone git@github.com:paolodona/rails-widgets.git
</code></pre>
<p>It did not work for me. I think it&#8217;s related to firewall issues, but since I am new to Git I didn&#8217;t investigate a lot. Third and bullet-proof method is to manually download files, extract it to plugin folder, rename it to let&#8217;s say widgeds, and then apply
<pre><code>rake widgets:setup
</code></pre>
<p>From then on it should work. You need to twaek your tabs.css file, here is what I&#8217;ve ended up with.<br />
<code>
<pre class="brush: css;">.main_tabnav {
      background-color: #FFFFFF;
      padding: 5px;
      height: 2em;
      font-size: 10pt;
}

.main_tabnav ul {
      height: 2em;
      margin: 0;
      padding: 0;
}

.main_tabnav li {
      display: inline;
      line-height: 1.167em;
      padding: 0;
}

.main_tabnav li.secondary {
      float: right;
}

.main_tabnav li a {
      background:#5ec998;
      color: #ebebeb;
      text-decoration: none;
      border: 0.083em solid #FFFFFF;
      border-bottom: 0.083em solid #5ec998;
      padding: 0.5em 0.667em 0.417em 0.5em;
      float: left;
}

.main_tabnav li a.active {
      color: #ebebeb;
      font-weight: bold;
      background:#004000;
      border: 0.083em solid #FFFFFF;
      border-bottom: 0.083em solid #004000;
}

.main_tabnav li a:hover {
      color: #004000;
}

.main_tabnav li a.active:hover {
      color: #ebebeb;
}

.main_tabnav_content {
      background-color: #FFFFFF;
      padding: 1.5em 0.417em;
}
</pre>
<p></code></pre>
]]></content:encoded>
			<wfw:commentRss>http://kresimirbojcic.com/2009/04/how-to-install-tabnav-plugin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
