api_me 0.7.0 → 0.7.1

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: 7eaa0bc3aa1012e62785ab01d14d6cfd43c213aa
4
- data.tar.gz: 8a5b6712ee27defbf5d2c45763bf19b5c57e381f
3
+ metadata.gz: a97089cfc6b7bf6d795b611f125e707765cd0556
4
+ data.tar.gz: 15fb80cbe68170c4f56453bddd63726f5fbe3e93
5
5
  SHA512:
6
- metadata.gz: e1a4bbc4b1dd7278b18c96f8e307f74395e4a2d678f27e0108c7d8f4bb886d26983e70b3539cfe620c370962b1be149040065c8505f364f7880d22705070890a
7
- data.tar.gz: dd6b5886ed746e190ec31b3c3ada9dac8761d6dedf5e92cd464f6ab336e769e521791a1778060f7b4a65e165771b369bccfad67d5749b252c9e987c6ffd93923
6
+ metadata.gz: d560b2e6b10738a5f85d3634d2516860b796046e80cf0ff60001f0a056a7cd70e1a3770eac520fbf662ed31e3c183252a113308cddf70337ab42c36cd9c41e9f
7
+ data.tar.gz: a405ad926c4611f2b325a750ab3aecd04851b9b70565158cbed3b70ad7f5c87bdfd19a511b0b9f221df3d836a4bef5f2ada8bceded6e63d242e7a52b5554e990
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  script: bundle exec rspec spec
3
3
  rvm:
4
- - 2.1.0
4
+ - 2.2.2
data/README.md CHANGED
@@ -85,7 +85,7 @@ The ApiMe::BaseFilter is called if no filter exists for the resource, by default
85
85
 
86
86
  ### Sorting
87
87
 
88
- To enable sorting just pass `sort` in your request with `sortCiteria` and `sortReverse`.
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.
89
89
 
90
90
  Ember Example
91
91
  ````js
@@ -71,14 +71,14 @@ module ApiMe
71
71
  @sorted_scope = sort_scope(@filter_scope, params[:sort])
72
72
  @pagination_object = ApiMe::Pagination.new(scope: @sorted_scope, page_params: params[:page])
73
73
 
74
- render json: @pagination_object.results, each_serializer: serializer_klass, meta: { page: @pagination_object.page_meta }
74
+ render json: @pagination_object.results, root: collection_root_key, each_serializer: serializer_klass, meta: { page: @pagination_object.page_meta }
75
75
  end
76
76
 
77
77
  def show
78
78
  @object = find_resource
79
79
  authorize @object
80
80
 
81
- render json: @object, serializer: serializer_klass
81
+ render json: @object, root: singular_root_key, serializer: serializer_klass
82
82
  end
83
83
 
84
84
  def create
@@ -86,7 +86,7 @@ module ApiMe
86
86
  authorize @object
87
87
  @object.save!(object_params)
88
88
 
89
- render status: 201, json: @object, serializer: serializer_klass
89
+ render status: 201, json: @object, root: singular_root_key, serializer: serializer_klass
90
90
  rescue ActiveRecord::RecordInvalid => e
91
91
  handle_errors(e)
92
92
  end
@@ -112,6 +112,14 @@ module ApiMe
112
112
 
113
113
  protected
114
114
 
115
+ def singular_root_key
116
+ model_klass.name.singularize.underscore
117
+ end
118
+
119
+ def collection_root_key
120
+ model_klass.name.pluralize.underscore
121
+ end
122
+
115
123
  def object_params
116
124
  params.require(params_klass_symbol).permit(*policy(@object || model_klass).permitted_attributes)
117
125
  end
@@ -0,0 +1,4 @@
1
+ module ApiMe
2
+ class Model < ActiveModel::Serializer
3
+ end
4
+ end
@@ -29,7 +29,7 @@ module ApiMe
29
29
  protected
30
30
 
31
31
  def page
32
- self.scope = Kaminari.paginate_array(scope).page(self.page_offset ? page_offset : 1)
32
+ self.scope = scope.page(self.page_offset ? page_offset : 1)
33
33
  self
34
34
  end
35
35
 
@@ -1,17 +1,18 @@
1
1
  module ApiMe
2
2
  class Sorting
3
- attr_accessor :sort_criteria, :sort_reverse, :scope
3
+ attr_accessor :sort_criteria, :sort_reverse, :sort_association, :scope
4
4
 
5
5
  def initialize(scope:, sort_params:)
6
6
  self.scope = scope
7
7
  if sort_params
8
- self.sort_criteria = sort_params[:criteria]
8
+ self.sort_association = sort_params[:assoCriteria]
9
+ self.sort_criteria = sort_params[:criteria] || default_sort_criteria
9
10
  self.sort_reverse = sort_params[:reverse]
10
11
  end
11
12
  end
12
13
 
13
14
  def results
14
- sorting? ? sort.scope : scope
15
+ sorting? ? sort(sort_criteria) : scope
15
16
  end
16
17
 
17
18
  def sort_meta
@@ -26,20 +27,22 @@ module ApiMe
26
27
 
27
28
  protected
28
29
 
29
- def sort
30
- unless sort_criteria === ""
31
- sort_p = sort_criteria
30
+ def sort(criteria = default_sort_criteria)
31
+ unless sort_association == ""
32
+ criteria_class = criteria.camelize.constantize
33
+
32
34
  if sort_reverse === "true"
33
- self.scope = scope.sort_by {|scope| scope[sort_p]}.reverse!
35
+ self.scope = scope.joins(criteria.to_sym).merge(criteria_class.order(sort_association => :desc))
34
36
  else
35
- self.scope = scope.sort_by {|scope| scope[sort_p]}
37
+ self.scope = scope.joins(criteria.to_sym).merge(criteria_class.order(sort_association => :asc))
36
38
  end
37
- self.scope
38
39
  else
39
- default_sort_criteria
40
- sort.scope
40
+ if sort_reverse === "true"
41
+ self.scope = scope.order(criteria => :desc)
42
+ else
43
+ self.scope = scope.order(criteria => :asc)
44
+ end
41
45
  end
42
- self
43
46
  end
44
47
 
45
48
  private
@@ -51,6 +54,5 @@ module ApiMe
51
54
  def sorting?
52
55
  sort_criteria || sort_reverse
53
56
  end
54
-
55
57
  end
56
58
  end
@@ -1,3 +1,3 @@
1
1
  module ApiMe
2
- VERSION = '0.7.0'
2
+ VERSION = '0.7.1'
3
3
  end
@@ -58,7 +58,6 @@ describe 'Posts API' do
58
58
  get '/api/v1/posts?page%5Boffset%5D=3'
59
59
  json = JSON.parse(last_response.body)
60
60
  expect(json['posts'].length).to eq(0)
61
-
62
61
  end
63
62
 
64
63
  it 'is page size of 10 working for default offset of 1' do
@@ -143,7 +142,7 @@ describe 'Posts API' do
143
142
  Post.create(name: 'Post' + i.to_s)
144
143
  end
145
144
 
146
- get '/api/v1/posts?sort%5Bcriteria%5D=id&sort%5Breverse%5D=true'
145
+ get '/api/v1/posts?sort%5Bcriteria%5D=id&sort%5Breverse%5D=true&sort%5BassoCriteria%5D='
147
146
  json = JSON.parse(last_response.body)
148
147
  expect(json['posts'].last['name']).to eq('Post0')
149
148
  end
@@ -153,10 +152,9 @@ describe 'Posts API' do
153
152
  Post.create(name: 'Post' + i.to_s)
154
153
  end
155
154
 
156
- get '/api/v1/posts?page%5Boffset%5D=1&page%5Bsize%5D=10&sort%5Bcriteria%5D=id&sort%5Breverse%5D=true'
155
+ get '/api/v1/posts?page%5Boffset%5D=1&page%5Bsize%5D=10&sort%5Bcriteria%5D=id&sort%5Breverse%5D=true&sort%5BassoCriteria%5D='
157
156
  json = JSON.parse(last_response.body)
158
157
  expect(json['posts'].first['name']).to eq('Post19')
159
158
  expect(json['posts'].length).to eq(10)
160
159
  end
161
-
162
160
  end
@@ -1,2 +1,3 @@
1
1
  class Post < ActiveRecord::Base
2
+ belongs_to :user
2
3
  end
@@ -1,4 +1,6 @@
1
- class TestModel
1
+ require 'api_me/model'
2
+
3
+ class TestModel < ApiMe::Model
2
4
  def self.create
3
5
  @created = true
4
6
  end
@@ -1,2 +1,3 @@
1
1
  class User < ActiveRecord::Base
2
+ has_many :posts
2
3
  end
@@ -1,3 +1,5 @@
1
1
  class PostSerializer < ActiveModel::Serializer
2
2
  attributes :name
3
+
4
+ has_one :user
3
5
  end
@@ -0,0 +1 @@
1
+ #ActiveModelSerializers.config.adapter = :json
@@ -6,6 +6,7 @@ ActiveRecord::Schema.define do
6
6
 
7
7
  create_table :posts, force: true do |t|
8
8
  t.string :name
9
+ t.belongs_to :user, foreign_key: true
9
10
  t.timestamps
10
11
  end
11
12
  end
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.0
4
+ version: 0.7.1
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-06-23 00:00:00.000000000 Z
12
+ date: 2016-12-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -187,6 +187,7 @@ files:
187
187
  - lib/api_me.rb
188
188
  - lib/api_me/base_filter.rb
189
189
  - lib/api_me/engine.rb
190
+ - lib/api_me/model.rb
190
191
  - lib/api_me/pagination.rb
191
192
  - lib/api_me/sorting.rb
192
193
  - lib/api_me/version.rb
@@ -223,6 +224,7 @@ files:
223
224
  - spec/internal/app/serializers/test_model_serializer.rb
224
225
  - spec/internal/app/serializers/user_serializer.rb
225
226
  - spec/internal/config/database.yml
227
+ - spec/internal/config/initializers/active_model_serializers.rb
226
228
  - spec/internal/config/routes.rb
227
229
  - spec/internal/db/schema.rb
228
230
  - spec/internal/log/.gitignore