Thursday, March 31, 2011

Setting up nginx maximum upload size

by sandipransing 0 comments
Edit nginx configuration and look for html block.
Inside html block add following line.
http { include conf/mime.types; default_type application/octet-stream; client_max_body_size 10m; .... }
In above configuration "application/octet-stream" supports any kind of file upload.
Read More…

Thursday, March 17, 2011

Twitter share and facebook like button for haml-rails, html/erb

by sandipransing 0 comments
Twitter share and facebook like button for html/erb
<div class='spread'>  <div class='twshare left'>   <a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="funonrails">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>  </div>  <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="" layout="button_count" show_faces="false" width="450" font=""></fb:like> </div> Twitter share and facebook like button for haml
.spread .twshare.left %a.twitter-share-button.left{"data-count" => "horizontal", "data-via" => "fuonrails", :href => "http://twitter.com/share"} Tweet %script{:src => "http://platform.twitter.com/widgets.js", :type => "text/javascript"} .fshare.left{:style => 'padding-left: 0px; margin-left: 10px;'} %script{:src => "http://connect.facebook.net/en_US/all.js#xfbml=1"} %fb:like{:layout => "button_count", :show_faces => "false", :width => "450"} .CLR Sample Buttons
Read More…

number to indian currency helper for rails with WebRupee

by sandipransing 0 comments
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…

Accessing View Helpers & Routes in Rails Console, Rake Tasks and Mailers

by sandipransing 0 comments
While looking for accessing rails template tags to be accessed on rails console in order to examine whatever written needs to be correct, found that - rails template tags can be tested on rails console using helper object
module ApplicationHelper def display_amount(amount) number_to_currency amount, :precision => 0 end end Helpers on rails console
rails c >> helper.text_field_tag :name, 'sandip' => "<input id="name" name="name" type="text" value="sandip" />" >> helper.link_to 'my blog', 'http://funonrails.com' => "<a href=''http://funonrails.com>my blog</a>" >> helper.display_amount(2000) => "$2,000" isn't cool ? though it's not finished yet. we can include helper files on console and access custom helper methods too
>> include ActionView::Helpers => Object >> include ApplicationHelper => Object >> include ActionView::Helpers::ApplicationHelper^C >> display_amount 2500 => "$2,500" Using same way one can make use helpers in rake tasks file. Make sure environment is getting loaded into rake task.
# lib/tasks/helper_task.rake namespace :helper do desc 'Access helper methods in rake tasks' task :test => :environment do include ActionView::Helpers include ApplicationHelper puts display_amount 2500 #=> "$2,500" end end Accessing helpers in Action Mailers
class Notifier < ActionMailer::Base add_template_helper(ApplicationHelper) def greet(user) subject "Hello #{user.name}" from 'no-reply@example.com' recipients user.email sent_on Time.now @user = user end # app/views/notifier/greet.html.erb Hi <%= @user.try(:name)%>,

Greetings !!! You have got <%= display_amount(3000) %>
Rails routes on rails console
rails c >> app.root_url => "http://www.example.com/" >> app.change_password_path => "/change/password" >> app.change_password_url => "http://www.example.com/change/password" Now you may have got requirement to access routes inside rake tasks. Here is the way to do that-
# lib/tasks/route_test.rake namespace :routes do desc 'Access helper methods in rake tasks' task :test => :environment do include ActionController::UrlWriter default_url_options[:host] = "myroutes.check" puts root_url #=> "http://myroutes.check" end end Optionally you can set value of host parameter from action mailer host configuration using
default_url_options[:host] = ActionMailer::Base.default_url_options[:hos]
Attention: This is my preferred way to use helpers and routes when needed and there may have other choices to do same. Anyone having better approach please feel free to add your valuable comment. I will definitely incorporate that in further developments.
Got Easy, Cheers ;)
Read More…

Tuesday, March 1, 2011

Monitor Delayed Job in rails

