Created
April 6, 2013 13:58
-
-
Save machty/5326208 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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