effective_resources 1.3.4 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/effective/resources/controller.rb +3 -3
- data/app/models/effective/resources/relation.rb +19 -6
- data/app/models/effective/resources/sql.rb +1 -1
- data/config/effective_resources.rb +7 -0
- data/lib/effective_resources/version.rb +1 -1
- data/lib/effective_resources.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f7e34ae7d9694f5c661938ca3be425aafa8e485777805079a9295284ace1028
|
4
|
+
data.tar.gz: 8421d429c3869a68583227823120a89ade1304b8ae3109c4a4be8f0f3f932a93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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)
|
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
|
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
|
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
|
-
|
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
|
data/lib/effective_resources.rb
CHANGED
@@ -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
|
+
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-
|
11
|
+
date: 2019-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|