Hi Railer - Thanks for visiting Fun On Rails !
This blog is to share my working rails experience, ideas, difficulties while developing with ruby and rails. It mainly contains information on ruby, rails, gems, plugins, radiant, jquery, vi-editor, linux and many more fun...
Recent Posts
by sandipransing
We all know that rails models associations gets defined while class definitions are loaded and once defined can't be changed. But still you can make use of block parameter to conditions to have dynamic query conditions inside associations.
Below line explains how to define dynamic associations -
has_one :code_sequence, :class_name => 'Sequence', :conditions => 'kind = "#{self.kind}"'
Please make a note that below code won't be working -
has_one :code_sequence, :class_name => 'Sequence', :conditions => proc { |c| ['kind = ?', c.kind] }
Read More…
by sandipransing
If your application is currently on any version of Rails 2.1.x, The following changes needs to be done for upgrading your application to Rails 2.3.11
1. First install Rails version 2.3.11
gem install rails -v2.3.11
2. Freeze app ruby gems
rake rails:freeze:gems
Hopefully it should work for you but it gave me following error
undefined method `manage_gems' for Gem:Module
3. Create sample rails 2.3.11 app
rails _2.3.11_ testsapp
Now, Copy all missing "config/initializers/*" files from new "testapp to the application that to be upgraded.
cp testapp/config/initializers/* config/initializers
4. Change Rails version inside environment.rb to Rails 2.3.11
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.11'
5. Rename app/controllers/application.rb file to app/controllers/application_controller.rb
OR
rails:update:application_controller
6. Start rails server and fix the issues one by one.
ruby script/server
Read More…
by sandipransing
Basically Enumerable mixin gives collection classes a variety of traverse, search, sort methods.
understanding ruby blocks i.e. proc
blocks are statements of code written in ruby. one can take them as similar to c language macro's
Different ways to define blocks
a = proc do
puts "hello"
end
a.call #=> hello
b = lambda do |u|
puts "hello #{u}"
end
b.call('sandip')#=> hello sandip
c = proc {|user| puts user }
c.call('sandip') #=> sandip
Passing block to enumerator
Lets assume we have collection array of strings and we want to print it
a = ['hi', 'sandip', 'how', 'you', 'doing', '?']
=> ["hi", "sandip", "how", "you", "doing", "?"]
a.each {|w| puts w }
q = proc {|w| puts w }
=> #
a.each(&q) #=>
hi
sandip
how
you
doing
?
a.map{|r| q.call(r)} #=>
hi
sandip
how
you
doing
?
Understanding symbol#to_proc
Symbol has method to_proc which converts symbol to block where symbol is taken as method to be executed on first argument of proc
How to_proc got implemented inside Symbol class
class Symbol
def to_proc
Proc.new { |*args| args.shift.__send__(self, *args) }
end
end
Lets have some examples
v = :even?.to_proc # equivalent to proc {|a| a.even?}
#=> #
q = [1, 2, 3, 5, 67]
q.map(&v)
=> [false, true, false, false, false]
Is there any shortcut?
Yes, there is shortcut to have block passed to enumerators on the fly using ampersand followed by colon (i.e. symbol)
q = [1, 2, 3, 5, 67]
q.map(&:even?) <=> q.map(&:even?.to_proc)
q.map(&:even?.to_proc)
#=> [false, true, false, false, false]
q.map(&:even?)
#=> [false, true, false, false, false]
Some handy examples
[1, 2, 3, 5, 67].inject(&:+) #=> 78
[1, 2, 3, 5, 67].inject(:+) #=> 78
[1, 2, 3, 5, 67].any?(&:even?) #=> true
[1, 2, 3, 5, 67].detect(&:even?) #=> 2
['ruby', 'on', 'rails'].map(&:upcase) #=> ["RUBY", "ON", "RAILS"]
Read More…
by sandipransing
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…
by sandipransing
`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…
by sandipransing
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…
by sandipransing
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…
by sandipransing
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…
by sandipransing
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…
by sandipransing
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…
|