Class: Jav::BaseResource
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#apply_pagination, #pagination_type
#get_stimulus_controllers, #stimulus_data_attributes
#has_show_controls?, #render_show_controls
#with_new_items
#fields, #get_field, #get_field_definitions, #get_fields, #get_items, #get_tabs, #hydrate_fields, #tab_groups, #tools
Constructor Details
Returns a new instance of BaseResource.
141
142
143
144
145
146
|
# File 'lib/jav/base_resource.rb', line 141
def initialize
return if self.class.model_class.present?
return unless model_class.present? && model_class.respond_to?(:base_class)
self.class.model_class = model_class.base_class
end
|
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
24
25
26
|
# File 'lib/jav/base_resource.rb', line 24
def params
@params
end
|
#reflection ⇒ Object
Returns the value of attribute reflection.
24
25
26
|
# File 'lib/jav/base_resource.rb', line 24
def reflection
@reflection
end
|
#user ⇒ Object
Returns the value of attribute user.
24
25
26
|
# File 'lib/jav/base_resource.rb', line 24
def user
@user
end
|
#view ⇒ Object
Returns the value of attribute view.
24
25
26
|
# File 'lib/jav/base_resource.rb', line 24
def view
@view
end
|
Class Method Details
.action(action_class, arguments: {}) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/jav/base_resource.rb', line 69
def action(action_class, arguments: {})
self.actions_loader ||= Jav::Loaders::Loader.new
action = { class: action_class, arguments: arguments }
self.actions_loader.use action
end
|
.filter(filter_class, arguments: {}) ⇒ Object
76
77
78
79
80
81
|
# File 'lib/jav/base_resource.rb', line 76
def filter(filter_class, arguments: {})
self.filters_loader ||= Jav::Loaders::Loader.new
filter = { class: filter_class, arguments: arguments }
self.filters_loader.use filter
end
|
.find_scope ⇒ Object
This resolves the scope when finding records (not "where" queries)
97
98
99
100
101
|
# File 'lib/jav/base_resource.rb', line 97
def find_scope
final_scope = resolve_find_scope.present? ? resolve_find_scope.call(model_class: model_class) : model_class
authorization.apply_policy final_scope
end
|
.get_available_models ⇒ Object
130
131
132
|
# File 'lib/jav/base_resource.rb', line 130
def get_available_models
ApplicationRecord.descendants
end
|
.get_record_associations(record) ⇒ Object
113
114
115
|
# File 'lib/jav/base_resource.rb', line 113
def get_record_associations(record)
record.class.reflections
end
|
.grid(&block) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/jav/base_resource.rb', line 62
def grid(&block)
grid_collector = GridCollector.new
grid_collector.instance_eval(&block)
self.grid_loader = grid_collector
end
|
.order_actions ⇒ Object
107
108
109
110
111
|
# File 'lib/jav/base_resource.rb', line 107
def order_actions
return {} if ordering.blank?
ordering[:actions] || {}
end
|
.query_scope ⇒ Object
This resolves the scope when doing "where" queries (not find queries)
90
91
92
93
94
|
# File 'lib/jav/base_resource.rb', line 90
def query_scope
final_scope = resolve_query_scope.present? ? resolve_query_scope.call(model_class: model_class) : model_class
authorization.apply_policy final_scope
end
|
.scope ⇒ Object
This is the search_query scope
This should be removed and passed to the search block
85
86
87
|
# File 'lib/jav/base_resource.rb', line 85
def scope
query_scope
end
|
.valid_association_name(record, association_name) ⇒ Object
117
118
119
120
121
|
# File 'lib/jav/base_resource.rb', line 117
def valid_association_name(record, association_name)
get_record_associations(record).keys.find do |name|
name == association_name
end
end
|
.valid_attachment_name(record, association_name) ⇒ Object
123
124
125
126
127
128
|
# File 'lib/jav/base_resource.rb', line 123
def valid_attachment_name(record, association_name)
association_exists = get_record_associations(record).keys.any? do |name|
["#{association_name}_attachment", "#{association_name}_attachments"].include?(name)
end
association_name if association_exists
end
|
.valid_model_class(model_class) ⇒ Object
134
135
136
137
138
|
# File 'lib/jav/base_resource.rb', line 134
def valid_model_class(model_class)
get_available_models.find do |m|
m.to_s == model_class.to_s
end
end
|
Instance Method Details
#attachment_fields ⇒ Object
303
304
305
306
307
|
# File 'lib/jav/base_resource.rb', line 303
def attachment_fields
get_field_definitions.select do |field|
[Jav::Fields::FileField, Jav::Fields::FilesField].include? field.class
end
end
|
#authorization(user: nil) ⇒ Object
337
338
339
340
|
# File 'lib/jav/base_resource.rb', line 337
def authorization(user: nil)
current_user = user || Jav::App.current_user
Jav::Services::AuthorizationService.new(current_user, model || model_class, policy_class: authorization_policy)
end
|
#available_view_types ⇒ Object
294
295
296
297
298
299
300
301
|
# File 'lib/jav/base_resource.rb', line 294
def available_view_types
view_types = [:table]
view_types << :grid if get_grid_fields.present?
view_types << :map if map_view.present?
view_types
end
|
#avatar ⇒ Object
444
445
446
447
448
449
450
451
452
|
# File 'lib/jav/base_resource.rb', line 444
def avatar
return avatar_field.to_image if avatar_field.respond_to? :to_image
return avatar_field.value.variant(resize_to_limit: [480, 480]) if avatar_field.type == "file"
avatar_field.value
rescue StandardError
nil
end
|
#avatar_field ⇒ Object
436
437
438
439
440
441
442
|
# File 'lib/jav/base_resource.rb', line 436
def avatar_field
get_field_definitions.find do |field|
field.as_avatar.present?
end
rescue StandardError
nil
end
|
#avatar_type ⇒ Object
454
455
456
457
458
|
# File 'lib/jav/base_resource.rb', line 454
def avatar_type
avatar_field.as_avatar
rescue StandardError
nil
end
|
#cache_hash(parent_model) ⇒ Object
356
357
358
359
360
361
362
|
# File 'lib/jav/base_resource.rb', line 356
def cache_hash(parent_model)
if parent_model.present?
[model, file_hash, parent_model]
else
[model, file_hash]
end
end
|
#class_name_without_resource ⇒ Object
208
209
210
|
# File 'lib/jav/base_resource.rb', line 208
def class_name_without_resource
self.class.name.demodulize.delete_suffix("Resource")
end
|
#default_panel_name ⇒ Object
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/jav/base_resource.rb', line 197
def default_panel_name
return @params[:related_name].capitalize if @params.present? && @params[:related_name].present?
case @view
when :show, :edit
model_title
when :new
t("jav.create_new_item", item: name.downcase).upcase_first
end
end
|
#description ⇒ Object
468
469
470
|
# File 'lib/jav/base_resource.rb', line 468
def description
description_field&.value
end
|
#description_field ⇒ Object
460
461
462
463
464
465
466
|
# File 'lib/jav/base_resource.rb', line 460
def description_field
get_field_definitions.find do |field|
field.as_description.present?
end
rescue StandardError
nil
end
|
#fields_by_database_id ⇒ Object
Map the received params to their actual fields.
310
311
312
313
314
315
316
|
# File 'lib/jav/base_resource.rb', line 310
def fields_by_database_id
get_field_definitions
.reject(&:computed)
.index_by do |field|
field.database_id.to_s
end
end
|
#file_hash ⇒ Object
342
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'lib/jav/base_resource.rb', line 342
def file_hash
content_to_be_hashed = ""
resource_path = Rails.root.join("app", "jav", "resources", "#{self.class.name.underscore}.rb").to_s
content_to_be_hashed += File.read(resource_path) if File.file? resource_path
policy_path = Rails.root.join("app", "policies", "#{self.class.name.underscore.gsub('_resource', '')}_policy.rb").to_s
content_to_be_hashed += File.read(policy_path) if File.file? policy_path
Digest::MD5.hexdigest(content_to_be_hashed)
end
|
#fill_model(model, params, extra_params: []) ⇒ Object
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
# File 'lib/jav/base_resource.rb', line 318
def fill_model(model, params, extra_params: [])
params.each do |key, value|
field = fields_by_database_id[key]
next if field.blank?
model = field.fill_field model, key, value, params
end
if .present?
model.assign_attributes params.permit()
end
model
end
|
#find_record(id, query: nil, params: nil) ⇒ Object
484
485
486
487
488
|
# File 'lib/jav/base_resource.rb', line 484
def find_record(id, query: nil, params: nil)
query ||= self.class.find_scope
self.class.find_record_method.call(model_class: query, id: id, params: params)
end
|
472
473
474
|
# File 'lib/jav/base_resource.rb', line 472
def form_scope
model_class.base_class.to_s.underscore.downcase
end
|
#get_action_arguments(action_class) ⇒ Object
191
192
193
194
195
|
# File 'lib/jav/base_resource.rb', line 191
def get_action_arguments(action_class)
action = get_actions.find { |act| act[:class].to_s == action_class.to_s }
action[:arguments]
end
|
#get_actions ⇒ Object
185
186
187
188
189
|
# File 'lib/jav/base_resource.rb', line 185
def get_actions
return [] if self.class.actions_loader.blank?
self.class.actions_loader.bag
end
|
#get_filter_arguments(filter_class) ⇒ Object
179
180
181
182
183
|
# File 'lib/jav/base_resource.rb', line 179
def get_filter_arguments(filter_class)
filter = get_filters.find { |flt| flt[:class] == filter_class.constantize }
filter[:arguments]
end
|
#get_filters ⇒ Object
173
174
175
176
177
|
# File 'lib/jav/base_resource.rb', line 173
def get_filters
return [] if self.class.filters_loader.blank?
self.class.filters_loader.bag
end
|
#get_grid_fields ⇒ Object
167
168
169
170
171
|
# File 'lib/jav/base_resource.rb', line 167
def get_grid_fields
return if self.class.grid_loader.blank?
self.class.grid_loader.hydrate(model: @model, view: @view, resource: self)
end
|
#has_model_id? ⇒ Boolean
480
481
482
|
# File 'lib/jav/base_resource.rb', line 480
def has_model_id?
model.present? && model.id.present?
end
|
#hydrate(model: nil, view: nil, user: nil, params: nil) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/jav/base_resource.rb', line 153
def hydrate(model: nil, view: nil, user: nil, params: nil)
@view = view if view.present?
@user = user if user.present?
@params = params if params.present?
if model.present?
@model = model
hydrate_model_with_default_values if @view == :new
end
self
end
|
#hydrate_model_with_default_values ⇒ Object
We will not overwrite any attributes that come pre-filled in the model.
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
# File 'lib/jav/base_resource.rb', line 365
def hydrate_model_with_default_values
default_values = get_fields.reject(&:computed).to_h do |field|
value = field.value
if field.type == "belongs_to"
reflection = @model.class.reflections[@params[:via_relation]]
if field.polymorphic_as.present? && field.types.map(&:to_s).include?(@params[:via_relation_class])
via_resource = ::Jav::App.get_resource_by_model_name(@params[:via_relation_class])
value = via_resource.find_record(@params[:via_resource_id])
elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s
resource = Jav::App.get_resource_by_model_name params[:via_relation_class]
model = resource.find_record @params[:via_resource_id], params: params
id_param = reflection.options[:primary_key] || :id
value = model.send(id_param)
end
end
[field, value]
end.compact_blank
default_values.each do |field, value|
field.assign_value record: @model, value: value
end
end
|
#label ⇒ Object
432
433
434
|
# File 'lib/jav/base_resource.rb', line 432
def label
label_field&.value || model_title
end
|
#label_field ⇒ Object
424
425
426
427
428
429
430
|
# File 'lib/jav/base_resource.rb', line 424
def label_field
get_field_definitions.find do |field|
field.as_label.present?
end
rescue StandardError
nil
end
|
#model_class ⇒ Object
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/jav/base_resource.rb', line 212
def model_class
return self.class.model_class if self.class.model_class.present?
return @model.base_class if @model.present?
class_name_without_resource.safe_constantize
end
|
#model_id ⇒ Object
223
224
225
|
# File 'lib/jav/base_resource.rb', line 223
def model_id
@model.send id
end
|
#model_key ⇒ Object
This is used as the model class ID
We use this instead of the route_key to maintain compatibility with uncountable models
With uncountable models route key appends an _index suffix (Fish->fish_index)
Example: User->users, MediaItem->media_items, Fish->fish
406
407
408
|
# File 'lib/jav/base_resource.rb', line 406
def model_key
model_class.model_name.plural
end
|
#model_title ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/jav/base_resource.rb', line 227
def model_title
return name if @model.nil?
case title
when Symbol
@model.send title
when Proc
Jav::ExecutionContext.new(target: title, resource: self, record: @model).handle
else
model_id
end
rescue StandardError
name
end
|
#name ⇒ Object
258
259
260
261
262
263
264
265
266
267
268
|
# File 'lib/jav/base_resource.rb', line 258
def name
default = class_name_without_resource.to_s.gsub('::', ' ').underscore.humanize
return @name if @name.present?
if translation_key && ::Jav::App.translation_enabled
t(translation_key, count: 1, default: default).humanize
else
default
end
end
|
#navigation_label ⇒ Object
290
291
292
|
# File 'lib/jav/base_resource.rb', line 290
def navigation_label
plural_name.humanize
end
|
#ordering_host(**args) ⇒ Object
476
477
478
|
# File 'lib/jav/base_resource.rb', line 476
def ordering_host(**args)
Jav::Hosts::Ordering.new resource: self, options: self.class.ordering, **args
end
|
#plural_name ⇒ Object
274
275
276
277
278
279
280
281
282
|
# File 'lib/jav/base_resource.rb', line 274
def plural_name
default = name.pluralize
if translation_key && ::Jav::App.translation_enabled
t(translation_key, count: 2, default: default).humanize
else
default
end
end
|
#record ⇒ Object
Also known as:
model
148
149
150
|
# File 'lib/jav/base_resource.rb', line 148
def record
@model
end
|
#record_path ⇒ Object
416
417
418
|
# File 'lib/jav/base_resource.rb', line 416
def record_path
resource_path(model: model, resource: self)
end
|
#records_path ⇒ Object
420
421
422
|
# File 'lib/jav/base_resource.rb', line 420
def records_path
resources_path(resource: self)
end
|
#resource_description ⇒ Object
242
243
244
245
246
247
248
249
250
|
# File 'lib/jav/base_resource.rb', line 242
def resource_description
return instance_exec(&self.class.description) if self.class.description.respond_to? :call
return unless view == :index
self.class.description if self.class.description.is_a? String
end
|
#route_key ⇒ Object
394
395
396
|
# File 'lib/jav/base_resource.rb', line 394
def route_key
class_name_without_resource.underscore.pluralize
end
|
#singular_model_key ⇒ Object
412
413
414
|
# File 'lib/jav/base_resource.rb', line 412
def singular_model_key
model_class.model_name.singular
end
|
#singular_name ⇒ Object
270
271
272
|
# File 'lib/jav/base_resource.rb', line 270
def singular_name
name
end
|
#singular_route_key ⇒ Object
398
399
400
|
# File 'lib/jav/base_resource.rb', line 398
def singular_route_key
route_key.singularize
end
|
#translation_key ⇒ Object
252
253
254
255
256
|
# File 'lib/jav/base_resource.rb', line 252
def translation_key
return "jav.resource_translations.#{class_name_without_resource.underscore}" if ::Jav::App.translation_enabled
self.class.translation_key
end
|
#underscore_name ⇒ Object
284
285
286
287
288
|
# File 'lib/jav/base_resource.rb', line 284
def underscore_name
return @name if @name.present?
self.class.name.demodulize.underscore
end
|