Tips – Use Named Routes outside of Controller and View

RubyOnRails,Tips,ruby 27 四月 2011 | View Comments

class UrlWriterSingleton
  include Singleton
  include ActionController::UrlWriter

  def self.default_url_options
    {:host => 'www.seravia.com'}
  end
end

module NameRouteHelper
  def self.included(base)
    base.send(:include, InstanceMethods)
  end

  module InstanceMethods
    def name_path_for(name, options = {})
      UrlWriterSingleton.instance.send("#{name}_path", options)
    end
  end
end

Usage:

Class TestNamedRouteHelper
  include NameRouteHelper
end

TestNamedRouteHelper.new.name_path_for("root")


Tagged in , ,

Rspec Tips – Implicit Subject “its”

ruby 7 四月 2011 | View Comments

Rspec has a convenient way to write simple test

describe [1, 2, 3, 3] do
  its(:size) { should == 4 }
  its("uniq.size") { should == 3 }
end


Tagged in , ,

Tips – How to Get Ruby Bin Path using RbConfig::CONFIG

RubyOnRails 18 十二月 2010 | View Comments

ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME'))
#=> /Users/allen/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby
You can see, there are lot’s of useful info in Constant RbConfig::CONFIG

Tagged in , ,

Tips – How Many Argument a Method Has

RubyOnRails 10 六月 2010 | View Comments

Find a method in arity

Here is doc from ruby doc

Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. For methods written in C, returns -1 if the call takes a variable number of arguments.

  class C
     def one;    end
     def two(a); end
     def three(*a);  end
     def four(a, b); end
     def five(a, b, *c);    end
     def six(a, b, *c, &d); end
   end
   c = C.new
   c.method(:one).arity     #=> 0
   c.method(:two).arity     #=> 1
   c.method(:three).arity   #=> -1
   c.method(:four).arity    #=> 2
   c.method(:five).arity    #=> -3
   c.method(:six).arity     #=> -3

   "cat".method(:size).arity      #=> 0
   "cat".method(:replace).arity   #=> 1
   "cat".method(:squeeze).arity   #=> -1
   "cat".method(:count).arity     #=> -1

Tagged in ,

Tips – Set Default Value for A Hash

RubyOnRails,Tips 10 六月 2010 | View Comments

Can hash have a default value, when it doesn’t have a key?

Answer is yes!

h = Hash.new("default")
h["wrong_value"] # => "default"

Tagged in , ,

Tips – Print test name and file name when testing (Final Solution)

RubyOnRails,Tips 13 五月 2010 | View Comments

As previous solution doesn’t works, after read source code of ActiveSupport::TestCase I find a final solution.

class ActiveSupport::TestCase
  setup :log_test
  private
  def log_test
    unless @already_logged_this_test  
      puts "\n\nStarting #{@method_name} in class:#{self.class.name}\n#{'-' * (20 + @method_name.length + self.class.name.length)}\n"  
    end  
    @already_logged_this_test = true

  end

setup is a callback of ActiveSupport::TestCase. So we doesn’t hack anything. It works fine.

And what’s more rake test already support verbose model

  rake test:units TESTOPTS="-v"

if you using run test directly like this, TESTOPTS="-v" doesn’t work

ruby -Itest test/units/some_test.rb

Tagged in , ,

Tips – Ruby Get Current File Name

RubyOnRails,Tips 11 五月 2010 | View Comments

How can I know what’s the file name of current ruby file?

Use __FILE__

#file /home/allen/test.rb

puts __FILE__ # => /home/allen/test.rb

# get file name
File.basename(__FILE__) # => "test.rb"
# exclude suffix
File.basename(__FILE__,".rb") # => "test"

#dir name
File.dirname(__FILE__) # => "/home/allen/"

Or

#file /home/allen/test.rb

puts __FILE__ # => /home/allen/test.rb

# get file name
Pathname.new(__FILE__).basename # => "test.rb"
# exclude suffix
Pathname.new(__FILE__).basename(".rb") # => "test"

#dir name
Pathname.new(__FILE__).dirname # => "/home/allen/"

For more API checkout Pathname

Tagged in ,

Tips – Merge ActiveRecord Condtions

RubyOnRails,Tips 1 四月 2010 | View Comments

Assuming We have Member model.
And one record with name Hnery Miller, with state draft

I have two conditions:

condition1 = ['name like ?','Hnery%']
condition2 = ['state = ?','draft']

What I gonna do is merge these two conditions, using Rails ActiveRecord buildin method merge_conditions

Member.first(:conditions => Member.merge_conditions(condition1,condition2))

Tagged in , ,

Tips – Use ActionController helper_method,hide_action methods

RubyOnRails,Tips 14 二月 2010 | View Comments

  • hide_action: hide a method in controller
  • helper_method: use this method as helper method, you can call this method in view
class BookController < ActionController::Base

  def category_name

    "RubyOnRails"

  end

  hide_action :category_name #you can't visit action category_name.

  helper_method :category_name #you can call 'category_name' in view

end

Tagged in , ,

Ads Plugin created by Jake Ruston's Wordpress Plugins - Powered by and football database.