-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a generator for the Version model and its migration
- Loading branch information
Showing
6 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rails/generators/active_record/migration' | ||
|
||
class ActiveVersioningGenerator < Rails::Generators::Base | ||
include ActiveRecord::Generators::Migration | ||
|
||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
def install_models | ||
copy_file 'models/version.rb', 'app/models/version.rb' | ||
end | ||
|
||
def install_migrations | ||
migration_template 'migrations/create_versions.rb', 'db/migrate/create_versions.rb' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class CreateVersions < ActiveRecord::Migration | ||
def change | ||
create_table :versions do |t| | ||
t.string :versionable_type, null: false | ||
t.integer :versionable_id, null: false | ||
t.string :event, null: false | ||
t.string :committer | ||
t.text :object | ||
t.boolean :draft, default: false | ||
t.text :commit_message | ||
t.datetime :committed_at | ||
|
||
t.timestamps null: false | ||
end | ||
|
||
add_index :versions, [:versionable_type, :versionable_id] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Version < ActiveRecord::Base | ||
include ActiveVersioning::Model | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe ActiveVersioningGenerator, type: :generator do | ||
destination File.expand_path('../../../../tmp', __FILE__) | ||
|
||
before do | ||
prepare_destination | ||
run_generator | ||
end | ||
|
||
it "generates the version model" do | ||
assert_file 'app/models/version.rb', /class Version < ActiveRecord::Base/ | ||
end | ||
|
||
it "generates the versions migration" do | ||
assert_migration 'db/migrate/*_create_versions.rb', /class CreateVersions < ActiveRecord::Migration/ | ||
end | ||
end |