Flowdock

Recent notes

RSS feed
June 1, 2011 - (>= v3.0.0)
0 thanks

Catching rollback and re-raise exception

In response to wiseleyb, I don’t believe that you could put “rescue” in a transaction block, let alone catching ActiveRecord::Rollback. It would lead you to an “unexpected kRESCUE” error.

I think this is more appropriate.

def start_transaction
  Company.transaction do
    # don't forget the bang to make sure it raise
    # exception or the transaction won't rollback
    user.save!
    company.save!
    x=1/0

    return true
  end

  # re-raise exception here
  raise "Exception!"
end

Then you could call the method in another place, and it would raise rollback and other exception.

...
  # would return the "Exception!" if rollback occurs
  # it would also still trigger another exception other
  # than rollback.
  start_transaction
...
May 31, 2011 - (>= v3.0.0)
0 thanks

return random element

>> a = [1,2,3] >> a.sample 2

the same as

>> a[rand(a.length - 1)] 1

May 26, 2011
0 thanks

Don't Use to_formatted_s(:db) on an Array of IDs

I thought using to_formatted_s(:db) on an array of ids would separate them with commas in a nice way. Wrong. It does, but it also changes the numbers.

Wrong

[60, 271, 280, 283].to_formatted_s(:db)
# => "121,543,561,567"    # Completely different numbers!

Instead, use the join method:

Right

[60, 271, 280, 283].join(",")
# => "60,271,280,283"      # Much better

I think this has to do with (:db) being used for formatting dates but I’m not sure.

May 24, 2011
2 thanks

must be first

A little gotcha I ran into.

This must be defined before you define your relationships.

ie:

class Ticket < ActiveRecord::Base
  set_table_name 'ticket'
  belongs_to :customer
  has_one :account, :through => :customer
end

if you do the relationships first it will not use the correct table name on the lookups.

May 23, 2011 - (>= v3.0.5)
5 thanks

Change to the way the block is handled

At least in 3.0.5, some of the previous examples no longer work: ActionView seems to quietly ignore Array content.

If you were using code of the form

content_tag(:li, nil, :class => 'someClass') {
  arr.collect { |x|
    content_tag(:ul, x)
  }
}

it now needs to look like

content_tag(:li, nil, :class => 'someClass') {
  arr.reduce('') { |c, x|
    c << content_tag(:ul, x)
  }.html_safe
}
May 20, 2011 - (>= v3.0.0)
0 thanks

relative_url_root= no longer exists in Rails 3.0

Rails 3.0 does this with scope in config/routes.rb

scope "/exampleapp" do
  resources :examples
  root :to => "examples#index"
end
May 6, 2011
0 thanks

Asserting Emails

–setup_mailer.rb in config/initializers –rails g mailer MailerName –methods in mailer_name.rb –files in views/mailer_name –MailerName.method(@object).deliver in object controller

May 6, 2011
0 thanks

Using OpenID

–rails g nifty:authentication –redo rake db:migrate –add: before_filter :login_required, :except => :index to appropriate controllers.

May 6, 2011
0 thanks

Using New

-rails new ProjectName -switch directories -git repo “git init”, then commit -add gems, bundle install -rails g scaffold TableName attribute:type attribute:type….. -authentication -add attribute_accessible/relationships/validations -get rid of index.html in public, set root “table#index” -rails g nifty:layout -rake db:migrate -git repo -rails s, see if works -change id’s to names in show and indexes -images go to public/images -partials are rendered in application.html.erb

April 28, 2011
1 thank

Backport from 1.9

Below is a backport of the Ruby 1.9 implementation (minus some encoding stuff). The main thing this provides you is the ability to say :foo => [‘bar’, ‘baz’] and have that turn into foo=bar&foo=baz (i.e. multiple values for the same key).

Just require into your project and use it like you are on 1.9.

module Net
  module HTTPHeader

    def set_form_data(params, sep = '&')
      query = URI.encode_www_form(params)
      query.gsub!(/&/, sep) if sep != '&'
      self.body = query
      self.content_type = 'application/x-www-form-urlencoded'
    end
    alias form_data= set_form_data

  end
end

module URI

  def self.encode_www_form(enum)
    enum.map do |k,v|
      if v.nil?
        k
      elsif v.respond_to?(:to_ary)
        v.to_ary.map do |w|
          str = k.dup
          unless w.nil?
            str << '='
            str << w
          end
        end.join('&')
      else
        str = k.dup
        str << '='
        str << v
      end
    end.join('&')
  end

end
April 24, 2011
0 thanks

Input strings are treated as regexp

Input strings are treated as regexp, but you can escape special regexp characters as usual:

"*test*".should match "\\*test\\*" #=> pass
"*test*".should match '\*test\*' #=> pass
"*test*".should match /\*test\*/ #=> pass
April 22, 2011
0 thanks

Remove non empty directories

To remove a non empty directory use FileUtils:

Dir.mkdir("test_dir")
Dir.mkdir("test_dir/sub_dir")
FileUtils.remove_dir("test_dir",true)
April 22, 2011
0 thanks

Careful when comparing strings

Input String is treated as Regexp:

"*test*".should match "*test*" #=> fail
"*test*".should match ".*test.*" #=> pass

Regexp special characters inside input String can’t [be] escaped:

"*test*".should match "\*test\*" #=> fail
"*test*".should match /\*test*\/ #=> pass
April 22, 2011
0 thanks

Eql equals ==

Use eql to compare values as you would use ==:

"test".should eql "test" #=> pass
"test".should == "test" #=> pass

Do not confuse with equal which compares objects.

April 22, 2011
0 thanks

Test strings with eql

Equal fails when comparing two different string objects:

"test".should equal "test" #=> fail
"test".should match "test" #=> pass
"test".should eql "test" #=> pass

In fact:

"test".object_id.should_not eql "test".object_id #=> pass

Match fails when the string contains regex special characters not escaped:

"*test*".should match "*test*" #=> fail for invalid regex
"*test*".should eql "*test*" #=> pass

In fact, match treats input as regexp:

"*test*".should match /\*test\*/ #=> pass
April 14, 2011
0 thanks

Example

Chops the last character off a string.

> a = "12345"
> a.chop
=> "1234"
> a
=> "12345"
> a.chop!
=> "1234"
> a
=> "1234
April 14, 2011
0 thanks

Why gsub!

I notice other adapters use sql.sub!, not sql.gsub! and in fact I had trouble with adding the limit parameter to any query involving nested selects. Replacing sql.gsub! with sql.sub! solved that problem for me. Has anyone else had a similar experience with this method?

In other words, replace:

sql.gsub!(/SELECT/i, 'SELECT B.* FROM (SELECT A.*, row_number() over () AS internal$rownum FROM (SELECT')

With:

sql.sub!(/SELECT/i, 'SELECT B.* FROM (SELECT A.*, row_number() over () AS internal$rownum FROM (SELECT')
April 14, 2011
0 thanks

logging before filters

If you need to track and log which before filters are called for the purpose of debugging your app/before_filters. Then here’s a suggestion, how this could be accomplished:

http://stackoverflow.com/questions/4951902/how-to-log-rails-controller-filters-during-rspec-controller-tests/5660475#5660475

April 8, 2011
0 thanks

customizing text when using label in a form

Instead of the default text Email you can change it to “Primary Email” like this: f.label :email, “Primary Email”

April 6, 2011 - (v3.0.5)
0 thanks

the source is wrong!

in 3.0.5, the source code in line 15 looks like this:

respond_to?(:"#{k}=") ? send(:"#{k}=", v) : raise(UnknownAttributeError, "unknown attribute: #{k}")

whats wrong with apidock?

April 4, 2011
2 thanks

Working with match captures

Let’s say you wanted to filter out passwords from:

s = "password=bob&password=jim&password=jane"

You’d do this:

r = /password\=([^\&]+)/
s.gsub!(r) { |m| m.gsub!($1, "[FILTERED]") }

Which would return

password=[FILTERED]&password=[FILTERED]&password=[FILTERED]
April 4, 2011 - (>= v3.0.0)
0 thanks
March 31, 2011 - (v3.0.0 - v3.0.5)
0 thanks

reload is deprecated? what's the new alternative?

What’s the recommended alternative to reload for more recent versions of Rails?

March 31, 2011
0 thanks

Test strings with match

To test a string use match, e.g.

"test".should match("test")
March 30, 2011 - (v3.0.0 - v3.0.5)
1 thank

After filters are not called when exception occurs

Please note after filters are not executed when an exception occurs. You have to handle these situations explicitly.

March 25, 2011
4 thanks

How to specify :only_path when non-hash options

When passing in an object, as opposed to a hash, you can’t do this because url_for accepts one argument:

url_for(post, :only_path => true)

Instead, do this:

polymorphic_url(object, :routing_type => :path)
March 23, 2011
0 thanks

Handling nested hashes and arrays

You can use this code to handle nested hashes and arrays. I’m not sure if it handles every case, and it could probably be refactored better, but it’s working quite well for us.

require 'active_support/core_ext/hash'

def normalize_params(params, key=nil)
  params = params.flatten_keys if params.is_a?(Hash)
  result = {}
  params.each do |k,v|
    case v
      when Hash
        result[k.to_s] = normalize_params(v)
      when Array
        v.each_with_index do |val,i|
          result["#{k.to_s}[#{i}]"] = val.to_s
        end
      else
        result[k.to_s] = v.to_s
    end
  end
  result
end

# Adapted from http://snippets.dzone.com/posts/show/6776
class Hash
  def flatten_keys(newhash={}, keys=nil)
    self.each do |k, v|
      k = k.to_s
      keys2 = keys ? keys+"[#{k}]" : k
      if v.is_a?(Hash)
        v.flatten_keys(newhash, keys2)
      else
        newhash[keys2] = v
      end
    end
    newhash
  end
end
March 23, 2011
2 thanks

Timestamps

Note that ActiveRecord will not update the timestamp fields (updated_at/updated_on) when using update_all().

March 23, 2011 - (v3.0.0 - v3.0.5)
0 thanks
March 23, 2011 - (v3.0.0 - v3.0.5)
1 thank