motor-admin 0.2.33 → 0.2.37

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: 2b496961d80291a72ecf0017c07d68dee193fff1f89238bd510ad1ca85afe37b
4
- data.tar.gz: 18222f2abe4752e73fddf640631b3dc179368d5dc55c0ee6e41a7a9111ad6391
3
+ metadata.gz: a582148a923d7c89a0944ba2c91315b434db0989a6b8918ba4db1f36d8f17480
4
+ data.tar.gz: ed1d8c3588dff89c78e468b6b4a9ac06def1a3af163dc74d6a6602321a0e6dc1
5
5
  SHA512:
6
- metadata.gz: 33de92dd60c1f53d6f833646ac388f86434a65be73c1bfde0ab5b037f8cca0f3cec8b51c8cd3fdaba788e4a2ef83c33369a79dba5f543726dfae8ce9e1aa6554
7
- data.tar.gz: 1eb89d9c5cc284aba7a1b343c6ebb566075c2c4b7a262c214243f4b141ace04a8eaaa6e7f48cf6cadb17b40f904467b1cfd19694377d538d6a59a8bb7649f763
6
+ metadata.gz: b3a9a370786068bf97186bf93ef42de50318a2bbbcf2ed52c83aeef3705693d90ecd4fa40e012cb55c56303576eb124a27cb19ac16db584b47138733cca1e223
7
+ data.tar.gz: 9e03d29bb4d5e9ee67c4c2b7d4d809294caaf2e4bedd2b3b9c2cba61d65888edf2d7cd47ef5c28503bbcef916092e34979c3b4ee34be137cb8fb9306f9cc216a
@@ -286,3 +286,10 @@ en:
286
286
  slash: Slash
287
287
  sql: SQL
288
288
  api: API
289
+ searchable_columns: Searchable columns
290
+ validate: Validate
291
+ error_message: Error message
292
+ validation_regexp: Validation RegExp
293
+ regexp: RegExp
294
+ message: Message
295
+ should_be_a_valid_regexp: Should be a valid RegExp string
@@ -286,6 +286,13 @@ es:
286
286
  slash: Slash
287
287
  sql: SQL
288
288
  api: API
289
+ searchable_columns: Columnas de búsqueda
290
+ validate: Validar
291
+ error_message: Mensaje de error
292
+ validation_regexp: Validación RegExp
293
+ regexp: RegExp
294
+ message: Mensaje
295
+ should_be_a_valid_regexp: Debe ser una cadena RegExp válida
289
296
  i:
290
297
  locale: es
291
298
  select:
@@ -282,6 +282,13 @@ pt:
282
282
  slash: Slash
283
283
  sql: SQL
284
284
  api: API
285
+ searchable_columns: Colunas pesquisáveis
286
+ validate: Validar
287
+ error_message: Mensagem de erro
288
+ validation_regexp: Validação RegExp
289
+ regexp: RegExp
290
+ message: Mensagem
291
+ should_be_a_valid_regexp: Deve ser uma string RegExp válida
285
292
  i:
286
293
  locale: pt
287
294
  select:
@@ -18,5 +18,16 @@ if Rails::VERSION::MAJOR == 7
18
18
  end
19
19
  end
20
20
  end
21
+
22
+ module Contexts
23
+ class BaseContext
24
+ def preload_records(association_name, records)
25
+ TemporaryPreloadConfig.within_context do
26
+ ActiveRecord::Associations::Preloader.new(records: records,
27
+ associations: association_name).call
28
+ end
29
+ end
30
+ end
31
+ end
21
32
  end
22
33
  end
data/lib/motor/admin.rb CHANGED
@@ -39,7 +39,7 @@ module Motor
39
39
  end
40
40
 
41
41
  initializer 'motor.filter_params' do
42
- Rails.application.config.filter_parameters += %i[io]
42
+ Rails.application.config.filter_parameters += [/\Aio\z/]
43
43
  end
44
44
 
45
45
  initializer 'motor.alerts.scheduler' do
@@ -3,9 +3,8 @@
3
3
  module Motor
4
4
  module ApiQuery
5
5
  module Search
6
- SELECT_COLUMNS_AMOUNT = 2
7
- COLUMN_TYPES = BuildSchema::SEARCHABLE_COLUMN_TYPES
8
- ID_REGEXP = /\A\d+\z/.freeze
6
+ STRING_COLUMN_TYPES = %i[text string].freeze
7
+ NUMBER_COLUMN_TYPES = %i[integer float].freeze
9
8
 
10
9
  module_function
11
10
 
@@ -20,19 +19,27 @@ module Motor
20
19
  end
21
20
 
22
21
  def fetch_filters(rel, keyword)
23
- arel_filters = []
24
-
25
22
  klass = rel.klass
26
- arel_table = klass.arel_table
27
23
 
28
- arel_filters << arel_table[klass.primary_key].eq(keyword) if keyword.match?(ID_REGEXP)
24
+ selected_columns = find_searchable_columns(klass)
25
+
26
+ build_arel_filters(klass, selected_columns, keyword)
27
+ end
29
28
 
