Skip to content

Commit

Permalink
Merge a65ac6c into 5f6031b
Browse files Browse the repository at this point in the history
  • Loading branch information
tlevi committed Dec 11, 2019
2 parents 5f6031b + a65ac6c commit 6f22b59
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 152 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: ruby
rvm:
- 2.5
- 2.6

script: "bundle exec rake spec"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* [TT-6539] Remove cache_all_attributes and other unused methods
* [TT-5384] Remove validates_presence_of_parent method (Use rails optional flag instead)
* [TT-6293] Drop breadcrumbs / cache without host and some unused view helpers

Expand Down
1 change: 0 additions & 1 deletion lib/rails_core_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module RailsCoreExtensions

if defined? ActiveRecord
require 'rails_core_extensions/active_record_cloning'
require 'rails_core_extensions/active_record_cache_all_attributes'
require 'rails_core_extensions/active_record_extensions'
require 'rails_core_extensions/active_record_liquid_extensions'
require 'rails_core_extensions/translations'
Expand Down
39 changes: 0 additions & 39 deletions lib/rails_core_extensions/active_record_cache_all_attributes.rb

This file was deleted.

56 changes: 0 additions & 56 deletions lib/rails_core_extensions/active_record_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,6 @@ def database(key)
establish_connection("#{key}_#{Rails.env}")
end

def cache_all_attributes(options = {})
method = options[:by] || 'id'
class_eval <<-CACHE
after_save :clear_attribute_cache
after_destroy :clear_attribute_cache
cattr_accessor :cache_attributes_by
self.cache_attributes_by = '#{method}'
CACHE
extend ActiveRecordCacheAllAttributes::ClassMethods
include ActiveRecordCacheAllAttributes::InstanceMethods
end

# Create a new object from the attributes passed in
# OR update an existing
#
# If an :id attribute is present it will assume it's an existing record, and needs update
def new_or_update!(hash={}, options = {:hard_update => true})
hash.symbolize_keys!
if hash[:id].blank?
self.new(hash)
else
rec = self.find(hash[:id])
if options[:hard_update]
rec.update_attributes!(hash.except(:id))
else
rec.update_attributes(hash.except(:id))
end
rec
end
end

def enum_int(field, values, options = {})
const_set("#{field.to_s.upcase}_OPTIONS", values)

Expand Down Expand Up @@ -108,31 +77,6 @@ def rebalance_#{collection.to_s.singularize}_positions!(object = nil)
EVAL
end
end

# Run a block, being respectful of connection pools
#
# Useful for when you're not in the standard rails request process,
# since normally rails will take, then clear you're connection in the
# processing of the request.
#
# If you don't do this in, say, a command line task with threads, then
# you'll run out of connections after 5 x Threads are run simultaneously...
def with_connection_pooling
# Verify active connections and remove and disconnect connections
# associated with stale threads.
ActiveRecord::Base.verify_active_connections!

yield

# This code checks in the connection being used by the current thread back
# into the connection pool. It repeats this if you are using multiple
# connection pools. It will not disconnect the connection.
#
# Returns any connections in use by the current thread back to the pool,
# and also returns connections to the pool cached by threads that are no
# longer alive.
ActiveRecord::Base.clear_active_connections!
end
end

module InstanceMethods
Expand Down
54 changes: 0 additions & 54 deletions spec/active_record_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,6 @@
end
end

describe "ActiveRecord::Base" do
let(:mock_model) { double }
let(:model_class) { Class.new(ActiveRecord::Base) }
before { stub_const 'Model', model_class }

it "should create a new record if new_or_update! is passed a hash without an :id" do
attributes = {:fake_column => 'nothing really'}
expect(Model).to receive(:new).with(attributes)
Model.new_or_update!(attributes)
end

it "should update record if new_or_update! is passed hash with :id" do
attributes = {:fake_column => 'nothing really', :id => 1}
expect(Model).to receive(:find) { mock_model }
expect(mock_model).to receive(:update_attributes!)
Model.new_or_update!(attributes)
end
end

describe RailsCoreExtensions::ActionControllerSortable do
class NormalController < ActionController::Base
end
Expand All @@ -101,38 +82,3 @@ class SortableController < ActionController::Base
expect(SortableController.new.methods.map(&:to_sym)).to include(:sort)
end
end

describe ActiveRecordExtensions do
let(:model_class) {
Class.new(ActiveRecord::Base) do
cache_all_attributes :by => 'name'
end
}
let(:first) { Model.create!(:name => 'First') }
let(:second) { Model.create!(:name => 'Second') }
let(:expected) {
{
'First' => first.attributes,
'Second' => second.attributes
}
}

before do
connect_to_sqlite
stub_const 'Model', model_class
allow(Model).to receive(:cache) { ActiveSupport::Cache::MemoryStore.new }
allow(Model).to receive(:should_cache?) { true }
[first, second]
end

it 'should cache all attributes' do
# Test underlying generate attributes hash method works
expect(Model.generate_attributes_hash).to eq expected
expect(Model.attribute_cache).to eq expected

# Test after save/destroy it updates
first.destroy
expect(Model.attribute_cache).to eq 'Second' => second.attributes
second.destroy # Clean up after
end
end
2 changes: 1 addition & 1 deletion spec/support/coverage_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
require 'coveralls'
require 'coverage/kit'

Coverage::Kit.setup(minimum_coverage: 86.6)
Coverage::Kit.setup(minimum_coverage: 86.7)

0 comments on commit 6f22b59

Please sign in to comment.