module Dase::RelationInstanceMethods
Public Instance Methods
dase_values()
click to toggle source
# File lib/dase/mp_active_record.rb, line 3 def dase_values @dase_values ||= {} end
extract_proc_argument(args, options)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Calls superclass method
# 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
Private Instance Methods
apply_synonyms(options)
click to toggle source
legacy syntax support
# File lib/dase/mp_active_record.rb, line 37 def apply_synonyms(options) VALID_SYNONYMS.each do |old, new| if options.has_key?(old) raise "Don't use #{old} and #{new} together" if options.has_key?(new) options[new] = options.delete(old) end end end
attach_dase_counters_to_records()
click to toggle source
# File lib/dase/mp_active_record.rb, line 54 def attach_dase_counters_to_records (dase_values || {}).each do |_, options| Dase::Preloader.new(options).preload(@records, options[:association]) end end
sanitize_dase_options(args, options)
click to toggle source
# File lib/dase/mp_active_record.rb, line 46 def sanitize_dase_options(args, options) valid_options = VALID_DASE_OPTIONS + VALID_ASSOCIATION_OPTIONS + VALID_SYNONYMS.keys options.assert_valid_keys(*valid_options) if options.present? and args.many? raise ArgumentError, 'includes_count_of takes either multiple associations OR single association + options' end end