simple_versioning 0.0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/README.markdown +86 -0
- data/Rakefile +2 -0
- data/lib/simple_versioning/version.rb +10 -0
- data/lib/simple_versioning.rb +41 -0
- data/simple_versioning.gemspec +23 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# simple_versioning for ActiveRecord
|
2
|
+
|
3
|
+
Simple versioning is a plain vanilla versioning support. It simply tracks all your attributes in a polymorphically associated model.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
In the Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_versioning'
|
10
|
+
|
11
|
+
Next, generate a simple migration, call it anything you like, the content needs to be (I'll add a generator in a couple of days):
|
12
|
+
|
13
|
+
def self.up
|
14
|
+
create_table :versions do |t|
|
15
|
+
t.belongs_to :versioned, :polymorphic => true
|
16
|
+
t.text :yaml
|
17
|
+
t.integer :number
|
18
|
+
t.timestamps
|
19
|
+
end
|
20
|
+
|
21
|
+
change_table :versions do |t|
|
22
|
+
t.index [:versioned_id, :versioned_type]
|
23
|
+
t.index :number
|
24
|
+
t.index :created_at
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.down
|
29
|
+
drop_table :versions
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
## Example
|
34
|
+
|
35
|
+
To version an ActiveRecord model, simply add `track` to your class like so:
|
36
|
+
|
37
|
+
class User < ActiveRecord::Base
|
38
|
+
track
|
39
|
+
|
40
|
+
validates_presence_of :first_name, :last_name
|
41
|
+
|
42
|
+
def name
|
43
|
+
"#{first_name} #{last_name}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
Thats it!
|
48
|
+
|
49
|
+
>> u = User.create(:first_name => "Yukihiro", :last_name => "Matsumoto")
|
50
|
+
=> #<User first_name: "Yukihiro", last_name: "Matsumoto">
|
51
|
+
>> u.version_number
|
52
|
+
=> 1
|
53
|
+
>> u.update_attribute(:last_name, "Matz")
|
54
|
+
=> true
|
55
|
+
>> u.name
|
56
|
+
=> "Yukihiro Matz"
|
57
|
+
>> u.version_number
|
58
|
+
=> 2
|
59
|
+
>> u.revert_to(1)
|
60
|
+
=> 1
|
61
|
+
>> u.name
|
62
|
+
=> "Yukihiro Matsumoto"
|
63
|
+
>> u.version_number
|
64
|
+
=> 1
|
65
|
+
>> u.save
|
66
|
+
=> true
|
67
|
+
>> u.version_number
|
68
|
+
=> 3
|
69
|
+
>> u.update_attribute(:last_name, "Matsumoto II")
|
70
|
+
=> true
|
71
|
+
>> u.name
|
72
|
+
=> "Yukihiro Matsumoto II"
|
73
|
+
>> u.version_number
|
74
|
+
=> 4
|
75
|
+
|
76
|
+
|
77
|
+
### What is coming in 0.0.2
|
78
|
+
|
79
|
+
- Save without versioning
|
80
|
+
- Mutator methods
|
81
|
+
|
82
|
+
|
83
|
+
### Thanks!
|
84
|
+
|
85
|
+
Thanks to [laserlemon](http://github.com/laserlemon "") and his [`vestal_versions`](https://github.com/laserlemon/vestal_versions "") gem!
|
86
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'simple_versioning/version'
|
2
|
+
|
3
|
+
module SimpleVersioning
|
4
|
+
module ClassMethods
|
5
|
+
def track(options = {}, &block)
|
6
|
+
options.merge!(
|
7
|
+
:as => :versioned,
|
8
|
+
:class_name => 'SimpleVersioning::Version'
|
9
|
+
)
|
10
|
+
has_many :versions, options, &block
|
11
|
+
before_save :prepare_version
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
def self.included(base)
|
17
|
+
base.extend(ClassMethods)
|
18
|
+
end
|
19
|
+
|
20
|
+
def prepare_version
|
21
|
+
return false unless changed?
|
22
|
+
self.versions.build :yaml => attributes, :number => version_number
|
23
|
+
end
|
24
|
+
|
25
|
+
def version_number
|
26
|
+
self.versions.maximum(:number).to_i + 1
|
27
|
+
end
|
28
|
+
|
29
|
+
def revert_to(revision)
|
30
|
+
@revert_to = self.versions.find_by_number revision
|
31
|
+
return false unless @revert_to.present?
|
32
|
+
@revert_to.yaml.each do |attribute_name, value|
|
33
|
+
write_attribute(attribute_name, value)
|
34
|
+
end
|
35
|
+
@revert_to.number
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# "test on file"
|
41
|
+
ActiveRecord::Base.send :include, SimpleVersioning::InstanceMethods
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "simple_versioning/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "simple_versioning"
|
7
|
+
s.version = SimpleVersioning::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Swanand Pagnis"]
|
10
|
+
s.email = ["swanand@kaverisoft.com"]
|
11
|
+
s.homepage = "http://github.com/spp/simple_versioning"
|
12
|
+
s.summary = %q{Provide a vanilla versioning support}
|
13
|
+
s.description = %q{Keeps track of your models attributes}
|
14
|
+
|
15
|
+
s.rubyforge_project = "simple_versioning"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency 'activerecord', '>= 2.2.2'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_versioning
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 73
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
- 1
|
11
|
+
version: 0.0.1.1
|
12
|
+
platform: ruby
|
13
|
+
authors:
|
14
|
+
- Swanand Pagnis
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-03-28 00:00:00 +05:30
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: activerecord
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 2
|
33
|
+
- 2
|
34
|
+
- 2
|
35
|
+
version: 2.2.2
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
description: Keeps track of your models attributes
|
39
|
+
email:
|
40
|
+
- swanand@kaverisoft.com
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
extra_rdoc_files: []
|
46
|
+
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- README.markdown
|
51
|
+
- Rakefile
|
52
|
+
- lib/simple_versioning.rb
|
53
|
+
- lib/simple_versioning/version.rb
|
54
|
+
- simple_versioning.gemspec
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/spp/simple_versioning
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project: simple_versioning
|
85
|
+
rubygems_version: 1.3.7
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Provide a vanilla versioning support
|
89
|
+
test_files: []
|
90
|
+
|