Class: JsonThing
- Inherits:
-
Object
- Object
- JsonThing
- 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
-
#ar_class ⇒ Object
readonly
Returns the value of attribute ar_class.
-
#cte_name ⇒ Object
readonly
Returns the value of attribute cte_name.
-
#full_name ⇒ Object
readonly
Returns the value of attribute full_name.
-
#jbs_name ⇒ Object
readonly
Returns the value of attribute jbs_name.
-
#json_key ⇒ Object
readonly
Returns the value of attribute json_key.
-
#json_type ⇒ Object
readonly
Returns the value of attribute json_type.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
-
#serializer_options ⇒ Object
readonly
Returns the value of attribute serializer_options.
Class Method Summary collapse
-
.json_key(k) ⇒ Object
TODO: tests.
Instance Method Summary collapse
- #enum?(field) ⇒ Boolean
-
#from_reflection(reflection_name) ⇒ Object
Constructs another JsonThing with this one as the parent, via
reflection_name. - #has_sql_method?(field) ⇒ Boolean
-
#initialize(ar_class, full_name, serializer = nil, serializer_options = {}, reflection = nil, parent_json_thing = nil) ⇒ JsonThing
constructor
A new instance of JsonThing.
-
#primary_key_attr ⇒ Object
Returns the primary key column as a string, but if there is a “#primary_key__sql” method, then call that and return it instead.
- #sql_method(field) ⇒ Object
-
#unaliased(field_name) ⇒ Object
Checks for alias_attribute and gets to the real attribute name.
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, ={}, 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, {}) = # 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_class ⇒ Object (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_name ⇒ Object (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_name ⇒ Object (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_name ⇒ Object (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_key ⇒ Object (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_type ⇒ Object (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 |
#name ⇒ Object (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 |
#parent ⇒ Object (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 |
#reflection ⇒ Object (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 |
#serializer ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute serializer_options.
112 113 114 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 112 def 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
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, , refl, self) end |
#has_sql_method?(field) ⇒ 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 |
#primary_key_attr ⇒ Object
Returns the primary key column as a string, but if there is a “#primary_key__sql” method, then call that and return it instead. We use this for the id reported in the jsonapi output, but not for foreign key relationships.
181 182 183 184 185 186 187 188 |
# File 'lib/active_model_serializers/adapter/json_api_pg.rb', line 181 def primary_key_attr pk = primary_key if has_sql_method?(pk) sql_method(pk) else pk end 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 |