Archive

Archive for March, 2010

tuna salad

March 15th, 2010 Comments off

components:
5 leaves of lettuce
small onion
tomato
soured cucumber
can of tuna in oil (200g)
100-150ml of yogurt
to taste salt and pepper

preparation:
- wash lettuce and cut into small strips/squares
- cover the salad with finely chopped onion
- sprinkle onion with salt (quite a lot) and a bit of pepper
- then successively chop tomato and cucumber and arrange next layers
- at the end drain tuna of the oil, and gently disintegrating arrange the last layer
- cover everything with yogurt and at the end … mix everything :D

alternatively instead of using a lot of salt on each layer you could use a bit of salt on each layer separately those avoiding mixing.

Categories: Food Tags: ,

rails current user

March 13th, 2010 6 comments

While playing with next app I thought a bit about so common current_user … so a lot of tutorials and descriptions uses this method in application controller.

After thinking a while I found other way to do this, and for me it looks a bit better, any thoughts on it ?

So get started with User model:

class User < ActiveRecord::Base
acts_as_authentic
cattr_reader :current
end

Now in application controller add filter to set current user:

class ApplicationController < ActionController::Base
before_filter :set_user
private
def set_user
current_user_session = UserSession.find
User.send :class_variable_set, :@@current, current_user_session > current_user_session.record
end
end

Now you can use whatever you want the same method to access current user:

User.current

Added 2010.03.14 :
Warning please do not use this method it is not thread safe – it is only good for single threaded applications.

Categories: Development Tags: , ,

rails acts_as_configurable file config

March 7th, 2010 1 comment

Working on my new app I thought it is necessary to get some data from configuration file, there was already plugin that had the necessary functionality – acts_as_configurable.

Unfortunately this plugin was not exactly what I was looking for It had possibility to write configuration in the class itself – I needed configuration in file.

So I did fork of the project, and now you have possibility to use rewritten version of the plugin which uses yaml file for storing configuration.

Here is example configuration file:

default:
  :first: 1st
Settings2:
  :first: 2nd
settings:
  :first: 3rd

With this configuration all the following definitions are valid:

class Settings1
  acts_as_configurable
end
class Settings2
  acts_as_configurable
end
class Settings30
  acts_as_configurable :for => 'settings'
end
class Settings31
  acts_as_configurable :for => :settings
end
class Settings4
  acts_as_configurable :for => Settings2
end
class Settings50
  acts_as_configurable 'conf'
end
class Settings51
  acts_as_configurable :conf
end
class Settings52
  acts_as_configurable :name=>'conf', :default=>'settings'
end
class Settings53
  acts_as_configurable :name=>:conf, :default=>'settings'
end
class Settings60
  acts_as_configurable :name=>'conf', :for => Settings1, :default=>'settings'
end
class Settings61
  acts_as_configurable :name=>'conf', :for => Settings1, :default=>:settings
end
class Settings7
  acts_as_configurable :name=>'conf', :for => Settings2, :default=>'settings'
end
class Settings8
  acts_as_configurable :name=>'conf1', :for => Settings1
  acts_as_configurable :name=>'conf2', :for => Settings2
end

and using following rspec code it will pass tests:

describe "ActsAsConfigurable" do
  it "should respond to 'configuration'" do
    Settings1.configuration.should_not be_nil
  end
  it "should contain default values" do
    Settings1 .configuration[:first].should eql '1st'
  end
  it "should contain values" do
    Settings2.configuration[:first].should eql "2nd"
  end
  it "should contain values for string" do
    Settings30.configuration[:first].should eql "3rd"
  end
  it "should contain values for symbol" do
    Settings31.configuration[:first].should eql "3rd"
  end
  it "should contain values for class" do
    Settings4.configuration[:first].should eql "2nd"
  end
  it "should contain values for object" do
    Settings2.new.configuration[:first].should eql "2nd"
  end
  it "should respond to 'conf' - passed as string" do
    Settings50 .conf[:first].should eql '1st'
  end
  it "should respond to 'conf' - passed as symbol" do
    Settings51 .conf[:first].should eql '1st'
  end
  it "should respond to 'conf' - passed as :name=>" do
    Settings52 .conf[:first].should eql '3rd'
  end
  it "should respond to 'conf' - passed as :name=>symbol" do
    Settings53 .conf[:first].should eql '3rd'
  end
  it "should work with other default - passed as string" do
    Settings60 .conf[:first].should eql '3rd'
  end
  it "should work with other default - passed as symbol" do
    Settings61 .conf[:first].should eql '3rd'
  end
  it "should work with other default but also exisitng for" do
    Settings7 .conf[:first].should eql '2nd'
  end
  it "should work with multiple definitions" do
    Settings8 .conf1[:first].should eql '1st'
    Settings8 .conf2[:first].should eql '2nd'
  end
end

Sources for the plugin are here http://github.com/mpapis/acts_as_configurable

Categories: Development Tags: , , ,

git quick start

March 5th, 2010 Comments off

Lately I’m using quite often very similar path for work with git, to not loose it with time, and of course to return my few bits to community I put my links here.

So my path of usage is

  1. Setting up private git repository – I use my own server for trial projects, instructions for windows/github follow bellow,
  2. Pushing & Pulling sources with remote git server – my instruction covers cloning remote repo but not connecting it with working sources, this instruction seams to cover it quite good,
  3. How to use git the right way, as it is not so clear how to use git with branches, remote branches, merging, or overall project flow – I find this instruction quite good source of knowledge

If you use Windows or you are interested in hosting open source project then github.com should be worth trying with the following instruction: use github on windows.

Categories: Development Tags:

Cucumber multi-session

March 2nd, 2010 3 comments

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

Categories: Development Tags: , , ,
Get Adobe Flash player