acts_as_versionable 0.0.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,79 +3,130 @@ require 'acts_as_versionable/version'
3
3
  # ActsAsVersionable
4
4
  module ActsAsVersionable
5
5
 
6
- class NoSuchVersionError < Exception; end
7
-
8
- def self.included(base)
9
- base.extend ClassMethods
6
+ class NoSuchVersionError < Exception
10
7
  end
11
-
12
- module ClassMethods
13
8
 
14
- def create_class(name, superclass, &block)
15
- klass = Class.new superclass, &block
16
- Object.const_set name, klass
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ def create_class(name, superclass, &block)
17
+ klass = Class.new superclass, &block
18
+ Object.const_set name, klass
19
+ end
20
+
21
+ def acts_as_versionable options={}
22
+ options.symbolize_keys!
23
+
24
+ has_many :versions, :class_name => "#{self.name.pluralize}Versions", :dependent => :destroy
25
+ after_save :apply_versioning
26
+ after_save :clear_old_versions
27
+
28
+ attr_accessor :local_changes
29
+ cattr_accessor :max_versions
30
+
31
+ self.max_versions = options[:limit].to_i
32
+
33
+ class << create_class("#{self.name.pluralize}Versions", ActiveRecord::Base)
34
+ def actual_columns
35
+ content_columns.reject { |c| c.type == :datetime || c.name == 'versioned_as' }
36
+ end
37
+ end
38
+
39
+ include InstanceMethods
40
+ end
41
+
17
42
  end
43
+
44
+ module InstanceMethods
45
+
46
+ def version
47
+ current_version || 0
48
+ end
18
49
 
19
- def acts_as_versionable(opts = {})
20
- has_many :versions, :class_name => "#{self}Version", :dependent => :destroy
21
- after_save :apply_versioning
50
+ def revert_to(version)
51
+ revision = versions.find_by_versioned_as(version)
22
52
 
23
- attr_accessor :local_changes
53
+ raise NoSuchVersionError, "Couldn't find #{version} version" if revision.blank?
24
54
 
25
- class << create_class("#{self}Version", ActiveRecord::Base)
26
- def actual_columns
27
- content_columns.reject { |c| c.type == :datetime || c.name == 'versioned_as' }
55
+ versions.actual_columns.each do |column|
56
+ self[column.name] = revision[column.name]
28
57
  end
58
+
59
+ self.current_version = version
60
+ self.local_changes = true
61
+ self.save
29
62
  end
30
63
 
31
- include InstanceMethods
32
- end
64
+ private
33
65
 
34
- end
66
+ def apply_versioning
67
+ unless self.local_changes
68
+ version_content = {}
69
+ last_version = version + 1
35
70
 
36
- module InstanceMethods
71
+ versions.actual_columns.each do |column|
72
+ version_content[column.name] = self[column.name]
73
+ end
37
74
 
38
- def version
39
- current_version || 0
40
- end
75
+ version_content.merge!(:versioned_as => last_version)
41
76
 
42
- def revert_to(version)
43
- revision = versions.find_by_versioned_as(version)
44
-
45
- raise NoSuchVersionError, "Couldn't find #{version} version" if revision.blank?
77
+ if versions.create(version_content)
78
+ self.local_changes = true
79
+ self.update_attribute(:current_version, last_version)
80
+ end
81
+ end
46
82
 
47
- versions.actual_columns.each do |column|
48
- self[column.name] = revision[column.name]
83
+ self.local_changes = false
49
84
  end
50
85
 
51
- self.current_version = version
52
- self.local_changes = true
53
- self.save
86
+ def clear_old_versions
87
+ return if self.max_versions == 0
88
+ excess_baggage = self.current_version - self.max_versions
89
+ if excess_baggage > 0
90
+ versions.where("versioned_as <= ?",excess_baggage).destroy_all
91
+ end
92
+ end
93
+
54
94
  end
95
+ end
55
96
 
56
- private
57
-
58
- def apply_versioning
59
- unless self.local_changes
60
- version_content = {}
61
- last_version = version + 1
97
+ ActiveRecord::Base.class_eval { include ActsAsVersionable }
62
98
 
63
- versions.actual_columns.each do |column|
64
- version_content[column.name] = self[column.name]
65
- end
99
+ #def self.included(base)
100
+ # base.extend ClassMethods
101
+ #end
66
102
 
67
- version_content.merge!(:versioned_as => last_version)
103
+ #module ClassMethods
104
+
105
+ # def create_class(name, superclass, &block)
106
+ # klass = Class.new superclass, &block
107
+ # Object.const_set name, klass
108
+ # end
68
109
 
69
- if versions.create(version_content)
70
- self.local_changes = true
71
- self.update_attribute(:current_version, last_version)
72
- end
73
- end
110
+ # def acts_as_versionable(opts = {})
74
111
 
