Skip to content

Commit

Permalink
Kill Whitespace!
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Mar 4, 2008
1 parent 95249fa commit f54f087
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions lib/acts_as_versioned.rb
Expand Up @@ -95,7 +95,7 @@ module ClassMethods
# end
#
# * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes
# either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have.
# either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have.
# Use this instead if you want to write your own attribute setters (and ignore if_changed):
#
# def name=(new_name)
Expand Down Expand Up @@ -148,7 +148,7 @@ module ClassMethods
# # that create_table does
# Post.create_versioned_table
# end
#
#
# def self.down
# Post.drop_versioned_table
# end
Expand All @@ -172,11 +172,11 @@ def acts_as_versioned(options = {}, &extension)
return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods)

send :include, ActiveRecord::Acts::Versioned::ActMethods

cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
:version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
:version_association_options

# legacy
alias_method :non_versioned_fields, :non_versioned_columns
alias_method :non_versioned_fields=, :non_versioned_columns=
Expand Down Expand Up @@ -209,7 +209,7 @@ class << self
silence_warnings do
self.const_set(extension_module_name, Module.new(&extension))
end

options[:extend] = self.const_get(extension_module_name)
end

Expand All @@ -219,7 +219,7 @@ class << self
def earliest
@earliest ||= find(:first)
end

# find latest version of this record
def latest
@latest ||= find(:first, :order => 'version desc')
Expand All @@ -230,7 +230,7 @@ def latest
after_update :save_version
after_save :clear_old_versions
after_save :clear_changed_attributes

unless options[:if_changed].nil?
self.track_changed_attributes = true
options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array)
Expand All @@ -240,7 +240,7 @@ def latest
end
end
end

include options[:extend] if options[:extend].is_a?(Module)
end

Expand All @@ -252,22 +252,22 @@ def self.before(version)
find :first, :order => 'version desc',
:conditions => ["#{original_class.versioned_foreign_key} = ? and version < ?", version.send(original_class.versioned_foreign_key), version.version]
end

# find first version after the given version.
def self.after(version)
find :first, :order => 'version',
:conditions => ["#{original_class.versioned_foreign_key} = ? and version > ?", version.send(original_class.versioned_foreign_key), version.version]
end

def previous
self.class.before(self)
end

def next
self.class.after(self)
end
end

versioned_class.cattr_accessor :original_class
versioned_class.original_class = self
versioned_class.set_table_name versioned_table_name
Expand All @@ -278,17 +278,17 @@ def next
versioned_class.set_sequence_name version_sequence_name if version_sequence_name
end
end

module ActMethods
def self.included(base) # :nodoc:
base.extend ClassMethods
end

# Saves a version of the model if applicable
def save_version
save_version_on_create if save_version?
end

# Saves a version of the model in the versioned table. This is called in the after_save callback by default
def save_version_on_create
rev = self.class.versioned_class.new
Expand Down Expand Up @@ -321,7 +321,7 @@ def revert_to(version)
true
end

# Reverts a model to a given version and saves the model.
# Reverts a model to a given version and saves the model.
# Takes either a version number or an instance of the versioned model
def revert_to!(version)
revert_to(version) ? save_without_revision : false
Expand All @@ -347,36 +347,36 @@ def save_without_revision!
def versioned_attributes
self.attributes.keys.select { |k| !self.class.non_versioned_columns.include?(k) }
end

# If called with no parameters, gets whether the current model has changed and needs to be versioned.
# If called with a single parameter, gets whether the parameter has changed.
def changed?(attr_name = nil)
attr_name.nil? ?
(!self.class.track_changed_attributes || (changed_attributes && changed_attributes.length > 0)) :
(changed_attributes && changed_attributes.include?(attr_name.to_s))
end

# keep old dirty? method
alias_method :dirty?, :changed?

# Clones a model. Used when saving a new version or reverting a model's version.
def clone_versioned_model(orig_model, new_model)
self.versioned_attributes.each do |key|
new_model.send("#{key}=", orig_model.send(key)) if orig_model.has_attribute?(key)
end

if orig_model.is_a?(self.class.versioned_class)
new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
elsif new_model.is_a?(self.class.versioned_class)
new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
end
end

# Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>.
def save_version?
version_condition_met? && changed?
end

# Checks condition set in the :if option to check whether a revision should be created or not. Override this for
# custom version condition checking.
def version_condition_met?
Expand All @@ -387,7 +387,7 @@ def version_condition_met?
version_condition.call(self)
else
version_condition
end
end
end

# Executes the block with the versioning callbacks disabled.
Expand All @@ -412,18 +412,18 @@ def without_locking(&block)

def empty_callback() end #:nodoc:

protected
protected
# sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
def set_new_version
self.send("#{self.class.version_column}=", self.next_version) if new_record? || (!locking_enabled? && save_version?)
end

# Gets the next available version for the current record, or 1 for a new record
def next_version
return 1 if new_record?
(versions.calculate(:max, :version) || 0) + 1
end

# clears current changed attributes. Called after save.
def clear_changed_attributes
self.changed_attributes = []
Expand All @@ -443,7 +443,7 @@ def find_version(id, version)
:conditions => ["#{versioned_foreign_key} = ? AND version = ?", id, version],
:limit => 1).first
end

# Finds versions of a specific model. Takes an options hash like <tt>find</tt>
def find_versions(id, options = {})
versioned_class.find :all, {
Expand All @@ -455,7 +455,7 @@ def find_versions(id, options = {})
def versioned_columns
self.columns.select { |c| !non_versioned_columns.include?(c.name) }
end

# Returns an instance of the dynamic versioned model
def versioned_class
const_get versioned_class_name
Expand All @@ -467,12 +467,12 @@ def create_versioned_table(create_table_options = {})
if !self.content_columns.find { |c| %w(version lock_version).include? c.name }
self.connection.add_column table_name, :version, :integer
end

self.connection.create_table(versioned_table_name, create_table_options) do |t|
t.column versioned_foreign_key, :integer
t.column :version, :integer
end

updated_col = nil
self.versioned_columns.each do |col|
updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name)
Expand All @@ -482,25 +482,25 @@ def create_versioned_table(create_table_options = {})
:scale => col.scale,
:precision => col.precision
end

if type_col = self.columns_hash[inheritance_column]
self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type,
:limit => type_col.limit,
:default => type_col.default,
:scale => type_col.scale,
:precision => type_col.precision
end

if updated_col.nil?
self.connection.add_column versioned_table_name, :updated_at, :timestamp
end
end

# Rake migration task to drop the versioned table
def drop_versioned_table
self.connection.drop_table versioned_table_name
end

# Executes the block with the versioning callbacks disabled.
#
# Foo.without_revision do
Expand Down Expand Up @@ -535,7 +535,7 @@ def without_locking(&block)
result = block.call
ActiveRecord::Base.lock_optimistically = true if current
result
end
end
end
end
end
Expand Down

0 comments on commit f54f087

Please sign in to comment.