Permalink
Browse files

Added uniqueness requirement for Dataset#key and conenvience method f…

…or processing stale datasets
  • Loading branch information...
1 parent df6bcaa commit 4567ec654c376cb4a3a263865193e905fc0059a0 CountCulture committed Jun 29, 2009
Showing with 34 additions and 3 deletions.
  1. +5 −0 app/models/dataset.rb
  2. +3 −3 app/views/datasets/data.html.erb
  3. +3 −0 config/schedule.rb
  4. +23 −0 test/unit/dataset_test.rb
View
@@ -2,6 +2,7 @@ class Dataset < ActiveRecord::Base
BASE_URL = 'http://spreadsheets.google.com/'
has_many :datapoints
validates_presence_of :title, :key, :query
+ validates_uniqueness_of :key
def data_for(council)
raw_response = _http_get(query_url(council))
@@ -23,6 +24,10 @@ def process
end
end
+ def self.process_stale
+ find(:all, :conditions => ["updated_at < ?", 7.days.ago]).each(&:process)
+ end
+
# This is the url where original datasheet in spreadsheet can be seen
def public_url
BASE_URL + "pub?key=#{key}"
@@ -1,10 +1,10 @@
-<h1><%= @title %></h1>
+<h1><%= link_for @dataset %></h1>
<div id="dataset_data">
<% if @datapoint.blank? %>
- <p class="alert">Sorry, no data found</p>
+ <p class="alert">Sorry, no data found for <%= link_for @council %></p>
<% else %>
<table>
- <caption><%= @dataset.title %> :: data for <%= @council.title %></caption>
+ <caption><%= link_for @dataset %> :: data for <%= link_for @council %></caption>
<tr>
<%- for column in @datapoint.data[0] -%>
<th><%= column %></th>
View
@@ -18,6 +18,9 @@
every 2.hours do
runner "ScraperRunner.new(:limit => 5, :email_results => true).refresh_stale"
end
+every 7.days do
+ runner "Dataset.process_stale"
+end
#
# every 4.days do
# runner "AnotherModel.prune_old_records"
View
@@ -11,6 +11,27 @@ class DatasetTest < ActiveSupport::TestCase
should "have base_url" do
assert_match /spreadsheets.google/, Dataset::BASE_URL
end
+
+ context "when processing stale datasets" do
+ setup do
+ @fresh_dataset = Factory(:dataset)
+ @stale_dataset = Factory(:dataset, :key => "foo123", :title => "Stale dataset")
+ Dataset.record_timestamps = false # update timestamp without triggering callbacks
+ @stale_dataset.update_attribute( :updated_at, 8.days.ago )
+ Dataset.record_timestamps = true # update timestamp without triggering callbacks
+ end
+
+ should "process and return stale datasets" do
+ # pretty cruddy test but not sure how best to do this
+ assert Dataset.process_stale.include?(@stale_dataset)
+ end
+
+ should "not process and return fresh datasets" do
+ # pretty cruddy test but not sure how best to do this
+ assert !Dataset.process_stale.include?(@fresh_dataset)
+ end
+ end
+
end
context "A Dataset instance" do
@@ -20,6 +41,8 @@ class DatasetTest < ActiveSupport::TestCase
@another_council = Factory(:another_council)
end
+ should_validate_uniqueness_of :key
+
should "constuct public_url from key" do
assert_equal "http://spreadsheets.google.com/pub?key=abc123", @dataset.public_url
end

0 comments on commit 4567ec6

Please sign in to comment.