Archive

Posts Tagged ‘ruby’

super action middleware

June 30th, 2010

As a proof of concept I created an super action that allows to process more than one action at once.
Probably most of you will ask a question ‘why’ if there is an posibility to use nested resources. Yes you can, but what if there are more different actions that do not have common root?

Code bellow is only proof of concept, it was tested only using integration tests, if you ever try it more please let me know.

First Step is to create middleware scaffold ‘lib/super_action.rb’:

class SuperAction
  include Rack::Utils

  def initialize(app)
    @app = app
  end

  def call(env)
    @app.call(env)
  end
end

Next we have to connect it, we can not do it as advised in environment file, we also do not want to change the whole stack, the way to go is to hook into application loading process soewhere after it initilaizes all automatic middlewares. Add the following code (module) into ‘config/environment.rb’:

...
require File.join(File.dirname(__FILE__), 'boot')

module Rails
  class Initializer
    alias :old_load_application_classes :load_application_classes
    def load_application_classes
      configuration.middleware.delete ActiveRecord::QueryCache
      configuration.middleware.delete ActiveRecord::ConnectionAdapters::ConnectionManagement
      configuration.middleware.insert_before ActionController::ParamsParser, 'SuperAction'
      configuration.middleware.insert_before 'SuperAction', ActiveRecord::ConnectionAdapters::ConnectionManagement
      configuration.middleware.insert_before 'SuperAction', ActiveRecord::QueryCache
      old_load_application_classes
    end
  end
end

Rails::Initializer.run do |config|
...

We can not do the middleware changes directly in initializer using config.middleware because then it raises exception NameError on ‘ActiveRecord::QueryCache’ - it is loaded in the initialization stack after environment. So this is the current stack looks now for me:

~/projects/super_action> rake middleware
use Rack::Lock
use ActionController::Failsafe
use ActionController::Session::CookieStore, #
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use SuperAction
use ActionController::ParamsParser
use Rack::MethodOverride
use Rack::Head
use ActionController::StringCoercion
run ActionController::Dispatcher.new

Important here was to be before ActionController::ParamsParser, because it is responsible for providing correct params to action. Putting ConnectionManagement and QueryCache before super action should be considered as an optimization.

When we have correct order of middlewares now the super action functionality, back to ‘lib/super_action.rb’:

class SuperAction
  include Rack::Utils

  def initialize(app)
    @app = app
  end

  def call(env)
    uri  = env['REQUEST_URI'] || env['PATH_INFO']
    wrapper = env['rack.input']
    body = wrapper.read
    wrapper.rewind

    unless body.blank?
      params, format = if uri == '/super_action.xml'
        [Hash.from_xml(body), :xml]
      end
    end

    first_response = nil

    if params
      response_array = params['requests'].map do |req|
        req_env = env.clone
        req_env['PATH_INFO']      = req['url']
        req_env['REQUEST_URI']    = req['url']
        req_env['REQUEST_METHOD'] = req['method']
        req_env['CONTENT_TYPE']   = req['content_type']
        req_env['rack.input']     = Rack::Lint::InputWrapper.new StringIO.new( req['body'] || '' )
        status, headers, req_response = @app.call(req_env)
        first_response ||= req_response
{
  :url => req['url'],
  :method => req['method'],
  :status => status,
  :body => req_response.body,
  :id => req['id']
}
      end

      response_body = case format
      when :xml then
        xml = Builder::XmlMarkup.new
        xml.responses 'type' => 'array' do
          response_array.each do |resp|
            xml.response do
              xml.url resp[:url]
              xml.method resp[:method]
              xml.id resp[:id], 'type' => 'integer'
              xml.body do
                xml.cdata! resp[:body]
              end
            end
          end
        end
      end

      header = HeaderHash.new
      header['Content-Type'] = 'application/xml; charset=utf-8'
      header['Content-Length'] = response_body.size.to_s
      header['Cache-Control'] = 'private, max-age=0, must-revalidate'

      first_response.instance_variable_set(:@body, response_body)
      [ 200, header, response_body]
    else
      @app.call(env)
    end
  end
end

I know the code looks tricky, it realy is, but finally it allowed me to run the following test:

  test "update many books" do
    Book.delete_all

    start = Time.now

    xml = Builder::XmlMarkup.new
    xml.requests 'type' => 'array' do
      xml.request do
        xml.url '/books.xml'
        xml.method 'POST'
        xml.id 0
        xml.content_type 'application/xml'
        xml.body do
          xml.cdata! '<title>book2</title>'
        end
      end
      xml.request do
        xml.url '/books.xml'
        xml.method 'POST'
        xml.id 1
        xml.content_type 'application/xml'
        xml.body do
          xml.cdata! '<title>book3</title>'
        end
      end
      xml.request do
        xml.url '/books.xml'
        xml.method 'GET'
        xml.id 2
      end
    end

    assert_difference "Book.count", 2 do
      post '/super_action.xml', xml.target!
    end
    responses = Hash.from_xml( response.body )['responses']

    stop = Time.now
    puts "separate:#{stop-start}:"

y [ xml.target!, response.body ]

    assert_response :success
    assert_equal 3, responses.size
    responses.sort! { |a,b| a['id']  b['id'] }

    assert_equal 0, responses[0]['id']
    assert_equal 'book2', Hash.from_xml(responses[0]['body'])['book']['title']

    assert_equal 1, responses[1]['id']
    assert_equal 'book3', Hash.from_xml(responses[1]['body'])['book']['title']

    assert_equal 2, responses[2]['id']
    assert_equal 'book2', Hash.from_xml(responses[2]['body'])['books'][0]['title']
    assert_equal 'book3', Hash.from_xml(responses[2]['body'])['books'][1]['title']
  end

Speed up in integration tests is over 2x on three actions, this makes this something considerable … only if the code would be not so tricky.

Development , , , , , ,

Deep associations in rails activerecord

May 22nd, 2010

Some time ago I wrote about complex associations, now time to add another method and corrections.

First the finder_by_sql, in that particular case It was necessary to add

:readonly => true

So the code looks now like this:

  has_many :roles,
    :readonly => true,
    :finder_sql =&gt; '
SELECT roles.name FROM roles
INNER JOIN responsibilities ON roles.id = responsibilities.role_id
INNER JOIN assigments ON responsibilities.group_id = assigments.group_id
WHERE assigments.user_id = #{id}
GROUP BY roles.id
  '

There is one realy big downside of using finder_sql - it does not work with find_by_… or named scopes, so this forced me to continue searching and this is the result:

  def roles
    Role.scoped(
     {
       :joins => { :responsibilities => { :group => { :assigments => :user } } },
       :conditions => {"users.id" => id},
       :group => "roles.id"
     }
   )
  end

and now I can write:

user.roles.by_name(:admin).count

where by_name is an named scope

  named_scope :by_name, lambda { |type| {:conditions => [ "roles.name = ", type.to_s ] } }

Development , , , ,

Cucumber multi-session

March 2nd, 2010

This article is indirect translation of Cucumber - obsluga kilku sesji - polish version (from andrzejsliwa.com devblog).

Most of standard testing related tasks can be achieved in a simple way using the default steps from Cucumber. By assumption Cucumber is for functional testing, but it can also be used to implement integration test. This is especially useful when we want test interaction between users, when they should get on-line notifications - and this should be tested without login/logout functionality, like in shout box which was popular some time ago.

When using integration tests you could use block open_session:

def login(user)
  open_session do |sess|
    sess.extend(CustomDsl)
    u = users(user)
    sess.https!
    sess.post "/login", :username => u.username, :password => u.password
    assert_equal '/welcome', path
    sess.https!(false)
  end
end

But in the case of cucumber which is based on the steps it was necessary to find a solution that is suited to the form in which scenarios are created.

So I have asked my friend Andrzej Sliwa to write an example code, this is the example:

module ActionController
  module Integration
    class Session
      def switch_session_by_name(name)
        if @sessions_by_name.nil?
          @sessions_by_name = { :default => @response.session.clone }
        end
        @sessions_by_name[name.to_sym] ||= @sessions_by_name[:default].clone
        @response.session = @sessions_by_name[name.to_sym]
      end
    end
  end
end

Given /^session name is "([^\"]*)"$/ do |name|
  switch_session_by_name(name)
end

Use of this mechanism is trivially easy to perform the following step:

Given session name is "guest no 1 session"

g

In this case, a named session is created which is not dependent on others (including the default). Access to default session is called by using the default name:

Given session name is "default"

You can always get back to named session using the same name as before, the state of it will be persisted for you.

More details on Integration tests: http://guides.rubyonrails.org/testing.html#integration-testing-examples

Development , , ,

exception iteration in ruby

January 29th, 2010

There are many ways to iterate in ruby, many methods, but going back to assembler roots of mine I found another one:

>> a=[1,2,3]
=> [1, 2, 3]
>> begin puts a.shift; raise unless a.blank?; rescue; retry; end
1
2
3
=> nil

This code is only prof of concept, it should not be used for iterations, by example if it would be used without prior initialization of “a” variable it would be an endless loop as on exception “NameError” it will retry in endless loop.

There are other places where this could be used, but even possible retry should not be used without some kind of endless loop protection like:

>> i=10; begin puts IO.read('some.file'); rescue; if i>0 then puts "#{i} ..."; i-=3; sleep 1; retry; end; end
10 ...
9 ...
8 ...
7 ...
some file created
=> nil

In this example a file was created during run of the command - on second console by just running “echo some file created > some.file”.

There are other ways to do the same, maybe not so cool like that above but probably most of developers will prefer something like:

>> 10.downto(1){|i| begin puts IO.read('some.file'); break; rescue; puts "#{i} ..."; sleep 3; end }
10 ...
9 ...
8 ...
some file created
=> nil

Did You liked this post or maybe not, vote on it at dzone.

Development , , ,

string to class in ruby on rails

January 26th, 2010

There are few ways to have a class from a string, most know are:

  • Kernel.const_get(’User’)
  • eval(’User’)
  • ‘User’.constantize

The order in which I have named them is important - a bit important, Kernel.const_get is 10 times faster then constantize and 5 times faster then eval.

The reason of such speed for Kernel.const_get is from it has to maintain list of all constants in the application, tests show it may even behave faster then storing names and Class mapping in a hash.

The difference is not big on simple calls to create just one or two classes, but on heavy loaded systems this might give some more percents of the hardware.

Did You liked this post or maybe not, vote on it at dzone.

Development , , , , ,

Get Adobe Flash playerPlugin by wpburn.com wordpress themes