Created
December 16, 2016 18:08
-
-
Save apanzerj/c09fc89ff713709e374580dcff6cbdca to your computer and use it in GitHub Desktop.
Playground for RSpec/Rails/ActiveRecord blah blah foo
This file contains hidden or 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
# frozen_string_literal: true | |
source "https://rubygems.org" | |
gem "rails" | |
gem "rspec" | |
gem "sqlite3" |
This file contains hidden or 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
require 'rails' | |
require 'active_record' | |
require 'rspec' | |
require 'sqlite3' | |
require 'fileutils' | |
FileUtils.rm("users.db") | |
ActiveRecord::Base.establish_connection( | |
adapter: 'sqlite3', | |
database: './users.db' | |
) | |
ActiveRecord::Schema.define do | |
create_table :widgets do |t| | |
t.string :name | |
t.timestamps | |
end | |
end | |
class Widget < ActiveRecord::Base | |
end | |
def last_modified(scopes, non_scopes=nil) | |
[ | |
scopes.map do |scope| | |
scope.maximum(:updated_at) | |
end, | |
non_scopes | |
].flatten.compact.max | |
end | |
require 'rspec/autorun' | |
describe '#last_modified' do | |
before do | |
Widget.create!(name: 'foo') | |
Widget.create!(name: 'af') | |
@expected = Widget.create!(name: 'noo') | |
end | |
it 'returns the max number for class Widget' do | |
@expected.touch | |
expect(last_modified([Widget])).to eq(@expected.updated_at) | |
end | |
it 'returns the greater of the scopes or non_scopes' do | |
ts = Time.now | |
expect(last_modified([Widget], ts)).to eq(ts) | |
end | |
it 'returns the scope if scope has the greater value' do | |
expect(last_modified([Widget], 10.weeks.ago)).to eq(@expected.updated_at) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to swap to
FileUtils.rm("users.db", force: true)
, but then it worked. Thanks for sharing!