mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
Add generator compatible with Rails 3.
This commit is contained in:
parent
cf75436723
commit
3954767072
3 changed files with 48 additions and 0 deletions
2
lib/generators/paper_trail/USAGE
Normal file
2
lib/generators/paper_trail/USAGE
Normal file
|
@ -0,0 +1,2 @@
|
|||
Description:
|
||||
Generates (but does not run) a migration to add a versions table.
|
28
lib/generators/paper_trail/paper_trail_generator.rb
Normal file
28
lib/generators/paper_trail/paper_trail_generator.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
require 'rails/generators/named_base'
|
||||
require 'rails/generators/migration'
|
||||
|
||||
class PaperTrailGenerator < Rails::Generators::NamedBase
|
||||
include Rails::Generators::Migration
|
||||
|
||||
desc "Generates (but does not run) a migration to add a versions table."
|
||||
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
argument :name, :type => :string, :default => "create_versions"
|
||||
|
||||
# Implement the required interface for Rails::Generators::Migration.
|
||||
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
|
||||
def self.next_migration_number(dirname)
|
||||
if ActiveRecord::Base.timestamped_migrations
|
||||
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
||||
else
|
||||
"%.3d" % (current_migration_number(dirname) + 1)
|
||||
end
|
||||
end
|
||||
|
||||
def create_migration_file
|
||||
migration_template 'create_versions.rb', 'db/migrate/create_versions.rb'
|
||||
end
|
||||
|
||||
|
||||
end
|
18
lib/generators/paper_trail/templates/create_versions.rb
Normal file
18
lib/generators/paper_trail/templates/create_versions.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
class CreateVersions < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :versions do |t|
|
||||
t.string :item_type, :null => false
|
||||
t.integer :item_id, :null => false
|
||||
t.string :event, :null => false
|
||||
t.string :whodunnit
|
||||
t.text :object
|
||||
t.datetime :created_at
|
||||
end
|
||||
add_index :versions, [:item_type, :item_id]
|
||||
end
|
||||
|
||||
def self.down
|
||||
remove_index :versions, [:item_type, :item_id]
|
||||
drop_table :versions
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue