Tuesday, January 31, 2012

dynamic & bounded parameters and named routes

by sandipransing 0 comments
Rails routes can be customized as your own routes with parameters but first you should understand how routes behaves.
Adding dynamic parameters to routes
Here exact parameters are matched to route and presence of each parameter is mandatory in order to construct urls. blank parameter will raise RoutingError exception.
Exact matched named route declared as - match ':a/:b/:c', :to => 'home#index', :as => :q now go to the rails console - ruby-1.9.3-head :005 > app.q_url(:a, :b, :c) => "http://www.example.com/a/b/c" ruby-1.9.3-head :006 > app.q_url(:a, :b, '') ActionController::RoutingError: No route matches {:controller=>"home", :a=>:a, :b=>:b, :c=>""} Bound parameters to named routes
If you are too sure that certain parameter can be blank then you can define it as optional parameter inside route -
match ':a/:b(/:c)', :to => 'home#index', :as => :q rails console ruby-1.9.3-head :010 > app.q_url(:a, :b, '') => "http://www.example.com/a/b?c=" ruby-1.9.3-head :011 > app.q_url(:a, :b) => "http://www.example.com/a/b"
Read More…

puts, to_s and inspect on ruby object

by sandipransing 0 comments
`puts` converts ruby object into string by invoking to_s method on object. The default to_s prints the object's class and an encoding of the object id. In order to print human readable form of object use inspect
locs = Location.find_by_sql('select * from locations') Location Load (0.5ms) select * from locations Puts Object internally invokes to_s method on object to print locs.each do |l| # it calls to_s method on object puts l end #<Location:0x000000055bb328> #<Location:0x000000055bb058>
puts object followed by subsequent invoke of inspect method outputs readable object locs.each do |l| puts l.inspect # prints actual object end #<Location id: 15, name: "Annettaside3", street: "71838 Ritchie Cape", city: "East Destanystad", state: "Utah", zip: "58054", phone: 123456, other_phone: 987654, staff_strength: 40, is_active: true, created_at: "2012-01-25 11:17:26", updated_at: "2012-01-25 11:17:26", country_name: "Korea"> #<Location id: 16, name: "Sporerbury4", street: "73057 Jerad Shoal", city: "South Kyliefurt", state: "Delaware", zip: "46553-3376", phone: 123456, other_phone: 987654, staff_strength: 40, is_active: true, created_at: "2012-01-25 11:24:48", updated_at: "2012-01-25 11:24:48", country_name: "Australia">
Read More…

Friday, January 27, 2012

csv file import / export in rails 3

by sandipransing 0 comments
CSV (comma separated values) files are frequently used to import/export data.
In rails 3, FasterCSV comes as default and below is the way to upload csv files inside rails applications. The code below will also show you how to generate csv in memory, parse on csv data, skip header, iterate over records, save records inside db, export upload error file and many more.
First, View to upload file
= form_tag upload_url, :multipart => true do %label{:for => "file"} File to Upload = file_field_tag "file" = submit_tag Assume upload_url maps to import action of customers controller
Controller code
class CustomersController < ApplicationController [...] def import if request.post? && params[:file].present? infile = params[:file].read n, errs = 0, [] CSV.parse(infile) do |row| n += 1 # SKIP: header i.e. first row OR blank row next if n == 1 or row.join.blank? # build_from_csv method will map customer attributes & # build new customer record customer = Customer.build_from_csv(row) # Save upon valid # otherwise collect error records to export if customer.valid? customer.save else errs << row end end # Export Error file for later upload upon correction if errs.any? errFile ="errors_#{Date.today.strftime('%d%b%y')}.csv" errs.insert(0, Customer.csv_header) errCSV = CSV.generate do |csv| errs.each {|row| csv << row} end send_data errCSV, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=#{errFile}.csv" else flash[:notice] = I18n.t('customer.import.success') redirect_to import_url #GET end end end [...] end Customer model
class Customer < ActiveRecord::Base scope :active, where(:active => true) scope :latest, order('created_at desc') def self.csv_header "First Name,Last Name,Email,Phone,Mobile, Address, FAX, City".split(',') end def self.build_from_csv(row) # find existing customer from email or create new cust = find_or_initialize_by_email(row[2]) cust.attributes ={:first_name => row[0], :last_name => row[1], :email => row[3], :phone => row[4], :mobile => row[5], :address => row[6], :fax => row[7], :city => row[8]} return cust end def to_csv [first_name, last_name, email, phone, mobile, address, fax, city] end end
Export customer records in CSV format
Below code loads customer records from database then generate csv_data inside memory and exports data to browser using send_data method.
Note: As we are not writing on file system hence code can easily work heroku. def export # CRITERIA : to select customer records #=> Customer.active.latest.limit(100) custs = Customer.limit(10) filename ="customers_#{Date.today.strftime('%d%b%y')}" csv_data = FasterCSV.generate do |csv| csv << Customer.csv_header custs.each do |c| csv << c.to_csv end end send_data csv_data, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=#{filename}.csv" end
Read More…

