sequel_acts_as_versionable 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sequel_acts_as_versionable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Misha Conway
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # SequelActsAsVersionable
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sequel_acts_as_versionable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sequel_acts_as_versionable
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,142 @@
1
+ require "sequel_acts_as_versionable/version"
2
+
3
+ module Sequel
4
+ module Plugins
5
+ module ActsAsVersionable
6
+ def self.configure(model, opts={})
7
+ model.instance_eval do
8
+ self.max_versions = opts[:max_versions] || 10
9
+
10
+ one_to_many :internal_versions,
11
+ :class => self,
12
+ :key => :version_id,
13
+ :order => :version_number.desc,
14
+ :dependent => :destroy
15
+
16
+ many_to_one :parent_version, :class => self, :key => :version_id
17
+
18
+
19
+ schema_errors = []
20
+ schema_errors << "error: #{self.table_name} must have a version_id column" unless columns.include? :version_id
21
+ schema_errors << "error: #{self.table_name} must have a version_number column" unless columns.include? :version_number
22
+ unless schema_errors.size.zero?
23
+ puts schema_errors.join "\n"
24
+ end
25
+ end
26
+ end
27
+
28
+
29
+ class NoSuchVersionError < Exception
30
+ end
31
+
32
+ #extend ActiveSupport::Concern
33
+
34
+ # included do |base|
35
+ # end
36
+
37
+ module ClassMethods
38
+ attr_accessor :max_versions
39
+
40
+ def last_versions
41
+ self.filter :version_id => nil
42
+ end
43
+
44
+ def get_versionable id, version
45
+ self.filter :version_id => id, :version_number => version
46
+ end
47
+ end
48
+
49
+ module InstanceMethods
50
+ def after_save
51
+ super
52
+ create_new_version
53
+ end
54
+
55
+ def validate
56
+ super
57
+ if !new? && !versionable?
58
+ errors.add :base, "Can't modify versioned record. Use version.editable_version!!"
59
+ end
60
+ end
61
+
62
+ def revert_to_version(number)
63
+ version = get_version number
64
+ editable = editable_version
65
+ copy_version_values version, editable
66
+ editable.version_number = nil
67
+ editable.version_id = nil
68
+ editable.save
69
+ editable
70
+ end
71
+
72
+ # return a determined version number
73
+ def get_version(number)
74
+ version = versions_dataset.where(:version_number => number).first
75
+ raise NoSuchVersionError if version.nil?
76
+ version
77
+ end
78
+
79
+ # return an array with all versions
80
+ def versions_dataset
81
+ if versionable?
82
+ internal_versions_dataset
83
+ else
84
+ self.class.where(:version_id => self.version_id).order(:version_number).reverse
85
+ end
86
+ end
87
+
88
+ def versions
89
+ versions_dataset.all
90
+ end
91
+
92
+ # check if we are in main version
93
+ def versionable?
94
+ version_id.nil?
95
+ end
96
+
97
+ # return the current version
98
+ def version
99
+ return last_version if versionable?
100
+ version_number
101
+ end
102
+
103
+ # return the last version number
104
+ def last_version
105
+ return 0 if versions.count == 0
106
+ versions.first.version_number
107
+ end
108
+
109
+ # return the last version editable
110
+ def editable_version
111
+ parent_version.nil? ? self : parent_version
112
+ end
113
+
114
+ private
115
+
116
+ # callback after save
117
+ def create_new_version
118
+ if versionable?
119
+
120
+ cloned = copy_version_values self, self.class.new
121
+ cloned.version_id = self.id
122
+ cloned.version_number = last_version + 1
123
+ cloned.save
124
+
125
+ # purge max limit
126
+ excess_baggage = cloned.version_number - self.class.max_versions
127
+ if excess_baggage > 0
128
+ versions_dataset.where{ |row| row.version_number <= excess_baggage}.delete
129
+ end
130
+ end
131
+ end
132
+
133
+ def copy_version_values(from, to)
134
+ columns = self.class.columns.reject { |c| c == :id }
135
+ columns.each { |c| to.values[c] = from.values[c] }
136
+ to
137
+ end
138
+ end
139
+
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,3 @@
1
+ module SequelActsAsVersionable
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sequel_acts_as_versionable/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sequel_acts_as_versionable"
8
+ gem.version = SequelActsAsVersionable::VERSION
9
+ gem.authors = ["Misha Conway"]
10
+ gem.email = ["MishaAConway@gmail.com"]
11
+ gem.description = %q{Versioning for Sequel ORM models. Port of Carlos Segura's acts_as_versionable.}
12
+ gem.summary = %q{Versioning for Sequel ORM models. Port of Carlos Segura's acts_as_versionable.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_acts_as_versionable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Misha Conway
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Versioning for Sequel ORM models. Port of Carlos Segura's acts_as_versionable.
15
+ email:
16
+ - MishaAConway@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/sequel_acts_as_versionable.rb
27
+ - lib/sequel_acts_as_versionable/version.rb
28
+ - sequel_acts_as_versionable.gemspec
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ none: false
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ none: false
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.24
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Versioning for Sequel ORM models. Port of Carlos Segura's acts_as_versionable.
53
+ test_files: []