Class: Avo::BaseResource

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker
Includes:
ActionView::Helpers::UrlHelper, Concerns::HasEditableControls, Concerns::HasFields, Concerns::HasStimulusControllers, Concerns::ModelClassConstantized
Defined in:
lib/avo/base_resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::HasStimulusControllers

#get_stimulus_controllers, #stimulus_data_attributes

Methods included from Concerns::HasEditableControls

#has_show_controls?, #render_show_controls

Methods included from Concerns::HasFields

#fields, #get_field, #get_field_definitions, #get_fields, #get_items, #get_tabs, #hydrate_fields, #tab_groups, #tools

Constructor Details

#initializeBaseResource

Returns a new instance of BaseResource.



106
107
108
109
110
111
112
# File 'lib/avo/base_resource.rb', line 106

def initialize
  unless self.class.model_class.present?
    if model_class.present? && model_class.respond_to?(:base_class)
      self.class.model_class = model_class.base_class
    end
  end
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



23
24
25
# File 'lib/avo/base_resource.rb', line 23

def model
  @model
end

#paramsObject

Returns the value of attribute params.



26
27
28
# File 'lib/avo/base_resource.rb', line 26

def params
  @params
end

#reflectionObject

Returns the value of attribute reflection.



24
25
26
# File 'lib/avo/base_resource.rb', line 24

def reflection
  @reflection
end

#userObject

Returns the value of attribute user.



25
26
27
# File 'lib/avo/base_resource.rb', line 25

def user
  @user
end

#viewObject

Returns the value of attribute view.



22
23
24
# File 'lib/avo/base_resource.rb', line 22

def view
  @view
end

Class Method Details

.action(action_class) ⇒ Object



63
64
65
66
67
# File 'lib/avo/base_resource.rb', line 63

def action(action_class)
  self.actions_loader ||= Avo::Loaders::Loader.new

  self.actions_loader.use action_class
end

.authorizationObject



95
96
97
# File 'lib/avo/base_resource.rb', line 95

def authorization
  Avo::Services::AuthorizationService.new Avo::App.current_user
end

.filter(filter_class) ⇒ Object



69
70
71
72
73
# File 'lib/avo/base_resource.rb', line 69

def filter(filter_class)
  self.filters_loader ||= Avo::Loaders::Loader.new

  self.filters_loader.use filter_class
end

.find_scopeObject

This resolves the scope when finding records (not “where” queries)



89
90
91
92
93
# File 'lib/avo/base_resource.rb', line 89

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

.grid(&block) ⇒ Object



56
57
58
59
60
61
# File 'lib/avo/base_resource.rb', line 56

def grid(&block)
  grid_collector = GridCollector.new
  grid_collector.instance_eval(&block)

  self.grid_loader = grid_collector
end

.order_actionsObject



99
100
101
102
103
# File 'lib/avo/base_resource.rb', line 99

def order_actions
  return {} if ordering.blank?

  ordering.dig(:actions) || {}
end

.query_scopeObject

This resolves the scope when doing “where” queries (not find queries)



82
83
84
85
86
# File 'lib/avo/base_resource.rb', line 82

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

.scopeObject

This is the search_query scope This should be removed and passed to the search block



77
78
79
# File 'lib/avo/base_resource.rb', line 77

def scope
  query_scope
end

Instance Method Details

#attached_file_fieldsObject



249
250
251
252
253
# File 'lib/avo/base_resource.rb', line 249

def attached_file_fields
  get_field_definitions.select do |field|
    [Avo::Fields::FileField, Avo::Fields::FilesField].include? field.class
  end
end

#authorizationObject



284
285
286
# File 'lib/avo/base_resource.rb', line 284

def authorization
  Avo::Services::AuthorizationService.new(user, model || model_class)
end

#available_view_typesObject



241
242
243
244
245
246
247
# File 'lib/avo/base_resource.rb', line 241

def available_view_types
  view_types = [:table]

  view_types << :grid if get_grid_fields.present?

  view_types
end

#avatarObject



405
406
407
408
409
410
411
412
413
# File 'lib/avo/base_resource.rb', line 405

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
  nil
end

#avatar_fieldObject



397
398
399
400
401
402
403
# File 'lib/avo/base_resource.rb', line 397

def avatar_field
  get_field_definitions.find do |field|
    field.as_avatar.present?
  end
rescue
  nil
end

#avatar_typeObject



415
416
417
418
419
# File 'lib/avo/base_resource.rb', line 415

def avatar_type
  avatar_field.as_avatar
rescue
  nil
end

#cache_hash(parent_model) ⇒ Object



306
307
308
309
310
311
312
# File 'lib/avo/base_resource.rb', line 306

def cache_hash(parent_model)
  if parent_model.present?
    [model, file_hash, parent_model]
  else
    [model, file_hash]
  end
end

#class_name_without_resourceObject



159
160
161
# File 'lib/avo/base_resource.rb', line 159

def class_name_without_resource
  self.class.name.demodulize.delete_suffix("Resource")
end

#default_panel_nameObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/avo/base_resource.rb', line 146

def default_panel_name
  return @params[:related_name].capitalize if @params.present? && @params[:related_name].present?

  case @view
  when :show
    model_title
  when :edit
    model_title
  when :new
    t("avo.create_new_item", item: name.downcase).upcase_first
  end
end

#descriptionObject



429
430
431
432
433
# File 'lib/avo/base_resource.rb', line 429

def description
  description_field.value
rescue
  nil
end

#description_fieldObject



421
422
423
424
425
426
427
# File 'lib/avo/base_resource.rb', line 421

def description_field
  get_field_definitions.find do |field|
    field.as_description.present?
  end
rescue
  nil
end

#file_hashObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/avo/base_resource.rb', line 288

def file_hash
  content_to_be_hashed = ""

  # resource file hash
  resource_path = Rails.root.join("app", "avo", "resources", "#{self.class.name.underscore}.rb").to_s
  if File.file? resource_path
    content_to_be_hashed += File.read(resource_path)
  end

  # policy file hash
  policy_path = Rails.root.join("app", "policies", "#{self.class.name.underscore.gsub("_resource", "")}_policy.rb").to_s
  if File.file? policy_path
    content_to_be_hashed += File.read(policy_path)
  end

  Digest::MD5.hexdigest(content_to_be_hashed)
end

#fill_model(model, params, extra_params: []) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/avo/base_resource.rb', line 255

def fill_model(model, params, extra_params: [])
  # Map the received params to their actual fields
  fields_by_database_id = get_field_definitions
    .reject do |field|
      field.computed
    end
    .map do |field|
      [field.database_id(model).to_s, field]
    end
    .to_h

  # Write the field values
  params.each do |key, value|
    field = fields_by_database_id[key]

    next unless field.present?

    model = field.fill_field model, key, value, params
  end

  # Write the user configured extra params to the model
  if extra_params.present?
    # Let Rails fill in the rest of the params
    model.assign_attributes params.permit(extra_params)
  end

  model
end

#form_scopeObject



435
436
437
# File 'lib/avo/base_resource.rb', line 435

def form_scope
  model_class.base_class.to_s.underscore.downcase
end

#get_actionsObject



140
141
142
143
144
# File 'lib/avo/base_resource.rb', line 140

def get_actions
  return [] if self.class.actions_loader.blank?

  self.class.actions_loader.bag
end

#get_filtersObject



134
135
136
137
138
# File 'lib/avo/base_resource.rb', line 134

def get_filters
  return [] if self.class.filters_loader.blank?

  self.class.filters_loader.bag
end

#get_grid_fieldsObject



128
129
130
131
132
# File 'lib/avo/base_resource.rb', line 128

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

Returns:

  • (Boolean)


443
444
445
# File 'lib/avo/base_resource.rb', line 443

def has_model_id?
  model.present? && model.id.present?
end

#hydrate(model: nil, view: nil, user: nil, params: nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/avo/base_resource.rb', line 114

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_valuesObject

We will not overwrite any attributes that come pre-filled in the model.



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/avo/base_resource.rb', line 315

def hydrate_model_with_default_values
  default_values = get_fields
    .select do |field|
      !field.computed
    end
    .map do |field|
      id = field.id
      value = field.value

      if field.type == "belongs_to"
        id = field.foreign_key.to_sym

        reflection = @model._reflections[@params[:via_relation]]

        if field.polymorphic_as.present? && field.types.map(&:to_s).include?(@params[:via_relation_class])
          # set the value to the actual record
          value = @params[:via_relation_class].safe_constantize.find(@params[:via_resource_id])
        elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s
          value = @params[:via_resource_id]
        end
      end

      [id, value]
    end
    .to_h
    .select do |id, value|
      value.present?
    end

  default_values.each do |id, value|
    if @model.send(id).nil?
      @model.send("#{id}=", value)
    end
  end