Friday, January 20, 2012

Mongoid embeded_in and Array field management

by sandipransing 0 comments
Previous post explains on mongoid document array field and rails form implementation
Below example shows rails form integration of array field of embedded mongoid document
consider scenario, student embeds one family who has many assets
class Student include Mongoid::Document field :name field :phone embeds_one :family validates_associated :family accepts_nested_attributes_for :family end
class Family include Mongoid::Document ASSETS = ['flat', 'car', 'business', 'bunglow', 'cash'] field :members, type: Integer field :assets, type: Array field :religon embedded_in :student end Brief controller code
class StudentsController < ApplicationController def new @student = Student.new @student.family ||= @student.build_family end def create @student = Student.new(params[:student]) @student.family.assets.reject!(&:blank?) if @student.save [...] else render :action => :new end end end view form will look like-
= form_for(@student) do |s| = s.text_field :name = s.text_field :phone - s.fields_for :family do |f| = f.text_field :members = f.text_field :religion - Family::ASSETS.each do |asset| /Here f.object_name #=> student[family] = f.check_box :assets, :name => "#{f.object_name}[assets][]", asset
Read More…

Thursday, January 19, 2012

mongoid array field and rails form

by sandipransing 0 comments
mongoid document supports array as field. array field in mongoid document is a ruby array but its quite complex to manage array field in rails forms.
After lot of google and reading comments from stack-overflow at last i felt helpless. Finally after doing research on rails form helper object(form_for, fields_for) am pleased to get it working as expected :)
In below example, product can have multiple categories
class Product CATEGORIES = %w(Apparel Media Software Sports Agri Education) include Mongoid::Document field :name, :type => String field :categories, :type => Array end Here is form code
= form_for(@product) do |f| = f.text_field :name - Product::CATEGORIES.each do |category| = f.check_box :categories, :name => "product[categories][]", category
Here is products controller code
class ProductsController < ApplicationController before_filter :load_product, :only => [:new, :create] [...] # We don't need new action to be defined def create @product.attributes = params[:product] # Here we need to reject blank categories @product.categories.reject!(&:blank?) if @product.save flash[:notice] = I18n.t('product.create.success') redirect_to(:action => :index) else render :action => :new end end [...] private def load_product @product = Product.new end end
Read More…

Tuesday, January 17, 2012

twitter-bootstrap form builder for rails

by sandipransing 0 comments
twitter-bootstrap is pluggable css suit provided by twitter.
To know more about how to get started on it click here
Below post will help you out in getting started bootstrap css with rails app. One need to add below files to helpers directory. MainForm can be used as base version of form builder and can be overriden for its subsequent use inside other custom form builders.
1. MainForm
# app/helpers/main_form.rb class MainForm < ActionView::Helpers::FormBuilder # NestedForm::Builder CSS = { :label => 'label-control', :hint => 'hint', :hint_ptr => 'hint-pointer', :error => 'help-inline', :field_error => 'error', :main_class => 'clearfix' } FIELDS = %w(radio_button check_box text_field text_area password_field select file_field collection_select email_field date_select) def main_class(error=nil) return CSS[:main_class] unless error [CSS[:main_class], CSS[:field_error]].join(' ') end def required(name) object.class.validators_on(name).map(&:class).include?(ActiveModel::Validations::PresenceValidator) rescue nil end def cancel(options={}) link = options.fetch(:return, "/") @template.content_tag(:a, "Cancel", :href => link, :class => "btn_form button np_cancel_btn #{options[:class]}") end def submit(value="Save", options={}) options[:class] = "send_form_btn #{options[:class]}" super end def label_class {:class => CSS[:label]} end def label_tag(attribute, arg) # Incase its a mandatory field, the '*' is added to the field. txt = arg[:label] && arg[:label].to_s || attribute.to_s.titleize txt<< '*' if(arg[:required] || required(attribute)) && arg[:required] != false label(attribute, txt, label_class) end def error_tag(method_name, attribute) errs = field_error(method_name, attribute) @template.content_tag(:span, errs.first, :class => CSS[:error]) if errs.present? end def field_error(method_name, attribute) return if @object && @object.errors.blank? return @object.errors[attribute] if method_name != 'file_field' @object.errors["#{attribute.to_s}_file_name"] | @object.errors["#{attribute.to_s}_file_size"] | @object.errors["#{attribute.to_s}_content_type"] end def hint_tag(txt) hintPtr = @template.content_tag(:span, '', :class => CSS[:hint_ptr]) hintT = @template.content_tag(:span, txt + hintPtr, {:class => CSS[:hint]}, false) end def spinner_tag @template.image_tag('spinner.gif', :class => :spinner,:id => :spinner) end end ZeroForm is custom form builder which is inherited from main_form and its going to be actually used inside forms. Feel free to make custom form related changes inside this
ZeroForm
cat app/helpers/zero_form.rb class ZeroForm < MainForm # Overridden label_class here as we dont need class to be applied def label_class {} end def self.create_tagged_field(method_name) define_method(method_name) do |attribute, *args| arg = args.last && args.last.is_a?(Hash) && args.last || {} # Bypass form-builder and do your own custom stuff! return super(attribute, *args) if arg[:skip] && args.last.delete(:skip) errT = error_tag(method_name, attribute) labelT = label_tag(attribute, arg) mainT = super(attribute, *args) baseT = @template.content_tag(:div, mainT + errT) hintT = hint_tag(arg[:hint]) if arg[:hint] spinnerT = spinner_tag if arg[:spinner] allT = labelT + baseT + spinnerT + hintT @template.content_tag(:div, allT, :class => main_class(errT)) end end FIELDS.each do |name| create_tagged_field(name) end end
In order to use Nested Forms you need to extend MainForm with NestedForm Builder
Integrate NestedForm with FormBuilder class MainForm < NestedForm::Builder end View Form
= form_for @address ||= Address.new, :builder => ZeroForm do |f| = f.text_field :street_address = f.text_area :detail_address, :rows => 2 = f.text_field :city = f.select :state, %w(US IN AUS UK UKRAINE) = f.submit 'Save & Continue', :class => 'btn primary' = link_to 'Skip »', '#'
To know more on twitter-bootstrap pagination in rails click here
Read More…

Get models list inside rails app

