effective_resources 1.3.4 → 1.3.5

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
  SHA256:
3
- metadata.gz: d7d0561a1b60dbeb0818d5f475f859e1e482624c9006fe9655330b516f98c6cb
4
- data.tar.gz: 721366e5cec1195d508f117a0cb7b4239720a698a94c338feb15f5f29bcf2936
3
+ metadata.gz: 2f7e34ae7d9694f5c661938ca3be425aafa8e485777805079a9295284ace1028
4
+ data.tar.gz: 8421d429c3869a68583227823120a89ade1304b8ae3109c4a4be8f0f3f932a93
5
5
  SHA512:
6
- metadata.gz: 7005aee8a57de67b42837378285189dd3906d72dfb8db5342cc57882d9e64f428107e55962d991ceaa1d710183cf8c4daf053615978d98b2e3719c050a4a56b4
7
- data.tar.gz: 3974eaf47339e51c3d3b473fb80ef53568b4e84d2aa83e45700b2a205ba9e0f20959c22664db512214b045bf1b0d17ce4ddcf6eb9f73d374fe4e829eb1fa6a5f
6
+ metadata.gz: f3d14ffd6009c288dc866f146e9aa88f26e1d1e1e69d45580cd4df347346b515e831078c23b7081f146af755e6d38ba28500268948b8875b23fe01d0861ede4f
7
+ data.tar.gz: 376071961ace632d78d96a870feac9cd9cd686aeea87dc1c9f68a9e9a839cc92ee69cfa364eadb944f71fc01ce746856a6760d029a35b51048a04c5bb255c008
@@ -8,15 +8,15 @@ module Effective
8
8
  # Saves a list of commit actions...
9
9
  def submits
10
10
  {}.tap do |submits|
11
- if (actions.find { |a| a == :create } || actions.find { |a| a == :update })
11
+ if actions.find { |a| a == :create || a == :update } && EffectiveResources.default_submits['Save']
12
12
  submits['Save'] = { action: :save, default: true }
13
13
  end
14
14
 
15
- if actions.find { |a| a == :index }
15
+ if actions.find { |a| a == :index } && EffectiveResources.default_submits['Continue']
16
16
  submits['Continue'] = { action: :save, redirect: :index, default: true, unless: -> { params[:_datatable_id].present? } }
17
17
  end
18
18
 
19
- if actions.find { |a| a == :new }
19
+ if actions.find { |a| a == :new } && EffectiveResources.default_submits['Add New']
20
20
  submits['Add New'] = { action: :save, redirect: :new, default: true, unless: -> { params[:_datatable_id].present? } }
21
21
  end
22
22
  end
@@ -1,6 +1,8 @@
1
1
  module Effective
2
2
  module Resources
3
3
  module Relation
4
+ TARGET_LIST_LIMIT = 1500
5
+ TARGET_KEYS_LIMIT = 30000
4
6
 
5
7
  def relation
6
8
  @relation ||= klass.where(nil)
@@ -254,7 +256,7 @@ module Effective
254
256
  # Order the target model for its matching records / keys
255
257
  sort_column = (sort unless sort == true) || resource.sort_column
256
258
 
257
- relation = resource.order(sort_column, direction, limit: limit, reorder: true).limit(1500)
259
+ relation = resource.order(sort_column, direction, limit: limit, reorder: true)
258
260
 
259
261
  if association.options[:as] # polymorphic
260
262
  relation = relation.where(association.type => klass.name)
@@ -270,19 +272,19 @@ module Effective
270
272
  key = sql_column(klass.primary_key)
271
273
 
272
274
  source = "#{association.join_table}.#{association.source_reflection.association_foreign_key}"
273
- values = relation.pluck(association.source_reflection.klass.primary_key).uniq.compact # The searched keys
275
+ values = relation.limit(TARGET_LIST_LIMIT).pluck(association.source_reflection.klass.primary_key).uniq.compact # The searched keys
274
276
 
275
277
  keys = klass.joins(association.name)
