by sandipransing
rails has built in number_to_currency helper which takes options like unit, delimeter, seperator which displays foreign currency correctly but somehow it is not best suited for indian currency.
Below is how we managed 2 years ago to display indian currency formatted properly with comma as seperator. personally i think it could be more better than what it is currently ;)
Number to indian currency(rupees) helper
module ApplicationHelper
def number_to_indian_currency(number)
if number
string = number.to_s.split('.')
number = string[0].gsub(/(\d+)(\d{3})$/){ p = $2;"#{$1.reverse.gsub(/(\d{2})/,'\1,').reverse},#{p}"}
number = number.gsub(/^,/, '') + '.' + string[1] if string[1]
# remove leading comma
number = number[1..-1] if number[0] == 44
end
"Rs.#{number}"
end
Sample Output for different combinations
>> helper.number_to_indian_currency(2000)
=> "Rs.2,000"
>> helper.number_to_indian_currency(2040)
=> "Rs.2,040"
>> helper.number_to_indian_currency(2040.50)
=> "Rs.2,040.5"
>> helper.number_to_indian_currency(2040.54)
=> "Rs.2,040.54"
>> helper.number_to_indian_currency(1222040.54)
=> "Rs.12,22,040.54"
After doing google today found from Piyush Ranjan's Blog that yes there are ways to optimize code.
Optimized Version
module ApplicationHelper
def number_to_indian_currency(number)
"Rs.#{number.to_s.gsub(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/, "\\1,")}"
end
end
Waw one line of code, Look at the beauty of regular expression :) Truely amazing !
Integrating Webrupee symbol
First include follwing stylesheet in your layout
//public/stylesheets/font.css
@font-face {
font-family: "WebRupee";
font-style: normal;
font-weight: normal;
src: local("WebRupee"), url("http://cdn.webrupee.com/WebRupee.V2.0.ttf") format("truetype"), url("http://cdn.webrupee.com/WebRupee.V2.0.woff") format("woff"), url("http://cdn.webrupee.com/WebRupee.V2.0.svg") format("svg");
}
.WebRupee {
font-family: 'WebRupee';
}
Improved Version of Helper
module ApplicationHelper
def number_to_indian_currency(number, html=true)
txt = html ? content_tag(:span, 'Rs.', :class => :WebRupee) : 'Rs.'
"#{txt} #{number.to_s.gsub(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/, "\\1,")}"
end
end
Usage
>> helper.number_to_indian_currency(400)
=> "<span class="WebRupee">Rs.</span> 400"
>> helper.number_to_indian_currency(5921, false)
=> "Rs. 5,921"
>> helper.number_to_indian_currency(9921)
=> "<span class="WebRupee">Rs.</span> 9,921"
This will show you rupees symbol on your webpages.
Read More…
by sandipransing
Methods defined in Helper modules can directly accessed in rails views because this is what they are pretended for but we often come across with situations where we wanted to use some helper methods in controllers, views, models and mailers and obvious we don't want to repeat same lines of code everywhere which also rail does not permit. forgotten DRY? Oh then how to do achieve same without violating rails principle.
Certainly there are ways to do this ..
1. Helper methods all the time for views
class ApplicationController < ActionController::Base
helper :all# include all helpers, all the time for views
end
2. Controller methods in views
class ApplicationController < ActionController::Base
helper_method :current_store
#now controller_method can be accessed in views
end
2. Helper methods in controller
class ApplicationController < ActionController::Base
include ActionView::Helpers::ApplicationHelper
end
3. Helper methods in model
class Student < ActiveRecord::Base
include ActionView::Helpers::ApplicationHelper
end
4. Helper methods in mailer
class Notifier < ActionMailer::Base
add_template_helper(ApplicationHelper)
#...
end
Read More…
by sandipransing
To render view from another controller
# In rail 2.3
render "controller/action"
# In rails 2.2 or below
render :template => 'controller/action'
To render partial from another controller's views folder
render :partial => "controller/partial"
Cheers ! Sandip
Read More…
|