Skip to content

Commit

Permalink
add Rails 3 style generators
Browse files Browse the repository at this point in the history
  • Loading branch information
gsusmonzon committed Apr 22, 2011
1 parent a936b39 commit 0b3a9cb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/generators/fast_session_migration/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Description:
Creates a migration to add the sessions table used by the FastSessions
session store plugin. Pass the migration name, either CamelCased or under_scored,
as an argument.

Example:
`./script/generate fast_session_migration CreateSessionTable`

With 4 existing migrations, this creates the CreateSessionTable migration
in db/migrate/005_add_session_table.rb

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class FastSessionMigrationGenerator < Rails::Generators::NamedBase
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)

def create_migration
migration_template 'migration.rb', 'db/migrate/add_fast_sessions'
end

protected

def default_session_table_name
ActiveRecord::Base.pluralize_table_names ? 'fast_session'.pluralize : 'fast_session'
end

def session_table_name
default_session_table_name
end

# FIXME: Should be proxied to ActiveRecord::Generators::Base
# Implement the required interface for Rails::Generators::Migration.
def self.next_migration_number(dirname) #:nodoc:
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end

end
20 changes: 20 additions & 0 deletions lib/generators/fast_session_migration/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class <%= class_name %> < ActiveRecord::Migration
ActiveRecord::SessionStore::FastSessions.table_name = "<%= session_table_name %>"
# If you're going to use this module with MySQL 5.1.22+, then you'd
# like to set this to +true+ because it will provide you with consecutive
# data inserts in InnoDB. Another cases when you'd like to use it is when
# your MySQL server is I/O bound now and you do not want to add random I/O
# because of randomized primary key.
ActiveRecord::SessionStore::FastSessions.use_auto_increment = false
def self.up
say "Creating sessions table..."
ActiveRecord::SessionStore::FastSessions.create_table!
end
def self.down
say "Destroying sessions table..."
ActiveRecord::SessionStore::FastSessions.drop_table!
end
end

0 comments on commit 0b3a9cb

Please sign in to comment.