Adding defaults and indexes to a script/generate command in a Rails Template?

Posted by charliepark on Stack Overflow See other posts from Stack Overflow or by charliepark
Published on 2009-09-20T17:10:48Z Indexed on 2010/03/23 20:03 UTC
Read the original article Hit count: 235

I'm trying to set up a Rails Template that would allow for comprehensive set-up of a specific Rails app. Using Pratik Naik's overview (http://m.onkey.org/2008/12/4/rails-templates), I was able to set up a couple of scaffolds and models, with a line that looks something like this ...

generate("scaffold", "post", "title:string", "body:string")

I'm now trying to add in Delayed Jobs, which normally has a migration file that looks like this:

create_table :delayed_jobs, :force => true do |table|
  table.integer  :priority, :default => 0      # Allows some jobs to jump to the front of the queue
  table.integer  :attempts, :default => 0      # Provides for retries, but still fail eventually.
  table.text     :handler                      # YAML-encoded string of the object that will do work
  table.text     :last_error                   # reason for last failure (See Note below)
  table.datetime :run_at                       # When to run. Could be Time.now for immediately, or sometime in the future.
  table.datetime :locked_at                    # Set when a client is working on this object
  table.datetime :failed_at                    # Set when all retries have failed (actually, by default, the record is deleted instead)
  table.string   :locked_by                    # Who is working on this object (if locked)
  table.timestamps
end

So, what I'm trying to do with the Rails template, is to add in that :default => 0 into the master template file. I know that the rest of the template's command should look like this:

generate("migration", "createDelayedJobs", "priority:integer", "attempts:integer", "handler:text", "last_error:text", "run_at:datetime", "locked_at:datetime", "failed_at:datetime", "locked_by:string")

Where would I put (or, rather, what is the syntax to add) the :default values in that? And if I wanted to add an index, what's the best way to do that?

© Stack Overflow or respective owner

Related posts about templates

Related posts about ruby-on-rails