by sandipransing 0 comments
Delayed Job & Monit configuration
We were struggling through how to monit delayed_job from past few months because monit doesn't work seamlessly with delayed_job start/stop commands and finally we got able to monit delayed_job.
Here is our old configuration that wasn't working anyhow-
check process delayed_job with pidfile /home/sandip/shared/pids/delayed_job.pid stop program = "/bin/bash -c 'cd /home/sandip/current && RAILS_ENV=production script/delayed_job stop'" start program = "/bin/bash -c 'cd /home/sandip/current && RAILS_ENV=production script/delayed_job start'" if totalmem > 100.0 MB for 3 cycles then restart if cpu usage > 95% for 3 cycles then restart
After doing google & looking at stackoverflow, we found different solutions to work with but none of them found useful to me. :(
After reading google group someone (not remembering exactly) directed to write a init script for delayed_job server and that perfectly worked for me and my headache of self moniting delayed_job ended up ;)
Here is delayed_job init script
## /etc/init.d/delayed_job
#! /bin/sh set_path="cd /home/sandip/current" case "$1" in start) echo -n "Starting delayed_job: " su - root -c "$set_path; RAILS_ENV=production script/delayed_job start" >> /var/log/delayed_job.log 2>&1 echo "done." ;; stop) echo -n "Stopping delayed_job: " su - root -c "$set_path; RAILS_ENV=production script/delayed_job stop" >> /var/log/delayed_job.log 2>&1 echo "done." ;; *) echo "Usage: $N {start|stop}" >&2 exit 1 ;; esac exit 0 finally here is the working monit delayed_job configuration
check process delayed_job with pidfile /home/sandip/shared/pids/delayed_job.pid stop program = "/etc/init.d/delayed_job stop" start program = "/etc/init.d/delayed_job start" if totalmem > 100.0 MB for 3 cycles then restart if cpu usage > 95% for 3 cycles then restart Thinking Sphinx monit configuration
check process sphinx with pidfile /home/sandip/shared/pids/searchd.pid stop program = "/bin/bash -c 'cd /home/sandip/current && /usr/bin/rake RAILS_ENV=production ts:stop'" start program = "/bin/bash -c 'cd /home/sandip/current && /usr/bin/rake RAILS_ENV=production ts:start'" if totalmem > 85.0 MB for 3 cycles then restart if cpu usage > 95% for 3 cycles then restart Adhearsion (ahn) monit confiuration
check process ahn with pidfile /home/josh/shared/pids/ahnctl.pid stop program = "/bin/bash -c 'cd /home/sandip/current && /usr/bin/ahnctl stop adhearsion'" start program = "/bin/bash -c 'cd /home/sandip/current && /usr/bin/ahnctl start adhearsion'" if totalmem > 100.0 MB for 3 cycles then restart if cpu usage > 95% for 3 cycles then restart Nginx monit configuration
check process nginx with pidfile /opt/nginx/logs/nginx.pid start program = "/opt/nginx/sbin/nginx" stop program = "/opt/nginx/sbin/nginx -s stop" if cpu is greater than 70% for 3 cycles then alert if cpu > 80% for 5 cycles then restart if 10 restarts within 10 cycles then timeout
Read More…

Getting started with rails 3 & postgres database

by sandipransing 0 comments
Rails 3 Installation
sudo gem install rails -v3.0.4 postgres as db installation<>br/ $ sudo apt-get install postgresql
Rails 3 App with postgres as database
$ rails new pg -d postgres bundle installation
It will install dependency gems & postgres adapter for db connection
bundle install
Here by default 'postgres' database user gets created while installation but i recommend to create new db user with name same as of system user owning project directory permissions.
User owning file permission can be found using
ls -l pg drwxr-xr-x 7 sandip sandip 4096 2011-02-23 15:38 app drwxr-xr-x 5 sandip sandip 4096 2011-02-23 18:14 config -rw-r--r-- 1 sandip sandip 152 2011-02-23 15:38 config.ru ... Default 'postgres' database user gets created while installation
## Snap of database.yml development: adapter: postgresql encoding: unicode database: pg_development username: sandip pool: 5 Create new database user
pg $ sudo su postgres pg $ createuser sandip Shall the new role be a superuser? (y/n) y pg $ exit exit
Create a first development database:
pg $ psql template1 Welcome to psql 8.4.6, the PostgreSQL interactive terminal. ... template1=# \l List of databases Name | Owner | Encoding -----------+----------+---------- postgres | postgres | UTF8 template0 | postgres | UTF8 template1 | postgres | UTF8 (3 rows) template1=# CREATE DATABASE pg_development; CREATE DATABASE template1=# \l List of databases Name | Owner | Encoding -------------------+----------+---------- postgres | postgres | UTF8 pg_development | sandip | UTF8 template0 | postgres | UTF8 template1 | postgres | UTF8 (4 rows) template1=# \q
Start rails server
pg $ rails s Getting hands on postgres terminal
1. Login onto terminal
psql -U DB_USERNAME -d DB_NAME -W DB_PASSWORD 2. List databases
\l 3. Display tables
\dt 4. Exit from terminal
\q
Read More…

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter