Module: Dase::RelationInstanceMethods

Defined in:
lib/dase/mp_active_record.rb

Instance Method Summary collapse

Instance Method Details

#dase_valuesObject



3
4
5
# File 'lib/dase/mp_active_record.rb', line 3

def dase_values
  @dase_values ||= {}
end

#extract_proc_argument(args, options) ⇒ Object



21
22
23
24
25
26
# File 'lib/dase/mp_active_record.rb', line 21

def extract_proc_argument(args, options)
  if args.last.is_a?(Proc)
    raise "Can't use :proc option together with ->{} syntax" if options.has_key?(:proc)
    options[:proc] = args.pop
  end
end

#includes_count_of(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dase/mp_active_record.rb', line 7

def includes_count_of(*args)
  options = args.extract_options!
  extract_proc_argument(args, options)
  sanitize_dase_options(args, options)
  apply_synonyms(options)
  return self if args.empty?
  clone.tap do |relation|
    args.each do |arg|
      counter_name = (options[:as] || "#{arg}_count").to_sym
      relation.dase_values[counter_name] = options.merge(as: counter_name, association: arg.to_sym)
    end
  end
end

#merge(other, *args) ⇒ Object



28
29
30
31
32
# File 'lib/dase/mp_active_record.rb', line 28

def merge(other, *args)
  super.tap do |result|
    result.dase_values.merge!(other.dase_values) if other # other == nil is fine too
  end
end