Module: ActiveFacet::Serializer::Base
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/active_facet/serializer/base.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#as_json(resources, options = {}) ⇒ JSON
This method returns a JSON of hash values representing the resource(s).
-
#config ⇒ Config
Memoized instance getter.
-
#custom_includes(field, options) ⇒ Field Set
Returns field_set serialized for dependant resources in custom attribute serializers & extensions.
-
#exposed_aliases(field_set_alias = :all, include_relations = false, include_nested_field_sets = false) ⇒ Array
Gets flattened fields from a Field Set Alias.
-
#from_hash(resource, attributes, options = {}) ⇒ ActiveRecord
This method returns a ActiveRecord model updated to match a JSON of hash values.
-
#get_association_reflection(field) ⇒ Reflection | nil
Constantizes an appropriate resource serializer class for relations.
-
#get_association_serializer_class(field, options) ⇒ Class | nil
Constantizes an appropriate resource serializer class.
-
#get_custom_serializer_class(attribute, options) ⇒ Class | nil
Constantizes an appropriate attribute serializer class.
-
#is_association?(field) ⇒ Boolean
Determines if public attribute maps to a private relation.
-
#resource_attribute_name(field, direction = :from) ⇒ Symbol
Renames attribute between resource.attribute_name and json.attribute_name.
-
#resource_class ⇒ Class
Constantizes the appropriate resource serializer class.
-
#scoped_include(field, nested_field_set, options) ⇒ Object
TODO – comment and move private.
-
#scoped_includes(field_set = nil, options = {}) ⇒ Hash
This method returns a hash suitable to pass into ActiveRecord.includes to avoid N+1 given: :basic is a defined collection :extended is a defined collection :orders is a defined association :line_items is a defined association on OrderSerializer examples: :basic [:basic] nil [:basic, :extended] [:basic, :extended, :orders] [:basic, :extended, :basic] [:basic, :extended, [:basic, :extended]] [:basic, :extended, [:basic, :line_items]] [:basic, :extended, [:basic, {line_items: :extended]}].
Instance Method Details
#as_json(resources, options = {}) ⇒ JSON
This method returns a JSON of hash values representing the resource(s)
190 191 192 193 194 195 |
# File 'lib/active_facet/serializer/base.rb', line 190 def as_json(resources, = {}) resource_itterator(resources) do |resource| facade = ActiveFacet::Serializer::Facade.new(self, resource, ) facade.as_json end end |
#config ⇒ Config
Memoized instance getter
199 200 201 |
# File 'lib/active_facet/serializer/base.rb', line 199 def config @config ||= self.class.config end |
#custom_includes(field, options) ⇒ Field Set
Returns field_set serialized for dependant resources in custom attribute serializers & extensions
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/active_facet/serializer/base.rb', line 150 def custom_includes(field, ) attribute = resource_attribute_name(field) custom_serializer_name = config.serializers[attribute] if custom_serializer_name custom_serializer = get_custom_serializer_class(custom_serializer_name, ) if custom_serializer.respond_to? :custom_scope custom_serializer.custom_scope else nil end else nil end end |
#exposed_aliases(field_set_alias = :all, include_relations = false, include_nested_field_sets = false) ⇒ Array
Gets flattened fields from a Field Set Alias
171 172 173 174 175 176 |
# File 'lib/active_facet/serializer/base.rb', line 171 def exposed_aliases(field_set_alias = :all, include_relations = false, include_nested_field_sets = false) return include_nested_field_sets ? field_set_alias : [field_set_alias] unless normalized_field_sets = config.normalized_field_sets[field_set_alias] result = normalized_field_sets[include_relations ? :fields : :attributes] return result if include_nested_field_sets result.keys.map(&:to_sym).sort end |
#from_hash(resource, attributes, options = {}) ⇒ ActiveRecord
This method returns a ActiveRecord model updated to match a JSON of hash values
182 183 184 |
# File 'lib/active_facet/serializer/base.rb', line 182 def from_hash(resource, attributes, = {}) ActiveFacet::Serializer::Facade.new(self, resource, ).from_hash(attributes) end |
#get_association_reflection(field) ⇒ Reflection | nil
Constantizes an appropriate resource serializer class for relations
215 216 217 218 |
# File 'lib/active_facet/serializer/base.rb', line 215 def get_association_reflection(field) @association_reflections ||= {} @association_reflections[field] ||= resource_class.reflect_on_association(resource_attribute_name(field).to_sym) end |
#get_association_serializer_class(field, options) ⇒ Class | nil
Constantizes an appropriate resource serializer class
223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/active_facet/serializer/base.rb', line 223 def get_association_serializer_class(field, ) @association_serializers ||= {} unless @association_serializers.key? field @association_serializers[field] = nil #return nil if field isn't an association if reflection = get_association_reflection(field) #return nil if association doesn't have a custom class @association_serializers[field] = ActiveFacet::Helper.serializer_for(reflection.klass, ) end end @association_serializers[field] end |
#get_custom_serializer_class(attribute, options) ⇒ Class | nil
Constantizes an appropriate attribute serializer class
240 241 242 243 |
# File 'lib/active_facet/serializer/base.rb', line 240 def get_custom_serializer_class(attribute, ) @custom_serializers ||= {} @custom_serializers[attribute] ||= ActiveFacet::Helper.attribute_serializer_class_for(resource_class, attribute, ) end |
#is_association?(field) ⇒ Boolean
Determines if public attribute maps to a private relation
248 249 250 |
# File 'lib/active_facet/serializer/base.rb', line 248 def is_association?(field) !!get_association_reflection(field) end |
#resource_attribute_name(field, direction = :from) ⇒ Symbol
Renames attribute between resource.attribute_name and json.attribute_name
256 257 258 |
# File 'lib/active_facet/serializer/base.rb', line 256 def resource_attribute_name(field, direction = :from) (config.transforms(direction)[field] || field).to_sym end |
#resource_class ⇒ Class
Constantizes the appropriate resource serializer class
208 209 210 |
# File 'lib/active_facet/serializer/base.rb', line 208 def resource_class @resource_class ||= config.resource_class end |
#scoped_include(field, nested_field_set, options) ⇒ Object
TODO – comment and move private
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/active_facet/serializer/base.rb', line 132 def scoped_include(field, nested_field_set, ) if is_association? field attribute = resource_attribute_name(field) if nested_field_set serializer_class = get_association_serializer_class(field, ) attribute = { attribute => serializer_class.present? ? serializer_class.scoped_includes(nested_field_set, ) : nested_field_set } end attribute else custom_includes(field, ) end end |
#scoped_includes(field_set = nil, options = {}) ⇒ Hash
This method returns a hash suitable to pass into ActiveRecord.includes to avoid N+1 given:
:basic is a defined collection
:extended is a defined collection
:orders is a defined association
:line_items is a defined association on OrderSerializer
examples:
:basic
[:basic]
{basic: nil}
[:basic, :extended]
[:basic, :extended, :orders]
[:basic, :extended, {orders: :basic}]
[:basic, :extended, {orders: [:basic, :extended]}]
[:basic, :extended, {orders: [:basic, :line_items]}]
[:basic, :extended, {orders: [:basic, {line_items: :extended}]}]
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_facet/serializer/base.rb', line 116 def scoped_includes(field_set = nil, = {}) result = {} config.field_set_itterator(field_set) do |field, nested_field_set| case value = scoped_include(field, nested_field_set, ) when nil when Hash result.deep_merge! value else result[value] ||= nil end end result end |