30
- string_column_names = find_searchable_columns(klass)
31
- selected_columns = select_columns(string_column_names)
29
+ def build_arel_filters(model, column_names, keyword)
30
+ arel_table = model.arel_table
32
31
 
33
- selected_columns.each { |name| arel_filters << arel_table[name].matches("%#{keyword}%") }
32
+ column_names.map do |name|
33
+ column_type = model.columns_hash[name].type
34
34
 
35
- arel_filters
35
+ if STRING_COLUMN_TYPES.include?(column_type)
36
+ arel_table[name].matches("%#{keyword}%")
37
+ elsif NUMBER_COLUMN_TYPES.include?(column_type)
38
+ arel_table[name].eq(keyword.to_f)
39
+ else
40
+ arel_table[name].eq(keyword)
41
+ end
42
+ end
36
43
  end
37
44
 
38
45
  def build_arel_or_query(filter_array)
@@ -43,23 +50,8 @@ module Motor
43
50
  end
44
51
  end
45
52
 
46
- def select_columns(columns)
47
- selected_columns =
48
- columns.grep(BuildSchema::FindDisplayColumn::DISPLAY_NAME_REGEXP).presence
49
-
50
- selected_columns ||= columns.first(SELECT_COLUMNS_AMOUNT)
51
-
52
- selected_columns
53
- end
54
-
55
53
  def find_searchable_columns(model)
56
- model.columns.map do |column|
57
- next unless column.type.in?(COLUMN_TYPES)
58
- next if column.respond_to?(:array?) && column.array?
59
- next if model.validators_on(column.name).any?(ActiveModel::Validations::InclusionValidator)
60
-
61
- column.name
62
- end.compact
54
+ model.try(:motor_searchable_columns) || Motor::BuildSchema::FindSearchableColumns.call(model)
63
55
  end
64
56
  end
65
57
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Motor
4
+ module BuildSchema
5
+ module FindSearchableColumns
6
+ SELECT_COLUMNS_AMOUNT = 2
7
+ COLUMN_TYPES = BuildSchema::SEARCHABLE_COLUMN_TYPES
8
+
9
+ module_function
10
+
11
+ def call(model)
12
+ columns = find_searchable_columns(model)
13
+
14
+ selected_columns =
15
+ columns.grep(BuildSchema::FindDisplayColumn::DISPLAY_NAME_REGEXP).presence
16
+
17
+ selected_columns ||= columns.first(SELECT_COLUMNS_AMOUNT)
18
+
19
+ ([model.primary_key] + selected_columns).sort
20
+ end
21
+
22
+ def find_searchable_columns(model)
23
+ model.columns.map do |column|
24
+ next unless column.type.in?(COLUMN_TYPES)
25
+ next if column.respond_to?(:array?) && column.array?
26
+ next if model.validators_on(column.name).any?(ActiveModel::Validations::InclusionValidator)
27
+
28
+ column.name
29
+ end.compact
30
+ end
31
+ end
32
+ end
33
+ end
@@ -56,6 +56,7 @@ module Motor
56
56
  models
57
57
  end
58
58
 
59
+ # rubocop:disable Metrics/MethodLength
59
60
  def build_model_schema(model)
60
61
  model_name = model.name
61
62
 
@@ -65,7 +66,7 @@ module Motor
65
66
  name: model_name.underscore,
66
67
  slug: Utils.slugify(model),
67
68
  table_name: model.table_name,
68
- class_name: model.name,
69
+ class_name: model_name,
69
70
  primary_key: model.primary_key,
70
71
  display_name: model.model_name.human(count: :many, default: model_name.titleize.pluralize),
71
72
  display_column: FindDisplayColumn.call(model),
@@ -75,11 +76,13 @@ module Motor
75
76
  scopes: fetch_scopes(model),
76
77
  actions: BuildSchema::Defaults.actions,
77
78
  tabs: BuildSchema::Defaults.tabs,
79
+ searchable_columns: FindSearchableColumns.call(model),
78
80
  custom_sql: nil,
79
81
  visible: true,
80
82
  display_primary_key: true
81
83
  }.with_indifferent_access
82
84
  end
85
+ # rubocop:enable Metrics/MethodLength
83
86
 
84
87
  def fetch_scopes(model)
85
88
  model.defined_scopes.map do |scope_name|
@@ -113,3 +113,4 @@ require_relative './build_schema/merge_schema_configs'
113
113
  require_relative './build_schema/apply_permissions'
114
114
  require_relative './build_schema/utils'
115
115
  require_relative './build_schema/defaults'
116
+ require_relative './build_schema/find_searchable_columns'
@@ -33,6 +33,7 @@ module Motor
33
33
  define_default_scope(klass, config)
34
34
  define_column_reflections(klass, config)
35
35
  define_associations(klass, config)
36
+ define_searchable_columns_method(klass, config)
36
37
 
37
38
  klass
38
39
  end
