weewar.com corner

script/console hack to load fixtures

Posted by railsbros_dirk
on Wednesday, February 27

This week I found out that the sexy fixtures were indeed really sexy. The so far pretty random ids you got with the new fixtures style (omitting the id) isn't that random after all.

This again a pretty nice example for RTFM, cause all those blogs I've read about Rails 2.0 told you that you don't have to worry about the id of your test data. As far as I can recall they all spoke of being generated, but after reading the API docs it is more something like being transformed. They absolute value of the hash of the fixtures label. And because all your labels have to be unique all your ids will be unique.

Now knowing that I came along with a little .irbrc hack to use something like this in your Rails console:

  
User.find('my_fixture_user')
  

and it will get you the database row with the id according to that string. Of course you must have loaded your fixtures into the current environment.

For me who is an excessive user of the Rails console this is a really handy feature.

Enough of the talking, show me the code already. - OK, here you go ...

  
if ENV['RAILS_ENV']
  require 'active_record'
  ActiveRecord::Base.class_eval do
    class << ActiveRecord::Base
      alias_method :classic_find_from_ids, :find_from_ids

      private
        def find_from_ids(ids, options)
          ids.collect! { |id| id.to_i <= 0 ? id.hash.abs : id }
          classic_find_from_ids(ids, options)
        end
    end
  end
end
  

Enjoy it!

UPDATE: Andi pointed out that you can omit the assignment of the local id variable within the block. So I updated the code snippet. Thanks Andi!

pkw.de Relaunch - Now everybody can offer a car

Posted by railsbros_dirk
on Tuesday, February 19

pkw.de - Screenshot

Like most of you already know we both work on the project pkw.de and starting with today everybody can offer a car. We have some nice features like multiple images upload and you can move completly free during the offering process between the steps and change nearly everything. We are really all very happy about this relaunch, because it is a major step towards our (speaking of the whole team) visions of this product. And one thing is for sure, we will keep putting new features and enhancements of this great product online.

Stay tuned Your Railsbros Dirk

Sake Task for Creating Rails Project + SVN Support

Posted by railsbros_dirk
on Wednesday, February 06

I put together a Sake task which allows you to create a Rails project and put directly into subversion. And the clue with this is, it asks you interactivly where your SVN repository lives and which name your project should have. Knowing this the tasks creates a directory in your Subversion repository named after your project, sets up directories for trunk, branches and tags. After that checking the trunk out as your project name, creates the Rails stubs in it, add everything but the stuff you usually don't want to see under version control (log files, temporary directory and the database.yml). In the end everything is where it belongs and you can start coding.

Hope you enjoy this little task.

  
namespace :rails do
  desc "Sake Tasks for setting up a new Rails project and put into SVN"
  task :setup_with_svn do
    puts "-----------------------------------------------"
    puts "| Setting up a Rails Project with SVN Support |"
    puts "-----------------------------------------------"
    puts
    puts "Enter your SVN URL where the Rails projects will live ..."
    print "> "
    svn_url = STDIN.gets.strip!
    puts "Enter the name of the Rails project ..."
    print "> "
    project_name = STDIN.gets.strip!
    system("svn mkdir #{svn_url}/#{project_name} -m 'ADDED - project to dir'")
    system("svn mkdir #{svn_url}/#{project_name}/trunk -m 'ADDED - initial trunk directory for the project'")
    system("svn mkdir #{svn_url}/#{project_name}/branches -m 'ADDED - initial branches directory for the project'")
    system("svn mkdir #{svn_url}/#{project_name}/tags -m 'ADDED - initial tags directory for the project'")
    system("svn checkout #{svn_url}/#{project_name}/trunk #{project_name}")
    system("cd #{project_name}; rails .")
    system("mv #{project_name}/config/database.{yml,yml.sample}")
    system("rm -rf #{project_name}/log/*")
    system("rm -rf #{project_name}/tmp/*")
    system("cd #{project_name}; svn status | grep '^? \+\(log\|tmp\)' -v | sed -e 's/? *//' | sed -e 's/ / /g' | xargs svn add")
    system("svn propset svn:ignore '*' #{project_name}/log/")
    system("svn propset svn:ignore '*' #{project_name}/tmp/")
    system("svn propset svn:ignore 'database.yml' #{project_name}/config/")
    system("cd #{project_name}; svn commit -m 'ADDED - the Rails Project has been added and all files that are not needed in the repo have been ignored.'")
    system("cp #{project_name}/config/database.{yml.sample,yml}")
    system("cd #{project_name}; rake tmp:create")
    system("touch #{project_name}/log/{development,test,server,production}.log")
  end
end
  

You can grep the tasks via pastie or via our SVN with the sake -i <URL> command.

SVN Repository

Posted by railsbros_dirk
on Thursday, January 17

Hi Folks!

I'm very sorry you didn't from us such a long time, but I'll promise to write more in the future. In the last weeks we had a lot to do both at work and university (especially me), but here I am and give you something to start.

I put a SVN repository online where we will publish some plugins, (more or less) useful code and some other stuff you can put into a SVN repository ;-) Feel free to browse it anytime! At the moment you will find two things there, one plugin for authentication (yes another one) and some code related to image processing. About both things I'll will write up something in the next days. Eventually the authentication plugin needs some words why we decided to do another authentication plugin ;-)

That's all for now and thanks for stayed tuned.

greetz

Ungreedy Regular Expressions in Ruby

Posted by railsbros_andi
on Friday, November 02

This Blog-Post saved much of my time today:

It's always about the little things which waste most of your time.

thx to Gregory