Class: ActiveRecord::DynamicMatchers::Method
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | activerecord/lib/active_record/dynamic_matchers.rb |
Class Method Summary
- .define(model, name)
- .match(name)
- .valid?(model, name) ⇒ Boolean
- .attribute_names(model, name) private
-
.attributes_hash(model, method_name)
private
Given that the parameters starts with
_
, the finder needs to use the same parameter name. - .body(model, method_name) private
- .make_pattern(prefix, suffix) private
-
.signature(model, method_name)
private
The parameters in the signature may have reserved Ruby words, in order to prevent errors, we start each param name with
_
.
Class Method Details
.attribute_names(model, name) (private)
[ GitHub ]# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 51
def attribute_names(model, name) attribute_names = name.match(pattern)[1].split("_and_") attribute_names.map! { |name| model.attribute_aliases[name] || name } end
.attributes_hash(model, method_name) (private)
Given that the parameters starts with _
, the finder needs to use the same parameter name.
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 68
def attributes_hash(model, method_name) "{" + attribute_names(model, method_name).map { |name| ":#{name} => _#{name}" }.join(",") + "}" end
.body(model, method_name) (private)
[ GitHub ]# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 56
def body(model, method_name) "#{finder}(#{attributes_hash(model, method_name)})" end
.define(model, name)
[ GitHub ].make_pattern(prefix, suffix) (private)
[ GitHub ]# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 47
def make_pattern(prefix, suffix) /\A#{prefix}_([_a-zA-Z]\w*)#{suffix}\Z/ end
.match(name)
[ GitHub ]# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 30
def match(name) FindBy.match?(name) || FindByBang.match?(name) end
.signature(model, method_name) (private)
The parameters in the signature may have reserved Ruby words, in order to prevent errors, we start each param name with _
.
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 62
def signature(model, method_name) attribute_names(model, method_name.to_s).map { |name| "_#{name}" }.join(", ") end
.valid?(model, name) ⇒ Boolean
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 34
def valid?(model, name) attribute_names(model, name.to_s).all? { |name| model.columns_hash[name] || model.reflect_on_aggregation(name.to_sym) } end