api_me 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a97089cfc6b7bf6d795b611f125e707765cd0556
4
- data.tar.gz: 15fb80cbe68170c4f56453bddd63726f5fbe3e93
3
+ metadata.gz: 8aefa13bb6c4cb741b5b745f763b478ec279004d
4
+ data.tar.gz: 36d2113a88594b323d188d7f6f2812b2ae591109
5
5
  SHA512:
6
- metadata.gz: d560b2e6b10738a5f85d3634d2516860b796046e80cf0ff60001f0a056a7cd70e1a3770eac520fbf662ed31e3c183252a113308cddf70337ab42c36cd9c41e9f
7
- data.tar.gz: a405ad926c4611f2b325a750ab3aecd04851b9b70565158cbed3b70ad7f5c87bdfd19a511b0b9f221df3d836a4bef5f2ada8bceded6e63d242e7a52b5554e990
6
+ metadata.gz: 7e9a8cff6b5ca7301ef12b2e9b5cc6c41f7b39e3c0bb63249c26392d1f3a031c132462e1f035d2b4bcc18e364be52b90b60dbf61b318d3a7995d3b1f781e4d2a
7
+ data.tar.gz: 4db327ab00687bb5c1378dc8839de1d2917156919fb80a82a2008cab093f68bdcaec6697c7535cca523edf0e150a616e0e9f4f65963be00d3633389320d36824
data/Gemfile CHANGED
@@ -10,5 +10,5 @@ group :test do
10
10
  gem 'combustion', '~> 0.5.2'
11
11
  gem 'rack-test'
12
12
  gem 'pundit'
13
- gem 'active_model_serializers', '~> 0.8.0'
13
+ gem 'active_model_serializers', '~> 0.10.0'
14
14
  end
