Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
More refactoring of linking to objects in versions#index.
Browse files Browse the repository at this point in the history
  • Loading branch information
nning committed Feb 15, 2014
1 parent f5c91cf commit b04836f
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions app/helpers/versions_helper.rb
Expand Up @@ -18,22 +18,7 @@ def icon_for_event(event)

# Link to object references in version or String representation.
def link_to_object(version)
clazz = version.item_type
id = version.item_id

# Fetch object or instanciate dummy if it does not exist.
begin
object = Object.const_get(clazz).find(id)
rescue ActiveRecord::RecordNotFound
if version.object
hash = YAML.load(version.object)
else
hash = object_changes_to_hash(version)
end

object = Object.const_get(clazz).new
object.assign_attributes hash, without_protection: true
end
object = fetch_or_fake_object(version)

# String representation of object.
html = object.to_s
Expand All @@ -60,12 +45,33 @@ def summarize_changes(changes)

private

# Instanciate dummy for version of deleted object.
def fake_object(version)
if version.object
hash = YAML.load(version.object)
else
hash = object_changes_to_hash(version)
end

object = Object.const_get(version.item_type).new
object.assign_attributes hash, without_protection: true

object
end

# Fetch object or instanciate dummy if it does not exist.
def fetch_or_fake_object(version)
Object.const_get(version.item_type).find(version.item_id)
rescue ActiveRecord::RecordNotFound
fake_object version
end

# Convert changes YAML to hash just containing the new values.
def object_changes_to_hash(version)
h = {}
hash = {}
YAML.load(version.object_changes).each do |key, value|
h[key] = value.last
hash[key] = value.last
end
h
hash
end
end

0 comments on commit b04836f

Please sign in to comment.