Class: Fields::Serializer::FieldsTree

Inherits:
Object
  • Object
show all
Defined in:
lib/fields/serializer/fields_tree.rb

Overview

A class to store a tree structure of a model klass, its attributes and associations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ FieldsTree

Returns a new instance of FieldsTree.



11
12
13
14
15
# File 'lib/fields/serializer/fields_tree.rb', line 11

def initialize(klass)
  @klass        = klass
  @fields       = []
  @associations = {}
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



9
10
11
# File 'lib/fields/serializer/fields_tree.rb', line 9

def associations
  @associations
end

#fieldsObject (readonly)

Returns the value of attribute fields.



9
10
11
# File 'lib/fields/serializer/fields_tree.rb', line 9

def fields
  @fields
end

#klassObject (readonly)

Returns the value of attribute klass.



9
10
11
# File 'lib/fields/serializer/fields_tree.rb', line 9

def klass
  @klass
end

Instance Method Details

#merge!(join_field) ⇒ Object

Adds a new field (json api notation) to the tree structure:

user_tree.notation
  #=> [:name, :surname, { subjects: [:title, { posts: { comments: :count } }], followers: :nickname }]

user_tree.merge!("subjects.posts.date").notation
  #=> [:name, :surname, { subjects: [:title, { posts: [{ comments: :count }, :date] }], followers: :nickname }]


30
31
32
33
34
35
# File 'lib/fields/serializer/fields_tree.rb', line 30

def merge!(join_field)
  return self if join_field.blank?
  parent, rest = join_field.to_s.split(".", 2)
  rest.present? ? add_association!(parent, rest) : add_field!(parent)
  self
end

#notationObject

Return the tree structure in Rails includes notation including both associations and fields

user_tree.notation
  #=> [:name, :surname, { subjects: [:title, { posts: :comments }], followers: :nickname }]


42
43
44
45
46
47
48
49
# File 'lib/fields/serializer/fields_tree.rb', line 42

def notation
  return associations_to_notation.presence if fields.blank?
  if associations.present?
    fields.dup << associations_to_notation
  else
    fields.one? ? fields.first.dup : fields.dup
  end
end

#presenceObject

Self if any fields or associations. Nil otherwise



18
19
20
# File 'lib/fields/serializer/fields_tree.rb', line 18

def presence
  self if fields.present? || associations.present?
end

#to_includesObject

Return the tree structure in Rails includes notation including only associations

user_tree.notation
  #=> [{ subjects: { posts: :comments }}, :followers]


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fields/serializer/fields_tree.rb', line 56

def to_includes
  to_includes = associations.inject([]) do |result, (association_name, association_tree)|
    association_includes = association_tree.to_includes
    if association_includes.present?
      add_association_includes_to_includes!(result, association_name, association_includes)
    else
      add_association_to_includes!(result, association_name)
    end
  end.presence
  Array.wrap(to_includes).one? ? to_includes.first : to_includes
end

#to_sObject



68
69
70
# File 'lib/fields/serializer/fields_tree.rb', line 68

def to_s
  notation.to_s
end