active_element 0.0.14 → 0.0.16

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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +9 -24
  3. data/app/assets/javascripts/active_element/form.js +2 -1
  4. data/app/assets/javascripts/active_element/json_field.js +8 -1
  5. data/app/views/active_element/components/button.html.erb +6 -4
  6. data/app/views/active_element/components/form/_check_boxes.html.erb +6 -4
  7. data/app/views/active_element/components/form/_date_range_field.html.erb +14 -0
  8. data/app/views/active_element/components/form/_field.html.erb +3 -0
  9. data/app/views/active_element/components/form/_summary.html.erb +10 -0
  10. data/app/views/active_element/components/form.html.erb +12 -2
  11. data/app/views/active_element/components/table/_collection_row.html.erb +3 -3
  12. data/app/views/active_element/components/table/_field.html.erb +2 -2
  13. data/app/views/active_element/components/table/collection.html.erb +20 -11
  14. data/app/views/active_element/components/table/item.html.erb +4 -0
  15. data/app/views/active_element/default_views/forbidden.html.erb +17 -3
  16. data/app/views/active_element/default_views/index.html.erb +14 -6
  17. data/config/locales/en.yml +3 -0
  18. data/example_app/.ruby-version +1 -1
  19. data/example_app/Gemfile +0 -2
  20. data/example_app/Gemfile.lock +5 -4
  21. data/lib/active_element/components/button.rb +6 -4
  22. data/lib/active_element/components/collection_table.rb +10 -3
  23. data/lib/active_element/components/form.rb +27 -2
  24. data/lib/active_element/components/item_table.rb +5 -3
  25. data/lib/active_element/components/navbar.rb +1 -1
  26. data/lib/active_element/components/util/association_mapping.rb +48 -26
  27. data/lib/active_element/components/util/default_display_value.rb +51 -0
  28. data/lib/active_element/components/util/display_value_mapping.rb +10 -0
  29. data/lib/active_element/components/util/field_mapping.rb +2 -2
  30. data/lib/active_element/components/util/form_field_mapping.rb +68 -15
  31. data/lib/active_element/components/util/record_mapping.rb +24 -3
  32. data/lib/active_element/components/util/record_path.rb +52 -20
  33. data/lib/active_element/components/util.rb +13 -0
  34. data/lib/active_element/controller_interface.rb +15 -1
  35. data/lib/active_element/controller_state.rb +5 -2
  36. data/lib/active_element/default_controller/controller.rb +1 -1
  37. data/lib/active_element/default_controller/params.rb +9 -1
  38. data/lib/active_element/default_controller/search.rb +22 -16
  39. data/lib/active_element/field_options.rb +20 -0
  40. data/lib/active_element/version.rb +1 -1
  41. data/lib/active_element.rb +1 -0
  42. data/rspec-documentation/pages/016-Default Controller.md +10 -1
  43. metadata +5 -2
@@ -25,9 +25,11 @@ module ActiveElement
25
25
  conditions = search_filters.to_h.map do |key, value|
26
26
  next relation_matches(key, value) if relation?(key)
27
27
  next datetime_between(key, value) if datetime?(key)
28
+ next model.arel_table[key].matches("#{value}%") if string_like_column?(key)
28
29
 
29
- model.arel_table[key].matches("#{value}%")
30
+ model.arel_table[key].eq(value)
30
31
  end
32
+
31
33
  conditions[1..].reduce(conditions.first) do |accumulated, condition|
32
34
  accumulated.and(condition)
33
35
  end
@@ -41,12 +43,27 @@ module ActiveElement
41
43
 
42
44
  attr_reader :controller, :model
43
45
 
46
+ def string_like_column?(key)
47
+ [:string, :text].include?(
48
+ model.columns.find { |column| column.name.to_s == key.to_s }&.type&.to_sym
49
+ )
50
+ end
51
+
44
52
  def searchable_fields
45
- controller.active_element.state.searchable_fields.map do |field|
53
+ fields = controller.active_element.state.searchable_fields.map do |field|
46
54
  next field unless field.to_s.end_with?('_at')
47
55
 
48
56
  { field => %i[from to] }
49
57
  end
58
+ (fields + relation_fields).uniq
59
+ end
60
+
61
+ def relation_fields
62
+ controller.active_element.state.searchable_fields.map do |field|
63
+ next nil unless relation?(field)
64
+
65
+ relation(field).try(:foreign_key)
66
+ end.compact
50
67
  end
51
68
 
52
69
  def noop
@@ -76,21 +93,10 @@ module ActiveElement
76
93
  end
77
94
 
78
95
  def relation_matches(key, value)
