Module: Influxer::Model::ClassMethods

Defined in:
lib/influxer/model.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#has_metrics(*args, **params) ⇒ Object

rubocop:disable Naming/PredicateName rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/influxer/model.rb', line 14

def has_metrics(*args, **params)
  metrics_name = args.empty? ? "metrics" : args.first.to_s

  klass = params[:class_name].present? ? params[:class_name] : "#{self}Metrics"
  klass = klass.constantize

  attrs = params[:inherits] if params[:inherits].present?

  foreign_key = params.fetch(:foreign_key, to_s.foreign_key)

  define_method(metrics_name) do
    rel_attrs = foreign_key ? { foreign_key => id } : {}

    unless attrs.nil?
      attrs.each do |key|
        rel_attrs[key] = send(key)
      end
    end
    Relation.new klass, attributes: rel_attrs
  end
end