by sandipransing 0 comments
How to get collection of models inside your application. Certainly there are many ways to do it.
Lets have a look at different ways starting from worst -
Get table names inside database and then iterating over to get model name @models = ActiveRecord::Base.connection.tables.collect{|t| t.underscore.singularize.camelize} #=> ["AdhearsionAudit", "AudioLog", "AuditDetail","TinyPrint", "TinyVideo", "UnknownCall", "UserAudit", "User"]
Select those with associated class
@models.delete_if{|m| m.constantize rescue true}
Load models dir
@models = Dir['app/models/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize.name } Select ActiveRecord::Base extended class only @models.reject!{|m| m.constantize.superclass != ActiveRecord::Base } Get Active Record subclasses
# make sure relevant models are loaded otherwise # require them prior # Dir.glob(RAILS_ROOT + '/app/models/*.rb').each { |file| require file } class A < ActiveRecord::Base end class B < A end ActiveRecord::Base.send(:subclasses).collect(&:name) #=> [...., A] How to get Inherited models too
class A < ActiveRecord::Base end class B < A end ActiveRecord::Base.descendants.collect(&:name) #=> [...., A, B] Below is more elegant solution provide by Vincent-robert over stack overflow which recursively looks for subsequent descendent's of class and gives you list from all over application
class Class def extend?(klass) not superclass.nil? and ( superclass == klass or superclass.extend? klass ) end end def models Module.constants.select do |constant_name| constant = eval constant_name if not constant.nil? and constant.is_a? Class and constant.extend? ActiveRecord::Base constant end end end
Read More…

Sunday, January 15, 2012

stripe gateway payment integration with rails

by sandipransing 0 comments
Stripe is simple website payment solution and its very easy to easy setup
It currently supports only in US and seems to be very popular compared to other payment gateways because of its api & pricing

Stripe API provides -
1. charge (regular payments)
2. subscription (recurring payments)
3. managing customers (via stripe_customer_token)

What you need to do ?
Create a stripe account by providing email address and password. There after go to the manage account page to obtain stripe public & api keys.
Rails Integration
# Gemfile gem stripe
# config/initializers/stripe.rb Stripe.api_key = "rGaNWsIG3Gy6zvXB8wv4rEcizJp6XjF5" STRIPE_PUBLIC_KEY = "vk_BcSyS2qPWdT5SdrwkQg0vTSyhZgqN"
# app/views/layouts/application.html.haml = javascript_include_tag 'https://js.stripe.com/v1/' = tag :meta, :name => 'stripe-key', :content => STRIPE_PUBLIC_KEY
Payment Form
# app/views/payments/new.html.haml #stripe_error %noscript JavaScript is not enabled and is required for this form. First enable it in your web browser settings. = form_for @payment ||= Payment.new, :html => {:id => :payForm} do |p| = p.hidden_field :stripe_card_token .field = p.text_field :amount .credit_card_form %h3.title Enter Credit Card - if @payment.stripe_card_token.present? Credit card has been provided. - else .field = label_tag :card_number, "Credit Card Number" = text_field_tag :card_number, nil, name: nil .field = label_tag :card_code, "Security Code (CVV)" = text_field_tag :card_code, nil, name: nil .field = label_tag :card_month, "Expiry Date" = select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} = select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}
Javascript Code
# app/views/payments/new.js var payment; jQuery(function() { Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')); return payment.setupForm(); }); payment = { setupForm: function() { $('.head').click(function() { $(this).css('disabled', true); if($('#payment_stripe_card_token').val()){ $('#payForm').submit(); } else{ payment.processCard(); } }); }, processCard: function() { var card; card = { number: $('#card_number').val(), cvc: $('#card_code').val(), expMonth: $('#card_month').val(), expYear: $('#card_year').val() }; return Stripe.createToken(card, payment.handleStripeResponse); }, handleStripeResponse: function(status, response) { if (status === 200) { $('#payment_stripe_card_token').val(response.id) $('#stripe_error').remove(); $('#payForm').submit(); } else { $('#stripe_error').addClass('error').text(response.error.message); $('.head').css('disabled', false); } } };
Generate & Migrate Payment Model
rails g model payment status:string amount:float email:string transaction_number:string rake db:migrate
Payment Model
# app/models/payment.rb class Payment < ActiveRecord::Base PROCESSING, FAILED, SUCCESS = 1, 2, 3 attr_accessible :stripe_card_token validates :amount, :stripe_card_token, :presence => true, :numericality => { :greater_than => 0 } def purchase self.status = PROCESSING customer = Stripe::Customer.create(description:email, card: stripe_card_token) # OPTIONAL: save customer token for further reference stripe_customer_token = customer.id # Charge charge = Stripe::Charge.create( :amount => amount * 100, # $15.00 this time :currency => "usd", :customer => stripe_customer_token ) if charge.paid self.transaction_num = charge.id self.status = SUCCESS else self.status = FAILED end return self rescue Exception => e errors.add :base, "There was a problem with your credit card." self.status = FAILED return self end end
Payments Controller
# app/controllers/payments_controller.rb class PaymentsController < ApplicationController def create @payment = Payment.new(params[:payment]) if @payment.valid? && @payment.purchase flash[:notice] = 'Thanks for Purchase!' redirect_to root_url else render :action => :new end end end
Read More…

Saturday, January 14, 2012

understanding rails uri

by sandipransing 0 comments
rails-uri module provide us with url manipulation methods
Parse string url url = URI.parse('http://funonrails.com/search/label/rails3') url.host #=> "http://funonrails.com" url.port #=> 80
URL with Basic Authentication
url = URI.parse('http://sandip:2121@funonrails.com/search/label/rails3') url.user #=> "sandip" url.password #=> "2121"
Extracting urls form string paragraph
URI.extract('http://funonrails.com is rails blog authored by http://sandipransing.github.com contact mailto://sandip@funonrails.com') #=> ["http://funonrails.com", "http://sandipransing.github.com", "mailto://sandip@funonrails.com"] Split & Join URI
URI.split('http://sandip:2121@funonrails.com/search/label/rails3') #=> ["http", "sandip:2121", "funonrails.com", nil, nil, "/search/label/rails3", nil, nil, nil] <=> [Scheme, Userinfo, Host, Port, Registry, Path, Opaque, Query, Fragment] URI.join('http://funonrails.com','search/label/rails3') #=> #
Escape & Unescape alias encode/decode URI
URI.escape('http://funonrails.com/search/?label=\\rails\3') URI.encode('http://funonrails.com/search/?label=\\rails\3') #=> "http://funonrails.com/search/?label=%5Crails%5C3" URI.unescape("http://funonrails.com/search/?label=%5Crails%5C3") URI.decode("http://funonrails.com/search/?label=%5Crails%5C3") #=> "http://funonrails.com/search/?label=\\rails\\3"
Match urls using regular expressions
"http://funonrails.com/search/label/rails3".sub(URI.regexp(['search'])) do |*matchs| p $& end #=> "http://funonrails.com/search/label/rails3"
Getting requested url inside rails
request.request_uri request.env['REQUEST_URI']
Getting previous page url inside rails
request.referrer
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