75
- self.local_changes = false
76
- end
112
+ # has_many :versions, :class_name => "#{self}Version", :dependent => :destroy
113
+ # after_save :apply_versioning
114
+ # after_save :clear_old_versions
77
115
 
78
- end
116
+ # attr_accessor :local_changes
79
117
 
80
- end
118
+ # class << create_class("#{self}Version", ActiveRecord::Base)
119
+ # def actual_columns
120
+ # content_columns.reject { |c| c.type == :datetime || c.name == 'versioned_as' }
121
+ # end
122
+ # end
123
+
124
+ #include InstanceMethods
125
+ # end
126
+
127
+ #end
128
+
129
+
130
+
131
+ #end
81
132
 
@@ -1,3 +1,3 @@
1
1
  module ActsAsVersionable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,16 +1,13 @@
1
1
  <%
2
- model = model_name.constantize
3
- model_name = model_name.downcase # model_name.downcase! - can't modify frozen string
4
- versions_table = model_name + '_versions'
5
- columns = model.content_columns.reject { |c| c.type == :datetime }
2
+ versions_table = "#{@model_name.pluralize}_versions".downcase
3
+ columns = @model.classify.constantize.content_columns.reject { |c| c.type == :datetime }
6
4
  %>
7
5
  class <%= migration_name %> < ActiveRecord::Migration
8
6
  def self.up
9
7
  create_table :<%= versions_table %> do |t|
10
8
  <% columns.each do |column| %>
11
- t.column :<%= column.name %>, :<%= column.type %>
9
+ t.column :<%= column.name %>, :<%= column.type %>
12
10
  <% end %>
13
-
14
11
  t.integer :<%= model_name + '_id' %>
15
12
  t.integer :versioned_as
16
13
  t.timestamps
@@ -0,0 +1,37 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class VersionableGenerator < Rails::Generators::NamedBase
5
+ include Rails::Generators::Migration
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ argument :klass, :type => :string, :description => "Model to versionate"
8
+
9
+ attr_accessor :migration_name, :model_name, :model
10
+
11
+ def self.next_migration_number(path)
12
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
13
+ end
14
+
15
+ def create_model_file
16
+ @model_name = klass.downcase
17
+ @model = klass
18
+ puts @model
19
+ @migration_name = "create_#{@model_name}_versions".camelize
20
+ puts @migration_name
21
+ migration_template "migration.rb.erb", "db/migrate/create_#{@model_name}_versions.rb"
22
+ end
23
+
24
+
25
+ #def manifest
26
+ #
27
+ # record do |m|
28
+ # m.migration_template('migration.rb', 'db/migrate',
29
+ # :assigns => {
30
+ # :model_name => @model_name,
31
+ # :migration_name => "CreateVersionsFor#{@model_name.pluralize}"
32
+ # },
33
+ # :migration_file_name => "create_versions_for_#{model_name_for_migration}")
34
+ # end
35
+ #end
36
+
37
+ end
@@ -2,7 +2,8 @@ require 'rubygems'
2
2
  gem 'activerecord'
3
3
  require 'active_record'
4
4
  require 'test/unit'
5
- require "#{File.dirname(__FILE__)}/../init"
5
+ #require "#{File.dirname(__FILE__)}/../init"
6
+ require File.join(File.dirname(__FILE__), '../lib', 'acts_as_versionable')
6
7
 
7
8
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
8
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_versionable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -25,8 +25,8 @@ files:
25
25
  - init.rb
26
26
  - lib/acts_as_versionable.rb
27
27
  - lib/acts_as_versionable/version.rb
28
- - lib/generators/versionize/templates/migration.rb
29
- - lib/generators/versionize/versionize_generator.rb
28
+ - lib/generators/versionable/templates/migration.rb.erb
29
+ - lib/generators/versionable/versionable_generator.rb
30
30
  - test/acts_as_versionable_test.rb
31
31
  homepage: ''
32
32
  licenses: []
@@ -1,28 +0,0 @@
1
- class VersionableGenerator < Rails::Generators::Base
2
- attr_accessor :model_name
3
-
4
- def initialize(args, opts = {})
5
- super
6
- @model_name = args.first
7
- end
8
-
9
- def manifest
10
- model_name_for_migration = @model_name.downcase.pluralize
11
-
12
- record do |m|
13
- m.migration_template('migration.rb', 'db/migrate',
14
- :assigns => {
15
- :model_name => @model_name,
16
- :migration_name => "CreateVersionsFor#{@model_name.pluralize}"
17
- },
18
- :migration_file_name => "create_versions_for_#{model_name_for_migration}")
19
- end
20
- end
21
-
22
- protected
23
-
24
- def banner
25
- "Usage: #{$0} versionize ModelName"
26
- end
27
-
28
- end