Class: JSONAPI::Resource
- Inherits:
-
Object
- Object
- JSONAPI::Resource
- Includes:
- ResourceFor
- Defined in:
- lib/jsonapi/resource.rb
Constant Summary collapse
- @@resource_types =
{}
Class Attribute Summary collapse
-
._allowed_filters ⇒ Object
Returns the value of attribute _allowed_filters.
-
._associations ⇒ Object
Returns the value of attribute _associations.
-
._attributes ⇒ Object
Returns the value of attribute _attributes.
-
._type ⇒ Object
Returns the value of attribute _type.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
-
._allowed_filter?(filter) ⇒ Boolean
:nocov:.
- ._as_parent_key ⇒ Object
- ._association(type) ⇒ Object
-
._attribute_options(attr) ⇒ Object
quasi private class methods.
- ._has_association?(type) ⇒ Boolean
- ._key ⇒ Object
- ._model_name ⇒ Object
- ._primary_key ⇒ Object
- ._resource_name_from_type(type) ⇒ Object
- ._updateable_associations ⇒ Object
- .attribute(attr, options = {}) ⇒ Object
-
.attributes(*attrs) ⇒ Object
Methods used in defining a resource class.
- .create(context) ⇒ Object
- .create_model ⇒ Object
-
.createable_fields(context = nil) ⇒ Object
Override in your resource to filter the createable keys.
- .default_attribute_options ⇒ Object
- .fields ⇒ Object
- .filter(attr) ⇒ Object
- .filters(*attrs) ⇒ Object
-
.find(filters, options = {}) ⇒ Object
Override this method if you have more complex requirements than this basic find method provides.
- .find_by_key(key, options = {}) ⇒ Object
- .find_by_keys(keys, options = {}) ⇒ Object
- .has_many(*attrs) ⇒ Object
- .has_one(*attrs) ⇒ Object
- .inherited(base) ⇒ Object
- .is_filter_association?(filter) ⇒ Boolean
- .key(key) ⇒ Object
- .model_name(model) ⇒ Object
- .primary_key(key) ⇒ Object
-
.records(options = {}) ⇒ Object
Override this method if you want to customize the relation for finder methods (find, find_by_key, find_by_keys).
- .routing_options(options) ⇒ Object
- .routing_resource_options ⇒ Object
-
.sortable_fields(context = nil) ⇒ Object
Override in your resource to filter the sortable keys.
-
.updateable_fields(context = nil) ⇒ Object
Override in your resource to filter the updateable keys.
-
.verify_association_filter(filter, raw, context = nil) ⇒ Object
override to allow for custom association logic, such as uuids, multiple keys or permission checks on keys.
-
.verify_custom_filter(filter, value, context = nil) ⇒ Object
override to allow for custom filters.
- .verify_filter(filter, raw, context = nil) ⇒ Object
- .verify_filters(filters, context = nil) ⇒ Object
-
.verify_key(key, context = nil) ⇒ Object
override to allow for key processing and checking.
-
.verify_keys(keys, context = nil) ⇒ Object
override to allow for key processing and checking.
Instance Method Summary collapse
- #_model_class ⇒ Object
- #create_has_many_link(association_type, association_key_value) ⇒ Object
- #create_has_one_link(association_type, association_key_value) ⇒ Object
-
#fetchable_fields ⇒ Object
Override this on a resource instance to override the fetchable keys.
- #id ⇒ Object
-
#initialize(model, context = nil) ⇒ Resource
constructor
A new instance of Resource.
- #remove ⇒ Object
- #remove_has_many_link(association_type, key) ⇒ Object
- #remove_has_one_link(association_type) ⇒ Object
- #replace_fields(field_data) ⇒ Object
- #replace_has_many_links(association_type, association_key_values) ⇒ Object
- #replace_has_one_link(association_type, association_key_value) ⇒ Object
- #save ⇒ Object
Methods included from ResourceFor
Constructor Details
#initialize(model, context = nil) ⇒ Resource
Returns a new instance of Resource.
14 15 16 17 |
# File 'lib/jsonapi/resource.rb', line 14 def initialize(model, context = nil) @model = model @context = context end |
Class Attribute Details
._allowed_filters ⇒ Object
Returns the value of attribute _allowed_filters.
128 129 130 |
# File 'lib/jsonapi/resource.rb', line 128 def _allowed_filters @_allowed_filters end |
._associations ⇒ Object
Returns the value of attribute _associations.
128 129 130 |
# File 'lib/jsonapi/resource.rb', line 128 def _associations @_associations end |
._attributes ⇒ Object
Returns the value of attribute _attributes.
128 129 130 |
# File 'lib/jsonapi/resource.rb', line 128 def _attributes @_attributes end |
._type ⇒ Object
Returns the value of attribute _type.
128 129 130 |
# File 'lib/jsonapi/resource.rb', line 128 def _type @_type end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
11 12 13 |
# File 'lib/jsonapi/resource.rb', line 11 def context @context end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
12 13 14 |
# File 'lib/jsonapi/resource.rb', line 12 def model @model end |
Class Method Details
._allowed_filter?(filter) ⇒ Boolean
:nocov:
392 393 394 |
# File 'lib/jsonapi/resource.rb', line 392 def _allowed_filter?(filter) _allowed_filters.include?(filter) end |
._as_parent_key ⇒ Object
363 364 365 |
# File 'lib/jsonapi/resource.rb', line 363 def _as_parent_key @_as_parent_key ||= "#{_type.to_s.singularize}_#{_primary_key}" end |
._association(type) ⇒ Object
343 344 345 346 |
# File 'lib/jsonapi/resource.rb', line 343 def _association(type) type = type.to_sym @_associations[type] end |
._attribute_options(attr) ⇒ Object
quasi private class methods
323 324 325 |
# File 'lib/jsonapi/resource.rb', line 323 def (attr) .merge(@_attributes[attr]) end |
._has_association?(type) ⇒ Boolean
338 339 340 341 |
# File 'lib/jsonapi/resource.rb', line 338 def _has_association?(type) type = type.to_s @_associations.has_key?(type.singularize.to_sym) || @_associations.has_key?(type.pluralize.to_sym) end |
._key ⇒ Object
352 353 354 355 356 357 |
# File 'lib/jsonapi/resource.rb', line 352 def _key # :nocov: warn '[DEPRECATION] `_key` is deprecated. Please use `_primary_key` instead.' _primary_key # :nocov: end |
._model_name ⇒ Object
348 349 350 |
# File 'lib/jsonapi/resource.rb', line 348 def _model_name @_model_name ||= self.name.demodulize.sub(/Resource$/, '') end |
._primary_key ⇒ Object
359 360 361 |
# File 'lib/jsonapi/resource.rb', line 359 def _primary_key @_primary_key ||= :id end |
._resource_name_from_type(type) ⇒ Object
371 372 373 374 375 376 377 378 |
# File 'lib/jsonapi/resource.rb', line 371 def _resource_name_from_type(type) class_name = @@resource_types[type] if class_name.nil? class_name = type.to_s.singularize.camelize + 'Resource' @@resource_types[type] = class_name end return class_name end |
._updateable_associations ⇒ Object
327 328 329 330 331 332 333 334 335 336 |
# File 'lib/jsonapi/resource.rb', line 327 def _updateable_associations associations = [] @_associations.each do |key, association| if association.is_a?(JSONAPI::Association::HasOne) || association.acts_as_set associations.push(key) end end associations end |
.attribute(attr, options = {}) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/jsonapi/resource.rb', line 153 def attribute(attr, = {}) check_reserved_attribute_name(attr) @_attributes[attr] = define_method attr do @model.send(attr) end unless method_defined?(attr) define_method "#{attr}=" do |value| @model.send "#{attr}=", value end unless method_defined?("#{attr}=") end |
.attributes(*attrs) ⇒ Object
Methods used in defining a resource class
147 148 149 150 151 |
# File 'lib/jsonapi/resource.rb', line 147 def attributes(*attrs) attrs.each do |attr| attribute(attr) end end |
.create(context) ⇒ Object
130 131 132 |
# File 'lib/jsonapi/resource.rb', line 130 def create(context) self.new(self.create_model, context) end |
.create_model ⇒ Object
134 135 136 |
# File 'lib/jsonapi/resource.rb', line 134 def create_model _model_class.new end |
.createable_fields(context = nil) ⇒ Object
Override in your resource to filter the createable keys
207 208 209 |
# File 'lib/jsonapi/resource.rb', line 207 def createable_fields(context = nil) _updateable_associations | _attributes.keys end |
.default_attribute_options ⇒ Object
166 167 168 |
# File 'lib/jsonapi/resource.rb', line 166 def {format: :default} end |
.fields ⇒ Object
216 217 218 |
# File 'lib/jsonapi/resource.rb', line 216 def fields _associations.keys | _attributes.keys end |
.filter(attr) ⇒ Object
186 187 188 |
# File 'lib/jsonapi/resource.rb', line 186 def filter(attr) @_allowed_filters.add(attr.to_sym) end |
.filters(*attrs) ⇒ Object
182 183 184 |
# File 'lib/jsonapi/resource.rb', line 182 def filters(*attrs) @_allowed_filters.merge(attrs) end |
.find(filters, options = {}) ⇒ Object
Override this method if you have more complex requirements than this basic find method provides
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/jsonapi/resource.rb', line 221 def find(filters, = {}) context = [:context] sort_params = .fetch(:sort_params) { [] } includes = [] where_filters = {} filters.each do |filter, value| if _associations.include?(filter) if _associations[filter].is_a?(JSONAPI::Association::HasMany) includes.push(filter) where_filters["#{filter}.#{_associations[filter].primary_key}"] = value else where_filters["#{_associations[filter].foreign_key}"] = value end else where_filters[filter] = value end end resources = [] = (sort_params) records().where(where_filters).order().includes(includes).each do |model| resources.push self.new(model, context) end return resources end |
.find_by_key(key, options = {}) ⇒ Object
249 250 251 252 253 254 255 256 |
# File 'lib/jsonapi/resource.rb', line 249 def find_by_key(key, = {}) context = [:context] model = records().where({_primary_key => key}).first if model.nil? raise JSONAPI::Exceptions::RecordNotFound.new(key) end self.new(model, context) end |
.find_by_keys(keys, options = {}) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/jsonapi/resource.rb', line 258 def find_by_keys(keys, = {}) context = [:context] _models = records().where({_primary_key => keys}) unless _models.length == keys.length key = (keys - _models.pluck(:id).map(&:to_s)).first raise JSONAPI::Exceptions::RecordNotFound.new(key) end _models.map { |model| self.new(model, context) } end |
.has_many(*attrs) ⇒ Object
174 175 176 |
# File 'lib/jsonapi/resource.rb', line 174 def has_many(*attrs) _associate(Association::HasMany, *attrs) end |
.has_one(*attrs) ⇒ Object
170 171 172 |
# File 'lib/jsonapi/resource.rb', line 170 def has_one(*attrs) _associate(Association::HasOne, *attrs) end |
.inherited(base) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/jsonapi/resource.rb', line 112 def inherited(base) base._attributes = (_attributes || {}).dup base._associations = (_associations || {}).dup base._allowed_filters = (_allowed_filters || Set.new).dup type = base.name.demodulize.sub(/Resource$/, '').underscore base._type = type.pluralize.to_sym check_reserved_resource_name(base._type, base.name) # If eager loading is on this is how all the resource types are setup # If eager loading is off some resource types will be initialized in # _resource_name_from_type @@resource_types[base._type] ||= base.name.demodulize end |
.is_filter_association?(filter) ⇒ Boolean
285 286 287 |
# File 'lib/jsonapi/resource.rb', line 285 def is_filter_association?(filter) filter == _type || _associations.include?(filter) end |
.key(key) ⇒ Object
190 191 192 193 194 195 |
# File 'lib/jsonapi/resource.rb', line 190 def key(key) # :nocov: warn '[DEPRECATION] `key` is deprecated. Please use `primary_key` instead.' @_primary_key = key.to_sym # :nocov: end |
.model_name(model) ⇒ Object
178 179 180 |
# File 'lib/jsonapi/resource.rb', line 178 def model_name(model) @_model_name = model.to_sym end |
.primary_key(key) ⇒ Object
197 198 199 |
# File 'lib/jsonapi/resource.rb', line 197 def primary_key(key) @_primary_key = key.to_sym end |
.records(options = {}) ⇒ Object
Override this method if you want to customize the relation for finder methods (find, find_by_key, find_by_keys)
272 273 274 |
# File 'lib/jsonapi/resource.rb', line 272 def records( = {}) _model_class end |
.routing_options(options) ⇒ Object
138 139 140 |
# File 'lib/jsonapi/resource.rb', line 138 def () @_routing_resource_options = end |
.routing_resource_options ⇒ Object
142 143 144 |
# File 'lib/jsonapi/resource.rb', line 142 def @_routing_resource_options ||= {} end |
.sortable_fields(context = nil) ⇒ Object
Override in your resource to filter the sortable keys
212 213 214 |
# File 'lib/jsonapi/resource.rb', line 212 def sortable_fields(context = nil) _attributes.keys end |
.updateable_fields(context = nil) ⇒ Object
Override in your resource to filter the updateable keys
202 203 204 |
# File 'lib/jsonapi/resource.rb', line 202 def updateable_fields(context = nil) _updateable_associations | _attributes.keys end |
.verify_association_filter(filter, raw, context = nil) ⇒ Object
override to allow for custom association logic, such as uuids, multiple keys or permission checks on keys
318 319 320 |
# File 'lib/jsonapi/resource.rb', line 318 def verify_association_filter(filter, raw, context = nil) return filter, raw end |
.verify_custom_filter(filter, value, context = nil) ⇒ Object
override to allow for custom filters
313 314 315 |
# File 'lib/jsonapi/resource.rb', line 313 def verify_custom_filter(filter, value, context = nil) return filter, value end |
.verify_filter(filter, raw, context = nil) ⇒ Object
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/jsonapi/resource.rb', line 289 def verify_filter(filter, raw, context = nil) filter_values = [] filter_values += CSV.parse_line(raw) unless raw.nil? || raw.empty? if is_filter_association?(filter) verify_association_filter(filter, filter_values, context) else verify_custom_filter(filter, filter_values, context) end end |
.verify_filters(filters, context = nil) ⇒ Object
276 277 278 279 280 281 282 283 |
# File 'lib/jsonapi/resource.rb', line 276 def verify_filters(filters, context = nil) verified_filters = {} filters.each do |filter, raw_value| verified_filter = verify_filter(filter, raw_value, context) verified_filters[verified_filter[0]] = verified_filter[1] end verified_filters end |
.verify_key(key, context = nil) ⇒ Object
override to allow for key processing and checking
301 302 303 |
# File 'lib/jsonapi/resource.rb', line 301 def verify_key(key, context = nil) return key end |
.verify_keys(keys, context = nil) ⇒ Object
override to allow for key processing and checking
306 307 308 309 310 |
# File 'lib/jsonapi/resource.rb', line 306 def verify_keys(keys, context = nil) return keys.collect do |key| verify_key(key, context) end end |
Instance Method Details
#_model_class ⇒ Object
382 383 384 |
# File 'lib/jsonapi/resource.rb', line 382 def _model_class @model ||= Object.const_get(_model_name.to_s) end |
#create_has_many_link(association_type, association_key_value) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jsonapi/resource.rb', line 27 def create_has_many_link(association_type, association_key_value) association = self.class._associations[association_type] = self.class.resource_for(association.type).find_by_key(association_key_value, context: @context) # ToDo: Add option to skip relations that already exist instead of returning an error? relation = @model.send(association.type).where(association.primary_key => association_key_value).first if relation.nil? @model.send(association.type) << .model else raise JSONAPI::Exceptions::HasManyRelationExists.new(association_key_value) end end |
#create_has_one_link(association_type, association_key_value) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jsonapi/resource.rb', line 46 def create_has_one_link(association_type, association_key_value) association = self.class._associations[association_type] # ToDo: Add option to skip relations that already exist instead of returning an error? relation = @model.send("#{association.foreign_key}") if relation.nil? send("#{association.foreign_key}=", association_key_value) else raise JSONAPI::Exceptions::HasOneRelationExists.new end end |
#fetchable_fields ⇒ Object
Override this on a resource instance to override the fetchable keys
107 108 109 |
# File 'lib/jsonapi/resource.rb', line 107 def fetchable_fields self.class.fields end |
#id ⇒ Object
23 24 25 |
# File 'lib/jsonapi/resource.rb', line 23 def id model.send(self.class._primary_key) end |
#remove ⇒ Object
19 20 21 |
# File 'lib/jsonapi/resource.rb', line 19 def remove @model.destroy end |
#remove_has_many_link(association_type, key) ⇒ Object
64 65 66 67 68 |
# File 'lib/jsonapi/resource.rb', line 64 def remove_has_many_link(association_type, key) association = self.class._associations[association_type] @model.send(association.type).delete(key) end |
#remove_has_one_link(association_type) ⇒ Object
70 71 72 73 74 |
# File 'lib/jsonapi/resource.rb', line 70 def remove_has_one_link(association_type) association = self.class._associations[association_type] send("#{association.foreign_key}=", nil) end |
#replace_fields(field_data) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/jsonapi/resource.rb', line 76 def replace_fields(field_data) field_data[:attributes].each do |attribute, value| begin send "#{attribute}=", value rescue ArgumentError # :nocov: Will be thrown if an enum value isn't allowed for an enum. Currently not tested as enums are a rails 4.1 and higher feature raise JSONAPI::Exceptions::InvalidFieldValue.new(attribute, value) # :nocov: end end field_data[:has_one].each do |association_type, value| if value.nil? remove_has_one_link(association_type) else replace_has_one_link(association_type, value) end end if field_data[:has_one] field_data[:has_many].each do |association_type, values| replace_has_many_links(association_type, values) end if field_data[:has_many] end |
#replace_has_many_links(association_type, association_key_values) ⇒ Object
40 41 42 43 44 |
# File 'lib/jsonapi/resource.rb', line 40 def replace_has_many_links(association_type, association_key_values) association = self.class._associations[association_type] send("#{association.foreign_key}=", association_key_values) end |
#replace_has_one_link(association_type, association_key_value) ⇒ Object
58 59 60 61 62 |
# File 'lib/jsonapi/resource.rb', line 58 def replace_has_one_link(association_type, association_key_value) association = self.class._associations[association_type] send("#{association.foreign_key}=", association_key_value) end |
#save ⇒ Object
100 101 102 103 104 |
# File 'lib/jsonapi/resource.rb', line 100 def save @model.save! rescue ActiveRecord::RecordInvalid => e raise JSONAPI::Exceptions::ValidationErrors.new(e.record.errors.) end |