spec/spec_helper.rb in grape-app-0.8.4 vs spec/spec_helper.rb in grape-app-0.8.5
- old
+ new
@@ -1,8 +1,9 @@
ENV['RACK_ENV'] ||= 'test'
require 'grape-app'
require 'rack/test'
+require 'virtus'
class Article
include Virtus.model
class Scope
@@ -11,22 +12,23 @@
def maximum(*)
map(&:updated_at).max
end
def each
- yield Article.new(id: 1, title: 'Welcome', updated_at: Time.at(1515151510).utc)
- yield Article.new(id: 2, title: 'Bye', updated_at: Time.at(1515151520).utc)
+ yield Article.new(id: 1, title: 'Welcome', updated_at: Time.at(1515151510).utc, created_at: Time.at(1515151500).utc)
+ yield Article.new(id: 2, title: 'Bye', updated_at: Time.at(1515151520).utc, created_at: Time.at(1515151500).utc)
end
end
def self.all
Scope.new
end
attribute :id
attribute :title
attribute :updated_at
+ attribute :created_at
def to_param
id.to_s
end
end
@@ -40,9 +42,16 @@
get '/articles' do
scope = Article.all
opts = params[:public] ? { public: params[:public] } : {}
fresh_when(scope, **opts)
scope.map(&:to_hash)
+ end
+
+ get '/articles/never_updated' do
+ article = Article.all.first
+ article.updated_at = nil
+
+ fresh_when(article, last_modified_field: :created_at)
end
get '/articles/:id' do
article = Article.all.first
article.to_hash if stale?(article, stale_if_error: 5, extras: { a: 1, b: 2 })