lib/avo/base_resource.rb in avo-2.9.2.pre1 vs lib/avo/base_resource.rb in avo-2.10.0
- old
+ new
@@ -2,14 +2,14 @@
class BaseResource
extend ActiveSupport::DescendantsTracker
extend HasContext
include ActionView::Helpers::UrlHelper
- include Avo::Concerns::HasTools
include Avo::Concerns::HasModel
include Avo::Concerns::HasFields
include Avo::Concerns::HasStimulusControllers
+ include Avo::Concerns::ModelClassConstantized
delegate :view_context, to: ::Avo::App
delegate :simple_format, :content_tag, to: :view_context
delegate :main_app, to: :view_context
delegate :avo, to: :view_context
@@ -27,11 +27,10 @@
class_attribute :title, default: :id
class_attribute :description, default: :id
class_attribute :search_query, default: nil
class_attribute :search_query_help, default: ""
class_attribute :includes, default: []
- class_attribute :model_class
class_attribute :translation_key
class_attribute :default_view_type, default: :table
class_attribute :devise_password_optional, default: false
class_attribute :actions_loader
class_attribute :filters_loader
@@ -42,11 +41,10 @@
class_attribute :resolve_find_scope
class_attribute :ordering
class_attribute :hide_from_global_search, default: false
class_attribute :after_create_path, default: :show
class_attribute :after_update_path, default: :show
- class_attribute :invalid_fields
class_attribute :record_selector, default: true
class_attribute :keep_filters_panel_open, default: false
class << self
delegate :t, to: ::I18n
@@ -121,84 +119,10 @@
end
self
end
- def get_field_definitions
- return [] if self.class.fields.blank?
-
- fields = self.class.fields.map do |field|
- field.hydrate(resource: self, panel_name: default_panel_name, user: user)
- end
-
- if Avo::App.license.lacks_with_trial(:custom_fields)
- fields = fields.reject do |field|
- field.custom?
- end
- end
-
- if Avo::App.license.lacks_with_trial(:advanced_fields)
- fields = fields.reject do |field|
- field.type == "tags"
- end
- end
-
- fields
- end
-
- def get_fields(panel: nil, reflection: nil)
- fields = get_field_definitions
- .select do |field|
- field.send("show_on_#{@view}")
- end
- .select do |field|
- field.visible?
- end
- .select do |field|
- is_valid = true
-
- # Strip out the reflection field in index queries with a parent association.
- if reflection.present?
- # regular non-polymorphic association
- # we're matching the reflection inverse_of foriegn key with the field's foreign_key
- if field.is_a?(Avo::Fields::BelongsToField)
- if field.respond_to?(:foreign_key) &&
- reflection.inverse_of.present? &&
- reflection.inverse_of.foreign_key == field.foreign_key
- is_valid = false
- end
-
- # polymorphic association
- if field.respond_to?(:foreign_key) &&
- field.is_polymorphic? &&
- reflection.respond_to?(:polymorphic?) &&
- reflection.inverse_of.foreign_key == field.reflection.foreign_key
- is_valid = false
- end
- end
- end
-
- is_valid
- end
-
- if panel.present?
- fields = fields.select do |field|
- field.panel_name == panel
- end
- end
-
- hydrate_fields(model: @model, view: @view)
-
- fields
- end
-
- def get_field(id)
- get_field_definitions.find do |f|
- f.id == id.to_sym
- end
- end
-
def get_grid_fields
return if self.class.grid_loader.blank?
self.class.grid_loader.hydrate(model: @model, view: @view, resource: self)
end
@@ -213,18 +137,10 @@
return [] if self.class.actions_loader.blank?
self.class.actions_loader.bag
end
- def hydrate_fields(model: nil, view: nil)
- fields.map do |field|
- field.hydrate(model: @model, view: @view, resource: self)
- end
-
- self
- end
-
def default_panel_name
return @params[:related_name].capitalize if @params.present? && @params[:related_name].present?
case @view
when :show
@@ -232,19 +148,9 @@
when :edit
model_title
when :new
t("avo.create_new_item", item: name.downcase).upcase_first
end
- end
-
- def panels
- [
- {
- name: default_panel_name,
- type: :fields,
- in_panel: true
- }
- ]
end
def class_name_without_resource
self.class.name.demodulize.chomp("Resource")
end