data/README.md CHANGED
@@ -12,6 +12,27 @@ Api controllers use the fantastic [Pundit](https://github.com/elabs/pundit) gem
12
12
 
13
13
  The primary goal of this gem was to keep things simple so that customization is fairly straight forward by separating concerns and providing overrides. Reusing existing libraries was a primary goal during the design, hence the overall simplicity of this gem. We currently use this gem internally at [Inigo](inigo.io) and are committed to its ongoing maintenance.
14
14
 
15
+ ### Upgrade from 0.7.X to 0.8.X
16
+ - Upgrade to the latest version of 0.7.X. Run the app/tests and check/fix all deprecations
17
+ - Upgrade to the latest version of 0.8.X, update active_model_serializers to 0.10.X.
18
+ - Add the following initializer:
19
+ `config/initializers/active_model_serializer.rb`
20
+ ```rb
21
+ ActiveModelSerializers.config.adapter = :json
22
+ ```
23
+ - Remove all `embed ...` methods from the serializers
24
+ - Update all belongs-to relationships to have FKs, serialize the FK id in all active model serializers instead of embeding the relationship (I.E. `has_one :foo` becomes `attributes :foo_id`)
25
+ - Update all has-many relationships to serialize the ids instead of embeding the relationship. (I.E. `has_many :foos` becomes `attributes :foo_ids`)
26
+ - Remove all has-one relationships and manually build a method to serialize the id. Ex:
27
+ ```rb
28
+ attributes :foo_id
29
+
30
+ def foo_id
31
+ object.foo ? object.foo.id : nil
32
+ end
33
+ ```
34
+ - Embeded has-one and belongs-to relationships look the same on the serializer, so look at the model to identify the differences.
35
+
15
36
  ### Installation
16
37
  Add the gem to your Gemfile: `gem api_me`.
17
38
 
@@ -85,7 +106,7 @@ The ApiMe::BaseFilter is called if no filter exists for the resource, by default
85
106
 
86
107
  ### Sorting
87
108
 
88
- To enable sorting simply pass `sort` in your API request with `sortCiteria` and `sortReverse` as hash parameters. Associated models will need a third parameter passed in `associationCriteria`, meaning the attribute of the associated model to sort by.
109
+ To enable sorting just pass `sort` in your request with `sortCiteria` and `sortReverse`.
89
110
 
90
111
  Ember Example
91
112
  ````js
data/api_me.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.add_runtime_dependency 'activerecord', '>= 3.2.0'
22
22
  s.add_runtime_dependency 'activesupport', '>= 3.2.0'
23
23
  s.add_runtime_dependency 'pundit', '~> 1.0'
24
- s.add_runtime_dependency 'active_model_serializers', '~> 0.8.0'
24
+ s.add_runtime_dependency 'active_model_serializers', '~> 0.10.0'
25
25
  s.add_runtime_dependency 'search_object', '~> 1.0'
26
26
  s.add_runtime_dependency 'kaminari', '~> 0.16.3'
27
27
 
data/lib/api_me/model.rb CHANGED
@@ -1,4 +1,6 @@
1
+ require 'active_model_serializers/model'
2
+
1
3
  module ApiMe
2
- class Model < ActiveModel::Serializer
4
+ class Model < ActiveModelSerializers::Model
3
5
  end
4
6
  end
@@ -29,7 +29,7 @@ module ApiMe
29
29
  protected
30
30
 
31
31
  def page
32
- self.scope = scope.page(self.page_offset ? page_offset : 1)
32
+ self.scope = Kaminari.paginate_array(scope).page(self.page_offset ? page_offset : 1)
33
33
  self
34
34
  end
35
35
 
@@ -1,18 +1,17 @@
1
1
  module ApiMe
2
2
  class Sorting
3
- attr_accessor :sort_criteria, :sort_reverse, :sort_association, :scope
3
+ attr_accessor :sort_criteria, :sort_reverse, :scope
4
4
 
5
5
  def initialize(scope:, sort_params:)
6
6
  self.scope = scope
7
7
  if sort_params
8
- self.sort_association = sort_params[:assoCriteria]
9
- self.sort_criteria = sort_params[:criteria] || default_sort_criteria
8
+ self.sort_criteria = sort_params[:criteria]
10
9
  self.sort_reverse = sort_params[:reverse]
11
10
  end
12
11
  end
13
12
 
14
13
  def results
15
- sorting? ? sort(sort_criteria) : scope
14
+ sorting? ? sort.scope : scope
16
15
  end
17
16
 
18
17
  def sort_meta
@@ -27,22 +26,20 @@ module ApiMe
27
26
 
28
27
  protected
29
28
 
30
- def sort(criteria = default_sort_criteria)
31
- unless sort_association == ""
32
- criteria_class = criteria.camelize.constantize
33
-
29
+ def sort
30
+ unless sort_criteria === ""
31
+ sort_p = sort_criteria
34
32
  if sort_reverse === "true"
35
- self.scope = scope.joins(criteria.to_sym).merge(criteria_class.order(sort_association => :desc))
33
+ self.scope = scope.sort_by {|scope| scope[sort_p]}.reverse!
36
34
  else
37
- self.scope = scope.joins(criteria.to_sym).merge(criteria_class.order(sort_association => :asc))
35
+ self.scope = scope.sort_by {|scope| scope[sort_p]}
38
36
  end
37
+ self.scope
39
38
  else
40
- if sort_reverse === "true"
41
- self.scope = scope.order(criteria => :desc)
42
- else
43
- self.scope = scope.order(criteria => :asc)
44
- end
39
+ default_sort_criteria
40
+ sort.scope
45
41
  end
42
+ self
46
43
  end
47
44
 
48
45
  private
@@ -54,5 +51,6 @@ module ApiMe
54
51
  def sorting?
55
52
  sort_criteria || sort_reverse
56
53
  end
54
+
57
55
  end
58
56
  end
@@ -1,3 +1,3 @@
1
1
  module ApiMe
2
- VERSION = '0.7.1'
2
+ VERSION = '0.8.0'
3
3
  end
@@ -142,7 +142,7 @@ describe 'Posts API' do
142
142
  Post.create(name: 'Post' + i.to_s)
143
143
  end
144
144
 
145
- get '/api/v1/posts?sort%5Bcriteria%5D=id&sort%5Breverse%5D=true&sort%5BassoCriteria%5D='
145
+ get '/api/v1/posts?sort%5Bcriteria%5D=id&sort%5Breverse%5D=true'
146
146
  json = JSON.parse(last_response.body)
147
147
  expect(json['posts'].last['name']).to eq('Post0')
148
148
  end
@@ -152,7 +152,7 @@ describe 'Posts API' do
152
152
  Post.create(name: 'Post' + i.to_s)
153
153
  end
154
154
 
155
- get '/api/v1/posts?page%5Boffset%5D=1&page%5Bsize%5D=10&sort%5Bcriteria%5D=id&sort%5Breverse%5D=true&sort%5BassoCriteria%5D='
155
+ get '/api/v1/posts?page%5Boffset%5D=1&page%5Bsize%5D=10&sort%5Bcriteria%5D=id&sort%5Breverse%5D=true'
156
156
  json = JSON.parse(last_response.body)
157
157
  expect(json['posts'].first['name']).to eq('Post19')
158
158
  expect(json['posts'].length).to eq(10)
@@ -1,6 +1,6 @@
1
- require 'api_me/model'
1
+ require 'active_model_serializers/model'
2
2
 
3
- class TestModel < ApiMe::Model
3
+ class TestModel < ActiveModelSerializers::Model
4
4
  def self.create
5
5
  @created = true
6
6
  end
@@ -1,4 +1,3 @@
1
1
  class UserSerializer < ActiveModel::Serializer
2
- embed :ids
3
2
  attributes :id, :username
4
3
  end
@@ -1 +1 @@
1
- #ActiveModelSerializers.config.adapter = :json
1
+ ActiveModelSerializers.config.adapter = :json
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Clopton
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-12-21 00:00:00.000000000 Z
12
+ date: 2016-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -59,14 +59,14 @@ dependencies:
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: 0.8.0
62
+ version: 0.10.0
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: 0.8.0
69
+ version: 0.10.0
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: search_object
72
72
  requirement: !ruby/object:Gem::Requirement