Rashmi Yadav

Programmer

Background Processing Using Rails Runner

Rails comes with a built-in tool for running tasks independent of the web cycle. The rails runner command simply loads the default Rails environment and then executes some specified Ruby code.

Read on →

Rails 3 Responders

So we have a problem here in Controllers. As our application grows our controllers grows. We will be in trouble when we have alternative formats in our controllers like JSON, XML.

Read on →

OAuth with OmniAuth and Github

If you want to authenticate your user in your app with Github  use omniauth-github strategy for that. Here I am describing how you can do that very quickly. OmniAuth provides list of  strategies to use many OAuth for your application. Here is the  List of Strategies.

Read on →


Proc call in ruby1.9.3

Call a proc in different ways

1
2
3
my_proc = Proc.new do|name|
  "this is #{name}"
end

READMORE You can call this proc by 4 ways

1
2
3
4
my_proc.call('rays')
my_proc.('rashmi')
my_proc.("rita")
my_proc === "richa"

Asset Pipeline with Rails 3.1

This is the new very interesting and useful feature in Rails 3.1.

Asset Pipeline

  • The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets.
  • It allows you to write these assets in other languages like CoffeeScript, Sass and ERB
  • Asset pipeline helpful in production environment cause it will reduce the number of request which browser make.
Read on →

jQuery Date/Time picker

To handle date and time both in calender use jQuery date time picker add-on

you need to inlcude javascript files

1
2
3
jquery-1.4.1.min
jquery-ui-1.8.9.custom.min
jquery-ui-timepicker-addon

Inclue CSS files

1
jquery-ui-1.8.9.custom
1
2
3
4
<%#= text_field_tag ' name','',:class => 'date_value'%>
jQuery('.'date_value).datetimepicker({
  dateFormat: 'MM dd,yy'
});

Twitter Client script to retrieves all the unique http links in the last recent tweets

Code to fetch link from tweets matched by given hash_tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
require 'rubygems'
require 'twitter'


hash_tag = ARGV.first

if hash_tag.nil?
  puts "Please provide tag you want to fetch. Example 'rails', '#rails'"
  exit
end
puts 'fetching......'
begin
  tweets = Twitter::Search.new.hashtag(hash_tag)
rescue Exception => e
  # Time out or any other error can occur here
  puts "Errors in fetching tweets #{e}"
end
links = []
tweets.each do |tweet|
  links << tweet.text.match(/\bhttps?:\/\/\S+\b/)
end

links = links.uniq
puts links

Include Class Methods and Validation Using Module in RubyOnRails

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class User
  include TestModule
end

module TestModule
  def test_instance_method
    puts "Test Method"
  end

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def my_class_method
      puts "Class Methods"
    end
  end
end

Here my_class_method is Class Method.we can define more class methods in module ClassMethods

Validations and call backs are called without prefix ‘self’. so for validations and call backs we have to explicitly set the class

1
2
3
4
5
6
module TestModule
  def self.included(base)
    base.validates_presence_of :bar
    base.before_save :some_method
  end
end

Routes in Rails3

Rails3 define routes in more efficient way

Routes for CRUD actions

1
     resources   :users

Also define for multiple resources

1
     resources :users,  :blogs, :books

Nested Routes

1
2
3
  resources :users do
    resources  :blogs
  end

Member and collection

1
2
3
4
  resources :blogs do
    :get => :preview, :on =>:member
    :get => :list, :on =>:collection
  end

if  we have more than one collection or member functions

1
2
3
4
5
6
7
8
9
10
resources  :blogs do
    member do
     :get  :preview
     :put :sort
    end
   collection do
     :get :list
     :get :detailed_info
   end
end

Named Routes

1
  match "history" => "site#index", :as => :history

Adding :as makes it a named route so that we can use history_path or history_url in our application.

Route for Root

1
  root   :to => "home#index"

Constraints and Parameters in routes

1
  match "search/:email(/:first_name/:last_name)" => "users#search",  :constraints => {:email => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i}

Here email is mandatory parameter and first_name and last is optional parameter and in constraints email format is defined