active_element 0.0.12 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +17 -17
- data/app/assets/javascripts/active_element/highlight.js +311 -0
- data/app/assets/javascripts/active_element/json_field.js +51 -20
- data/app/assets/javascripts/active_element/popover.js +6 -4
- data/app/assets/stylesheets/active_element/_dark.scss +1 -1
- data/app/assets/stylesheets/active_element/application.scss +39 -1
- data/app/controllers/concerns/active_element/default_controller_actions.rb +7 -7
- data/app/views/active_element/_title.html.erb +1 -0
- data/app/views/active_element/components/fields/_json.html.erb +3 -2
- data/app/views/active_element/components/form/_field.html.erb +2 -1
- data/app/views/active_element/components/form/_json.html.erb +2 -0
- data/app/views/active_element/components/form/_label.html.erb +7 -0
- data/app/views/active_element/components/form.html.erb +2 -2
- data/app/views/layouts/active_element.html.erb +29 -6
- data/example_app/Gemfile.lock +1 -1
- data/lib/active_element/components/form.rb +1 -8
- data/lib/active_element/components/util/display_value_mapping.rb +0 -2
- data/lib/active_element/components/util/form_field_mapping.rb +2 -1
- data/lib/active_element/components/util.rb +7 -0
- data/lib/active_element/controller_interface.rb +2 -1
- data/lib/active_element/controller_state.rb +1 -1
- data/lib/active_element/default_controller/actions.rb +3 -0
- data/lib/active_element/default_controller/controller.rb +145 -0
- data/lib/active_element/default_controller/json_params.rb +48 -0
- data/lib/active_element/default_controller/params.rb +97 -0
- data/lib/active_element/default_controller/search.rb +112 -0
- data/lib/active_element/default_controller.rb +10 -132
- data/lib/active_element/version.rb +1 -1
- data/lib/active_element.rb +0 -2
- data/rspec-documentation/_head.html.erb +2 -0
- data/rspec-documentation/pages/000-Introduction.md +8 -5
- data/rspec-documentation/pages/005-Setup.md +21 -28
- data/rspec-documentation/pages/010-Components/Form Fields.md +35 -0
- data/rspec-documentation/pages/015-Custom Controllers.md +32 -0
- data/rspec-documentation/pages/016-Default Controller.md +132 -0
- data/rspec-documentation/pages/Themes.md +3 -0
- metadata +12 -4
- data/lib/active_element/default_record_params.rb +0 -62
- data/lib/active_element/default_search.rb +0 -110
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveElement
|
4
|
+
module DefaultController
|
5
|
+
# Encapsulation of all logic performed for default controller actions when no action is defined
|
6
|
+
# by the current controller.
|
7
|
+
class Controller # rubocop:disable Metrics/ClassLength
|
8
|
+
def initialize(controller:)
|
9
|
+
@controller = controller
|
10
|
+
end
|
11
|
+
|
12
|
+
def index
|
13
|
+
return render_forbidden(:listable) unless configured?(:listable)
|
14
|
+
|
15
|
+
controller.render 'active_element/default_views/index',
|
16
|
+
locals: {
|
17
|
+
collection: ordered(collection),
|
18
|
+
search_filters: default_text_search.search_filters
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def show
|
23
|
+
return render_forbidden(:viewable) unless configured?(:viewable)
|
24
|
+
|
25
|
+
controller.render 'active_element/default_views/show', locals: { record: record }
|
26
|
+
end
|
27
|
+
|
28
|
+
def new
|
29
|
+
return render_forbidden(:editable) unless configured?(:editable)
|
30
|
+
|
31
|
+
controller.render 'active_element/default_views/new', locals: { record: model.new, namespace: namespace }
|
32
|
+
end
|
33
|
+
|
34
|
+
def create # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
35
|
+
return render_forbidden(:editable) unless configured?(:editable)
|
36
|
+
|
37
|
+
new_record = model.new(default_record_params.params)
|
38
|
+
# XXX: Ensure associations are applied - there must be a better way.
|
39
|
+
if new_record.save && new_record.reload.update(default_record_params.params)
|
40
|
+
controller.flash.notice = "#{new_record.model_name.to_s.titleize} created successfully."
|
41
|
+
controller.redirect_to record_path(new_record, :show).path
|
42
|
+
else
|
43
|
+
controller.flash.now.alert = "Failed to create #{model.name.to_s.titleize}."
|
44
|
+
controller.render 'active_element/default_views/new', locals: { record: new_record, namespace: namespace }
|
45
|
+
end
|
46
|
+
rescue ActiveRecord::RangeError => e
|
47
|
+
render_range_error(error: e, action: :new)
|
48
|
+
end
|
49
|
+
|
50
|
+
def edit
|
51
|
+
return render_forbidden(:editable) unless configured?(:editable)
|
52
|
+
|
53
|
+
controller.render 'active_element/default_views/edit', locals: { record: record, namespace: namespace }
|
54
|
+
end
|
55
|
+
|
56
|
+
def update # rubocop:disable Metrics/AbcSize
|
57
|
+
return render_forbidden(:editable) unless configured?(:editable)
|
58
|
+
|
59
|
+
if record.update(default_record_params.params)
|
60
|
+
controller.flash.notice = "#{record.model_name.to_s.titleize} updated successfully."
|
61
|
+
controller.redirect_to record_path(record, :show).path
|
62
|
+
else
|
63
|
+
controller.flash.now.alert = "Failed to update #{model.name.to_s.titleize}."
|
64
|
+
controller.render 'active_element/default_views/edit', locals: { record: record, namespace: namespace }
|
65
|
+
end
|
66
|
+
rescue ActiveRecord::RangeError => e
|
67
|
+
render_range_error(error: e, action: :edit)
|
68
|
+
end
|
69
|
+
|
70
|
+
def destroy
|
71
|
+
return render_forbidden(:deletable) unless configured?(:deletable)
|
72
|
+
|
73
|
+
record.destroy
|
74
|
+
controller.flash.notice = "Deleted #{record.model_name.to_s.titleize}."
|
75
|
+
controller.redirect_to record_path(model, :index).path
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
attr_reader :controller
|
81
|
+
|
82
|
+
def ordered(unordered_collection)
|
83
|
+
return unordered_collection if state.list_order.blank?
|
84
|
+
|
85
|
+
unordered_collection.order(state.list_order)
|
86
|
+
end
|
87
|
+
|
88
|
+
def render_forbidden(type)
|
89
|
+
controller.render 'active_element/default_views/forbidden', locals: { type: type }
|
90
|
+
end
|
91
|
+
|
92
|
+
def configured?(type)
|
93
|
+
return state.deletable? if type == :deletable
|
94
|
+
|
95
|
+
state.public_send("#{type}_fields").present?
|
96
|
+
end
|
97
|
+
|
98
|
+
def state
|
99
|
+
@state ||= controller.active_element.state
|
100
|
+
end
|
101
|
+
|
102
|
+
def default_record_params
|
103
|
+
@default_record_params ||= DefaultController::Params.new(controller: controller, model: model)
|
104
|
+
end
|
105
|
+
|
106
|
+
def default_text_search
|
107
|
+
@default_text_search ||= DefaultController::Search.new(controller: controller, model: model)
|
108
|
+
end
|
109
|
+
|
110
|
+
def record_path(record, type = nil)
|
111
|
+
ActiveElement::Components::Util::RecordPath.new(record: record, controller: controller, type: type)
|
112
|
+
end
|
113
|
+
|
114
|
+
def namespace
|
115
|
+
controller.controller_path.rpartition('/').first.presence&.to_sym
|
116
|
+
end
|
117
|
+
|
118
|
+
def model
|
119
|
+
controller.controller_name.classify.constantize
|
120
|
+
end
|
121
|
+
|
122
|
+
def record
|
123
|
+
@record ||= model.find(controller.params[:id])
|
124
|
+
end
|
125
|
+
|
126
|
+
def collection
|
127
|
+
return model.all unless default_text_search.text_search?
|
128
|
+
|
129
|
+
model.left_outer_joins(default_text_search.search_relations).where(*default_text_search.text_search)
|
130
|
+
end
|
131
|
+
|
132
|
+
def render_range_error(error:, action:)
|
133
|
+
controller.flash.now.alert = formatted_error(error)
|
134
|
+
controller.render "active_element/default_views/#{action}", locals: { record: record, namespace: namespace }
|
135
|
+
end
|
136
|
+
|
137
|
+
def formatted_error(error)
|
138
|
+
return error.cause.message.split("\n").join(', ') if error.try(:cause)&.try(:message).present?
|
139
|
+
return error.message if error.try(:message).present?
|
140
|
+
|
141
|
+
I18n.t('active_element.unexpected_error')
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveElement
|
4
|
+
module DefaultController
|
5
|
+
# Provides permitted parameters for fields generated from a JSON schema file.
|
6
|
+
class JsonParams
|
7
|
+
def initialize(schema:)
|
8
|
+
@base_schema = schema
|
9
|
+
end
|
10
|
+
|
11
|
+
def params(schema = base_schema)
|
12
|
+
return simple_object_field(schema) if simple_object_field?(schema)
|
13
|
+
return simple_array_field(schema) if simple_array_field?(schema)
|
14
|
+
return complex_array_field(schema) if complex_array_field?(schema)
|
15
|
+
|
16
|
+
schema[:name]
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :fields, :base_schema
|
22
|
+
|
23
|
+
def simple_object_field(schema)
|
24
|
+
schema.key?(:name) ? { schema[:name] => {} } : {}
|
25
|
+
end
|
26
|
+
|
27
|
+
def simple_array_field(schema)
|
28
|
+
schema.key?(:name) ? { schema[:name] => [] } : []
|
29
|
+
end
|
30
|
+
|
31
|
+
def simple_object_field?(schema)
|
32
|
+
schema[:type] == 'object'
|
33
|
+
end
|
34
|
+
|
35
|
+
def simple_array_field?(schema)
|
36
|
+
schema[:type] == 'array' && schema.dig(:shape, :type) != 'object'
|
37
|
+
end
|
38
|
+
|
39
|
+
def complex_array_field?(schema)
|
40
|
+
schema[:type] == 'array' && schema.dig(:shape, :type) == 'object'
|
41
|
+
end
|
42
|
+
|
43
|
+
def complex_array_field(schema)
|
44
|
+
schema.dig(:shape, :shape, :fields).map { |field| params(field) }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveElement
|
4
|
+
module DefaultController
|
5
|
+
# Provides params for ActiveRecord models when using the default boilerplate controller
|
6
|
+
# actions. Navigates input parameters and maps them to appropriate relations as needed.
|
7
|
+
class Params
|
8
|
+
def initialize(controller:, model:)
|
9
|
+
@controller = controller
|
10
|
+
@model = model
|
11
|
+
end
|
12
|
+
|
13
|
+
def params
|
14
|
+
with_transformed_relations(
|
15
|
+
controller.params.require(controller.controller_name.singularize)
|
16
|
+
.permit(*permitted_fields)
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :controller, :model
|
23
|
+
|
24
|
+
def with_transformed_relations(params)
|
25
|
+
params.to_h.to_h do |key, value|
|
26
|
+
next [key, value] unless relation?(key)
|
27
|
+
|
28
|
+
relation_param(key, value)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def permitted_fields
|
33
|
+
scalar, json = controller.active_element.state.editable_fields.partition do |field|
|
34
|
+
scalar?(field)
|
35
|
+
end
|
36
|
+
scalar + [json_params(json)]
|
37
|
+
end
|
38
|
+
|
39
|
+
def scalar?(field)
|
40
|
+
return true if relation?(field)
|
41
|
+
return true if %i[json jsonb].exclude?(column(field)&.type)
|
42
|
+
|
43
|
+
false
|
44
|
+
end
|
45
|
+
|
46
|
+
def json_params(fields)
|
47
|
+
# XXX: We assume all non-scalar fields are JSON fields, i.e. they must have a definition
|
48
|
+
# defined as `config/forms/<model>/<field>.yml`. If that file does not exist, allow
|
49
|
+
# Errno::ENOENT to raise to let the form submission fail and avoid losing data. This
|
50
|
+
# would need to be adjusted if we start allowing non-JSON nested fields in the default
|
51
|
+
# controller.
|
52
|
+
fields.index_with do |field|
|
53
|
+
DefaultController::JsonParams.new(schema: schema_for(field)).params
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def schema_for(field)
|
58
|
+
ActiveElement::Components::Util.json_schema(model: model, field: field)
|
59
|
+
end
|
60
|
+
|
61
|
+
def relation_param(key, value)
|
62
|
+
case relation(key).macro
|
63
|
+
when :belongs_to
|
64
|
+
belongs_to_param(key, value)
|
65
|
+
when :has_one
|
66
|
+
has_one_param(key, value)
|
67
|
+
when :has_many
|
68
|
+
has_many_param(key, value)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def belongs_to_param(key, value)
|
73
|
+
[relation(key).foreign_key, value]
|
74
|
+
end
|
75
|
+
|
76
|
+
def has_one_param(key, value) # rubocop:disable Naming/PredicateName
|
77
|
+
[relation(key).name, relation(key).klass.find_by(relation(key).klass.primary_key => value)]
|
78
|
+
end
|
79
|
+
|
80
|
+
def has_many_param(key, _value) # rubocop:disable Naming/PredicateName
|
81
|
+
[relation(key).name, relation(key).klass.where(relation(key).klass.primary_key => relation(key).value)]
|
82
|
+
end
|
83
|
+
|
84
|
+
def relation?(attribute)
|
85
|
+
relation(attribute.to_sym).present?
|
86
|
+
end
|
87
|
+
|
88
|
+
def relation(attribute)
|
89
|
+
model.reflect_on_association(attribute.to_sym)
|
90
|
+
end
|
91
|
+
|
92
|
+
def column(attribute)
|
93
|
+
model.columns.find { |column| column.name.to_s == attribute.to_s }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveElement
|
4
|
+
module DefaultController
|
5
|
+
# Full text search and datetime querying for DefaultController, provides full text search
|
6
|
+
# filters for all controllers with configured searchable fields. Includes support for querying
|
7
|
+
# across relations.
|
8
|
+
class Search
|
9
|
+
def initialize(controller:, model:)
|
10
|
+
@controller = controller
|
11
|
+
@model = model
|
12
|
+
end
|
13
|
+
|
14
|
+
def search_filters
|
15
|
+
@search_filters ||= controller.params.permit(*searchable_fields).transform_values do |value|
|
16
|
+
value.try(:compact_blank) || value
|
17
|
+
end.compact_blank
|
18
|
+
end
|
19
|
+
|
20
|
+
def text_search?
|
21
|
+
search_filters.present?
|
22
|
+
end
|
23
|
+
|
24
|
+
def text_search
|
25
|
+
conditions = search_filters.to_h.map do |key, value|
|
26
|
+
next relation_matches(key, value) if relation?(key)
|
27
|
+
next datetime_between(key, value) if datetime?(key)
|
28
|
+
|
29
|
+
model.arel_table[key].matches("#{value}%")
|
30
|
+
end
|
31
|
+
conditions[1..].reduce(conditions.first) do |accumulated, condition|
|
32
|
+
accumulated.and(condition)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def search_relations
|
37
|
+
search_filters.to_h.keys.map { |key| relation?(key) ? key.to_sym : nil }.compact
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
attr_reader :controller, :model
|
43
|
+
|
44
|
+
def searchable_fields
|
45
|
+
controller.active_element.state.searchable_fields.map do |field|
|
46
|
+
next field unless field.to_s.end_with?('_at')
|
47
|
+
|
48
|
+
{ field => %i[from to] }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def noop
|
53
|
+
Arel::Nodes::True.new.eq(Arel::Nodes::True.new)
|
54
|
+
end
|
55
|
+
|
56
|
+
def datetime?(key)
|
57
|
+
model.columns.find { |column| column.name.to_s == key.to_s }&.type == :datetime
|
58
|
+
end
|
59
|
+
|
60
|
+
def datetime_between(key, value)
|
61
|
+
return noop if value[:from].blank? && value[:to].blank?
|
62
|
+
|
63
|
+
model.arel_table[key].between(range_begin(value)...range_end(value))
|
64
|
+
end
|
65
|
+
|
66
|
+
def range_begin(value)
|
67
|
+
value[:from].present? ? Time.zone.parse(value[:from]) + timezone_offset : -Float::INFINITY
|
68
|
+
end
|
69
|
+
|
70
|
+
def range_end(value)
|
71
|
+
value[:to].present? ? Time.zone.parse(value[:to]) + timezone_offset : Float::INFINITY
|
72
|
+
end
|
73
|
+
|
74
|
+
def timezone_offset
|
75
|
+
controller.request.cookies['timezone_offset'].to_i.minutes
|
76
|
+
end
|
77
|
+
|
78
|
+
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?
|
86
|
+
|
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
|
94
|
+
end
|
95
|
+
|
96
|
+
def searchable_relation_fields(key)
|
97
|
+
Components::Util.relation_controller(model, controller, key)
|
98
|
+
&.active_element
|
99
|
+
&.state
|
100
|
+
&.fetch(:searchable_fields, []) || []
|
101
|
+
end
|
102
|
+
|
103
|
+
def relation?(attribute)
|
104
|
+
relation(attribute.to_sym).present?
|
105
|
+
end
|
106
|
+
|
107
|
+
def relation(attribute)
|
108
|
+
model.reflect_on_association(attribute.to_sym)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -1,137 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@controller = controller
|
9
|
-
end
|
10
|
-
|
11
|
-
def index
|
12
|
-
return render_forbidden(:listable) unless configured?(:listable)
|
13
|
-
|
14
|
-
controller.render 'active_element/default_views/index',
|
15
|
-
locals: {
|
16
|
-
collection: collection,
|
17
|
-
search_filters: default_text_search.search_filters
|
18
|
-
}
|
19
|
-
end
|
20
|
-
|
21
|
-
def show
|
22
|
-
return render_forbidden(:viewable) unless configured?(:viewable)
|
23
|
-
|
24
|
-
controller.render 'active_element/default_views/show', locals: { record: record }
|
25
|
-
end
|
26
|
-
|
27
|
-
def new
|
28
|
-
return render_forbidden(:editable) unless configured?(:editable)
|
29
|
-
|
30
|
-
controller.render 'active_element/default_views/new', locals: { record: model.new, namespace: namespace }
|
31
|
-
end
|
32
|
-
|
33
|
-
def create # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
34
|
-
return render_forbidden(:editable) unless configured?(:editable)
|
35
|
-
|
36
|
-
new_record = model.new(default_record_params.params)
|
37
|
-
# XXX: Ensure associations are applied - there must be a better way.
|
38
|
-
if new_record.save && new_record.reload.update(default_record_params.params)
|
39
|
-
controller.flash.notice = "#{new_record.model_name.to_s.titleize} created successfully."
|
40
|
-
controller.redirect_to record_path(new_record, :show).path
|
41
|
-
else
|
42
|
-
controller.flash.now.alert = "Failed to create #{model.name.to_s.titleize}."
|
43
|
-
controller.render 'active_element/default_views/new', locals: { record: new_record, namespace: namespace }
|
44
|
-
end
|
45
|
-
rescue ActiveRecord::RangeError => e
|
46
|
-
render_range_error(error: e, action: :new)
|
47
|
-
end
|
48
|
-
|
49
|
-
def edit
|
50
|
-
return render_forbidden(:editable) unless configured?(:editable)
|
51
|
-
|
52
|
-
controller.render 'active_element/default_views/edit', locals: { record: record, namespace: namespace }
|
53
|
-
end
|
54
|
-
|
55
|
-
def update # rubocop:disable Metrics/AbcSize
|
56
|
-
return render_forbidden(:editable) unless configured?(:editable)
|
57
|
-
|
58
|
-
if record.update(default_record_params.params)
|
59
|
-
controller.flash.notice = "#{record.model_name.to_s.titleize} updated successfully."
|
60
|
-
controller.redirect_to record_path(record, :show).path
|
61
|
-
else
|
62
|
-
controller.flash.now.alert = "Failed to update #{model.name.to_s.titleize}."
|
63
|
-
controller.render 'active_element/default_views/edit', locals: { record: record, namespace: namespace }
|
64
|
-
end
|
65
|
-
rescue ActiveRecord::RangeError => e
|
66
|
-
render_range_error(error: e, action: :edit)
|
67
|
-
end
|
68
|
-
|
69
|
-
def destroy
|
70
|
-
return render_forbidden(:deletable) unless configured?(:deletable)
|
71
|
-
|
72
|
-
record.destroy
|
73
|
-
controller.flash.notice = "Deleted #{record.model_name.to_s.titleize}."
|
74
|
-
controller.redirect_to record_path(model, :index).path
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
3
|
+
require_relative 'default_controller/controller'
|
4
|
+
require_relative 'default_controller/actions'
|
5
|
+
require_relative 'default_controller/params'
|
6
|
+
require_relative 'default_controller/json_params'
|
7
|
+
require_relative 'default_controller/search'
|
78
8
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
def configured?(type)
|
86
|
-
return state.deletable? if type == :deletable
|
87
|
-
|
88
|
-
state.public_send("#{type}_fields").present?
|
89
|
-
end
|
90
|
-
|
91
|
-
def state
|
92
|
-
@state ||= controller.active_element.state
|
93
|
-
end
|
94
|
-
|
95
|
-
def default_record_params
|
96
|
-
@default_record_params ||= ActiveElement::DefaultRecordParams.new(controller: controller, model: model)
|
97
|
-
end
|
98
|
-
|
99
|
-
def default_text_search
|
100
|
-
@default_text_search ||= ActiveElement::DefaultSearch.new(controller: controller, model: model)
|
101
|
-
end
|
102
|
-
|
103
|
-
def record_path(record, type = nil)
|
104
|
-
ActiveElement::Components::Util::RecordPath.new(record: record, controller: controller, type: type)
|
105
|
-
end
|
106
|
-
|
107
|
-
def namespace
|
108
|
-
controller.controller_path.rpartition('/').first.presence&.to_sym
|
109
|
-
end
|
110
|
-
|
111
|
-
def model
|
112
|
-
controller.controller_name.classify.constantize
|
113
|
-
end
|
114
|
-
|
115
|
-
def record
|
116
|
-
@record ||= model.find(controller.params[:id])
|
117
|
-
end
|
118
|
-
|
119
|
-
def collection
|
120
|
-
return model.all unless default_text_search.text_search?
|
121
|
-
|
122
|
-
model.left_outer_joins(default_text_search.search_relations).where(*default_text_search.text_search)
|
123
|
-
end
|
124
|
-
|
125
|
-
def render_range_error(error:, action:)
|
126
|
-
controller.flash.now.alert = formatted_error(error)
|
127
|
-
controller.render "active_element/default_views/#{action}", locals: { record: record, namespace: namespace }
|
128
|
-
end
|
129
|
-
|
130
|
-
def formatted_error(error)
|
131
|
-
return error.cause.message.split("\n").join(', ') if error.try(:cause)&.try(:message).present?
|
132
|
-
return error.message if error.try(:message).present?
|
133
|
-
|
134
|
-
I18n.t('active_element.unexpected_error')
|
135
|
-
end
|
9
|
+
module ActiveElement
|
10
|
+
# Provides default boilerplate functionality for quick setup of an application.
|
11
|
+
# Implements all standard Rails controller actions, provides parameter permitting of configured
|
12
|
+
# fields and text search functionality.
|
13
|
+
module DefaultController
|
136
14
|
end
|
137
15
|
end
|
data/lib/active_element.rb
CHANGED
@@ -15,8 +15,6 @@ require_relative 'active_element/controller_interface'
|
|
15
15
|
require_relative 'active_element/controller_state'
|
16
16
|
require_relative 'active_element/controller_action'
|
17
17
|
require_relative 'active_element/default_controller'
|
18
|
-
require_relative 'active_element/default_record_params'
|
19
|
-
require_relative 'active_element/default_search'
|
20
18
|
require_relative 'active_element/pre_render_processors'
|
21
19
|
require_relative 'active_element/rails_component'
|
22
20
|
require_relative 'active_element/route'
|
@@ -3,6 +3,7 @@
|
|
3
3
|
<%= File.read('app/assets/javascripts/active_element/setup.js') %>
|
4
4
|
<%= File.read('app/assets/javascripts/active_element/json_field.js') %>
|
5
5
|
<%= File.read('app/assets/javascripts/active_element/text_search_field.js') %>
|
6
|
+
<%= File.read('app/assets/javascripts/active_element/popover.js') %>
|
6
7
|
ActiveElement.debug = true;
|
7
8
|
ActiveElement.getRequestId = () => 'fake-request-id';
|
8
9
|
const textSearchNames = <%= text_search_names.to_json %>;
|
@@ -24,6 +25,7 @@ window.fetch = (_, { body }) => {
|
|
24
25
|
<style>
|
25
26
|
<%= application_css %>
|
26
27
|
</style>
|
28
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.9.2/umd/popper.min.js" integrity="sha512-2rNj2KJ+D8s1ceNasTIex6z4HWyOnEYLVC3FigGOmyQCZc2eBXKgOxQmo3oKLHyfcj53uz4QMsRCWNbLd32Q1g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
27
29
|
<script>
|
28
30
|
/*!
|
29
31
|
* Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com
|
@@ -1,18 +1,21 @@
|
|
1
1
|
# Introduction
|
2
2
|
|
3
|
-
_ActiveElement_
|
3
|
+
_ActiveElement_ is a [Ruby on Rails](https://rubyonrails.org/) framework primarily intended for building admin applications with minimal effort.
|
4
4
|
|
5
5
|
An [authorization framework](access-control.html) is provided, intended to work alongside existing frameworks such as [Devise](https://github.com/heartcombo/devise), [Pundit](https://github.com/varvet/pundit), and [CanCanCan](https://github.com/CanCanCommunity/cancancan).
|
6
6
|
|
7
|
+
_ActiveElement_ is designed to avoid the all-or-nothing approach that many frameworks provide, allowing you to build a fully functional administration tool in minutes, while still allowing you to customize every aspect of your application. _ActiveElement_ is just a _Rails_ application with extra features: when you need to build custom functionality, you do it the same way you would any other _Rails_ application and you can use as much or as little of _ActiveElement_ as you like.
|
8
|
+
|
9
|
+
Take a look at the [Setup Guide](setup.html) to build your first _ActiveElement_ application and see how easily you can mix in standard _Rails_ behaviours.
|
10
|
+
|
7
11
|
## Highlights
|
8
12
|
|
13
|
+
* Build an entire application by defining models and controllers with just a few lines of configuration in each controller.
|
14
|
+
* [Components](components.html) that can be re-used throughout your application.
|
9
15
|
* Feature-rich [forms](components/forms.html) including a powerful [JSON form field component](components/form-fields/json.html).
|
10
16
|
* Simple and secure [auto-suggest text search](components/form-fields/text-search.html) widgets.
|
11
|
-
* [Tables](components/tables.html) with built-in pagination and action buttons for viewing/editing/deleting records.
|
12
17
|
* [Decorators](decorators.html) for overriding default display fields with simple _Rails_ view partials.
|
13
|
-
* Automated [permissions](access-control/authorization/permissions.html) that can be applied to all application endpoints with minimal effort.
|
14
|
-
* Sensible defaults to help you build your application quickly while also allowing you to customize when needed.
|
15
|
-
* _ActiveElement_ attempts to provide a framework of familiar patterns that work with you instead of against you. It does not attempt to do everything for you and avoids behind-the-scenes magic where possible.
|
18
|
+
* Automated [route-based permissions](access-control/authorization/permissions.html) that can be applied to all application endpoints with minimal effort.
|
16
19
|
* [Bootstrap](https://getbootstrap.com/) styling with [customizable themes](themes.html).
|
17
20
|
|
18
21
|
See the [Setup Guide](setup.html) and browse the rest of the documentation for full usage examples.
|