Rails Migration to Convert all Table Names and Column Names to underscore

Ruby on Rails 1 Comment »

This may be useful for legacy database, whose table and column names are in CamelCase. Use with extreme caution:

class ChangeAllColumnsToUnderscore < ActiveRecord::Migration
  include ActiveRecord::ConnectionAdapters::SchemaStatements
  require ‘active_record/connection_adapters/abstract_adapter’

  #not needed in Rails 3
  #pilfered from here:
  #http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-column_exists-3F
  def self.column_exists?(table_name, column_name, type = nil, options = {})
    columns(table_name).any?{ |c| c.name == column_name.to_s &&
        (!type                 || c.type == type) &&
        (!options[:limit]      || c.limit == options[:limit]) &&
        (!options[:precision]  || c.precision == options[:precision]) &&
        (!options[:scale]      || c.scale == options[:scale]) }
  end

  def self.up
    tables.reject {|t| t == “schema_migrations”}.each do |table|
        columns(table.to_sym).each do |column|
          unless self.column_exists?(table.to_sym, column.name.underscore.to_sym)
            rename_column table.to_sym, column.name.to_sym, column.name.underscore.to_sym
          end
        end
        unless table_exists?(table.underscore.to_sym)
          rename_table table, table.underscore
        end
    end
  end

  def self.down
    tables.reject {|t| t == “schema_migrations”}.each do |table|
      columns(table.to_sym).each do |column|
        unless column_exists?(table.to_sym, column.name.camelize.to_sym)
          rename_column table.to_sym, column.name.to_sym, column.name.camelize.to_sym
        end
      end
      unless table_exists?(table.camelize.to_sym)
        rename_table table, table.camelize
      end
    end
  end
end

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Install RubyGems Legacy Versions

Ruby, Ruby on Rails No Comments »

To install a legacy version of RubyGems, you will need some version installed first, so use aptitude, yum, zypper etc:

apt-get install rubygems

Then revert to an older version (for example 1.3.6):

gem install rubygems-update -v=1.3.6

update_rubygems

Check your version with

gem env

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Enabling gzip Compression with SOAP4R

Ruby on Rails, SOAP4R No Comments »

It’s a one-liner, but info isn’t easy to find (as usual with soap4r).

When you generate your local driver with for example

wsdl2ruby.rb --wsdl MyService.wsdl --classdef MyService --type client --force

and you instantiate your proxy driver like so

ms = MyService.new
ms.wiredump_dev = STDERR

you simply add

ms.streamhandler.accept_encoding_gzip = true

That’s all. No need for custom headers etc.

And: zlib (compression library used by soap4r) is built into Ruby since version 1.8. No need for any gems or other shenenigans.

Link List

As an added year-end bonus, here’s a list of links that helped me with SOAP and ruby over the years:

Excellent SAP article:

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/505ac48e-0578-2a10-60be-c5ffe14a0134

Correct usage of wsdl2ruby (must be wsdl2ruby.rb)

http://www.hostingrails.com/236/Using-Web-Services-Description-Language-wsdl2ruby-and-soap4r-as-a-clientI

ActionWebService:

http://www.datanoise.com/articles/2008/7/2/actionwebservice-is-back

Links:

http://www.brendonwilson.com/blog/2006/04/02/ruby-soap4r-wsdl-hell/

WSDL/Ruby:

http://codeidol.com/other/rubyckbk/Web-Services-and-Distributed-Programming/Using-a-WSDL-File-to-Make-SOAP-Calls-Easier/

Very Helpful Detailed Article:

http://www.ibm.com/developerworks/opensource/library/os-ws-rubyrails/index.html

xsltproc examples:

http://linux.byexamples.com/archives/463/xslt-processor-command-line/

xsd2wsdl:

http://jira.springframework.org/secure/attachment/12306/XsdToWsdl.xsl

linked from here:

http://jira.springframework.org/browse/SWS-79

Don’t forget to enable wiredump:

fe.wiredump_dev = STDERR

Get rid of NS1:

http://www.pluitsolutions.com/2007/08/10/remove-n1-namespace-for-soap4r/

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

script/runner with Debugger in Rails

Ruby on Rails No Comments »
script/runner myscript.rb --debugger

won’t work, so you have to use

rdebug script/runner myscript.rb

That’s all, folks! Merry Christmas!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Prepend to javascript_include_tag :defaults

JavaScript, Ruby on Rails No Comments »

Yes, there is register_javascript_include_default but it will only append to your sources.

To prepend (I’ll use this to have the excellent JS.Class libraries loaded before anything else), proceed like so:

While in Rails 3, you can set

config.action_view.javascript_expansions[:defaults].unshift(’my_prepend_1′, my_prepend_2′)

in your application.rb file,

in Rails 2.3.x you create an initializer (e.g. config/initializers/my_js_defaults.rb) and reset the JAVASCRIPT_DEFAULT_SOURCES constant as so:

module ActionView::Helpers::AssetTagHelper
  JAVASCRIPT_DEFAULT_SOURCES = (remove_const :JAVASCRIPT_DEFAULT_SOURCES).unshift(’my_prepend_1′, ‘my_prepend_2′)
   reset_javascript_include_default
end

This post gave me the idea. It concerns Rails 3 (before the above config.action_view.javascript_expansions which was introduced in Rails 3 RC)  but the code also works with Rails 2.3.x.

 

 

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in