Class: ActiveRecord::DynamicMatchers::Instantiator

Inherits:
Method
  • Object
show all
Includes:
DeprecationWarning
Defined in:
lib/active_record/deprecated_finders/dynamic_matchers.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeprecationWarning

#deprecation_warning

Class Method Details

.dispatch(klass, attribute_names, instantiator, args, block) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/active_record/deprecated_finders/dynamic_matchers.rb', line 128

def self.dispatch(klass, attribute_names, instantiator, args, block)
  if args.length == 1 && args.first.is_a?(Hash)
    attributes = args.first.stringify_keys
    conditions = attributes.slice(*attribute_names)
    rest       = [attributes.except(*attribute_names)]
  else
    raise ArgumentError, "too few arguments" unless args.length >= attribute_names.length

    conditions = Hash[attribute_names.map.with_index { |n, i| [n, args[i]] }]
    rest       = args.drop(attribute_names.length)
  end

  klass.where(conditions).first ||
    klass.create_with(conditions).send(instantiator, *rest, &block)
end

Instance Method Details

#bodyObject



148
149
150
151
152
153
# File 'lib/active_record/deprecated_finders/dynamic_matchers.rb', line 148

def body
  <<-CODE
    #{deprecation_warning}
    #{self.class}.dispatch(self, #{attribute_names.inspect}, #{instantiator.inspect}, args, block)
  CODE
end

#deprecation_alternativeObject



159
160
161
# File 'lib/active_record/deprecated_finders/dynamic_matchers.rb', line 159

def deprecation_alternative
  "Post.#{self.class.prefix}#{self.class.suffix}(name: 'foo')"
end

#instantiatorObject

Raises:

  • (NotImplementedError)


155
156
157
# File 'lib/active_record/deprecated_finders/dynamic_matchers.rb', line 155

def instantiator
  raise NotImplementedError
end

#signatureObject



144
145
146
# File 'lib/active_record/deprecated_finders/dynamic_matchers.rb', line 144

def signature
  "*args, &block"
end