Module: ActiveRecordToHash

Defined in:
lib/active_record_to_hash.rb,
lib/active_record_to_hash/version.rb,
lib/active_record_to_hash/utilities.rb,
lib/active_record_to_hash/active_record.rb

Defined Under Namespace

Modules: ActiveRecord Classes: Railtie

Constant Summary collapse

VERSION =
'1.5.1'

Class Method Summary collapse

Class Method Details

.call_scope(relation, scope) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/active_record_to_hash/utilities.rb', line 6

def call_scope(relation, scope)
  if scope.is_a? Proc
    ret = relation.instance_exec(&scope)
    return ret || relation
  end

  relation.public_send(scope)
end

.handle_alter(result, options) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/active_record_to_hash/utilities.rb', line 63

def handle_alter(result, options)
  return result if options[:alter].nil?

  res = options[:alter].call(result)
  return result if res.nil?

  res
end

.handle_with_options(options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_record_to_hash/utilities.rb', line 72

def handle_with_options(options)
  options.each_key do |key|
    next unless key.to_s.start_with?('with_')
    next if options[key] == false

    attr_name = key[5..-1].to_sym # 5 is 'with_'.length
    hash_key = !options[key].is_a?(Hash) || options[key][:key].nil? ? attr_name : options[key][:key]
    child_options = ActiveRecordToHash.normalize_child_options(options[key])
    yield(hash_key, attr_name, child_options)
  end
end

.normalize_child_options(options) ⇒ Object



84
85
86
87
88
89
# File 'lib/active_record_to_hash/utilities.rb', line 84

def normalize_child_options(options)
  return {} if options == true
  return { exists: true } if options == :exists

  options
end

.retrieve_child_attribute(record, attr_name, options, callee) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/LineLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_record_to_hash/utilities.rb', line 22

def retrieve_child_attribute(record, attr_name, options, callee) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/LineLength
  if options.key?(:value)
    return options[:value].call(record) if options[:value].is_a? Proc

    return options[:value]
  end

  if options.key?(:delegate)
    key = options[:delegate].keys.first
    value = options[:delegate].values.first

    delegator = record.public_send(key)
    return nil if delegator.nil?

    return delegator.public_send(value)
  end

  args = options[:args] || []
  value = record.public_send(attr_name, *args)
  if options[:exists]
    raise 'You can use `exists` option only with ActiveRecord::Relation' unless value.is_a? ::ActiveRecord::Relation

    return value.exists?
  end

  ActiveRecordToHash.to_a(options[:scope]).each do |scope|
    value = ActiveRecordToHash.call_scope(value, scope)
  end
  return value.public_send(callee, options.except(:alter)) if value.is_a? ::ActiveRecordToHash::ActiveRecord

  if value.is_a?(Array) || value.is_a?(::ActiveRecord::Relation)
    return value.map do |obj|
      next obj.public_send(callee, options.except(:alter)) if obj.is_a? ::ActiveRecordToHash::ActiveRecord

      obj
    end
  end

  value
end

.to_a(value) ⇒ Object



15
16
17
18
19
20
# File 'lib/active_record_to_hash/utilities.rb', line 15

def to_a(value)
  return value if value.is_a? Array
  return [] if value.nil?

  [value]
end