@@ -75,6 +76,18 @@ module Motor
75
76
  klass
76
77
  end
77
78
 
79
+ def define_searchable_columns_method(klass, config)
80
+ return if config[:searchable_columns].blank?
81
+
82
+ klass.instance_variable_set(:@__motor_searchable_columns, config[:searchable_columns])
83
+
84
+ klass.instance_eval do
85
+ def motor_searchable_columns
86
+ @__motor_searchable_columns
87
+ end
88
+ end
89
+ end
90
+
78
91
  def define_columns_hash(klass, config)
79
92
  return klass if config[:custom_sql].blank?
80
93
 
@@ -82,7 +95,13 @@ module Motor
82
95
 
83
96
  columns_hash =
84
97
  columns.each_with_object({}) do |column, acc|
85
- acc[column[:name]] = ActiveRecord::ConnectionAdapters::Column.new(column[:name], nil)
98
+ acc[column[:name]] =
99
+ ActiveRecord::ConnectionAdapters::Column.new(
100
+ column[:name],
101
+ nil,
102
+ ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(sql_type: column[:column_type],
103
+ type: column[:column_type].to_sym)
104
+ )
86
105
  end
87
106
 
88
107
  klass.instance_variable_set(:@__motor_custom_sql_columns_hash, columns_hash)
@@ -2,8 +2,9 @@
2
2
 
3
3
  module Motor
4
4
  module Resources
5
- RESOURCE_ATTRS = %w[display_name display_column icon custom_sql visible display_primary_key].freeze
6
- COLUMN_ATTRS = %w[name display_name column_type access_type default_value reference virtual format].freeze
5
+ RESOURCE_ATTRS = %w[display_name display_column icon custom_sql visible display_primary_key
6
+ searchable_columns].freeze
7
+ COLUMN_ATTRS = %w[name display_name column_type access_type default_value reference virtual format validators].freeze
7
8
  ASSOCIATION_ATTRS = %w[name display_name model_name icon visible foreign_key primary_key options virtual
8
9
  polymorphic slug].freeze
9
10
  SCOPE_ATTRS = %w[name display_name scope_type preferences visible].freeze
data/lib/motor/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Motor
4
- VERSION = '0.2.33'
4
+ VERSION = '0.2.37'
5
5
  end
@@ -2601,9 +2601,9 @@
2601
2601
  "icons/zoom-out.svg.gz": "icons/zoom-out.svg.gz",
2602
2602
  "icons/zoom-question.svg": "icons/zoom-question.svg",
2603
2603
  "icons/zoom-question.svg.gz": "icons/zoom-question.svg.gz",
2604
- "main-1d882f91707272b39943.css.gz": "main-1d882f91707272b39943.css.gz",
2605
- "main-1d882f91707272b39943.js.LICENSE.txt": "main-1d882f91707272b39943.js.LICENSE.txt",
2606
- "main-1d882f91707272b39943.js.gz": "main-1d882f91707272b39943.js.gz",
2607
- "main.css": "main-1d882f91707272b39943.css",
2608
- "main.js": "main-1d882f91707272b39943.js"
2604
+ "main-caf0e2eb1b4372864962.css.gz": "main-caf0e2eb1b4372864962.css.gz",
2605
+ "main-caf0e2eb1b4372864962.js.LICENSE.txt": "main-caf0e2eb1b4372864962.js.LICENSE.txt",
2606
+ "main-caf0e2eb1b4372864962.js.gz": "main-caf0e2eb1b4372864962.js.gz",
2607
+ "main.css": "main-caf0e2eb1b4372864962.css",
2608
+ "main.js": "main-caf0e2eb1b4372864962.js"
2609
2609
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motor-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.33
4
+ version: 0.2.37
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Matsyburka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-05 00:00:00.000000000 Z
11
+ date: 2021-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord-filter
@@ -191,6 +191,7 @@ files:
191
191
  - lib/motor/build_schema/defaults.rb
192
192
  - lib/motor/build_schema/find_display_column.rb
193
193
  - lib/motor/build_schema/find_icon.rb
194
+ - lib/motor/build_schema/find_searchable_columns.rb
194
195
  - lib/motor/build_schema/load_from_rails.rb
195
196
  - lib/motor/build_schema/merge_schema_configs.rb
196
197
  - lib/motor/build_schema/reorder_schema.rb
@@ -1525,8 +1526,8 @@ files:
1525
1526
  - ui/dist/icons/zoom-money.svg.gz
1526
1527
  - ui/dist/icons/zoom-out.svg.gz
1527
1528
  - ui/dist/icons/zoom-question.svg.gz
1528
- - ui/dist/main-1d882f91707272b39943.css.gz
1529
- - ui/dist/main-1d882f91707272b39943.js.gz
1529
+ - ui/dist/main-caf0e2eb1b4372864962.css.gz
1530
+ - ui/dist/main-caf0e2eb1b4372864962.js.gz
1530
1531
  - ui/dist/manifest.json
1531
1532
  homepage:
1532
1533
  licenses: