<?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>Ruby on Rails Notes &#187; money</title> <atom:link href="http://railsnotes.com/tag/money/feed/" rel="self" type="application/rss+xml" /><link>http://railsnotes.com</link> <description>A code-heavy brain dump of stuff I come across working on Ruby on Rails projects including Models, ActiveRecord, Views, Controllers, RESTful rails, deployment, server stuff, etc.</description> <lastBuildDate>Sun, 29 Aug 2010 23:45:16 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Number to Currency Inside Models</title><link>http://railsnotes.com/110-number-to-currency-inside-models/</link> <comments>http://railsnotes.com/110-number-to-currency-inside-models/#comments</comments> <pubDate>Fri, 03 Apr 2009 09:13:05 +0000</pubDate> <dc:creator>Melvin Ram</dc:creator> <category><![CDATA[Ruby & Rails Core]]></category> <category><![CDATA[models]]></category> <category><![CDATA[money]]></category><guid
isPermaLink="false">http://railsnotes.wordpress.com/?p=110</guid> <description><![CDATA[I was working on something where I needed to use number_to_currency inside a model. I wanted @service.price to use the amount &#38; setup_amount fields to output something like this: &#8220;$120.00 setup + $23/month&#8221; This is a problem since number_to_currency is part of the ActionView module and not available inside models. Some say that it violates [...]]]></description> <content:encoded><![CDATA[<p></p><p>I was working on something where I needed to use number_to_currency inside a model. I wanted @service.price to use the amount &amp; setup_amount fields to output something like this: &#8220;$120.00 setup + $23/month&#8221;</p><p>This is a problem since number_to_currency is part of the ActionView module and not available inside models. Some say that it violates MVC principles since in a way, you&#8217;re messing with view-related code inside a model. That part is up for debate. In anycase, ravocx from the rubyonrails IRC channel put together a small snippet that extends Fixnum so you can call some_number.to_currency on it.</p><p>Here&#8217;s how it works:</p><ol><li>Make a folder RAILS ROOT/lib/core_extensions</li><li>In there, create a file named fixenum_extensions.rb</li><li>Copy/paste the code below into that file.</li><li>In your environment.rb file, put this line:</li><pre>RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)</pre><li>And this line:</li><pre>require "#{RAILS_ROOT}/lib/core_extensions/fixnum_extensions"</pre><li>And restart your application</li></ol><p>###</p><pre>require 'rubygems'
require 'action_view'

class Fixnum
  def to_currency(options = {})
    ActionView::Base.new.number_to_currency(self, options)
  end
end</pre><p>###</p><p>For my purpose, I used end deciding to leave the code in the helper for now but I may move to this method later.</p><p>DISCLAIMER: If you do use this approach, you might consider moving this code into a module and using that module to extend Fixnum.</p> ]]></content:encoded> <wfw:commentRss>http://railsnotes.com/110-number-to-currency-inside-models/feed/</wfw:commentRss> <slash:comments>1</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 (user agent is rejected)
Database Caching 4/16 queries in 0.039 seconds using disk

Served from: railsnotes.com @ 2010-09-10 05:46:44 -->