Module: ActiverecordToPoro::MapperExtension

Included in:
ObjectMapper
Defined in:
lib/activerecord_to_poro/mapper_extension.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.is_an_ar_collection?(ar_class, association_name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/activerecord_to_poro/mapper_extension.rb', line 46

def is_an_ar_collection?(ar_class, association_name)
  ref = reflection_for_association(ar_class, association_name)
  (ref && ref.collection?)
end

.is_an_has_many_through(ar_class, association_name) ⇒ Object



51
52
53
54
# File 'lib/activerecord_to_poro/mapper_extension.rb', line 51

def is_an_has_many_through(ar_class, association_name)
  ref = reflection_for_association(ar_class, association_name)
  ref && ref.macro == :has_many && ref.options.has_key?(:through)
end

.reflection_for_association(ar_class, association_name) ⇒ Object



56
57
58
# File 'lib/activerecord_to_poro/mapper_extension.rb', line 56

def reflection_for_association(ar_class, association_name)
  ar_class.reflections[association_name.to_sym] || ar_class.reflections[association_name.to_s]
end

Instance Method Details

#association_rule(to: nil, from: to, reverse_to: from, reverse_from: to, converter: nil, reverse_converter: converter, is_collection: false, lazy_loading: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/activerecord_to_poro/mapper_extension.rb', line 4

def association_rule(to: nil,
    from: to,
    reverse_to: from,
    reverse_from: to,
    converter: nil,
    reverse_converter: converter,
    is_collection: false,
    lazy_loading: nil
)

  map_collection =  ActiverecordToPoro::MapperExtension.is_an_ar_collection?(self.dump_result_source, from) || is_collection

  options ={
      to: to,
      from: from,
      reverse_to: reverse_to,
      reverse_from: reverse_from,
      reverse_lazy_loading: false, #AR doesn't like ToProcDelegator
      is_collection: map_collection,
      lazy_loading: lazy_loading
  }

  if converter.nil?
    options[:converter] = noop
    options[:object_converter] = nil
  else
    options[:object_converter] = converter#.mapper
  end

  if reverse_converter.nil? ||  ActiverecordToPoro::MapperExtension.is_an_has_many_through(self.dump_result_source, from)
    options[:reverse_converter] = noop
    options[:reverse_object_converter] = nil
  else
    options[:reverse_object_converter] =  reverse_converter#.mapper
  end

  rule options

end