lib/avo/base_resource.rb in avo-3.0.0.pre3 vs lib/avo/base_resource.rb in avo-3.0.0.pre4
- old
+ new
@@ -7,33 +7,43 @@
include Avo::Concerns::CanReplaceItems
include Avo::Concerns::HasControls
include Avo::Concerns::HasStimulusControllers
include Avo::Concerns::ModelClassConstantized
+ # Avo::Current methods
delegate :context, to: Avo::Current
delegate :current_user, to: Avo::Current
delegate :params, to: Avo::Current
delegate :request, to: Avo::Current
delegate :view_context, to: Avo::Current
+
+ # view_context methods
delegate :simple_format, :content_tag, to: :view_context
delegate :main_app, to: :view_context
delegate :avo, to: :view_context
delegate :resource_path, to: :view_context
delegate :resources_path, to: :view_context
+
+ # I18n methods
delegate :t, to: ::I18n
+ # class methods
+ delegate :class_name, to: :class
+ delegate :route_key, to: :class
+ delegate :singular_route_key, to: :class
+
attr_accessor :view
attr_accessor :reflection
attr_accessor :user
- attr_accessor :params
attr_accessor :record
class_attribute :id, default: :id
class_attribute :title, default: :id
class_attribute :description, default: :id
class_attribute :search_query, default: nil
class_attribute :search_query_help, default: ""
+ class_attribute :search_result_path
class_attribute :includes, default: []
class_attribute :authorization_policy
class_attribute :translation_key
class_attribute :default_view_type, default: :table
class_attribute :devise_password_optional, default: false
@@ -44,11 +54,11 @@
class_attribute :visible_on_sidebar, default: true
class_attribute :unscoped_queries_on_index, default: false
class_attribute :resolve_index_query
class_attribute :resolve_find_scope
# TODO: refactor this into a Host without args
- class_attribute :find_record_method, default: ->(model_class:, id:, params:) {
+ class_attribute :find_record_method, default: -> {
model_class.find id
}
class_attribute :hide_from_global_search, default: false
class_attribute :after_create_path, default: :show
class_attribute :after_update_path, default: :show
@@ -149,22 +159,53 @@
def get_model_by_name(model_name)
get_available_models.find do |m|
m.to_s == model_name.to_s
end
end
+
+ def class_name
+ name.demodulize
+ end
+
+ def route_key
+ class_name.underscore.pluralize
+ end
+
+ def singular_route_key
+ route_key.singularize
+ end
end
delegate :context, to: ::Avo::Current
def initialize
+ detect_fields
+
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
+ def detect_fields
+ self.items_holder = Avo::Resources::Items::Holder.new
+
+ # Used in testing to replace items
+ if temporary_items.present?
+ instance_eval(&temporary_items)
+ else
+ fields
+ end
+
+ self
+ end
+
+ def fields
+ # blank fields method
+ end
+
def hydrate(record: nil, view: nil, user: nil, params: nil)
@view = view if view.present?
@user = user if user.present?
@params = params if params.present?
@@ -224,14 +265,10 @@
when :new
t("avo.create_new_item", item: name.downcase).upcase_first
end
end
- def class_name
- self.class.name.demodulize
- end
-
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 record
@@ -391,11 +428,12 @@
reflection = @record._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_record_id])
+ via_resource = Avo::App.resources.get_resource_by_model_class(@params[:via_relation_class])
+ value = via_resource.find_record(@params[:via_record_id])
elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s
resource = Avo::App.resources.get_resource_by_model_class params[:via_relation_class]
record = resource.find_record @params[:via_record_id], params: params
id_param = reflection.options[:primary_key] || :id
@@ -415,18 +453,10 @@
@record.send("#{id}=", value)
end
end
end
- def route_key
- class_name.underscore.pluralize
- end
-
- def singular_route_key
- route_key.singularize
- end
-
# 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
def model_key
@@ -506,10 +536,15 @@
end
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)
+ Avo::ExecutionContext.new(
+ target: self.class.find_record_method,
+ model_class: query,
+ id: id,
+ params: params
+ ).handle
end
def id_attribute
:id
end