Skip to content

Commit

Permalink
Buming to Rails 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
malomalo committed May 3, 2017
1 parent 427f68f commit 905e594
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 63 deletions.
40 changes: 20 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
language: ruby
sudo: false

cache:
bundler: true

rvm:
- 2.3.3
- 2.4.0
- 2.3.4
- 2.4.1

env:
- RAILS_VERSION=v5.0.1

cache:
bundler: true
directories:
- /home/travis/.rvm/gems
matrix:
- RAILS_VERSION=v5.1.0 GEM=ar:mysql2
- RAILS_VERSION=v5.1.0 GEM=ar:sqlite3
- RAILS_VERSION=v5.1.0 GEM=ar:postgresql

addons:
postgresql: "9.4"

before_install:
- unset BUNDLE_GEMFILE
- gem update --system
- gem update bundler

install:
- bundle install --jobs=3 --retry=3
- git clone https://github.com/rails/rails.git ~/build/rails

before_script:
- pushd ~/build/rails
- git checkout $RAILS_VERSION
- sed -i "/require 'support\/connection'/a \$LOAD_PATH.unshift\(File.expand_path\('~\/build\/malomalo\/activerecord-cached_at\/lib'\)\)\nrequire 'cached_at/base'\nrequire 'cached_at/helpers'" ~/build/rails/activerecord/test/cases/helper.rb
- sed -i "s/Gem.ruby, '-w'/Gem.ruby, '-w0'/" ~/build/rails/activerecord/Rakefile
- sed -i "s/t.warning = true/t.warning = false/g" ~/build/rails/activerecord/Rakefile
- sed -i "/require 'support\/connection'/a \$LOAD_PATH.unshift\(File.expand_path\('~\/build\/malomalo\/activerecord-cached_at\/lib'\)\)\nrequire 'cached_at'" ~/build/rails/activerecord/test/cases/helper.rb
- cat ~/build/rails/Gemfile
- "sed -i \"/group :db do/a gem 'activerecord-cached_at', require: false, path: File.expand_path\\('~\\/build\\/malomalo\\/activerecord-cached_at'\\)\" ~/build/rails/Gemfile"
- "sed -i \"/group :db do/a gem 'activerecord-cached_at', require: 'cached_at', path: File.expand_path\\('~\\/build\\/malomalo\\/activerecord-cached_at'\\)\" ~/build/rails/Gemfile"
- cat ~/build/rails/Gemfile
- bundle install --jobs=3 --retry=3
- createdb activerecord_unittest
- createdb activerecord_unittest2
- mysql -e "create database IF NOT EXISTS activerecord_unittest DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;"
- mysql -e "create database IF NOT EXISTS activerecord_unittest2 DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;"
- gem environment gempath
- popd
- bundle install --jobs=3 --retry=3
- gem environment gempath

script: bundle exec rake test && cd ~/build/rails/activerecord && bundle exec rake test --verbose
script:
- bundle exec rake test
- cd ~/build/rails && ci/travis.rb
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
source "https://rubygems.org"

gem 'json', github: 'flori/json', branch: 'v1.8'

# Specify your gem's dependencies in sunstone.gemspec
gemspec
37 changes: 15 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
GIT
remote: git://github.com/flori/json.git
revision: 7f4cfd853f2c919d854fb95548a19980feff17e8
branch: v1.8
specs:
json (1.8.6)

PATH
remote: .
specs:
activerecord-cached_at (2.0.13)
activerecord (~> 5.0.0)
activerecord (~> 5.1.0)

