Check Existence of DelayedJob Records

I’m using the collectiveidea fork of DelayedJob. There are two ways to create a DelayedJob record. First you can use the delay method.

class User
  class << self

    def long_process(args)
      #long running process
    end

    def create_delayed_job_for_long_process(args)
      #check the existence of DelayedJob record
      unless Delayed::Job.exists?(:handler => Delayed::Job.new(:payload_object => Delayed::PerformableMethod.new(self, :request_without_send_later, Array.wrap(args))).handler, :locked_at => nil)
       self.delay.long_process(args)
      end
    end

  end
end
view raw dj_delay.rb This Gist brought to you by GitHub.

Second way to create DelayedJob records is custom jobs.

#A Custom job is a class with a method called perform
class User
  def perform
    #long running process
  end
end

#check existence of DelayedJob record
Delayed::Job.exists?(:handler => Delayed::Job.new(:payload_object => User.new).handler, :locked_at => nil)


Dynamically Defining Methods with define_method

If you have been hacking ruby for a while you should have heard that “Everything is an object”. To understand this concept knowing what a singleton class is important. Yehuda Katz’s post is a good source for it.

My ruby version 1.9.2.

$ ruby -v
ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]

First I’m going to show you how to add methods to instances by using define_method.

You can also write an instance method to create instance methods. send must be used because define_method is private.

Write a singleton method to create instance methods.

Now it is time to add singleton methods. When we call define_method, it creates an instance method. What we have to do is call define_method on class’ singleton class.

Getting singleton class of a class:

First defining singleton method to create singleton methods.

Then defining instance method to create singleton methods.

Nested Object Forms with has_one Relation

Rails 2.3 has a good solution for multi model forms. But there is not much examples of  how to use accepts_nested_attributes_for with has_one relation.

Assume that we have a Book model which has one Author.



Controller :

View:


No special action is needed for create method of BooksController,  attr_accessible is not necessary for Book model. Just don’t forget to add the second parameter (@book) of form_for and build your nested object (@book.build_author).

Tidbit: Converting Strings with Line Feed to Array

If you have trouble when converting string to array, check if your string includes any line feed (\n). Check how Array("string") behaves:

irb(main):001:0> Array("izzet emre \nkutlu")
=> ["izzet emre \n", "kutlu"]

This output is not what most of the people expected. Be careful!

WorkingWithRails WordPress Plugin

A simple plugin which displays Working With Rails recommendation badge.

  1. Download the plugin.
  2. Extract the plugin to your wordpress plugin directory.
  3. Activate the plugin.
  4. Add the widget to your sidebar.
  5. Enter your information.

It’s version is 0.1 so comments will be appreciated.

Rails 2.2 & Inflector

If you have recently updated to Rails 2.2.2, you may encounter this error when you want to start your application:

/.gem/ruby/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:445:in
`load_missing_constant': uninitialized constant Inflector (NameError)

As I learned from Paul’s post usage of Inflector class is changed a bit. You can see the difference when you compare the inflections.rb files. Path of the file is yourApp/config/initializers/inflections.rb

inflections.rb (Rails 2.1.0)

 Inflector.inflections do |inflect|
  .
  .
  .
 end

inflections.rb (Rails 2.2.2)

 ActiveSupport::Inflector.inflections do |inflect|
  .
  .
  .
 end

In my situation changing Inflector to ActiveSupport::Inflector was enough to solve the problem.

Updating RubyGems to 1.2 (Manually)

This is what happened when I want to install rmagick gem.

username@username-desktop:~$ sudo gem install rmagick
Bulk updating Gem source index for: http://gems.rubyforge.org/
ERROR:  could not find rmagick locally or in a repository

What the hell…
After some googling I found that RubGems 1.1.x is buggy and and update to RubyGems 1.2 is necessary.
To learn your RubyGems version:

username@username-desktop:~$ gem -v
1.1.0

Click here to read more »

Recursive methods with block in Ruby

def comic(cast)
   cast.each do |character|
      unless character.is_a?(Array)
         yield(character)
      else
	 comic(character) {|x| yield x}
      end
   end
end

This method takes an array as argument and checks each element if it is an Array.
If the element is not an Array then the block is called.
If the element is an Array then the same method is called with that element as an argument and with the same block.

Now let’s look how we can call this method.

names = ['lucky luke', 'jolly jumper', 'rin tin tin', ['joe', 'william','jack', 'averell']]
comic(names) {|c| puts c}

Output :

lucky luke
jolly jumper
rin tin tin
joe
william
jack
averell

How to check if a div exists in rjs?

<body>
   <div id='main'>
      Main Div
   </div>
</body>

render :update do |page|
   page << "if ($('main')){"
   page.replace_html 'main', :inline => 'Here it is'
   page << "}"
end

This ruby code checks if there is a DOM object that its id = “main”.
If there is then changes its content to “Here it is”, if there is not do nothing.