Class: Jbuilder::Schema::Template

Inherits:
JbuilderTemplate
  • Object
show all
Defined in:
lib/jbuilder/schema/template.rb

Defined Under Namespace

Classes: Handler, ModelScope

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, **options) ⇒ Template

Returns a new instance of Template.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jbuilder/schema/template.rb', line 48

def initialize(context, **options)
  @type = :object
  @inline_array = false
  @collection = false

  @model_scope = ModelScope.new(**options)

  super(context)

  @ignore_nil = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, **options, &block) ⇒ Object

standard:disable Style/MissingRespondToMissing



183
184
185
186
# File 'lib/jbuilder/schema/template.rb', line 183

def method_missing(*args, **options, &block) # standard:disable Style/MissingRespondToMissing
  # TODO: Remove once Jbuilder passes keyword arguments along to `set!` in its `method_missing`.
  set!(*args, **options, &block)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/jbuilder/schema/template.rb', line 8

def attributes
  @attributes
end

#model_scopeObject (readonly)

Returns the value of attribute model_scope.



9
10
11
# File 'lib/jbuilder/schema/template.rb', line 9

def model_scope
  @model_scope
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/jbuilder/schema/template.rb', line 8

def type
  @type
end

Class Method Details

.build(view_context, local_assigns) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/jbuilder/schema/template.rb', line 21

def self.build(view_context, local_assigns)
  if (options = local_assigns[:__jbuilder_schema_options])
    new(view_context, **options)
  else
    ::JbuilderTemplate.new(view_context)
  end
end

Instance Method Details

#array!(collection = [], *args, schema: {}, **options, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/jbuilder/schema/template.rb', line 130

def array!(collection = [], *args, schema: {}, **options, &block)
  if _partial_options?(options)
    @collection = true
    _set_ref(options[:partial].split("/").last)
  else
    array = _make_array(collection, *args, schema: schema, &block)

    if @inline_array
      @attributes = {}
      _set_value(:type, :array)
      _set_value(:items, array)
    elsif _is_collection_array?(array)
      @attributes = {}
      @inline_array = true
      @collection = true
      array! array, *array.first&.attribute_names(&:to_sym)
    else
      @type = :array
      @attributes = {}
      _set_value(:items, array)
    end
  end
end

#cache!(key = nil, **options) ⇒ Object



179
180
181
# File 'lib/jbuilder/schema/template.rb', line 179

def cache!(key = nil, **options)
  yield # TODO: Our schema generation breaks Jbuilder's fragment caching.
end

#extract!(object, *attributes, schema: {}) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/jbuilder/schema/template.rb', line 122

def extract!(object, *attributes, schema: {})
  if ::Hash === object
    _extract_hash_values(object, attributes, schema: schema)
  else
    _extract_method_values(object, attributes, schema: schema)
  end
end

#merge!(object) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jbuilder/schema/template.rb', line 166

def merge!(object)
  hash_or_array = ::Jbuilder === object ? object.attributes! : object
  hash_or_array = _format_keys(hash_or_array)
  if hash_or_array.is_a?(::Hash)
    hash_or_array = hash_or_array.each_with_object({}) do |(key, value), a|
      result = _schema(key, value)
      result = _set_description(key, result) if model_scope.model
      a[key] = result
    end
  end
  @attributes = _merge_values(@attributes, hash_or_array)
end

#partial!(*args) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/jbuilder/schema/template.rb', line 154

def partial!(*args)
  if args.one? && _is_active_model?(args.first)
    # TODO: Find where it is being used
    _render_active_model_partial args.first
  elsif args.first.is_a?(::Hash)
    _set_ref(args.first[:partial].split("/").last)
  else
    @collection = true if args[1].key?(:collection)
    _set_ref(args.first&.split("/")&.last)
  end
end

#schema!Object



64
65
66
# File 'lib/jbuilder/schema/template.rb', line 64

def schema!
  {type: type}.merge(type == :object ? _object(**attributes.merge) : attributes)
end

#set!(key, value = BLANK, *args, schema: {}, **options, &block) ⇒ Object



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jbuilder/schema/template.rb', line 68

def set!(key, value = BLANK, *args, schema: {}, **options, &block)
  result = if block
    if !_blank?(value)
      # OBJECTS ARRAY:
      # json.comments @article.comments { |comment| ... }
      # { "comments": [ { ... }, { ... } ] }
      _scope { array! value, &block }
    else
      # BLOCK:
      # json.comments { ... }
      # { "comments": ... }
      @inline_array = true

      _with_model_scope(**schema) do
        _merge_block(key) { yield self }
      end
    end
  elsif args.empty?
    if ::Jbuilder === value
      # ATTRIBUTE1:
      # json.age 32
      # json.person another_jbuilder
      # { "age": 32, "person": { ...  }
      _schema(key, _format_keys(value.attributes!), **schema)
    elsif _is_collection_array?(value)
      # ATTRIBUTE2:
      _scope { array! value }
    # json.articles @articles
    else
      # json.age 32
      # { "age": 32 }
      _schema(key, _format_keys(value), **schema)
    end
  elsif _is_collection?(value)
    # COLLECTION:
    # json.comments @article.comments, :content, :created_at
    # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
    @inline_array = true
    @collection = true

    _scope { array! value, *args }
  else
    # EXTRACT!:
    # json.author @article.creator, :name, :email_address
    # { "author": { "name": "David", "email_address": "[email protected]" } }
    _with_model_scope(**schema) do
      _merge_block(key) { extract! value, *args, schema: schema }
    end
  end

  result = _set_description key, result if model_scope.model
  _set_value key, result
end

#target!Object



60
61
62
# File 'lib/jbuilder/schema/template.rb', line 60

def target!
  schema!
end