Skip to content

Instantly share code, notes, and snippets.

@machty
Created April 6, 2013 13:58
Show Gist options
  • Save machty/5326208 to your computer and use it in GitHub Desktop.
Save machty/5326208 to your computer and use it in GitHub Desktop.
class StatusSnapshot < ActiveRecord::Base
attr_accessible :error_message, :failed, :final_json, :processed_yaml, :raw_json, :tree
#serialize :tree, StatusNode
def version
tree.version
end
class << self
def successful
where(failed: false)
end
def latest
order('updated_at ASC')
end
def from_stypi_json(json_string)
last_snapshot = latest.successful.last
new_snapshot_hash = JSON.parse(json_string)
# Only create a new snapshot if there's a newer version.
if last_snapshot && new_snapshot_hash["version"].to_s == last_snapshot.version.to_s
return last_snapshot
end
# Create the new snapshot
new_snapshot = new
new_snapshot.tree =
StatusNode.from_stypi_hash(new_snapshot_hash,
last_snapshot &&
JSON.parse(last_snapshot.raw_json))
new_snapshot
end
end
protected
# TODO: Hack! FactoryGirl tries to invoke `singleton_method_added=` for some reason?
#attr_accessor :singleton_method_added if if Rails.env.test?
end
FactoryGirl.define do
factory :status_snapshot do
# Empty factory still causes the error
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment