Skip to content

Commit

Permalink
Create a generator for the Version model and its migration
Browse files Browse the repository at this point in the history
  • Loading branch information
h0tl33t committed Sep 11, 2015
1 parent 14a3c74 commit b025669
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ Or install it yourself as:

$ gem install active_versioning

Once installed, generate the necessary files and run migrations:

$ rails generate active_versioning
$ bundle exec rake db:migrate

## Setup

To set up versioning in your Rails app, include the following module in each model you'd like to version:
```ruby
class Post < ActiveRecord::Base
Expand Down Expand Up @@ -57,6 +63,7 @@ post.destroy_draft
```

## Working with Versions

A draft is just a version with a particular state. To access all the versions for a particular record, use...
```ruby
post.versions # => All versions, whether the version state is 'create', 'draft', or 'commit'
Expand All @@ -74,6 +81,7 @@ post.create_draft_from_version(old_version.id)
This will set `post.current_draft`'s attributes to the attributes stored in the given version's record. Returns boolean based on the save's success.

## Capturing Version Metadata

In addition to manually committing a version with a committer and commit message...
```ruby
post.current_draft.commit(committer: current_admin.full_name, commit_message: 'Update post title.')
Expand Down
1 change: 1 addition & 0 deletions lib/active_versioning.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_versioning/version'
require 'generators/active_versioning_generator'

module ActiveVersioning
autoload :Model, 'active_versioning/model'
Expand Down
15 changes: 15 additions & 0 deletions lib/generators/active_versioning_generator.rb
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
18 changes: 18 additions & 0 deletions lib/generators/templates/migrations/create_versions.rb
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
3 changes: 3 additions & 0 deletions lib/generators/templates/models/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Version < ActiveRecord::Base
include ActiveVersioning::Model
end
18 changes: 18 additions & 0 deletions spec/lib/generators/active_versioning_generator_spec.rb
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

0 comments on commit b025669

Please sign in to comment.