Class: JsonThing

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_serializers/adapter/json_api_pg.rb

Overview

Each JsonThing is a struct collecting all the stuff we need to know about a model you want in the JSONAPI output.

It has the ActiveRecord class, the name of the thing, and how to reach it from its parent.

The full_name param should be a dotted path like you’d pass to the ‘includes` option of ActiveModelSerializers, except it should also start with the name of the top-level entity.

The reflection should be from the perspective of the parent, i.e. how you got here, not how you’d leave: “Reflection” seems to be the internal ActiveRecord lingo for a belongs_to or has_many relationship. (The public documentation calls these “associations”. I think older versions of Rails even used that internally, but nowadays the method names use “reflection”.)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ar_class, full_name, serializer = nil, serializer_options = {}, reflection = nil, parent_json_thing = nil) ⇒ JsonThing

Returns a new instance of JsonThing.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 116

def initialize(ar_class, full_name, serializer=nil, serializer_options={}, reflection=nil, parent_json_thing=nil)
  @ar_class = ar_class
  @full_name = full_name
  @name = full_name.split('.').last
  @serializer = serializer || ActiveModel::Serializer.serializer_for(ar_class.new, {})
  @serializer_options = serializer_options

  # json_key and json_type might be the same thing, but not always.
  # json_key is the name of the belongs_to/has_many association,
  # and json_type is the name of the thing's class.
  @json_key = JsonThing.json_key(name)
  @json_type = JsonThing.json_key(ar_class.name.underscore.pluralize)

  @reflection = reflection
  @parent = parent_json_thing

  @cte_name = _cte_name
  @jbs_name = _jbs_name
  @sql_methods = {}
end

Instance Attribute Details

#ar_classObject (readonly)

Returns the value of attribute ar_class.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def ar_class
  @ar_class
end

#cte_nameObject (readonly)

Returns the value of attribute cte_name.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def cte_name
  @cte_name
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def full_name
  @full_name
end

#jbs_nameObject (readonly)

Returns the value of attribute jbs_name.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def jbs_name
  @jbs_name
end

#json_keyObject (readonly)

Returns the value of attribute json_key.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def json_key
  @json_key
end

#json_typeObject (readonly)

Returns the value of attribute json_type.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def json_type
  @json_type
end

#nameObject (readonly)

Returns the value of attribute name.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def parent
  @parent
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def reflection
  @reflection
end

#serializerObject (readonly)

Returns the value of attribute serializer.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def serializer
  @serializer
end

#serializer_optionsObject (readonly)

Returns the value of attribute serializer_options.



112
113
114
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112

def serializer_options
  @serializer_options
end

Class Method Details

.json_key(k) ⇒ Object

TODO: tests



158
159
160
161
162
163
164
165
166
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 158

def self.json_key(k)
  # TODO: technically the serializer could have an option overriding the default:
  case ActiveModelSerializers.config.key_transform
  when :dash
    k.to_s.gsub('_', '-')
  else
    k.to_s
  end
end

Instance Method Details

#enum?(field) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 144

def enum?(field)
  @ar_class.attribute_types[field.to_s].is_a? ActiveRecord::Enum::EnumType
end

#from_reflection(reflection_name) ⇒ Object

Constructs another JsonThing with this one as the parent, via ‘reflection_name`. TODO: tests



139
140
141
142
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 139

def from_reflection(reflection_name)
  refl = JsonApiReflection.new(reflection_name, ar_class, serializer)
  JsonThing.new(refl.klass, "#{full_name}.#{reflection_name}", nil, serializer_options, refl, self)
end

#has_sql_method?(field) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 168

def has_sql_method?(field)
  sql_method(field).present?
end

#sql_method(field) ⇒ Object



172
173
174
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 172

def sql_method(field)
  (@sql_methods[field] ||= _sql_method(field))[0]
end

#unaliased(field_name) ⇒ Object

Checks for alias_attribute and gets to the real attribute name.



149
150
151
152
153
154
155
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 149

def unaliased(field_name)
  ret = field_name
  while field_name = @ar_class.attribute_aliases[field_name.to_s]
    ret = field_name
  end
  ret
end