effective_resources 0.4.10 → 0.5.0

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: 57a9fea58220e51746d9c8467b84e884a3039049
4
- data.tar.gz: 3252c3a12d1dbff7591df1f8cbab3d9ea4961ddf
3
+ metadata.gz: e5800f90d584446cae530052c580fc7d43e690a7
4
+ data.tar.gz: e5cb47b3fea2a63222a5bbb6f06ca417372b8b3d
5
5
  SHA512:
6
- metadata.gz: e869b9eb37fe264c797927817fb67f30abf8415217e572ac2dd628ac6e15105e4f425f807bca12f690d01c908cc61b2fad5fe3f67eb2178a7c61b0e88486d2b4
7
- data.tar.gz: cd024f08251b9b0ebcbfa22e7e3671b281bcaa603675c10eea7ca89fc05f90f32eddbae710e68a95277c13da844a2e08384f79b4ad6f936bd029abe1710a4e4a
6
+ metadata.gz: bdb4b94bc22efb2b723df96467b5d7f03c467fd9e173cbeea8ca64edb2e45eba98bb011f5c69fe92d2e997656b614e9593a36179dcc31e91a6e6e2a7d1168a5a
7
+ data.tar.gz: e7c659d37e240c593c751fee96270c06aef5983b028efdfb5585857d5cd1ca98c06913a5aeb14ca7354ddc216f8f12ecc361c277dfc152c1655c8384c04505c6
@@ -9,7 +9,7 @@ module Effective
9
9
  # name: sort by this column, or this relation
10
10
  # sort: when a symbol or boolean, this is the relation's column to sort by
11
11
 
12
- def order(name, direction = :asc, as: nil, sort: nil, sql_column: nil)
12
+ def order(name, direction = :asc, as: nil, sort: nil, sql_column: nil, limit: nil, reorder: false)
13
13
  raise 'expected relation to be present' unless relation
14
14
 
15
15
  sql_column ||= sql_column(name)
@@ -17,23 +17,24 @@ module Effective
17
17
 
18
18
  association = associated(name)
19
19
  sql_direction = sql_direction(direction)
20
+ @relation = relation.reorder(nil) if reorder
20
21
 
21
22
  case sql_type
22
23
  when :belongs_to
23
24
  relation
24
25
  .order("#{is_null(sql_column)} ASC")
25
- .order(order_by_associated_conditions(association, sort: sort, direction: direction))
26
+ .order(order_by_associated_conditions(association, sort: sort, direction: direction, limit: limit))
26
27
  when :belongs_to_polymorphic
27
28
  relation
28
29
  .order("#{sql_column.sub('_id', '_type')} #{sql_direction}")
29
30
  .order("#{sql_column} #{sql_direction}")
30
31
  when :has_and_belongs_to_many, :has_many, :has_one
31
32
  relation
32
- .order(order_by_associated_conditions(association, sort: sort, direction: direction))
33
+ .order(order_by_associated_conditions(association, sort: sort, direction: direction, limit: limit))
33
34
  .order("#{sql_column(klass.primary_key)} #{sql_direction}")
34
35
  when :effective_addresses
35
36
  relation
36
- .order(order_by_associated_conditions(associated(:addresses), sort: sort, direction: direction))
37
+ .order(order_by_associated_conditions(associated(:addresses), sort: sort, direction: direction, limit: limit))
37
38
  .order("#{sql_column(klass.primary_key)} #{sql_direction}")
38
39
  when :effective_roles
39
40
  relation.order("#{sql_column(:roles_mask)} #{sql_direction}")
@@ -219,13 +220,13 @@ module Effective
219
220
  "#{key} IN (#{(keys.uniq.compact.presence || [0]).join(',')})"
220
221
  end
221
222
 
222
- def order_by_associated_conditions(association, sort: nil, direction: :asc)
223
+ def order_by_associated_conditions(association, sort: nil, direction: :asc, limit: nil)
223
224
  resource = Effective::Resource.new(association)
224
225
 
225
226
  # Order the target model for its matching records / keys
226
227
  sort_column = (sort unless sort == true) || resource.sort_column
227
228
 
228
- relation = resource.relation.reorder(nil).order("#{resource.sql_column(sort_column)} #{sql_direction(direction)}")
229
+ relation = resource.order(sort_column, direction, limit: limit, reorder: true).limit([limit, 1000].compact.min)
229
230
 
230
231
  if association.options[:as] # polymorphic
231
232
  relation = relation.where(association.type => klass.name)
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.4.10'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.10
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-10 00:00:00.000000000 Z
11
+ date: 2017-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails