active_element 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +8 -23
- data/app/assets/javascripts/active_element/form.js +2 -1
- data/app/assets/javascripts/active_element/json_field.js +8 -1
- data/app/views/active_element/components/button.html.erb +6 -4
- data/app/views/active_element/components/form/_check_boxes.html.erb +6 -4
- data/app/views/active_element/components/form/_date_range_field.html.erb +14 -0
- data/app/views/active_element/components/form/_field.html.erb +3 -0
- data/app/views/active_element/components/form/_summary.html.erb +10 -0
- data/app/views/active_element/components/form.html.erb +12 -2
- data/app/views/active_element/components/table/_collection_row.html.erb +3 -3
- data/app/views/active_element/components/table/_field.html.erb +2 -2
- data/app/views/active_element/components/table/collection.html.erb +20 -11
- data/app/views/active_element/components/table/item.html.erb +4 -0
- data/app/views/active_element/default_views/forbidden.html.erb +17 -3
- data/app/views/active_element/default_views/index.html.erb +14 -6
- data/config/locales/en.yml +3 -0
- data/example_app/.ruby-version +1 -1
- data/example_app/Gemfile +0 -2
- data/example_app/Gemfile.lock +5 -4
- data/lib/active_element/components/button.rb +6 -4
- data/lib/active_element/components/collection_table.rb +10 -3
- data/lib/active_element/components/form.rb +27 -2
- data/lib/active_element/components/item_table.rb +5 -3
- data/lib/active_element/components/navbar.rb +1 -1
- data/lib/active_element/components/util/association_mapping.rb +50 -26
- data/lib/active_element/components/util/default_display_value.rb +51 -0
- data/lib/active_element/components/util/display_value_mapping.rb +10 -0
- data/lib/active_element/components/util/field_mapping.rb +2 -2
- data/lib/active_element/components/util/form_field_mapping.rb +68 -15
- data/lib/active_element/components/util/record_mapping.rb +24 -3
- data/lib/active_element/components/util/record_path.rb +51 -20
- data/lib/active_element/components/util.rb +13 -0
- data/lib/active_element/controller_interface.rb +11 -1
- data/lib/active_element/controller_state.rb +4 -2
- data/lib/active_element/default_controller/params.rb +9 -1
- data/lib/active_element/default_controller/search.rb +22 -16
- data/lib/active_element/field_options.rb +20 -0
- data/lib/active_element/version.rb +1 -1
- data/lib/active_element.rb +1 -0
- data/rspec-documentation/pages/016-Default Controller.md +10 -1
- 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].
|
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
|
-
|
80
|
-
|
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
|
-
|
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
|
data/lib/active_element.rb
CHANGED
@@ -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.
|
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.
|
4
|
+
version: 0.0.15
|
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-
|
11
|
+
date: 2023-12-14 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
|