diff --git a/README.md b/README.md index b0f22172f..13f9c7bba 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/lib/mobility/backends/active_record/container.rb b/lib/mobility/backends/active_record/container.rb index 83ca63984..5f1454021 100644 --- a/lib/mobility/backends/active_record/container.rb +++ b/lib/mobility/backends/active_record/container.rb @@ -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) diff --git a/lib/mobility/plugins/active_record/dirty.rb b/lib/mobility/plugins/active_record/dirty.rb index 3d2ae0711..93cec5ea4 100644 --- a/lib/mobility/plugins/active_record/dirty.rb +++ b/lib/mobility/plugins/active_record/dirty.rb @@ -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(*) diff --git a/lib/mobility/plugins/active_record/query.rb b/lib/mobility/plugins/active_record/query.rb index f21d59e8b..813ffbbef 100644 --- a/lib/mobility/plugins/active_record/query.rb +++ b/lib/mobility/plugins/active_record/query.rb @@ -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 diff --git a/lib/mobility/plugins/arel/nodes/pg_ops.rb b/lib/mobility/plugins/arel/nodes/pg_ops.rb index 14481b883..9f6e8b3fb 100644 --- a/lib/mobility/plugins/arel/nodes/pg_ops.rb +++ b/lib/mobility/plugins/arel/nodes/pg_ops.rb @@ -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 diff --git a/spec/integration/active_record_compatibility_spec.rb b/spec/integration/active_record_compatibility_spec.rb index e87053aea..0f4e4b6af 100644 --- a/spec/integration/active_record_compatibility_spec.rb +++ b/spec/integration/active_record_compatibility_spec.rb @@ -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 diff --git a/spec/mobility/backends/active_record/container_spec.rb b/spec/mobility/backends/active_record/container_spec.rb index c62d3c665..00e501f7b 100644 --- a/spec/mobility/backends/active_record/container_spec.rb +++ b/spec/mobility/backends/active_record/container_spec.rb @@ -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 } ) diff --git a/spec/mobility/backends/active_record/json_spec.rb b/spec/mobility/backends/active_record/json_spec.rb index 4762758ee..aaaed85c7 100644 --- a/spec/mobility/backends/active_record/json_spec.rb +++ b/spec/mobility/backends/active_record/json_spec.rb @@ -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 diff --git a/spec/mobility/backends/active_record/jsonb_spec.rb b/spec/mobility/backends/active_record/jsonb_spec.rb index 06d3d4b73..02d5909c2 100644 --- a/spec/mobility/backends/active_record/jsonb_spec.rb +++ b/spec/mobility/backends/active_record/jsonb_spec.rb @@ -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 @@ -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 diff --git a/spec/mobility/backends/active_record/serialized_spec.rb b/spec/mobility/backends/active_record/serialized_spec.rb index 908fee6e7..84f51b8be 100644 --- a/spec/mobility/backends/active_record/serialized_spec.rb +++ b/spec/mobility/backends/active_record/serialized_spec.rb @@ -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 } ) diff --git a/spec/mobility/plugins/active_model/dirty_spec.rb b/spec/mobility/plugins/active_model/dirty_spec.rb index bf733667a..98a459a2f 100644 --- a/spec/mobility/plugins/active_model/dirty_spec.rb +++ b/spec/mobility/plugins/active_model/dirty_spec.rb @@ -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) @@ -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 diff --git a/spec/mobility/plugins/active_record/dirty_spec.rb b/spec/mobility/plugins/active_record/dirty_spec.rb index 52d3381bf..cbef87905 100644 --- a/spec/mobility/plugins/active_record/dirty_spec.rb +++ b/spec/mobility/plugins/active_record/dirty_spec.rb @@ -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 @@ -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 @@ -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 @@ -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) @@ -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({}) @@ -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) diff --git a/spec/support/shared_examples/querying_examples.rb b/spec/support/shared_examples/querying_examples.rb index a404af76a..fd58878d4 100644 --- a/spec/support/shared_examples/querying_examples.rb +++ b/spec/support/shared_examples/querying_examples.rb @@ -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], @@ -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], @@ -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")