Skip to content

Commit

Permalink
Stop supporting AR versions < 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Nov 30, 2024
1 parent 6283528 commit 7f471c1
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ gem 'mobility', '~> 1.3.0.rc3'
### ActiveRecord (Rails)

Requirements:
- ActiveRecord >= 6.1
- ActiveRecord >= 7.0

To translate attributes on a model, extend `Mobility`, then call `translates`
passing in one or more attributes as well as a hash of options (see below).
Expand Down
19 changes: 0 additions & 19 deletions lib/mobility/backends/active_record/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,6 @@ def each_locale
end
end

setup do |_attributes, options|
# Fix for duping depth-2 jsonb column in AR < 5.0
if ::ActiveRecord::VERSION::STRING < '5.0'
column_name = options[:column_name]
module_name = "MobilityArContainer#{column_name.to_s.camelcase}"
unless const_defined?(module_name)
dupable = Module.new do
class_eval <<-EOM, __FILE__, __LINE__ + 1
def initialize_dup(source)
super
self.#{column_name} = source.#{column_name}.deep_dup
end
EOM
end
include const_set(module_name, dupable)
end
end
end

private

def model_translations(locale)
Expand Down
32 changes: 14 additions & 18 deletions lib/mobility/plugins/active_record/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,24 @@ def self.after_update; end
)

module InstanceMethods
if ::ActiveRecord::VERSION::STRING >= '5.1' # define patterns added in 5.1
def saved_changes
super.merge(mutations_from_mobility.previous_changes)
end
def saved_changes
super.merge(mutations_from_mobility.previous_changes)
end

def changes_to_save
super.merge(mutations_from_mobility.changes)
end
def changes_to_save
super.merge(mutations_from_mobility.changes)
end

def changed_attribute_names_to_save
super + mutations_from_mobility.changed
end
def changed_attribute_names_to_save
super + mutations_from_mobility.changed
end

def attributes_in_database
super.merge(mutations_from_mobility.changed_attributes)
end
def attributes_in_database
super.merge(mutations_from_mobility.changed_attributes)
end

if ::ActiveRecord::VERSION::STRING >= '6.0'
def has_changes_to_save?
super || mutations_from_mobility.changed?
end
end
def has_changes_to_save?
super || mutations_from_mobility.changed?
end

def reload(*)
Expand Down
32 changes: 15 additions & 17 deletions lib/mobility/plugins/active_record/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,29 +158,27 @@ def order(opts, *rest)
end
end

if ::ActiveRecord::VERSION::STRING >= '5.0'
%w[pluck group select].each do |method_name|
define_method method_name do |*attrs, &block|
return super(*attrs, &block) if (method_name == 'select' && block.present?)
%w[pluck group select].each do |method_name|
define_method method_name do |*attrs, &block|
return super(*attrs, &block) if (method_name == 'select' && block.present?)

return super(*attrs, &block) unless klass.respond_to?(:mobility_attribute?)
return super(*attrs, &block) unless klass.respond_to?(:mobility_attribute?)

return super(*attrs, &block) unless attrs.any?(&klass.method(:mobility_attribute?))
return super(*attrs, &block) unless attrs.any?(&klass.method(:mobility_attribute?))

keys = attrs.dup
keys = attrs.dup

base = keys.each_with_index.inject(self) do |query, (key, index)|
next query unless klass.mobility_attribute?(key)
keys[index] = backend_node(key)
if method_name == "select" && query.order_values.any?
keys[index] = keys[index]
.as(::Mobility::Plugins::ActiveRecord::Query.attribute_alias(key.to_s))
end
klass.mobility_backend_class(key).apply_scope(query, backend_node(key))
base = keys.each_with_index.inject(self) do |query, (key, index)|
next query unless klass.mobility_attribute?(key)
keys[index] = backend_node(key)
if method_name == "select" && query.order_values.any?
keys[index] = keys[index]
.as(::Mobility::Plugins::ActiveRecord::Query.attribute_alias(key.to_s))
end

base.public_send(method_name, *keys, &block)
klass.mobility_backend_class(key).apply_scope(query, backend_node(key))
end

base.public_send(method_name, *keys, &block)
end
end

Expand Down
11 changes: 0 additions & 11 deletions lib/mobility/plugins/arel/nodes/pg_ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@ def lower
end)
end

# Needed for AR 4.2, can be removed when support is deprecated
if ::ActiveRecord::VERSION::STRING < '5.0'
[JsonbDashDoubleArrow, HstoreDashArrow].each do |klass|
klass.class_eval do
def quoted_node other
other && super
end
end
end
end

class Jsonb < JsonbDashDoubleArrow
def to_dash_arrow
JsonbDashArrow.new left, right
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/active_record_compatibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
expect(Post.first.title).to eq("association changed value")
end

it "resets cache when model is reloaded", active_record_geq: '5.0' do
it "resets cache when model is reloaded" do
expect(post.mobility_backends[:title]).to receive(:clear_cache).once
post.reload
end
Expand Down
2 changes: 1 addition & 1 deletion spec/mobility/backends/active_record/container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
end

describe "non-text values" do
it "stores non-string types as-is when saving", active_record_geq: '5.0' do
it "stores non-string types as-is when saving" do
post = ContainerPost.new
backend = post.mobility_backends[:title]
backend.write(:en, { foo: :bar } )
Expand Down
2 changes: 1 addition & 1 deletion spec/mobility/backends/active_record/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
end

describe "non-text values" do
it "stores non-string types as-is when saving", active_record_geq: '5.0' do
it "stores non-string types as-is when saving" do
backend = post.mobility_backends[:title]
backend.write(:en, { foo: :bar } )
post.save
Expand Down
5 changes: 1 addition & 4 deletions spec/mobility/backends/active_record/jsonb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
end

describe "non-text values" do
it "stores non-string types as-is when saving", active_record_geq: '5.0' do
it "stores non-string types as-is when saving" do
backend = post.mobility_backends[:title]
backend.write(:en, { foo: :bar } )
post.save
Expand Down Expand Up @@ -103,8 +103,5 @@

include_accessor_examples 'JsonbPost'
include_serialization_examples 'JsonbPost', column_affix: column_affix

# regression for https://github.com/shioyama/mobility/issues/308
include_querying_examples 'JsonbPost' if ActiveRecord::VERSION::STRING == '5.1'
end
end
2 changes: 1 addition & 1 deletion spec/mobility/backends/active_record/serialized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
include_cache_key_examples 'SerializedPost'

describe "non-text values" do
it "converts non-string types to strings when saving", active_record_geq: '5.0' do
it "converts non-string types to strings when saving" do
post = SerializedPost.new
backend = post.mobility_backends[:title]
backend.write(:en, { foo: :bar } )
Expand Down
4 changes: 2 additions & 2 deletions spec/mobility/plugins/active_model/dirty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def save

# This is a regression spec to ensure we don't change the scope of
# ActiveModel handler methods.
it 'does not change private status of attribute handler methods', active_record_geq: '5.0' do
it 'does not change private status of attribute handler methods' do
expect(instance.respond_to?(:attribute_change)).to eq(false)
expect(instance.respond_to?(:attribute_change, true)).to eq(true)
expect(instance.respond_to?(:attribute_previous_change)).to eq(false)
Expand All @@ -284,7 +284,7 @@ def save
end
end

it "returns changes on attribute for current locale", active_record_geq: '5.0' do
it "returns changes on attribute for current locale" do
Mobility.locale = locale = :en
backend.write(locale, 'foo')
instance.save
Expand Down
14 changes: 6 additions & 8 deletions spec/mobility/plugins/active_record/dirty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
stub_const 'Article', Class.new(ActiveRecord::Base)
Article.include translations

# ensure we include these methods as a module rather than override in class
changes_applied_method = ::ActiveRecord::VERSION::STRING < '5.1' ? :changes_applied : :changes_internally_applied
Article.class_eval do
define_method changes_applied_method do
define_method :changes_internally_applied do
super()
end

Expand Down Expand Up @@ -429,7 +427,7 @@ def self.after_update; end
it_behaves_like "resets on model action", :reload
end

describe "#saved_changes", active_record_geq: '5.1' do
describe "#saved_changes" do
it "includes translated and untranslated attributes" do
instance = model_class.create

Expand All @@ -448,7 +446,7 @@ def self.after_update; end
end
end

describe '#changes_to_save', active_record_geq: '5.1' do
describe '#changes_to_save' do
it "includes translated and untranslated attributes" do
instance = model_class.new

Expand All @@ -464,7 +462,7 @@ def self.after_update; end
end
end

describe '#has_changes_to_save?', active_record_geq: '6.0' do
describe '#has_changes_to_save?' do
it 'detects changes to translated and untranslated attributes' do
instance = model_class.new
backend = backend_for(instance, :title)
Expand All @@ -480,7 +478,7 @@ def self.after_update; end
end
end

describe '#attributes_in_database', active_record_geq: '6.0' do
describe '#attributes_in_database' do
it 'includes translated and untranslated attributes' do
instance = model_class.create
expect(instance.attributes_in_database).to eq({})
Expand All @@ -502,7 +500,7 @@ def self.after_update; end
end
end

describe '#changed_attribute_names_to_save', active_record_geq: '5.1' do
describe '#changed_attribute_names_to_save' do
it 'includes translated attributes and untranslated attributes' do
instance = model_class.new
backend = backend_for(instance, :title)
Expand Down
6 changes: 3 additions & 3 deletions spec/support/shared_examples/querying_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
end
end

describe ".pluck", active_record_geq: '5.0' do
describe ".pluck" do
before do
[["foo0", "bar3", true],
["foo2", "bar0", false],
Expand Down Expand Up @@ -341,7 +341,7 @@
end
end

describe ".select/.group", active_record_geq: '5.0' do
describe ".select/.group" do
before do
[["foo", "baz", true],
["foo", "baz", false],
Expand Down Expand Up @@ -555,7 +555,7 @@ def query(*args, **kwargs, &block); model_class.i18n(*args, **kwargs, &block); e
expect(query { __send__(a1).matches("%foo") }).to match_array([i[0], *i[5..6], barfoo])
end

if ENV['DB'] == 'postgres' && ::ActiveRecord::VERSION::STRING >= '5.0'
if ENV['DB'] == 'postgres'
it "works with case_sensitive option" do
foobar = model_class.create(a1 => "foObar")
barfoo = model_class.create(a1 => "barfOo")
Expand Down

0 comments on commit 7f471c1

Please sign in to comment.