brianjlandau-vestal_versions 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +27 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +196 -0
  5. data/Rakefile +50 -0
  6. data/VERSION +1 -0
  7. data/generators/vestal_versions/templates/initializer.rb +9 -0
  8. data/generators/vestal_versions/templates/migration.rb +28 -0
  9. data/generators/vestal_versions/vestal_versions_generator.rb +10 -0
  10. data/init.rb +1 -0
  11. data/lib/vestal_versions.rb +104 -0
  12. data/lib/vestal_versions/associations.rb +67 -0
  13. data/lib/vestal_versions/changes.rb +125 -0
  14. data/lib/vestal_versions/conditions.rb +69 -0
  15. data/lib/vestal_versions/configuration.rb +40 -0
  16. data/lib/vestal_versions/control.rb +175 -0
  17. data/lib/vestal_versions/creation.rb +85 -0
  18. data/lib/vestal_versions/deletion.rb +46 -0
  19. data/lib/vestal_versions/options.rb +45 -0
  20. data/lib/vestal_versions/reload.rb +22 -0
  21. data/lib/vestal_versions/reset.rb +28 -0
  22. data/lib/vestal_versions/reversion.rb +92 -0
  23. data/lib/vestal_versions/tagging.rb +54 -0
  24. data/lib/vestal_versions/users.rb +56 -0
  25. data/lib/vestal_versions/version.rb +76 -0
  26. data/lib/vestal_versions/versioned.rb +30 -0
  27. data/lib/vestal_versions/versions.rb +74 -0
  28. data/test/associations_test.rb +49 -0
  29. data/test/changes_test.rb +169 -0
  30. data/test/conditions_test.rb +137 -0
  31. data/test/configuration_test.rb +39 -0
  32. data/test/control_test.rb +152 -0
  33. data/test/creation_test.rb +110 -0
  34. data/test/deletion_test.rb +121 -0
  35. data/test/options_test.rb +52 -0
  36. data/test/reload_test.rb +19 -0
  37. data/test/reset_test.rb +112 -0
  38. data/test/reversion_test.rb +99 -0
  39. data/test/schema.rb +62 -0
  40. data/test/tagging_test.rb +39 -0
  41. data/test/test_helper.rb +12 -0
  42. data/test/users_test.rb +25 -0
  43. data/test/version_test.rb +61 -0
  44. data/test/versioned_test.rb +18 -0
  45. data/test/versions_test.rb +172 -0
  46. data/vestal_versions.gemspec +124 -0
  47. metadata +245 -0
@@ -0,0 +1,85 @@
1
+ module VestalVersions
2
+ # Adds the functionality necessary to control version creation on a versioned instance of
3
+ # ActiveRecord::Base.
4
+ module Creation
5
+ def self.included(base) # :nodoc:
6
+ base.class_eval do
7
+ extend ClassMethods
8
+ include InstanceMethods
9
+
10
+ after_update :create_version, :if => :create_version?
11
+ after_update :update_version, :if => :update_version?
12
+
13
+ class << self
14
+ alias_method_chain :prepare_versioned_options, :creation
15
+ end
16
+ end
17
+ end
18
+
19
+ # Class methods added to ActiveRecord::Base to facilitate the creation of new versions.
20
+ module ClassMethods
21
+ # Overrides the basal +prepare_versioned_options+ method defined in VestalVersions::Options
22
+ # to extract the <tt>:only</tt> and <tt>:except</tt> options into +vestal_versions_options+.
23
+ def prepare_versioned_options_with_creation(options)
24
+ result = prepare_versioned_options_without_creation(options)
25
+
26
+ self.vestal_versions_options[:only] = Array(options.delete(:only)).map(&:to_s).uniq if options[:only]
27
+ self.vestal_versions_options[:except] = Array(options.delete(:except)).map(&:to_s).uniq if options[:except]
28
+
29
+ result
30
+ end
31
+ end
32
+
33
+ # Instance methods that determine whether to save a version and actually perform the save.
34
+ module InstanceMethods
35
+ private
36
+ # Returns whether a new version should be created upon updating the parent record.
37
+ def create_version?
38
+ !version_changes.blank?
39
+ end
40
+
41
+ # Creates a new version upon updating the parent record.
42
+ def create_version(attributes = nil)
43
+ versions.create(attributes || version_attributes)
44
+ reset_version_changes
45
+ reset_version
46
+ end
47
+
48
+ # Returns whether the last version should be updated upon updating the parent record.
49
+ # This method is overridden in VestalVersions::Control to account for a control block that
50
+ # merges changes onto the previous version.
51
+ def update_version?
52
+ false
53
+ end
54
+
55
+ # Updates the last version's changes by appending the current version changes.
56
+ def update_version
57
+ return create_version unless v = versions.last
58
+ v.modifications_will_change!
59
+ v.update_attributes(:modifications => v.changes.append_changes(version_changes))
60
+ reset_version_changes
61
+ reset_version
62
+ end
63
+
64
+ # Returns an array of column names that should be included in the changes of created
65
+ # versions. If <tt>vestal_versions_options[:only]</tt> is specified, only those columns
66
+ # will be versioned. Otherwise, if <tt>vestal_versions_options[:except]</tt> is specified,
67
+ # all columns will be versioned other than those specified. Without either option, the
68
+ # default is to version all columns. At any rate, the four "automagic" timestamp columns
69
+ # maintained by Rails are never versioned.
70
+ def versioned_columns
71
+ case
72
+ when vestal_versions_options[:only] then self.class.column_names & vestal_versions_options[:only]
73
+ when vestal_versions_options[:except] then self.class.column_names - vestal_versions_options[:except]
74
+ else self.class.column_names
75
+ end - %w(created_at created_on updated_at updated_on)
76
+ end
77
+
78
+ # Specifies the attributes used during version creation. This is separated into its own
79
+ # method so that it can be overridden by the VestalVersions::Users feature.
80
+ def version_attributes
81
+ {:modifications => version_changes, :number => last_version + 1}
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,46 @@
1
+ module VestalVersions
2
+ # Allows version creation to occur conditionally based on given <tt>:if</tt> and/or
3
+ # <tt>:unless</tt> options.
4
+ module Deletion
5
+ def self.included(base) # :nodoc:
6
+ base.class_eval do
7
+ extend ClassMethods
8
+ include InstanceMethods
9
+
10
+ before_destroy :create_destroyed_version, :if => :delete_version?
11
+
12
+ class << self
13
+ alias_method_chain :prepare_versioned_options, :deletion
14
+ end
15
+ end
16
+ end
17
+
18
+ # Class methods on ActiveRecord::Base
19
+ module ClassMethods
20
+ # After the original +prepare_versioned_options+ method cleans the given options, this alias
21
+ # also extracts the <tt>:depedent</tt> if it set to <tt>:tracking</tt>
22
+ def prepare_versioned_options_with_deletion(options)
23
+ result = prepare_versioned_options_without_deletion(options)
24
+ if result[:dependent] == :tracking
25
+ self.vestal_versions_options[:track_destroy] = true
26
+ options.delete(:dependent)
27
+ end
28
+
29
+ result
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+ private
35
+
36
+ def delete_version?
37
+ vestal_versions_options[:track_destroy]
38
+ end
39
+
40
+ def create_destroyed_version
41
+ create_version({:modifications => attributes, :number => last_version + 1, :tag => 'deleted'})
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,45 @@
1
+ module VestalVersions
2
+ # Provides +versioned+ options conversion and cleanup.
3
+ module Options
4
+ def self.included(base) # :nodoc:
5
+ base.class_eval do
6
+ extend ClassMethods
7
+ end
8
+ end
9
+
10
+ # Class methods that provide preparation of options passed to the +versioned+ method.
11
+ module ClassMethods
12
+ # The +prepare_versioned_options+ method has three purposes:
13
+ # 1. Populate the provided options with default values where needed
14
+ # 2. Prepare options for use with the +has_many+ association
15
+ # 3. Save user-configurable options in a class-level variable
16
+ #
17
+ # Options are given priority in the following order:
18
+ # 1. Those passed directly to the +versioned+ method
19
+ # 2. Those specified in an initializer +configure+ block
20
+ # 3. Default values specified in +prepare_versioned_options+
21
+ #
22
+ # The method is overridden in feature modules that require specific options outside the
23
+ # standard +has_many+ associations.
24
+ def prepare_versioned_options(options)
25
+ options.symbolize_keys!
26
+ options.reverse_merge!(Configuration.options)
27
+ options.reverse_merge!(
28
+ :class_name => 'VestalVersions::Version',
29
+ :dependent => :delete_all
30
+ )
31
+ options.reverse_merge!(
32
+ :order => "#{options[:class_name].constantize.table_name}.number ASC"
33
+ )
34
+
35
+ class_inheritable_accessor :vestal_versions_options
36
+ self.vestal_versions_options = options.dup
37
+
38
+ options.merge!(
39
+ :as => :versioned,
40
+ :extend => Array(options[:extend]).unshift(Versions)
41
+ )
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,22 @@
1
+ module VestalVersions
2
+ # Ties into the existing ActiveRecord::Base#reload method to ensure that version information
3
+ # is properly reset.
4
+ module Reload
5
+ def self.included(base) # :nodoc:
6
+ base.class_eval do
7
+ include InstanceMethods
8
+
9
+ end
10
+ end
11
+
12
+ # Adds instance methods into ActiveRecord::Base to tap into the +reload+ method.
13
+ module InstanceMethods
14
+ # Overrides ActiveRecord::Base#reload, resetting the instance-variable-cached version number
15
+ # before performing the original +reload+ method.
16
+ def reload(*args)
17
+ reset_version
18
+ super
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ module VestalVersions
2
+ # Adds the ability to "reset" (or hard revert) a versioned ActiveRecord::Base instance.
3
+ module Reset
4
+ def self.included(base) # :nodoc:
5
+ base.class_eval do
6
+ include InstanceMethods
7
+ end
8
+ end
9
+
10
+ # Adds the instance methods required to reset an object to a previous version.
11
+ module InstanceMethods
12
+ # Similar to +revert_to!+, the +reset_to!+ method reverts an object to a previous version,
13
+ # only instead of creating a new record in the version history, +reset_to!+ deletes all of
14
+ # the version history that occurs after the version reverted to.
15
+ #
16
+ # The action taken on each version record after the point of reversion is determined by the
17
+ # <tt>:dependent</tt> option given to the +versioned+ method. See the +versioned+ method
18
+ # documentation for more details.
19
+ def reset_to!(value)
20
+ if saved = skip_version{ revert_to!(value) }
21
+ versions.send(:delete_records, versions.after(value))
22
+ reset_version
23
+ end
24
+ saved
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,92 @@
1
+ module VestalVersions
2
+ # Enables versioned ActiveRecord::Base instances to revert to a previously saved version.
3
+ module Reversion
4
+ def self.included(base) # :nodoc:
5
+ base.class_eval do
6
+ include InstanceMethods
7
+ end
8
+ end
9
+
10
+ # Provides the base instance methods required to revert a versioned instance.
11
+ module InstanceMethods
12
+ # Returns the current version number for the versioned object.
13
+ def version
14
+ @version ||= last_version
15
+ end
16
+
17
+ # Accepts a value corresponding to a specific version record, builds a history of changes
18
+ # between that version and the current version, and then iterates over that history updating
19
+ # the object's attributes until the it's reverted to its prior state.
20
+ #
21
+ # The single argument should adhere to one of the formats as documented in the +at+ method of
22
+ # VestalVersions::Versions.
23
+ #
24
+ # After the object is reverted to the target version, it is not saved. In order to save the
25
+ # object after the reversion, use the +revert_to!+ method.
26
+ #
27
+ # The version number of the object will reflect whatever version has been reverted to, and
28
+ # the return value of the +revert_to+ method is also the target version number.
29
+ def revert_to(value)
30
+ self.class.reflections.each do |association, reflection|
31
+ self.send(association).reset if reflection.macro == :has_many
32
+ end
33
+
34
+ to_number = versions.number_at(value)
35
+
36
+ changes_between(version, to_number).each do |attribute, change|
37
+ write_attribute(attribute, change.last)
38
+ end
39
+
40
+ reset_version(to_number)
41
+ end
42
+
43
+ # Behaves similarly to the +revert_to+ method except that it automatically saves the record
44
+ # after the reversion. The return value is the success of the save.
45
+ def revert_to!(value)
46
+ revert_to(value)
47
+ reset_version if saved = save
48
+ saved
49
+ end
50
+
51
+ # Returns a boolean specifying whether the object has been reverted to a previous version or
52
+ # if the object represents the latest version in the version history.
53
+ def reverted?
54
+ version != last_version
55
+ end
56
+
57
+
58
+ private
59
+
60
+ # Mixes in the reverted_from value if it is currently within a revert
61
+ def version_attributes
62
+ attributes = super
63
+
64
+ if @reverted_from.nil?
65
+ attributes
66
+ else
67
+ attributes.merge(:reverted_from => @reverted_from)
68
+ end
69
+ end
70
+
71
+
72
+ # Returns the number of the last created version in the object's version history.
73
+ #
74
+ # If no associated versions exist, the object is considered at version 1.
75
+ def last_version
76
+ @last_version ||= versions.maximum(:number) || 1
77
+ end
78
+
79
+ # Clears the cached version number instance variables so that they can be recalculated.
80
+ # Useful after a new version is created.
81
+ def reset_version(version = nil)
82
+ if version.nil?
83
+ @last_version = nil
84
+ @reverted_from = nil
85
+ else
86
+ @reverted_from = version
87
+ end
88
+ @version = version
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,54 @@
1
+ module VestalVersions
2
+ # Allows specific versions to be tagged with a custom string. Useful for assigning a more
3
+ # meaningful value to a version for the purpose of reversion.
4
+ module Tagging
5
+ def self.included(base) # :nodoc:
6
+ Version.send(:include, VersionMethods)
7
+
8
+ base.class_eval do
9
+ include InstanceMethods
10
+ end
11
+ end
12
+
13
+ # Adds an instance method which allows version tagging through the parent object.
14
+ module InstanceMethods
15
+ # Accepts a single string argument which is attached to the version record associated with
16
+ # the current version number of the parent object.
17
+ #
18
+ # Returns the given tag if successful, nil if not. Tags must be unique within the scope of
19
+ # the parent object. Tag creation will fail if non-unique.
20
+ #
21
+ # Version records corresponding to version number 1 are not typically created, but one will
22
+ # be built to house the given tag if the parent object's current version number is 1.
23
+ def tag_version(tag)
24
+ v = versions.at(version) || versions.build(:number => 1)
25
+ v.tag!(tag)
26
+ end
27
+ end
28
+
29
+ # Instance methods included into VestalVersions::Version to enable version tagging.
30
+ module VersionMethods
31
+ def self.included(base) # :nodoc:
32
+ base.class_eval do
33
+ validates_uniqueness_of :tag, :scope => [:versioned_id, :versioned_type], :if => :validate_tags?
34
+ end
35
+ end
36
+
37
+ # Attaches the given string to the version tag column. If the uniqueness validation fails,
38
+ # nil is returned. Otherwise, the given string is returned.
39
+ def tag!(tag)
40
+ write_attribute(:tag, tag)
41
+ save ? tag : nil
42
+ end
43
+
44
+ # Simply returns a boolean signifying whether the version instance has a tag value attached.
45
+ def tagged?
46
+ !tag.nil?
47
+ end
48
+
49
+ def validate_tags?
50
+ tagged? && tag != 'deleted'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,56 @@
1
+ module VestalVersions
2
+ # Provides a way for information to be associated with specific versions as to who was
3
+ # responsible for the associated update to the parent.
4
+ module Users
5
+ def self.included(base) # :nodoc:
6
+ Version.send(:include, VersionMethods)
7
+
8
+ base.class_eval do
9
+ include InstanceMethods
10
+
11
+ attr_accessor :updated_by
12
+ end
13
+ end
14
+
15
+ # Methods added to versioned ActiveRecord::Base instances to enable versioning with additional
16
+ # user information.
17
+ module InstanceMethods
18
+ private
19
+ # Overrides the +version_attributes+ method to include user information passed into the
20
+ # parent object, by way of a +updated_by+ attr_accessor.
21
+ def version_attributes
22
+ super.merge(:user => updated_by)
23
+ end
24
+ end
25
+
26
+ # Instance methods added to VestalVersions::Version to accomodate incoming user information.
27
+ module VersionMethods
28
+ def self.included(base) # :nodoc:
29
+ base.class_eval do
30
+ belongs_to :user, :polymorphic => true
31
+
32
+ alias_method_chain :user, :name
33
+ alias_method_chain :user=, :name
34
+ end
35
+ end
36
+
37
+ # Overrides the +user+ method created by the polymorphic +belongs_to+ user association. If
38
+ # the association is absent, defaults to the +user_name+ string column. This allows
39
+ # VestalVersions::Version#user to either return an ActiveRecord::Base object or a string,
40
+ # depending on what is sent to the +user_with_name=+ method.
41
+ def user_with_name
42
+ user_without_name || user_name
43
+ end
44
+
45
+ # Overrides the +user=+ method created by the polymorphic +belongs_to+ user association.
46
+ # Based on the class of the object given, either the +user+ association columns or the
47
+ # +user_name+ string column is populated.
48
+ def user_with_name=(value)
49
+ case value
50
+ when ActiveRecord::Base then self.user_without_name = value
51
+ else self.user_name = value
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,76 @@
1
+ module VestalVersions
2
+ # The ActiveRecord model representing versions.
3
+ class Version < ActiveRecord::Base
4
+ include Comparable
5
+
6
+ # Associate polymorphically with the parent record.
7
+ belongs_to :versioned, :polymorphic => true
8
+
9
+ # ActiveRecord::Base#changes is an existing method, so before serializing the +changes+ column,
10
+ # the existing +changes+ method is undefined. The overridden +changes+ method pertained to
11
+ # dirty attributes, but will not affect the partial updates functionality as that's based on
12
+ # an underlying +changed_attributes+ method, not +changes+ itself.
13
+ undef_method :changes
14
+ def changes
15
+ self[:modifications]
16
+ end
17
+ serialize :modifications, Hash
18
+
19
+ # In conjunction with the included Comparable module, allows comparison of version records
20
+ # based on their corresponding version numbers, creation timestamps and IDs.
21
+ def <=>(other)
22
+ [number, created_at, id].map(&:to_i) <=> [other.number, other.created_at, other.id].map(&:to_i)
23
+ end
24
+
25
+ # Returns whether the version has a version number of 1. Useful when deciding whether to ignore
26
+ # the version during reversion, as initial versions have no serialized changes attached. Helps
27
+ # maintain backwards compatibility.
28
+ def initial?
29
+ number == 1
30
+ end
31
+
32
+ # Returns the original version number that this version was.
33
+ def original_number
34
+ if reverted_from.nil?
35
+ number
36
+ else
37
+ version = versioned.versions.at(reverted_from)
38
+ version.nil? ? 1 : version.original_number
39
+ end
40
+ end
41
+
42
+ def restore!
43
+ model = restore
44
+
45
+ if model
46
+ model.save!
47
+ destroy
48
+ end
49
+
50
+ model
51
+ end
52
+
53
+ def restore
54
+ if tag == 'deleted'
55
+ attrs = modifications
56
+
57
+ class_name = attrs['type'].blank? ? versioned_type : attrs['type']
58
+ klass = class_name.constantize
59
+ model = klass.new
60
+
61
+ attrs.each do |k, v|
62
+ begin
63
+ model.send "#{k}=", v
64
+ rescue NoMethodError
65
+ logger.warn "Attribute #{k} does not exist on #{class_name} (Version id: #{id})." rescue nil
66
+ end
67
+ end
68
+
69
+ model
70
+ else
71
+ latest_version = self.class.find(:first, :conditions => {:versioned_id => versioned_id, :versioned_type => versioned_type, :tag => 'deleted'})
72
+ latest_version.nil? ? nil : latest_version.restore
73
+ end
74
+ end
75
+ end
76
+ end