end

#labelObject



391
392
393
394
395
# File 'lib/avo/base_resource.rb', line 391

def label
  label_field.value || model_title
rescue
  model_title
end

#label_fieldObject



383
384
385
386
387
388
389
# File 'lib/avo/base_resource.rb', line 383

def label_field
  get_field_definitions.find do |field|
    field.as_label.present?
  end
rescue
  nil
end

#model_classObject



163
164
165
166
167
168
169
170
171
172
# File 'lib/avo/base_resource.rb', line 163

def model_class
  # get the model class off of the static property
  return self.class.model_class if self.class.model_class.present?

  # get the model class off of the model
  return @model.base_class if @model.present?

  # generate a model class
  class_name_without_resource.safe_constantize
end

#model_idObject



174
175
176
# File 'lib/avo/base_resource.rb', line 174

def model_id
  @model.send id
end

#model_keyObject

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



363
364
365
# File 'lib/avo/base_resource.rb', line 363

def model_key
  model_class.model_name.plural
end

#model_nameObject



367
368
369
# File 'lib/avo/base_resource.rb', line 367

def model_name
  model_class.model_name
end

#model_titleObject



178
179
180
181
182
183
184
185
186
187
# File 'lib/avo/base_resource.rb', line 178

def model_title
  return name if @model.nil?

  the_title = @model.send title
  return the_title if the_title.present?

  model_id
rescue
  name
end

#nameObject



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/avo/base_resource.rb', line 205

def name
  default = class_name_without_resource.to_s.gsub('::', ' ').underscore.humanize

  return @name if @name.present?

  if translation_key && ::Avo::App.translation_enabled
    t(translation_key, count: 1, default: default).capitalize
  else
    default
  end
end


237
238
239
# File 'lib/avo/base_resource.rb', line 237

def navigation_label
  plural_name.humanize
end

#ordering_host(**args) ⇒ Object



439
440
441
# File 'lib/avo/base_resource.rb', line 439

def ordering_host(**args)
  Avo::Hosts::Ordering.new resource: self, options: self.class.ordering, **args
end

#plural_nameObject



221
222
223
224
225
226
227
228
229
# File 'lib/avo/base_resource.rb', line 221

def plural_name
  default = name.pluralize

  if translation_key && ::Avo::App.translation_enabled
    t(translation_key, count: 2, default: default).capitalize
  else
    default
  end
end

#record_pathObject



375
376
377
# File 'lib/avo/base_resource.rb', line 375

def record_path
  resource_path(model: model, resource: self)
end

#records_pathObject



379
380
381
# File 'lib/avo/base_resource.rb', line 379

def records_path
  resources_path(resource: self)
end

#resource_descriptionObject



189
190
191
192
193
194
195
196
197
# File 'lib/avo/base_resource.rb', line 189

def resource_description
  return instance_exec(&self.class.description) if self.class.description.respond_to? :call

  # Show the description only on the resource index view.
  # If the user wants to conditionally it on all pages, they should use a block.
  if view == :index
    return self.class.description if self.class.description.is_a? String
  end
end

#route_keyObject



351
352
353
# File 'lib/avo/base_resource.rb', line 351

def route_key
  class_name_without_resource.underscore.pluralize
end

#singular_model_keyObject



371
372
373
# File 'lib/avo/base_resource.rb', line 371

def singular_model_key
  model_class.model_name.singular
end

#singular_nameObject



217
218
219
# File 'lib/avo/base_resource.rb', line 217

def singular_name
  name
end

#singular_route_keyObject



355
356
357
# File 'lib/avo/base_resource.rb', line 355

def singular_route_key
  route_key.singularize
end

#translation_keyObject



199
200
201
202
203
# File 'lib/avo/base_resource.rb', line 199

def translation_key
  return "avo.resource_translations.#{class_name_without_resource.underscore}" if ::Avo::App.translation_enabled

  self.class.translation_key
end

#underscore_nameObject



231
232
233
234
235
# File 'lib/avo/base_resource.rb', line 231

def underscore_name
  return @name if @name.present?

  self.class.name.demodulize.underscore
end