GEM
remote: https://rubygems.org/
specs:
activemodel (5.0.1)
activesupport (= 5.0.1)
activerecord (5.0.1)
activemodel (= 5.0.1)
activesupport (= 5.0.1)
arel (~> 7.0)
activesupport (5.0.1)
activemodel (5.1.0)
activesupport (= 5.1.0)
activerecord (5.1.0)
activemodel (= 5.1.0)
activesupport (= 5.1.0)
arel (~> 8.0)
activesupport (5.1.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
ansi (1.5.0)
arel (7.1.4)
arel (8.0.0)
builder (3.2.2)
byebug (9.0.5)
concurrent-ruby (1.0.4)
concurrent-ruby (1.0.5)
docile (1.1.5)
i18n (0.7.0)
i18n (0.8.1)
json (2.1.0)
minitest (5.10.1)
minitest-reporters (1.1.3)
ansi
Expand All @@ -46,8 +40,8 @@ GEM
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
sqlite3 (1.3.11)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (0.3.6)
tzinfo (1.2.3)
thread_safe (~> 0.1)

PLATFORMS
Expand All @@ -57,12 +51,11 @@ DEPENDENCIES
activerecord-cached_at!
bundler
byebug
json!
minitest
minitest-reporters
rake
simplecov
sqlite3

BUNDLED WITH
1.13.7
1.14.4
2 changes: 1 addition & 1 deletion activerecord-cached_at.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.test_files = `git ls-files -- {test}/*`.split("\n")
spec.require_paths = ["lib"]

spec.add_runtime_dependency 'activerecord', '~> 5.0.0'
spec.add_runtime_dependency 'activerecord', '~> 5.1.0'

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
Expand Down
6 changes: 3 additions & 3 deletions lib/cached_at/associations/belongs_to_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def touch_cached_at(timestamp, method)

cache_column = "#{options[:inverse_of]}_cached_at"
if options[:polymorphic]
oldtype = owner.send("#{reflection.foreign_type}_was")
oldid = owner.send("#{reflection.foreign_key}_was")
oldtype = owner.send("#{reflection.foreign_type}_before_last_save")
oldid = owner.send("#{reflection.foreign_key}_before_last_save")
newtype = owner.send(reflection.foreign_type)
newid = owner.send(reflection.foreign_key)
if !oldtype.nil? && oldtype == newtype
Expand All @@ -38,7 +38,7 @@ def touch_cached_at(timestamp, method)
end
end
else
ids = [owner.send(reflection.foreign_key), owner.send("#{reflection.foreign_key}_was")].compact.uniq
ids = [owner.send(reflection.foreign_key), owner.send("#{reflection.foreign_key}_before_last_save")].compact.uniq
query = klass.where({ reflection.association_primary_key => ids })
query.update_all({ cache_column => timestamp })
traverse_relationships(klass, options[:cached_at], query, cache_column, timestamp)
Expand Down
2 changes: 1 addition & 1 deletion lib/cached_at/associations/collection_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def touch_cached_at(timestamp, method)
end

cache_column = "#{reflection.inverse_of.name}_cached_at"
ids = [owner.send(reflection.association_primary_key), owner.send("#{reflection.association_primary_key}_was")].compact.uniq
ids = [owner.send(reflection.association_primary_key), owner.send("#{reflection.association_primary_key}_before_last_save")].compact.uniq
query = klass.where({ reflection.foreign_key => ids })

if loaded?
Expand Down
2 changes: 1 addition & 1 deletion lib/cached_at/associations/has_many_through_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def touch_cached_at(timestamp, method)
target.each { |r| r.raw_write_attribute(cache_column, timestamp) }
query = klass.where(klass.primary_key => target.map(&:id))
else
ids = [owner.send(using_reflection.association_primary_key), owner.send("#{using_reflection.association_primary_key}_was")].compact.uniq
ids = [owner.send(using_reflection.association_primary_key), owner.send("#{using_reflection.association_primary_key}_before_last_save")].compact.uniq
arel_table = klass._reflections[using_reflection.inverse_of.options[:through].to_s].klass.arel_table
query = klass.joins(using_reflection.inverse_of.options[:through])
query = if using_reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
Expand Down
2 changes: 1 addition & 1 deletion lib/cached_at/associations/has_one_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def touch_cached_at(timestamp, method)
end

cache_column = "#{reflection.inverse_of.name}_cached_at"
ids = [owner.send(reflection.association_primary_key), owner.send("#{reflection.association_primary_key}_was")].compact.uniq
ids = [owner.send(reflection.association_primary_key), owner.send("#{reflection.association_primary_key}_before_last_save")].compact.uniq
query = klass.where({ reflection.foreign_key => ids })

case options[:dependent]
Expand Down
5 changes: 3 additions & 2 deletions lib/cached_at/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def update_relations_cached_at_from_cached_at(method: nil)
def update_relations_cached_at(timestamp: nil, method: nil)
method = instance_variable_defined?(:@new_record_before_save) && @new_record_before_save ? :create : :update if method.nil?

return if method == :create && changes.empty?
return if method == :update && changes.empty?
diff = saved_changes.transform_values(&:first)
return if method == :create && diff.empty?
return if method == :update && diff.empty?

timestamp ||= current_time_from_proper_timezone

Expand Down
27 changes: 17 additions & 10 deletions lib/cached_at/timestamp.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
module ActiveRecord
module Timestamp

private
module CachedAt
module Timestamp
extend ActiveSupport::Concern

class_methods do
private

def timestamp_attributes_for_update
[:updated_at, :cached_at]
end
def timestamp_attributes_for_update
['updated_at', 'cached_at']
end

def timestamp_attributes_for_create
['created_at', 'udpated_at', 'cached_at'] + column_names.select{|c| c.end_with?('_cached_at') }
end

def timestamp_attributes_for_create
[:created_at, :udpated_at, :cached_at] + self.class.column_names.select{|c| c.end_with?('_cached_at') }.map(&:to_sym)
end
end

end
end
end

ActiveRecord::Base.include(ActiveRecord::CachedAt::Timestamp)

0 comments on commit 905e594

Please sign in to comment.