jsonapi-resources 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1860b6f54c5c18361eee885c58d9a44ca799223
4
- data.tar.gz: 75520fb96150d1185d444c474f9b803e1efada5b
3
+ metadata.gz: 50581a29495af11bb597deb7c7ca7369eb084b77
4
+ data.tar.gz: 07aff054fd599cfa81fd11c3934900cce12f2317
5
5
  SHA512:
6
- metadata.gz: f335fd832a84998b16b4c10fb349c87fba4bfbc5e97166e3c7875c6d07ec46a6fe8453aba8d2297d12ab1a9c22ee28355695e009384a7bb3f2f41fd2ade7d8db
7
- data.tar.gz: bf31f588b77dfa2c95d9ddcd6461ef984aa329c2c17641082662156b01a36c69aa6f51fcb2741f84886f0ccdee5c9d2f6108966cfd842db8bcae5509ebb44dd1
6
+ metadata.gz: 626ce6b524027111ef04fed7988313018cb9daa9eda844bf2a05b7cd5b7acf8b45c0dd213aafbeb13dff473a50180037c434a5715092bb624801e3cca1cc5e06
7
+ data.tar.gz: e08ff693bb02c582ff4685d209c0fee6e7d5fbd0dce21a6b43b614103d4d98ae0c2eba60ae63eafda808d79ef02b10e18faf2d38b81b978e65fd5ec6b8e14328
data/README.md CHANGED
@@ -54,6 +54,25 @@ class ContactResource < JSONAPI::Resource
54
54
  end
55
55
  ```
56
56
 
57
+ ##### Abstract Resources
58
+
59
+ Resources that are not backed by a model (purely used as base classes for other resources) should be declared as
60
+ abstract.
61
+
62
+ Because abstract resources do not expect to be backed by a model, they won't attempt to discover the model class
63
+ or any of its relationships.
64
+
65
+ ```ruby
66
+ class BaseResource < JSONAPI::Resource
67
+ abstract
68
+
69
+ has_one :creator
70
+ end
71
+
72
+ class ContactResource < BaseResource
73
+ end
74
+ ```
75
+
57
76
  #### Attributes
58
77
 
59
78
  Any of a resource's attributes that are accessible must be explicitly declared. Single attributes can be declared using
@@ -234,7 +234,7 @@ module JSONAPI
234
234
  end
235
235
 
236
236
  def parse_sort_criteria(sort_criteria)
237
- return unless sort_criteria
237
+ return unless sort_criteria.present?
238
238
 
239
239
  @sort_criteria = CSV.parse_line(URI.unescape(sort_criteria)).collect do |sort|
240
240
  if sort.start_with?('-')
@@ -257,6 +257,7 @@ module JSONAPI
257
257
 
258
258
  class << self
259
259
  def inherited(base)
260
+ base.abstract(false)
260
261
  base._attributes = (_attributes || {}).dup
261
262
  base._relationships = (_relationships || {}).dup
262
263
  base._allowed_filters = (_allowed_filters || Set.new).dup
@@ -620,10 +621,20 @@ module JSONAPI
620
621
  @_paginator = paginator
621
622
  end
622
623
 
624
+ def abstract(val = true)
625
+ @abstract = val
626
+ end
627
+
628
+ def _abstract
629
+ @abstract
630
+ end
631
+
623
632
  def _model_class
633
+ return nil if _abstract
634
+
624
635
  return @model if @model
625
636
  @model = _model_name.to_s.safe_constantize
626
- fail NameError, "model could not be found for #{self.name}" if @model.nil?
637
+ warn "[MODEL NOT FOUND] Model could not be found for #{self.name}. If this a base Resource declare it as abstract." if @model.nil?
627
638
  @model
628
639
  end
629
640
 
@@ -675,7 +686,7 @@ module JSONAPI
675
686
  check_reserved_relationship_name(attr)
676
687
 
677
688
  # Initialize from an ActiveRecord model's properties
678
- if _model_class < ActiveRecord::Base
689
+ if _model_class && _model_class < ActiveRecord::Base
679
690
  model_association = _model_class.reflect_on_association(attr)
680
691
  if model_association
681
692
  options[:class_name] ||= model_association.class_name
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = '0.5.1'
3
+ VERSION = '0.5.2'
4
4
  end
5
5
  end
@@ -205,6 +205,12 @@ class PostsControllerTest < ActionController::TestCase
205
205
  assert_equal 3, json_response['data'].size
206
206
  end
207
207
 
208
+ def test_sorting_blank
209
+ get :index, {sort: ''}
210
+
211
+ assert_response :success
212
+ end
213
+
208
214
  def test_sorting_asc
209
215
  get :index, {sort: 'title'}
210
216
 
@@ -648,7 +648,11 @@ module Api
648
648
  end
649
649
 
650
650
  ### RESOURCES
651
- class PersonResource < JSONAPI::Resource
651
+ class BaseResource < JSONAPI::Resource
652
+ abstract
653
+ end
654
+
655
+ class PersonResource < BaseResource
652
656
  attributes :id, :name, :email
653
657
  attribute :date_joined, format: :date_with_timezone
654
658
 
@@ -11,6 +11,10 @@ end
11
11
  class NoMatchResource < JSONAPI::Resource
12
12
  end
13
13
 
14
+ class NoMatchAbstractResource < JSONAPI::Resource
15
+ abstract
16
+ end
17
+
14
18
  class CatResource < JSONAPI::Resource
15
19
  attribute :id
16
20
  attribute :name
@@ -55,12 +59,25 @@ class ResourceTest < ActiveSupport::TestCase
55
59
  assert_equal(PostResource._model_class, Post)
56
60
  end
57
61
 
62
+ def test_base_resource_abstract
63
+ assert BaseResource._abstract
64
+ end
65
+
66
+ def test_derived_not_abstract
67
+ assert PersonResource < BaseResource
68
+ refute PersonResource._abstract
69
+ end
70
+
58
71
  def test_nil_model_class
59
- error = assert_raises(NameError) { NoMatchResource._model_class }
60
- assert_equal(
61
- error.message,
62
- "model could not be found for NoMatchResource"
63
- )
72
+ assert_output nil, "[MODEL NOT FOUND] Model could not be found for NoMatchResource. If this a base Resource declare it as abstract.\n" do
73
+ assert_nil NoMatchResource._model_class
74
+ end
75
+ end
76
+
77
+ def test_nil_abstract_model_class
78
+ assert_output nil, '' do
79
+ assert_nil NoMatchAbstractResource._model_class
80
+ end
64
81
  end
65
82
 
66
83
  def test_model_alternate
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Gebhardt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-27 00:00:00.000000000 Z
12
+ date: 2015-07-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler