Skip to content

Commit

Permalink
Define only one version of a method
Browse files Browse the repository at this point in the history
  • Loading branch information
rewritten committed Nov 23, 2021
1 parent 2b36bf3 commit c415b9b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 40 deletions.
19 changes: 11 additions & 8 deletions lib/active_data/model/representation.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'active_model/version'
require 'active_data/model/attributes/reflections/represents'
require 'active_data/model/attributes/represents'

Expand Down Expand Up @@ -71,19 +72,21 @@ def emerge_represented_attributes_errors!
end
end

def move_errors(from, to)
if ActiveModel.gem_version < Gem::Version.new('6.1.0')
# Rails 5 pollutes messages with an empty array on key data fetch attempt
return unless errors.messages.key?(from) && errors.messages[from].present?

errors[to].concat(errors.messages[from])
errors.delete(from)
else
case ActiveModel.version.to_s
when /^6\.1\./, /^7\./
def move_errors(from, to)
errors[from].each do |error_message|
errors.add(to, error_message)
errors.delete(from)
end
end
else # up to 6.0.x
def move_errors(from, to)
return unless errors.messages.key?(from) && errors.messages[from].present?

errors[to].concat(errors.messages[from])
errors.delete(from)
end
end
end
end
Expand Down
56 changes: 33 additions & 23 deletions lib/active_data/model/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,39 @@ def respond_to_missing?(method, _)
super || self.class._scope_model.respond_to?(method)
end

def method_missing(method, *args, **kwargs, &block)
with_scope do
model = self.class._scope_model
if model.respond_to?(method)
# ruby 2.6 does not understand kwargs?
# Fixed in 2.7 - https://rubyreferences.github.io/rubychanges/2.7.html#keyword-argument-related-changes
#
# > empty hash splat doesn’t pass empty hash as a positional argument.
result = if kwargs.empty?
model.public_send(method, *args, &block)
else
model.public_send(method, *args, **kwargs, &block)
end

# ruby 3.0 returns plain arrays when subclasses receive standard methods
# so we need to wrap again.
# https://rubyreferences.github.io/rubychanges/3.0.html#array-always-returning-array
#
# > On custom classes inherited from `Array`, some methods previously were returning an instance of
# > this class, and others returned `Array`. Now they all do the latter.
result.is_a?(ActiveData::Model::Scopes) ? result : model.scope_class.new(result)
else
super
case RAILS_VERSION
when /\A3\./
def method_missing(method, *args, **kwargs, &block)
with_scope do
model = self.class._scope_model
if model.respond_to?(method)
result = model.public_send(method, *args, **kwargs, &block)
result.is_a?(ActiveData::Model::Scopes) ? result : model.scope_class.new(result)
else
super
end
end
end
when /\A2\.7\./
def method_missing(method, *args, **kwargs, &block)
with_scope do
model = self.class._scope_model
if model.respond_to?(method)
model.public_send(method, *args, **kwargs, &block)
else
super
end
end
end
else # up to 2.6.x
def method_missing(method, *args, &block)
with_scope do
model = self.class._scope_model
if model.respond_to?(method)
model.public_send(method, *args, &block)
else
super
end
end
end
end
Expand Down
21 changes: 12 additions & 9 deletions lib/active_data/model/validations/nested.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_model/version'

module ActiveData
module Model
module Validations
Expand All @@ -12,20 +14,21 @@ def self.validate_nested(record, name, value)
end
end

def self.import_errors(from, to, prefix)
if ActiveModel.gem_version < Gem::Version.new('6.1.0')
# legacy ActiveModel iterates over key/message pairs
case ActiveModel.version.to_s
when /^6\.1\./, /^7\./
def self.import_errors(from, to, prefix)
from.each do |error|
key = "#{prefix}.#{error.attribute}"
to.import(error, attribute: key) unless to.added?(key, error.type, error.options)
end
end
else # up to 6.0.x
def self.import_errors(from, to, prefix)
from.each do |key, message|
key = "#{prefix}.#{key}"
to[key] << message
to[key].uniq!
end
else
# newer ActiveModel iterates over ActiveMode::Error instances
from.each do |error|
key = "#{prefix}.#{error.attribute}"
to.import(error, attribute: key) unless to.added?(key, error.type, error.options)
end
end
end

Expand Down

0 comments on commit c415b9b

Please sign in to comment.