lib/jsonapi/resource.rb in jsonapi-resources-0.7.0 vs lib/jsonapi/resource.rb in jsonapi-resources-0.7.1.beta1

- old
+ new

@@ -103,12 +103,13 @@ change :replace_fields do _replace_fields(field_data) end end + # Override this on a resource instance to override the fetchable keys def fetchable_fields - self.class.fetchable_fields(context) + self.class.fields end # Override this on a resource to customize how the associated records # are fetched for a model. Particularly helpful for authorization. def records_for(relation_name) @@ -117,10 +118,33 @@ def model_error_messages _model.errors.messages end + # Add metadata to validation error objects. + # + # Suppose `model_error_messages` returned the following error messages + # hash: + # + # {password: ["too_short", "format"]} + # + # Then to add data to the validation error `validation_error_metadata` + # could return: + # + # { + # password: { + # "too_short": {"minimum_length" => 6}, + # "format": {"requirement" => "must contain letters and numbers"} + # } + # } + # + # The specified metadata is then be merged into the validation error + # object. + def validation_error_metadata + {} + end + # Override this to return resource level meta data # must return a hash, and if the hash is empty the meta section will not be serialized with the resource # meta keys will be not be formatted with the key formatter for the serializer by default. They can however use the # serializer's format_key and format_value methods if desired # the _options hash will contain the serializer and the serialization_options @@ -171,12 +195,13 @@ :completed end def _remove - @model.destroy - + unless @model.destroy + fail JSONAPI::Exceptions::ValidationErrors.new(self) + end :completed end def _create_to_many_links(relationship_type, relationship_key_values) relationship = self.class._relationships[relationship_type] @@ -438,15 +463,10 @@ super end end # :nocov: - # Override in your resource to filter the fetchable keys - def fetchable_fields(_context = nil) - fields - end - # Override in your resource to filter the updatable keys def updatable_fields(_context = nil) _updatable_relationships | _attributes.keys - [:id] end @@ -521,15 +541,15 @@ required_includes = [] if filters filters.each do |filter, value| if _relationships.include?(filter) - if _relationships[filter].is_a?(JSONAPI::Relationship::ToMany) - required_includes.push(filter.to_s) - records = apply_filter(records, "#{filter}.#{_relationships[filter].primary_key}", value, options) + if _relationships[filter].belongs_to? + records = apply_filter(records, _relationships[filter].foreign_key, value, options) else - records = apply_filter(records, "#{_relationships[filter].foreign_key}", value, options) + required_includes.push(filter.to_s) + records = apply_filter(records, "#{_relationships[filter].table_name}.#{_relationships[filter].primary_key}", value, options) end else records = apply_filter(records, filter, value, options) end end @@ -585,11 +605,11 @@ end # Override this method if you want to customize the relation for # finder methods (find, find_by_key) def records(_options = {}) - _model_class + _model_class.all end def verify_filters(filters, context = nil) verified_filters = {} filters.each do |filter, raw_value| @@ -692,9 +712,13 @@ _abstract ? '' : @_model_name ||= name.demodulize.sub(/Resource$/, '') end def _primary_key @_primary_key ||= _model_class.respond_to?(:primary_key) ? _model_class.primary_key : :id + end + + def _table_name + @_table_name ||= _model_class.respond_to?(:table_name) ? _model_class.table_name : _model_name.tableize end def _as_parent_key @_as_parent_key ||= "#{_type.to_s.singularize}_id" end