Change default user shell

Example of how to use chsh command to change current user shell to bash:

chsh -s /bin/bash

Git push to remote

Just a note, this is the way to push the current branch to a branch on a remote git repository using ssh trough non default port:

git push ssh://remote_user@domain.com:remote_port/~/remote_repository remote_branch

Git diff pacth

How to create a patch with git diff:

git diff HEAD~2 > changes.diff


How to use it:

patch -Np2 -i changes.diff

thanks to Andy ;)

Hash to Object

Sometimes I have the need to create objects that responds to some methods with a specific values.
Something that I can use like
HashObject.new :method1 => value_for_method1, :method2 => value_for_method2
There is already a way to do it, with OpenStruct, but I created a HashObject. Just for academic proposals. I didn’t knew the OpenStruct at the time.

class HashObject
  def initialize(hash)
    hash.each do |k,v|
      self.instance_variable_set("@#{k}", v)
      self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
      self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
    end
  end

  def to_hash
    hash_to_return = {}
    self.instance_variables.each do |var|
      hash_to_return[var.gsub("@","")] = self.instance_variable_get(var)
    end
    return hash_to_return
  end
end

Object.tot

I know Rails 2.3 is out and has a new “try” method, But I was looking into an old app and I saw some code I have written. It’s extending the object class with a method called tot (that or that lolol) so that you can use it anywhere like in the example:

tot do params[:search][:name] end

or like that

tot(:also => ["","nome feio"], return => "nome invalido") do params[:search][:name] end

this last example case will return “nome invalido” in case of params[:search][:name] being empty string, “nome feio”, or if a NoMethodError happens”, other wise it will return params[:search][:name] value

And that is my code extending object class.

class Object
  def tot(options = {})
    begin
      (options[:also] and options[:also].include?(yield)) ? options[:return] : yield
      rescue NoMethodError
      options[:return]
    end
  end
end

I found it funny, anyway Ruby on Rails got it’s own try method built in some months later.

Git clone from remote repositorium with ssh and non default port

just a reminder, suppose your ssh port is 3022: (sometimes I swap the way to specify the port in those two commands)

git clone ssh://user@domain.com:3022/~/Projects/my_project

ssh user@domain.com -p 3022


(when connecting through ssh the non default port goes as param, the default port is 22)

Javascript auto include rails plugin

I need to check this link latter:

http://blog.media72.net/2008/05/13/javascript-auto-include-rails-plugin/

I haven’t tried it yet but seems a good way to maintain your .js files organized.

Get all values of a single column efficiently

Imagine you want this:

@names = MyModel.find(:all).map{ |i| i.name }.uniq

Yes, that works! You get all distinct names of one column, you can even do something like that:

#Using Shortcut Blocks with Symbol to_proc
@names = MyModel.all.map(&:name).uniq

Cool, rely small code(almost seems I’m using Haskel) but not that efficient if your model’s table has lots of columns. (Imagine! The whole table is being loaded to memory) So what is the solution?

MyModel.find( :all, :select > 'DISTINCT name' )

If you specify wich columns you need, in the sql query, you will spare some memory. And in my earlier example you can also pass the “uniq” work to the database.

will_paginate without losing params

I’m sure you like will_paginate, it seams magic! But I was having the issue, that I simply solved like that:

will_paginate(@reports, :params => params.except(:page))

Rewrite humanize

I have an application with all tables and field names in English, but the whole views now need to be in Portuguese.

Because I’m always using the humanize method, a simple solutions should emerge.

First solution:

class Ticket
  HUMANIZED_ATTRIBUTES = {
    :category > "Categoria",
    :title > "Assunto"
  }
  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end
end


Setting the human_attribue_name in each model works ok,
but if like in my case, you have 20 models and all of them have a description, title,… much duplication…

So I thought in doing something similar to this in my enviroment.rb:

Inflector.inflections do |inflect|
  inflect.plural /^(foo)$/i, '\1ze'
  inflect.singular /^(foo)ze/i, '\1'
end


But for the humanized method instead of pluralize. Yah It would make sense that way, but rails doesn’t provide that feature. You can go here to find a patch for getting that functionality into your rails, I hope they get that into the core… anyway If didn’t want to download the patch, how do I solve the problem?

Maybe a rubbish solution but perfect to get what I was needing, translate my views with few lines of code!
What I’ve done? Just added those lines into my environment.rb

class String
  def humanize
    {:movie              => "Filme",
     :movies             => "Filmes",
     :name               => "Nome",
     :title              => "Título",
     :synopsis           => "Sinopse",
     :genre              => "Genero",
     :author             => "Autor",
     :authors            => "Autores"
     ...
    }[self.gsub(/_id$/, "").to_sym] || super
  end
end


I simply redefined the humanize method for whole strings. It works perfectly in my case, because I’m always calling humanize in my views.

keep humanizing the world!

Next Page »


Last Regedor’s twitts

Last Regedor's bookmarks