linked_rails 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/linked_rails/bulk_controller.rb +26 -6
- data/app/controllers/linked_rails/enum_values_controller.rb +0 -42
- data/app/models/linked_rails/actions/item.rb +46 -30
- data/app/models/linked_rails/actions/list.rb +6 -25
- data/app/models/linked_rails/collection/configuration.rb +55 -0
- data/app/models/linked_rails/collection/filter.rb +1 -1
- data/app/models/linked_rails/collection/filter_field.rb +5 -1
- data/app/models/linked_rails/collection/filter_option.rb +1 -1
- data/app/models/linked_rails/collection/filterable.rb +3 -8
- data/app/models/linked_rails/collection/infinite.rb +102 -0
- data/app/models/linked_rails/collection/infinite_view.rb +1 -90
- data/app/models/linked_rails/collection/iri.rb +33 -52
- data/app/models/linked_rails/collection/iri_mapping.rb +14 -6
- data/app/models/linked_rails/collection/paginated.rb +45 -0
- data/app/models/linked_rails/collection/paginated_view.rb +1 -33
- data/app/models/linked_rails/collection/sortable.rb +1 -9
- data/app/models/linked_rails/collection/sorting.rb +1 -1
- data/app/models/linked_rails/collection/view.rb +15 -5
- data/app/models/linked_rails/collection.rb +33 -67
- data/app/models/linked_rails/creative_work.rb +1 -1
- data/app/models/linked_rails/entry_point.rb +8 -5
- data/app/models/linked_rails/enum_value.rb +39 -1
- data/app/models/linked_rails/form/field/resource_field.rb +2 -0
- data/app/models/linked_rails/form/field.rb +6 -8
- data/app/models/linked_rails/form/field_factory.rb +21 -10
- data/app/models/linked_rails/form/group.rb +2 -2
- data/app/models/linked_rails/form/page.rb +4 -4
- data/app/models/linked_rails/form.rb +13 -15
- data/app/models/linked_rails/manifest.rb +8 -2
- data/app/models/linked_rails/menus/item.rb +1 -1
- data/app/models/linked_rails/menus/list.rb +3 -2
- data/app/models/linked_rails/ontology/base.rb +1 -1
- data/app/models/linked_rails/ontology/class.rb +3 -3
- data/app/models/linked_rails/ontology.rb +5 -5
- data/app/models/linked_rails/sequence.rb +2 -2
- data/app/models/linked_rails/shacl/property_shape.rb +1 -1
- data/app/models/linked_rails/widget.rb +1 -1
- data/app/serializers/linked_rails/actions/item_serializer.rb +2 -1
- data/app/serializers/linked_rails/collection_serializer.rb +7 -2
- data/app/serializers/linked_rails/entry_point_serializer.rb +1 -1
- data/app/serializers/linked_rails/ontology_serializer.rb +2 -2
- data/lib/generators/linked_rails/install/install_generator.rb +5 -8
- data/lib/generators/linked_rails/install/templates/README +2 -0
- data/lib/generators/linked_rails/install/templates/app_menu_list.rb +36 -7
- data/lib/generators/linked_rails/install/templates/application_menu_list.rb +40 -1
- data/lib/generators/linked_rails/install/templates/initializer.rb +1 -2
- data/lib/generators/linked_rails/install/templates/locales.yml +12 -0
- data/lib/generators/linked_rails/install/templates/vocab.rb +1 -0
- data/lib/generators/linked_rails/install/templates/vocab.yml +2 -2
- data/lib/generators/linked_rails/model/model_generator.rb +0 -1
- data/lib/generators/linked_rails/model/templates/controller.rb.tt +5 -1
- data/lib/generators/linked_rails/model/templates/form.rb.tt +3 -0
- data/lib/generators/linked_rails/model/templates/menu_list.rb.tt +15 -0
- data/lib/generators/linked_rails/model/templates/policy.rb.tt +13 -0
- data/lib/generators/linked_rails/model/templates/serializer.rb.tt +1 -1
- data/lib/linked_rails/active_response/controller/collections.rb +1 -1
- data/lib/linked_rails/active_response/controller/crud_defaults.rb +3 -3
- data/lib/linked_rails/active_response/controller/params.rb +5 -5
- data/lib/linked_rails/active_response/controller.rb +11 -0
- data/lib/linked_rails/active_response/responders/rdf.rb +19 -10
- data/lib/linked_rails/callable_variable.rb +1 -1
- data/lib/linked_rails/collection_params_parser.rb +93 -0
- data/lib/linked_rails/controller/actionable.rb +118 -0
- data/lib/linked_rails/controller/authorization.rb +6 -0
- data/lib/linked_rails/controller/default_actions/create.rb +52 -0
- data/lib/linked_rails/controller/default_actions/destroy.rb +42 -0
- data/lib/linked_rails/controller/default_actions/update.rb +43 -0
- data/lib/linked_rails/controller.rb +20 -3
- data/lib/linked_rails/enhancements/creatable/controller.rb +1 -1
- data/lib/linked_rails/enhancements/destroyable/controller.rb +1 -1
- data/lib/linked_rails/enhancements/updatable/controller.rb +1 -1
- data/lib/linked_rails/enhancements.rb +0 -16
- data/lib/linked_rails/helpers/delta_helper.rb +26 -3
- data/lib/linked_rails/helpers/ontola_actions_helper.rb +2 -2
- data/lib/linked_rails/helpers/resource_helper.rb +1 -1
- data/lib/linked_rails/iri_mapper.rb +17 -39
- data/lib/linked_rails/middleware/linked_data_params.rb +7 -127
- data/lib/linked_rails/model/actionable.rb +69 -0
- data/lib/linked_rails/model/collections.rb +195 -39
- data/lib/linked_rails/model/dirty.rb +1 -1
- data/lib/linked_rails/model/enhancements.rb +1 -6
- data/lib/linked_rails/model/filtering.rb +2 -4
- data/lib/linked_rails/model/indexable.rb +5 -15
- data/lib/linked_rails/model/iri.rb +15 -17
- data/lib/linked_rails/model/iri_mapping.rb +35 -6
- data/lib/linked_rails/model/menuable.rb +34 -0
- data/lib/linked_rails/model/serialization.rb +0 -9
- data/lib/linked_rails/model/singularable.rb +55 -0
- data/lib/linked_rails/model/sorting.rb +0 -5
- data/lib/linked_rails/model/tables.rb +26 -0
- data/lib/linked_rails/model.rb +13 -5
- data/lib/linked_rails/params_parser.rb +131 -55
- data/lib/linked_rails/policy/attribute_conditions.rb +2 -2
- data/lib/linked_rails/policy.rb +24 -17
- data/lib/linked_rails/rdf_error.rb +2 -2
- data/lib/linked_rails/routes.rb +37 -22
- data/lib/linked_rails/serializer/actionable.rb +27 -0
- data/lib/linked_rails/serializer/menuable.rb +31 -0
- data/lib/linked_rails/serializer/singularable.rb +26 -0
- data/lib/linked_rails/serializer.rb +23 -10
- data/lib/linked_rails/test_methods.rb +114 -0
- data/lib/linked_rails/translate.rb +19 -9
- data/lib/linked_rails/uri_template.rb +30 -0
- data/lib/linked_rails/version.rb +1 -1
- data/lib/linked_rails/vocab.rb +8 -0
- data/lib/linked_rails.rb +25 -12
- data/lib/rails/welcome_controller.rb +3 -3
- data/lib/rdf/query_fix.rb +15 -0
- metadata +22 -25
- data/app/models/linked_rails/actions/default_actions/create.rb +0 -60
- data/app/models/linked_rails/actions/default_actions/destroy.rb +0 -45
- data/app/models/linked_rails/actions/default_actions/update.rb +0 -50
- data/app/models/linked_rails/actions/default_actions.rb +0 -17
- data/lib/generators/linked_rails/install/templates/application_action_list.rb +0 -3
- data/lib/generators/linked_rails/model/templates/action_list.rb.tt +0 -6
- data/lib/linked_rails/enhancements/actionable/model.rb +0 -71
- data/lib/linked_rails/enhancements/actionable/serializer.rb +0 -25
- data/lib/linked_rails/enhancements/creatable/action.rb +0 -15
- data/lib/linked_rails/enhancements/destroyable/action.rb +0 -15
- data/lib/linked_rails/enhancements/destroyable/routing.rb +0 -19
- data/lib/linked_rails/enhancements/indexable/model.rb +0 -10
- data/lib/linked_rails/enhancements/menuable/model.rb +0 -36
- data/lib/linked_rails/enhancements/menuable/serializer.rb +0 -33
- data/lib/linked_rails/enhancements/route_concerns.rb +0 -56
- data/lib/linked_rails/enhancements/singularable/controller.rb +0 -43
- data/lib/linked_rails/enhancements/singularable/model.rb +0 -47
- data/lib/linked_rails/enhancements/singularable/serializer.rb +0 -28
- data/lib/linked_rails/enhancements/tableable/model.rb +0 -28
- data/lib/linked_rails/enhancements/updatable/action.rb +0 -15
- data/lib/linked_rails/enhancements/updatable/routing.rb +0 -20
@@ -2,5 +2,18 @@
|
|
2
2
|
|
3
3
|
<% module_namespacing do -%>
|
4
4
|
class <%= class_name %>Policy < ApplicationPolicy
|
5
|
+
permit_attributes %i[<%= attributes.map { |attribute| attribute.name }.join(' ') %>]
|
6
|
+
|
7
|
+
def create?
|
8
|
+
false
|
9
|
+
end
|
10
|
+
|
11
|
+
def update?
|
12
|
+
false
|
13
|
+
end
|
14
|
+
|
15
|
+
def destroy?
|
16
|
+
false
|
17
|
+
end
|
5
18
|
end
|
6
19
|
<% end -%>
|
@@ -3,7 +3,7 @@
|
|
3
3
|
<% module_namespacing do -%>
|
4
4
|
class <%= class_name %>Serializer < ApplicationSerializer
|
5
5
|
<% attributes.each do |attribute| -%>
|
6
|
-
|
6
|
+
attribute :<%= attribute.name %>, predicate: Vocab.app[:<%= attribute.name %>]
|
7
7
|
<% end -%>
|
8
8
|
end
|
9
9
|
<% end -%>
|
@@ -10,8 +10,8 @@ module LinkedRails
|
|
10
10
|
current_resource!
|
11
11
|
.class
|
12
12
|
.try(:reflect_on_all_associations)
|
13
|
-
|
14
|
-
|
13
|
+
&.select(&:collection?)
|
14
|
+
&.each { |association| current_resource!.association(association.name).reset }
|
15
15
|
end
|
16
16
|
|
17
17
|
def create_execute
|
@@ -50,7 +50,7 @@ module LinkedRails
|
|
50
50
|
def requested_resource
|
51
51
|
@requested_resource ||= controller_class.try(
|
52
52
|
:requested_resource,
|
53
|
-
LinkedRails.iri_mapper.route_params_to_opts(params.dup),
|
53
|
+
LinkedRails.iri_mapper.route_params_to_opts(params.dup, request.original_url),
|
54
54
|
user_context
|
55
55
|
)
|
56
56
|
end
|
@@ -11,13 +11,11 @@ module LinkedRails
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def params_parser
|
14
|
-
@params_parser ||= LinkedRails::ParamsParser.new(params)
|
14
|
+
@params_parser ||= LinkedRails::ParamsParser.new(params: params, user_context: user_context)
|
15
15
|
end
|
16
16
|
|
17
17
|
def parsed_filter_params
|
18
|
-
|
19
|
-
|
20
|
-
ActionController::Parameters.new(controller_class.try(:attributes_from_filters, filters) || {})
|
18
|
+
params_parser.attributes_from_filters(controller_class)
|
21
19
|
end
|
22
20
|
|
23
21
|
def permit_filter_params
|
@@ -37,7 +35,9 @@ module LinkedRails
|
|
37
35
|
end
|
38
36
|
|
39
37
|
def permit_params_with_filters
|
40
|
-
permit_filter_params.merge(permit_params.to_h)
|
38
|
+
permitted = permit_filter_params.merge(permit_params.to_h)
|
39
|
+
permitted[:singular_resource] = true if params[:singular_route].to_s == 'true'
|
40
|
+
permitted
|
41
41
|
end
|
42
42
|
|
43
43
|
def resource_params
|
@@ -33,5 +33,16 @@ module LinkedRails
|
|
33
33
|
def serializer_params
|
34
34
|
{}
|
35
35
|
end
|
36
|
+
|
37
|
+
def success_message_translation_key
|
38
|
+
"actions.#{Translate.translation_key(controller_class)}.#{action_name}.success"
|
39
|
+
end
|
40
|
+
|
41
|
+
def success_message_translation_opts
|
42
|
+
{
|
43
|
+
default: [:"actions.default.#{action_name}.success", ''],
|
44
|
+
type: I18n.t("#{controller_class.model_name.collection}.type").capitalize
|
45
|
+
}
|
46
|
+
end
|
36
47
|
end
|
37
48
|
end
|
@@ -37,7 +37,7 @@ module LinkedRails
|
|
37
37
|
response_headers(opts)
|
38
38
|
controller.respond_with_resource(
|
39
39
|
resource: nil,
|
40
|
-
meta: error_meta(opts[:resource]),
|
40
|
+
meta: error_meta(opts[:resource], opts[:errors] || {}),
|
41
41
|
status: :unprocessable_entity
|
42
42
|
)
|
43
43
|
end
|
@@ -47,15 +47,22 @@ module LinkedRails
|
|
47
47
|
controller.respond_with_resource(opts)
|
48
48
|
end
|
49
49
|
|
50
|
-
def redirect(**opts) # rubocop:disable Metrics/AbcSize
|
50
|
+
def redirect(**opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
51
51
|
return super if controller.request.head?
|
52
52
|
|
53
|
+
opts[:status] ||= 200
|
54
|
+
|
53
55
|
response_headers(opts)
|
54
56
|
add_exec_action_header(
|
55
57
|
controller.response.headers,
|
56
58
|
ontola_redirect_action(opts[:location], reload: opts[:reload])
|
57
59
|
)
|
58
|
-
|
60
|
+
if opts[:meta].blank? || head_request?
|
61
|
+
controller.head(opts[:status], location: opts[:location], content_type: content_type)
|
62
|
+
else
|
63
|
+
opts[format] = []
|
64
|
+
controller.render opts
|
65
|
+
end
|
59
66
|
end
|
60
67
|
|
61
68
|
def resource(**opts) # rubocop:disable Metrics/AbcSize
|
@@ -89,10 +96,10 @@ module LinkedRails
|
|
89
96
|
]
|
90
97
|
end
|
91
98
|
|
92
|
-
def error_statements(iri, resource)
|
99
|
+
def error_statements(iri, resource, resource_errors)
|
93
100
|
index = 0
|
94
|
-
|
95
|
-
predicate = resource
|
101
|
+
resource_errors.map do |key, values|
|
102
|
+
predicate = resource&.class&.predicate_for_key(key.to_s.split('.').first)
|
96
103
|
if predicate
|
97
104
|
error_statements_for(iri, predicate, values)
|
98
105
|
else
|
@@ -106,16 +113,18 @@ module LinkedRails
|
|
106
113
|
values.map { |value| [iri, predicate, value.sub(/\S/, &:upcase)] }
|
107
114
|
end
|
108
115
|
|
109
|
-
def error_meta(resource)
|
116
|
+
def error_meta(resource, custom_errors)
|
110
117
|
form_iri = controller.request.headers['Request-Referrer']
|
111
|
-
|
118
|
+
resource_errors = (resource.try(:errors)&.messages || {}).merge(custom_errors)
|
119
|
+
|
120
|
+
return [] unless form_iri && resource_errors.present?
|
112
121
|
|
113
122
|
error_object = ::RDF::Node.new
|
114
123
|
[
|
115
124
|
error_mapping(form_iri, error_object),
|
116
125
|
error_type(error_object),
|
117
126
|
error_status(error_object)
|
118
|
-
] + error_statements(error_object, resource)
|
127
|
+
] + error_statements(error_object, resource, resource_errors)
|
119
128
|
end
|
120
129
|
|
121
130
|
def error_type(error_object)
|
@@ -149,7 +158,7 @@ module LinkedRails
|
|
149
158
|
error_statements_for(
|
150
159
|
iri,
|
151
160
|
::RDF["_#{index}"],
|
152
|
-
values.map { |value| resource
|
161
|
+
values.map { |value| resource&.errors&.full_message(key, value) || value }
|
153
162
|
)
|
154
163
|
end
|
155
164
|
end
|
@@ -5,7 +5,7 @@ module LinkedRails
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
-
def callable_variable(method, opts
|
8
|
+
def callable_variable(method, **opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
9
9
|
variable ||= opts[:variable] || method
|
10
10
|
default ||= opts[:default]
|
11
11
|
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LinkedRails
|
4
|
+
class CollectionParamsParser
|
5
|
+
attr_reader :params, :user_context
|
6
|
+
|
7
|
+
def initialize(params)
|
8
|
+
@user_context = params[:user_context]
|
9
|
+
@params = params.is_a?(Hash) ? ActionController::Parameters.new(params) : params
|
10
|
+
end
|
11
|
+
|
12
|
+
def collection_params
|
13
|
+
return @collection_params if instance_variable_defined?(:@collection_params)
|
14
|
+
|
15
|
+
values = permit_params(:display, :page_size, :title, :type)
|
16
|
+
|
17
|
+
filter = filter_params
|
18
|
+
values[:filter] = filter if filter
|
19
|
+
|
20
|
+
sort = sorting_params
|
21
|
+
values[:sort] = sort if sort
|
22
|
+
|
23
|
+
values[:user_context] = user_context if user_context
|
24
|
+
|
25
|
+
@collection_params = values
|
26
|
+
end
|
27
|
+
|
28
|
+
def collection_view_params
|
29
|
+
return @collection_view_params if instance_variable_defined?(:@collection_view_params)
|
30
|
+
|
31
|
+
values = permit_params(:page)
|
32
|
+
|
33
|
+
before = before_params
|
34
|
+
values[:before] = before if before
|
35
|
+
|
36
|
+
@collection_view_params = values
|
37
|
+
end
|
38
|
+
|
39
|
+
def filter_params # rubocop:disable Metrics/AbcSize
|
40
|
+
return @filter_params if instance_variable_defined?(:@filter_params)
|
41
|
+
|
42
|
+
values = permit_params(filter: [])[:filter] || permit_params(filter: {})[:filter]
|
43
|
+
return @filter_params = values if values.is_a?(Hash)
|
44
|
+
return @filter_params = {} if values.blank?
|
45
|
+
|
46
|
+
@filter_params = values.each_with_object({}) do |f, hash|
|
47
|
+
values = f.split('=')
|
48
|
+
key = RDF::URI(CGI.unescape(values.first))
|
49
|
+
hash[key] ||= []
|
50
|
+
hash[key] << CGI.unescape(values.second)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def before_params
|
57
|
+
return @before_params if instance_variable_defined?(:@before_params)
|
58
|
+
|
59
|
+
values = permit_params(before: [])[:before]
|
60
|
+
return @before_params = nil if values.blank?
|
61
|
+
|
62
|
+
@before_params = values.map do |f|
|
63
|
+
key, value = f.split('=')
|
64
|
+
{key: RDF::URI(CGI.unescape(key)), value: value}
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def parse_filter_value(value)
|
69
|
+
return value if value.is_a?(Hash)
|
70
|
+
|
71
|
+
key, value = value.split('=')
|
72
|
+
{key: RDF::URI(CGI.unescape(key)), direction: value}
|
73
|
+
end
|
74
|
+
|
75
|
+
def permit_params(*keys)
|
76
|
+
params
|
77
|
+
.permit(*keys)
|
78
|
+
.to_h
|
79
|
+
.with_indifferent_access
|
80
|
+
end
|
81
|
+
|
82
|
+
def sorting_params
|
83
|
+
return @sorting_params if instance_variable_defined?(:@sorting_params)
|
84
|
+
|
85
|
+
values = permit_params(sort: [])[:sort]
|
86
|
+
return @sorting_params = nil if values.blank?
|
87
|
+
|
88
|
+
@sorting_params = values.map do |f|
|
89
|
+
parse_filter_value(f)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'default_actions/create'
|
4
|
+
require_relative 'default_actions/destroy'
|
5
|
+
require_relative 'default_actions/update'
|
6
|
+
|
7
|
+
module LinkedRails
|
8
|
+
module Controller
|
9
|
+
module Actionable
|
10
|
+
ACTION_METHOD_MAPPING = {
|
11
|
+
execute: :execute,
|
12
|
+
on_failure: :failure,
|
13
|
+
on_success: :success
|
14
|
+
}.freeze
|
15
|
+
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
|
18
|
+
included do
|
19
|
+
extend LinkedRails::Controller::DefaultActions::Create
|
20
|
+
extend LinkedRails::Controller::DefaultActions::Destroy
|
21
|
+
extend LinkedRails::Controller::DefaultActions::Update
|
22
|
+
end
|
23
|
+
|
24
|
+
class_methods do
|
25
|
+
def has_collection_action(action, **opts)
|
26
|
+
has_action(:collection, action, **opts)
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_singular_action(action, **opts)
|
30
|
+
has_action(:singular, action, **opts)
|
31
|
+
end
|
32
|
+
|
33
|
+
def has_resource_action(action, **opts)
|
34
|
+
has_action(:resource, action, **opts)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# @param [:resource, :singular, :collection] type To which action list the action is added
|
40
|
+
# @param [symbol] action_key A key to identify the action
|
41
|
+
# @param [Hash] opts The options for the action
|
42
|
+
# @option opts [symbol] :action_name The name for the controller action for GETting this action.
|
43
|
+
# A route is automatically generated. Defaults to the action key
|
44
|
+
# @option opts [proc<string>, string] :action_path The path to be appended to the resource iri.
|
45
|
+
# Defaults to "#{action_key}_action"
|
46
|
+
# @option opts [proc<string>, string] :description The description of this action
|
47
|
+
# @option opts [symbol] :execute The controller method to use when this action is executed.
|
48
|
+
# Defaults to "#{action}_execute"
|
49
|
+
# @option opts [proc<boolean>, boolean] :favorite Whether or not this is a favorite action
|
50
|
+
# @option opts [proc<LinkedRails::Form>, LinkedRails::Form] :form The form for this action
|
51
|
+
# @option opts [proc<string>, string] :http_method The method to be used in the request.
|
52
|
+
# GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH
|
53
|
+
# Defaults to POST
|
54
|
+
# @option opts [proc<string>, string] :image A fontawesome icon, prepended with 'fa-'
|
55
|
+
# @option opts [boolean] :inherit Whether descendant controllers should clone this action.
|
56
|
+
# Defaults to true
|
57
|
+
# @option opts [boolean] :one_click Whether the action can be executed with one click.
|
58
|
+
# Defaults to false
|
59
|
+
# @option opts [proc<string>, string] :label The label of the action
|
60
|
+
# @option opts [LinkedRails::Model] :object The object on which the action will be executed
|
61
|
+
# @option opts [symbol] :on_failure Callback when the execute method failed.
|
62
|
+
# Defaults to "#{action}_failure"
|
63
|
+
# @option opts [symbol] :on_success Callback when the execute method succeeded.
|
64
|
+
# Defaults to "#{action}_success"
|
65
|
+
# @option opts [symbol] :policy The method to be called on the policy of the policy_resource
|
66
|
+
# @option opts [Array<symbol>] :policy_arguments The arguments passed to the policy method
|
67
|
+
# @option opts [proc<LinkedRails::Model>, LinkedRails::Model] :policy_resource
|
68
|
+
# The resource on which a policy needs to be authorized
|
69
|
+
# @option opts [proc<RDF::URI>, RDF::URI] :predicate The relation between the resource and the action
|
70
|
+
# @option opts [proc<LinkedRails::Model>, LinkedRails::Model] :result
|
71
|
+
# @option opts [proc<string>, string] :submit_label The label for the submit button of the action
|
72
|
+
# @option opts [proc<string>, string] :target_url The url whereto the request will be made.
|
73
|
+
# Use target_path when the url starts with the iri of the resource, so a route can be generated.
|
74
|
+
# @option opts [string] :target_path The path appended to the iri of the resource to determine
|
75
|
+
# the url whereto the request will be made. A route is automatically generated.
|
76
|
+
# Defaults to the action key
|
77
|
+
# @option opts [array<RDF::URI>, RDF::URI] :type The rdf type of the action.
|
78
|
+
# Defaults to schema.UpdateAction
|
79
|
+
def has_action(type, action_key, opts)
|
80
|
+
opts[:action_name] ||= "#{action_key}_action"
|
81
|
+
opts[:http_method] ||= 'POST'
|
82
|
+
opts[:target_path] ||= action_key if opts[:target_url].blank?
|
83
|
+
|
84
|
+
active_response(action_key, opts[:action_name])
|
85
|
+
define_action_show_method(action_key, opts)
|
86
|
+
define_action_execute_methods(action_key, opts)
|
87
|
+
controller_class.action_list.defined_actions[type][action_key] = opts
|
88
|
+
end
|
89
|
+
|
90
|
+
def define_action_execute_methods(action_key, opts)
|
91
|
+
ACTION_METHOD_MAPPING.each do |key, value|
|
92
|
+
define_action_execute_method(action_key, key, value, opts)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def define_action_execute_method(action, key, suffix, opts)
|
97
|
+
call_method = opts.delete(key)
|
98
|
+
method_name = :"#{action}_#{suffix}"
|
99
|
+
return if call_method.blank? || method_name == call_method
|
100
|
+
|
101
|
+
define_method(method_name) do
|
102
|
+
if call_method.is_a?(Symbol)
|
103
|
+
send(call_method)
|
104
|
+
else
|
105
|
+
instance_exec(&call_method)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def define_action_show_method(_action, opts)
|
111
|
+
define_method("#{opts[:action_name]}_success") do
|
112
|
+
respond_with_resource(show_success_options)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -6,10 +6,16 @@ module LinkedRails
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
def authorize_action
|
9
|
+
return authorize_action_item if current_resource!.is_a?(LinkedRails.actions_item_class)
|
10
|
+
|
9
11
|
query = action_name == 'index' ? :show? : "#{params[:action].chomp('!')}?"
|
10
12
|
|
11
13
|
authorize(current_resource!, query)
|
12
14
|
end
|
15
|
+
|
16
|
+
def authorize_action_item
|
17
|
+
authorize(current_resource!.resource, :show?) if current_resource!.resource
|
18
|
+
end
|
13
19
|
end
|
14
20
|
end
|
15
21
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LinkedRails
|
4
|
+
module Controller
|
5
|
+
module DefaultActions
|
6
|
+
module Create
|
7
|
+
def has_collection_create_action(**overwrite)
|
8
|
+
has_collection_action(:create, **create_collection_options(**overwrite))
|
9
|
+
end
|
10
|
+
|
11
|
+
def has_singular_create_action(**overwrite)
|
12
|
+
has_singular_action(:create, **create_singular_options(**overwrite))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def create_collection_options(**overwrite)
|
18
|
+
default_create_options(
|
19
|
+
form: -> { resource.association_class.try(:form_class) },
|
20
|
+
object: -> { resource.build_child },
|
21
|
+
policy: :create_child?
|
22
|
+
).merge(overwrite)
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_singular_options(**overwrite)
|
26
|
+
default_create_options(
|
27
|
+
form: -> { resource.class.try(:form_class) },
|
28
|
+
object: -> { resource },
|
29
|
+
policy: :create?
|
30
|
+
).merge(overwrite)
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_create_options(**overwrite) # rubocop:disable Metrics/MethodLength
|
34
|
+
{
|
35
|
+
action_name: :new,
|
36
|
+
action_path: :new,
|
37
|
+
execute: :create_execute,
|
38
|
+
http_method: :post,
|
39
|
+
image: 'fa-plus',
|
40
|
+
on_failure: :create_failure,
|
41
|
+
on_success: :create_success,
|
42
|
+
result: -> { result_class },
|
43
|
+
target_path: '',
|
44
|
+
type: lambda {
|
45
|
+
[Vocab.ontola["Create::#{result_class}"], Vocab.schema.CreateAction]
|
46
|
+
}
|
47
|
+
}.merge(overwrite)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LinkedRails
|
4
|
+
module Controller
|
5
|
+
module DefaultActions
|
6
|
+
module Destroy
|
7
|
+
def has_resource_destroy_action(**overwrite)
|
8
|
+
has_resource_action(:destroy, **destroy_resource_options(**overwrite))
|
9
|
+
end
|
10
|
+
|
11
|
+
def has_singular_destroy_action(**overwrite)
|
12
|
+
has_singular_action(:destroy, **destroy_singular_options(**overwrite))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def destroy_resource_options(**overwrite)
|
18
|
+
default_destroy_options(**overwrite)
|
19
|
+
end
|
20
|
+
|
21
|
+
def destroy_singular_options(**overwrite)
|
22
|
+
default_destroy_options(**overwrite)
|
23
|
+
end
|
24
|
+
|
25
|
+
def default_destroy_options(**overwrite) # rubocop:disable Metrics/MethodLength
|
26
|
+
{
|
27
|
+
action_name: :delete,
|
28
|
+
action_path: :delete,
|
29
|
+
execute: :destroy_execute,
|
30
|
+
http_method: :delete,
|
31
|
+
image: 'fa-close',
|
32
|
+
on_failure: :destroy_failure,
|
33
|
+
on_success: :destroy_success,
|
34
|
+
policy: :destroy?,
|
35
|
+
target_path: '',
|
36
|
+
type: [Vocab.schema.Action, Vocab.ontola[:DestroyAction]]
|
37
|
+
}.merge(overwrite)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LinkedRails
|
4
|
+
module Controller
|
5
|
+
module DefaultActions
|
6
|
+
module Update
|
7
|
+
def has_resource_update_action(**overwrite)
|
8
|
+
has_resource_action(:update, **update_resource_options(**overwrite))
|
9
|
+
end
|
10
|
+
|
11
|
+
def has_singular_update_action(**overwrite)
|
12
|
+
has_singular_action(:update, **update_singular_options(**overwrite))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def update_resource_options(**overwrite)
|
18
|
+
default_update_options(**overwrite)
|
19
|
+
end
|
20
|
+
|
21
|
+
def update_singular_options(**overwrite)
|
22
|
+
default_update_options(**overwrite)
|
23
|
+
end
|
24
|
+
|
25
|
+
def default_update_options(**overwrite) # rubocop:disable Metrics/MethodLength
|
26
|
+
{
|
27
|
+
action_name: :edit,
|
28
|
+
action_path: :edit,
|
29
|
+
execute: :update_execute,
|
30
|
+
form: -> { resource.class.try(:form_class) },
|
31
|
+
http_method: :put,
|
32
|
+
image: 'fa-pencil-square-o',
|
33
|
+
on_failure: :update_failure,
|
34
|
+
on_success: :update_success,
|
35
|
+
policy: :update?,
|
36
|
+
target_path: '',
|
37
|
+
type: Vocab.schema.UpdateAction
|
38
|
+
}.merge(overwrite)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,19 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'active_response/controller'
|
4
|
+
require_relative 'controller/actionable'
|
4
5
|
require_relative 'controller/authorization'
|
5
6
|
require_relative 'controller/error_handling'
|
6
7
|
|
7
8
|
module LinkedRails
|
8
9
|
module Controller
|
9
10
|
extend ActiveSupport::Concern
|
11
|
+
include ::ActiveResponse::Controller
|
10
12
|
|
11
13
|
included do
|
12
14
|
extend Enhanceable
|
13
15
|
|
14
16
|
enhanceable :controller_class, :Controller
|
15
17
|
|
18
|
+
include ActionController::MimeResponds
|
16
19
|
include LinkedRails::ActiveResponse::Controller
|
20
|
+
include LinkedRails::Controller::Actionable
|
17
21
|
include LinkedRails::Controller::Authorization
|
18
22
|
include LinkedRails::Controller::ErrorHandling
|
19
23
|
include LinkedRails::Helpers::OntolaActionsHelper
|
@@ -34,9 +38,22 @@ module LinkedRails
|
|
34
38
|
end
|
35
39
|
|
36
40
|
module ClassMethods
|
37
|
-
def controller_class
|
38
|
-
|
39
|
-
|
41
|
+
def controller_class(klass = nil)
|
42
|
+
if klass.present?
|
43
|
+
@controller_class = klass
|
44
|
+
else
|
45
|
+
@controller_class ||= compute_controller_class || superclass.try(:controller_class)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def compute_controller_class
|
52
|
+
klass =
|
53
|
+
name.sub(/Controller$/, '').classify.safe_constantize ||
|
54
|
+
try(:controller_name)&.classify&.safe_constantize
|
55
|
+
|
56
|
+
klass if klass&.include?(LinkedRails::Model)
|
40
57
|
end
|
41
58
|
end
|
42
59
|
end
|
@@ -1,22 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative 'enhancements/route_concerns'
|
4
|
-
|
5
|
-
require_relative 'enhancements/actionable/model'
|
6
|
-
require_relative 'enhancements/actionable/serializer'
|
7
|
-
require_relative 'enhancements/creatable/action'
|
8
3
|
require_relative 'enhancements/creatable/controller'
|
9
|
-
require_relative 'enhancements/destroyable/action'
|
10
4
|
require_relative 'enhancements/destroyable/controller'
|
11
|
-
require_relative 'enhancements/destroyable/routing'
|
12
|
-
require_relative 'enhancements/indexable/model'
|
13
|
-
require_relative 'enhancements/menuable/model'
|
14
|
-
require_relative 'enhancements/menuable/serializer'
|
15
|
-
require_relative 'enhancements/singularable/controller'
|
16
|
-
require_relative 'enhancements/singularable/model'
|
17
|
-
require_relative 'enhancements/singularable/serializer'
|
18
|
-
require_relative 'enhancements/tableable/model'
|
19
|
-
require_relative 'enhancements/updatable/action'
|
20
5
|
require_relative 'enhancements/updatable/controller'
|
21
|
-
require_relative 'enhancements/updatable/routing'
|
22
6
|
require_relative 'enhancements/updatable/serializer'
|