fetcheable_on_api 0.1.9.0 → 0.2

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
  SHA256:
3
- metadata.gz: 05a553bcde4c9048e8293d114936e2e9b4192e1499adcbc22bfdf78997a6e856
4
- data.tar.gz: 532213cc5c36c522a8f2bcb0f46f7cbbf56ffeb94c8e1d83072bf18db6f6a768
3
+ metadata.gz: c9c8bca94ebf7b39b0dfee1fc94b7322afbbe19959712f67c0b1785c07944ff9
4
+ data.tar.gz: b0b9b05f0f4071c051c469e7092d1d17f7d216373c81ad13d29d73026cdd261c
5
5
  SHA512:
6
- metadata.gz: 02b31b34d84a9ff79b034881a3fe8d1cdbf62620572625ba977183daf498cbdd9a82134f3bafc9ddd413c348b9e08f9151c78f7abde4fb14542cbe3b586ef781
7
- data.tar.gz: 61dda83e7a0a5fe2d07b6e251347c42282be5ac904ca64632c00f46314441feb012628f1a462e5a3ae6f7c6d499dfdc900852473ab7b924b0ae5ba0c96e15bd3
6
+ metadata.gz: 5d9ecd3bad835fca0821ed277b808c400b3f3c8b6b93661d43c031e4d11063a3d67f34c9194ec0c4a88121cf1f8f502f5c104770b703d409147b50e7f2ece24b
7
+ data.tar.gz: d61db97cdf938aefcbfe31d6ca491a15bdd68ed50b5bcfcdb995abc198239f0d5b0ca0a437d64bf534269a767977b315a30693b6f612da8b6099e8f2b7401a31
data/README.md CHANGED
@@ -259,6 +259,55 @@ $ curl -X GET \
259
259
  ]
260
260
  ```
261
261
 
262
+ You can also sort through an association like this:
263
+
264
+ ```ruby
265
+ class QuestionsController < ActionController::Base
266
+ #
267
+ # FetcheableOnApi
268
+ #
269
+ sort_by :position, :id
270
+ sort_by :answer,
271
+ class_name: Answer,
272
+ as: 'content'
273
+
274
+ # GET /questions
275
+ def index
276
+ questions = apply_fetcheable(Question.joins(:answer).includes(:answer).all)
277
+ render json: questions
278
+ end
279
+ end
280
+ ```
281
+
282
+ ```bash
283
+ $ curl -X GET \
284
+ 'http://localhost:3000/questions?sort=answer'
285
+
286
+ [
287
+ {
288
+ "id": 3,
289
+ "position": 1,
290
+ "category_id": 1,
291
+ "content": "How to simply sort a collection with this gem ?",
292
+ "answer": "Just add sort_by in your controller and call the apply_fetcheable method"
293
+ },
294
+ {
295
+ "id": 4,
296
+ "position": 2,
297
+ "category_id": 2,
298
+ "content": "Is it so simple?",
299
+ "answer": "Yes"
300
+ },
301
+ {
302
+ "id": 5,
303
+ "position": 3,
304
+ "category_id": 2,
305
+ "content": "Is this real life?",
306
+ "answer": "Yes this is real life"
307
+ }
308
+ ]
309
+ ```
310
+
262
311
  ### Pagination
263
312
 
264
313
  Pagination is automatically set on the controller and allows the use of a new parameter `page`.
@@ -82,9 +82,10 @@ module FetcheableOnApi
82
82
  def valid_keys
83
83
  keys = filters_configuration.keys
84
84
  keys.each_with_index do |key, index|
85
- predicate = filters_configuration[key.to_sym].fetch(:with, :ilike).to_sym
85
+ predicate = filters_configuration[key.to_sym].fetch(:with, :ilike)
86
+ next if predicate.respond_to?(:call) || PREDICATES_WITH_ARRAY.exclude?(predicate.to_sym)
86
87
 
87
- keys[index] = { key => [] } if PREDICATES_WITH_ARRAY.include?(predicate)
88
+ keys[index] = { key => [] }
88
89
  end
89
90
 
90
91
  keys
@@ -51,23 +51,28 @@ module FetcheableOnApi
51
51
  return collection if params[:sort].blank?
52
52
  foa_valid_parameters!(:sort, foa_permitted_types: [String])
53
53
 
54
- ordering = {}
55
- sorted_params = params[:sort].split(',')
54
+ ordering = []
56
55
 
57
- sorted_params.each do |attr|
58
- sort_sign = (attr =~ /\A[+-]/) ? attr.slice!(0) : '+'
59
- klass = collection.klass
60
-
61
- if klass.attribute_names.include?(attr)
62
- ordering[attr] = SORT_ORDER[sort_sign]
63
- end
64
- end
56
+ clean_params(params[:sort]).each do |attr|
57
+ klass = sorts_configuration[attr.to_sym].fetch(:class_name, collection.klass)
58
+ field = sorts_configuration[attr.to_sym].fetch(:as, attr.to_sym)
59
+ next unless klass.attribute_names.include?(field)
65
60
 
66
- ordering.select! do |attr|
67
- sorts_configuration.key?(attr.to_sym)
61
+ sort_sign = (attr =~ /\A[+-]/) ? attr.slice!(0) : '+'
62
+ ordering << klass
63
+ .arel_table[field.to_sym]
64
+ .send(SORT_ORDER[sort_sign])
68
65
  end
69
66
 
70
67
  collection.order(ordering)
71
68
  end
69
+
70
+ private
71
+
72
+ def clean_params(params)
73
+ params
74
+ .split(',')
75
+ .select { |e| sorts_configuration.keys.map(&:to_s).include?(e) }
76
+ end
72
77
  end
73
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FetcheableOnApi
4
- VERSION = '0.1.9.0'.freeze
4
+ VERSION = '0.2'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetcheable_on_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9.0
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabien
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-04 00:00:00.000000000 Z
11
+ date: 2019-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport