Class: SvelteOnRails::Lib::ToSvelteValues

Inherits:
Object
  • Object
show all
Defined in:
lib/svelte_on_rails/lib/to_svelte_values.rb

Class Method Summary collapse

Class Method Details

.active_record_collection?(obj) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 119

def self.active_record_collection?(obj)
  (obj.is_a?(ActiveRecord::Relation) ||
    obj.is_a?(ActiveRecord::Associations::CollectionProxy)) rescue false
end

.apply_pagination(coll, associations) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 25

def self.apply_pagination(coll, associations)
  if associations.key?(:offset) || associations.key?(:limit)
    if active_record_collection?(coll)
      scope = coll
      scope = scope.offset(associations[:offset]) if associations.key?(:offset)
      scope = scope.limit(associations[:limit]) if associations.key?(:limit)
      scope.to_a
    elsif coll.respond_to?(:drop) && coll.respond_to?(:take)
      res = coll
      res = res.drop(associations[:offset]) if associations.key?(:offset)
      res = res.take(associations[:limit]) if associations.key?(:limit)
      res
    else
      raise "[SOR] to_svelte Cannot paginate #{coll.class}"
    end
  else
    coll.respond_to?(:to_a) ? coll.to_a : coll
  end
end

.collection_like?(obj) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 115

def self.collection_like?(obj)
  looks_like_collection?(obj) || obj.is_a?(Array)
end

.extract_single_record(record, attributes, associations, next_stack) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 57

def self.extract_single_record(record, attributes, associations, next_stack)
  return {} unless record # or raise – depending on your preference

  values = {}

  enums = record.class.try(:defined_enums) || {}

  attributes.each do |attr|
    validate_attribute!(record, attr)

    key_s = attr.to_s
    attr_key = key_s.camelize(:lower)
    value = record.send(attr)

    values[attr_key] = enums.key?(key_s) ? record.send(key_s) : value
  end

  associations.each do |key, val|
    key_s = key.to_s
    attr_key = key_s.camelize(:lower)
    next if %w[offset limit].include?(key_s)

    reflection = record.class.reflect_on_association(key_s)
    raise ArgumentError, "No association #{key_s.inspect} on #{record.class}" unless reflection

    related = record.send(key_s)

    if related.respond_to?(:each) && related.empty?
      values[attr_key] = []
    elsif !related
      values[attr_key] = nil
    else

      sub_attrs, sub_assocs = SvelteOnRails::Lib::ToSvelteSupport.separate_associations(val)

      values[attr_key] = extract_values_recursive(
        related,
        sub_attrs,
        sub_assocs,
        call_stack: next_stack
      )
    end
  end

  values
end

.extract_values(record, attributes = [], associations = {}) ⇒ Object

Public API – returns Hash for single record, Array<Hash> for collections



5
6
7
8
9
10
11
12
13
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 5

def self.extract_values(record, attributes = [], associations = {})

  records = normalize_to_enumerable(record, associations)

  result = extract_values_recursive(records, attributes, associations, call_stack: 0)

  # Mirror input shape for the top-level return value
  looks_like_single?(record) ? (result.first || {}) : result
end

.extract_values_recursive(thing, attributes, associations, call_stack:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 45

def self.extract_values_recursive(thing, attributes, associations, call_stack:)
  next_stack = call_stack + 1

  if collection_like?(thing)
    thing.map do |record|
      extract_single_record(record, attributes, associations, next_stack)
    end
  else
    extract_single_record(thing, attributes, associations, next_stack)
  end
end

.looks_like_collection?(obj) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 110

def self.looks_like_collection?(obj)
  return false if obj.nil? || obj.is_a?(Hash) || obj.is_a?(String)
  obj.respond_to?(:each)
end

.looks_like_single?(obj) ⇒ Boolean

Helpers ────────────────────────────────────────────────

Returns:

  • (Boolean)


106
107
108
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 106

def self.looks_like_single?(obj)
  !looks_like_collection?(obj)
end

.normalize_to_enumerable(record, associations) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 17

def self.normalize_to_enumerable(record, associations)
  if looks_like_collection?(record)
    apply_pagination(record, associations)
  else
    [record] # single → wrap → recursive code can be uniform
  end
end

.validate_attribute!(record, attr) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/svelte_on_rails/lib/to_svelte_values.rb', line 124

def self.validate_attribute!(record, attr)
  unless attr.is_a?(Symbol) || attr.is_a?(String)
    raise ArgumentError, "Attribute must be Symbol or String, got #{attr.inspect}"
  end
  unless record.respond_to?(attr)
    raise "[SOR] to_svelte: No method #{attr.inspect} on #{record.class}"
  end
end