276
- .order(values.uniq.compact.map { |value| "#{source}=#{value} DESC" }.join(','))
278
+ .order(order_by_array_position(values, source))
277
279
  .pluck(klass.primary_key)
278
280
  elsif association.macro == :has_many && association.options[:through].present?
279
281
  key = sql_column(klass.primary_key)
280
282
 
281
283
  source = association.source_reflection.foreign_key
282
- values = relation.pluck(association.source_reflection.klass.primary_key).uniq.compact # The searched keys
284
+ values = relation.limit(TARGET_LIST_LIMIT).pluck(association.source_reflection.klass.primary_key).uniq.compact # The searched keys
283
285
 
284
286
  keys = association.through_reflection.klass
285
- .order(values.uniq.compact.map { |value| "#{source}=#{value} DESC" }.join(','))
287
+ .order(order_by_array_position(values, source))
286
288
  .pluck(association.through_reflection.foreign_key)
287
289
  elsif association.macro == :has_many
288
290
  key = sql_column(klass.primary_key)
@@ -292,7 +294,18 @@ module Effective
292
294
  keys = relation.pluck(association.foreign_key)
293
295
  end
294
296
 
295
- Arel.sql(keys.uniq.compact.map { |value| "#{key}=#{value} DESC" }.join(','))
297
+ order_by_array_position(keys, key)
298
+ end
299
+
300
+ def order_by_array_position(keys, field)
301
+ keys = Array(keys).uniq.compact
302
+
303
+ if postgres?
304
+ Arel.sql("array_position(ARRAY[#{keys.first(TARGET_KEYS_LIMIT).join(',')}]::text::int[], #{field}::int)")
305
+ else
306
+ Arel.sql(keys.first(TARGET_LIST_LIMIT).map { |value| "#{key}=#{value} DESC" }.join(','))
307
+ end
308
+
296
309
  end
297
310
 
298
311
  end
@@ -71,7 +71,7 @@ module Effective
71
71
  def sort_column
72
72
  return @_sort_column if @_sort_column
73
73
 
74
- ['name', 'title', 'label', 'subject', 'full_name', 'first_name', 'email', 'description'].each do |name|
74
+ ['name', 'title', 'label', 'subject', 'full_name', 'first_name', 'email', 'number', 'description'].each do |name|
75
75
  return name if column_names.include?(name)
76
76
  end
77
77
 
@@ -21,4 +21,11 @@ EffectiveResources.setup do |config|
21
21
  # end
22
22
  config.authorization_method = Proc.new { |controller, action, resource| authorize!(action, resource) }
23
23
 
24
+ # These default submit actions will be added to each controller
25
+ # and rendered when calling effective_submit(f)
26
+ # based on the controller, and its `submits` if any.
27
+ #
28
+ # Supported values: 'Save', 'Continue', and 'Add New'
29
+ config.default_submits = ['Save', 'Continue', 'Add New']
30
+
24
31
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.3.4'.freeze
2
+ VERSION = '1.3.5'.freeze
3
3
  end
@@ -5,6 +5,7 @@ module EffectiveResources
5
5
 
6
6
  # The following are all valid config keys
7
7
  mattr_accessor :authorization_method
8
+ mattr_accessor :default_submits
8
9
 
9
10
  def self.setup
10
11
  yield self
@@ -26,4 +27,11 @@ module EffectiveResources
26
27
  def self.authorize!(controller, action, resource)
27
28
  raise Effective::AccessDenied.new('Access Denied', action, resource) unless authorized?(controller, action, resource)
28
29
  end
30
+
31
+ def self.default_submits
32
+ @_default_submits ||= begin
33
+ (['Save', 'Continue', 'Add New'] & Array(@@default_submits)).inject({}) { |h, v| h[v] = true; h }
34
+ end
35
+ end
36
+
29
37
  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: 1.3.4
4
+ version: 1.3.5
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: 2019-06-07 00:00:00.000000000 Z
11
+ date: 2019-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails