Skip to content

Commit

Permalink
fixed deprecation warnings for ActiveSupport::Concern
Browse files Browse the repository at this point in the history
  • Loading branch information
Timo Göllner committed Feb 3, 2012
1 parent 6f3dc6b commit 4c11b03
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 394 deletions.
74 changes: 36 additions & 38 deletions lib/vestal_versions/changes.rb
Expand Up @@ -11,50 +11,48 @@ module Changes

# Methods available to versioned ActiveRecord::Base instances in order to manage changes used
# for version creation.
module InstanceMethods
# Collects an array of changes from a record's versions between the given range and compiles
# them into one summary hash of changes. The +from+ and +to+ arguments can each be either a
# version number, a symbol representing an association proxy method, a string representing a
# version tag or a version object itself.
def changes_between(from, to)
from_number, to_number = versions.number_at(from), versions.number_at(to)
return {} if from_number == to_number
chain = versions.between(from_number, to_number).reject(&:initial?)
return {} if chain.empty?
# Collects an array of changes from a record's versions between the given range and compiles
# them into one summary hash of changes. The +from+ and +to+ arguments can each be either a
# version number, a symbol representing an association proxy method, a string representing a
# version tag or a version object itself.
def changes_between(from, to)
from_number, to_number = versions.number_at(from), versions.number_at(to)
return {} if from_number == to_number
chain = versions.between(from_number, to_number).reject(&:initial?)
return {} if chain.empty?

backward = from_number > to_number
backward ? chain.pop : chain.shift unless from_number == 1 || to_number == 1
backward = from_number > to_number
backward ? chain.pop : chain.shift unless from_number == 1 || to_number == 1

chain.inject({}) do |changes, version|
changes.append_changes!(backward ? version.changes.reverse_changes : version.changes)
end
chain.inject({}) do |changes, version|
changes.append_changes!(backward ? version.changes.reverse_changes : version.changes)
end
end

private
# Before a new version is created, the newly-changed attributes are appended onto a hash
# of previously-changed attributes. Typically the previous changes will be empty, except in
# the case that a control block is used where versions are to be merged. See
# VestalVersions::Control for more information.
def merge_version_changes
version_changes.append_changes!(incremental_version_changes)
end
private
# Before a new version is created, the newly-changed attributes are appended onto a hash
# of previously-changed attributes. Typically the previous changes will be empty, except in
# the case that a control block is used where versions are to be merged. See
# VestalVersions::Control for more information.
def merge_version_changes
version_changes.append_changes!(incremental_version_changes)
end

# Stores the cumulative changes that are eventually used for version creation.
def version_changes
@version_changes ||= {}
end
# Stores the cumulative changes that are eventually used for version creation.
def version_changes
@version_changes ||= {}
end

# Stores the incremental changes that are appended to the cumulative changes before version
# creation. Incremental changes are reset when the record is saved because they represent
# a subset of the dirty attribute changes, which are reset upon save.
def incremental_version_changes
changes.slice(*versioned_columns)
end
# Stores the incremental changes that are appended to the cumulative changes before version
# creation. Incremental changes are reset when the record is saved because they represent
# a subset of the dirty attribute changes, which are reset upon save.
def incremental_version_changes
changes.slice(*versioned_columns)
end

# Simply resets the cumulative changes after version creation.
def reset_version_changes
@version_changes = nil
end
# Simply resets the cumulative changes after version creation.
def reset_version_changes
@version_changes = nil
end

# Instance methods included into Hash for dealing with manipulation of hashes in the specific
Expand Down Expand Up @@ -84,7 +82,7 @@ module HashMethods
def append_changes(changes)
changes.inject(self) do |new_changes, (attribute, change)|
new_change = [new_changes.fetch(attribute, change).first, change.last]
new_changes.merge(attribute => new_change)
new_changes.merge(attribute => new_change)
end.reject do |attribute, change|
change.first == change.last
end
Expand Down
48 changes: 23 additions & 25 deletions lib/vestal_versions/conditions.rb
Expand Up @@ -25,33 +25,31 @@ def prepare_versioned_options(options)

# Instance methods that determine based on the <tt>:if</tt> and <tt>:unless</tt> conditions,
# whether a version is to be create or updated.
module InstanceMethods
private
# After first determining whether the <tt>:if</tt> and <tt>:unless</tt> conditions are
# satisfied, the original, unaliased +create_version?+ method is called to determine
# whether a new version should be created upon update of the ActiveRecord::Base instance.
def create_version?
version_conditions_met? && super
end
private
# After first determining whether the <tt>:if</tt> and <tt>:unless</tt> conditions are
# satisfied, the original, unaliased +create_version?+ method is called to determine
# whether a new version should be created upon update of the ActiveRecord::Base instance.
def create_version?
version_conditions_met? && super
end

# After first determining whether the <tt>:if</tt> and <tt>:unless</tt> conditions are
# satisfied, the original, unaliased +update_version?+ method is called to determine
# whther the last version should be updated to include changes merged from the current
# ActiveRecord::Base instance update.
#
# The overridden +update_version?+ method simply returns false, effectively delegating
# the decision to whether the <tt>:if</tt> and <tt>:unless</tt> conditions are met.
def update_version?
version_conditions_met? && super
end
# After first determining whether the <tt>:if</tt> and <tt>:unless</tt> conditions are
# satisfied, the original, unaliased +update_version?+ method is called to determine
# whther the last version should be updated to include changes merged from the current
# ActiveRecord::Base instance update.
#
# The overridden +update_version?+ method simply returns false, effectively delegating
# the decision to whether the <tt>:if</tt> and <tt>:unless</tt> conditions are met.
def update_version?
version_conditions_met? && super
end

# Simply checks whether the <tt>:if</tt> and <tt>:unless</tt> conditions given in the
# +versioned+ options are met: meaning that all procs in the <tt>:if</tt> array must
# evaluate to a non-false, non-nil value and that all procs in the <tt>:unless</tt> array
# must all evaluate to either false or nil.
def version_conditions_met?
vestal_versions_options[:if].all?{|p| p.call(self) } && !vestal_versions_options[:unless].any?{|p| p.call(self) }
end
# Simply checks whether the <tt>:if</tt> and <tt>:unless</tt> conditions given in the
# +versioned+ options are met: meaning that all procs in the <tt>:if</tt> array must
# evaluate to a non-false, non-nil value and that all procs in the <tt>:unless</tt> array
# must all evaluate to either false or nil.
def version_conditions_met?
vestal_versions_options[:if].all?{|p| p.call(self) } && !vestal_versions_options[:unless].any?{|p| p.call(self) }
end
end
end

0 comments on commit 4c11b03

Please sign in to comment.