79
- fields = searchable_relation_fields(key)
80
- relation_model = relation(key).klass
81
- fields.select! do |field|
82
- relation_model.columns.find { |column| column.name.to_s == field.to_s }&.type == :string
83
- end
84
-
85
- return noop if fields.empty?
96
+ foreign_key = relation(key).try(:foreign_key)
97
+ return noop unless foreign_key.present?
86
98
 
87
- relation_conditions(fields, value, relation_model)
88
- end
89
-
90
- def relation_conditions(fields, value, relation_model)
91
- fields[1..].reduce(relation_model.arel_table[fields.first].matches("#{value}%")) do |condition, field|
92
- condition.or(relation_model.arel_table[field].matches("#{value}%"))
93
- end
99
+ model.arel_table[foreign_key].eq(value)
94
100
  end
95
101
 
96
102
  def searchable_relation_fields(key)
@@ -0,0 +1,20 @@
1
+ module ActiveElement
2
+ class FieldOptions
3
+ attr_accessor :type, :options
4
+ attr_reader :field
5
+
6
+ def self.from_state(field, state, record)
7
+ block = state.field_options[field]
8
+ return nil if block.blank?
9
+
10
+ field_options = new(field)
11
+ block.call(field_options, record)
12
+ field_options
13
+ end
14
+
15
+ def initialize(field)
16
+ @field = field
17
+ @options = {}
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveElement
4
- VERSION = '0.0.14'
4
+ VERSION = '0.0.16'
5
5
  end
@@ -14,6 +14,7 @@ require_relative 'active_element/permissions_report'
14
14
  require_relative 'active_element/controller_interface'
15
15
  require_relative 'active_element/controller_state'
16
16
  require_relative 'active_element/controller_action'
17
+ require_relative 'active_element/field_options'
17
18
  require_relative 'active_element/default_controller'
18
19
  require_relative 'active_element/pre_render_processors'
19
20
  require_relative 'active_element/rails_component'
@@ -105,7 +105,7 @@ The results will be rendered in a horizontal table with one row for each item, i
105
105
  # app/controllers/restaurants_controller.rb
106
106
 
107
107
  class RestaurantsController < ApplicationController
108
- active_element.searchable_fields :name, :restaurateur, :created_at
108
+ active_element.viewable_fields :name, :restaurateur, :created_at
109
109
  end
110
110
  ```
111
111
 
@@ -119,6 +119,15 @@ Note that each field type can be overridden and configured by defining `config/f
119
119
 
120
120
  By default, `json` and `jsonb` fields use the [JSON Field](form-fields/json.html) type, allowing you to edit complex _JSON_ data structures via user-friendly _HTML_ forms. A [schema file](form-fields/json/schema.html) **must** be defined for these fields. See the `json_field` documentation for more details.
121
121
 
122
+
123
+ ```ruby
124
+ # app/controllers/restaurants_controller.rb
125
+
126
+ class RestaurantsController < ApplicationController
127
+ active_element.editable_fields :name, :restaurateur
128
+ end
129
+ ```
130
+
122
131
  ## Deletable
123
132
 
124
133
  The `active_element.deletable` declaration does not receive any arguments but specifies that a record can be deleted by a user.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_element
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Farrell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-12 00:00:00.000000000 Z
11
+ date: 2024-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bootstrap
@@ -141,6 +141,7 @@ files:
141
141
  - app/views/active_element/components/form.html.erb
142
142
  - app/views/active_element/components/form/_check_box.html.erb
143
143
  - app/views/active_element/components/form/_check_boxes.html.erb
144
+ - app/views/active_element/components/form/_date_range_field.html.erb
144
145
  - app/views/active_element/components/form/_datetime_range_field.html.erb
145
146
  - app/views/active_element/components/form/_field.html.erb
146
147
  - app/views/active_element/components/form/_generic_field.html.erb
@@ -308,6 +309,7 @@ files:
308
309
  - lib/active_element/components/util.rb
309
310
  - lib/active_element/components/util/association_mapping.rb
310
311
  - lib/active_element/components/util/decorator.rb
312
+ - lib/active_element/components/util/default_display_value.rb
311
313
  - lib/active_element/components/util/display_value_mapping.rb
312
314
  - lib/active_element/components/util/field_mapping.rb
313
315
  - lib/active_element/components/util/form_field_mapping.rb
@@ -326,6 +328,7 @@ files:
326
328
  - lib/active_element/default_controller/params.rb
327
329
  - lib/active_element/default_controller/search.rb
328
330
  - lib/active_element/engine.rb
331
+ - lib/active_element/field_options.rb
329
332
  - lib/active_element/json_field_schema.rb
330
333
  - lib/active_element/permissions_check.rb
331
334
  - lib/active_element/permissions_report.rb