lib/avo/base_resource.rb in avo-3.0.0.pre18 vs lib/avo/base_resource.rb in avo-3.0.0.pre19
- old
+ new
@@ -7,14 +7,15 @@
include Avo::Concerns::CanReplaceItems
include Avo::Concerns::HasControls
include Avo::Concerns::HasStimulusControllers
include Avo::Concerns::ModelClassConstantized
include Avo::Concerns::HasDescription
+ include Avo::Concerns::HasHelpers
# Avo::Current methods
delegate :context, to: Avo::Current
- def curent_user
+ def current_user
Avo::Current.user
end
delegate :params, to: Avo::Current
delegate :request, to: Avo::Current
delegate :view_context, to: Avo::Current
@@ -63,10 +64,11 @@
class_attribute :record_selector, default: true
class_attribute :keep_filters_panel_open, default: false
class_attribute :extra_params
class_attribute :link_to_child_resource, default: false
class_attribute :map_view
+ class_attribute :components, default: {}
# EXTRACT:
class_attribute :ordering
class << self
@@ -230,18 +232,18 @@
delegate :underscore_name, to: :class
delegate :find_record, to: :class
delegate :model_key, to: :class
def initialize(record: nil, view: nil, user: nil, params: nil)
- @view = view if view.present?
+ @view = Avo::ViewInquirer.new(view) if view.present?
@user = user if user.present?
@params = params if params.present?
if record.present?
@record = record
- hydrate_model_with_default_values if @view == :new
+ hydrate_model_with_default_values if @view&.new?
end
detect_fields
unless self.class.model_class.present?
@@ -291,23 +293,27 @@
entity_loader(entity).bag
end
# def get_action_arguments / def get_filter_arguments / def get_scope_arguments
define_method "get_#{entity}_arguments" do |entity_class|
- send("get_#{plural_entity}").find { |entity| entity[:class].to_s == entity_class.to_s }[:arguments]
+ klass = send("get_#{plural_entity}").find { |entity| entity[:class].to_s == entity_class.to_s }
+
+ raise "Couldn't find '#{entity_class}' in the 'def #{plural_entity}' method on your '#{self.class}' resource." if klass.nil?
+
+ klass[:arguments]
end
end
def hydrate(record: nil, view: nil, user: nil, params: nil)
- @view = view if view.present?
+ @view = Avo::ViewInquirer.new(view) if view.present?
@user = user if user.present?
@params = params if params.present?
if record.present?
@record = record
- hydrate_model_with_default_values if @view == :new
+ hydrate_model_with_default_values if @view&.new?
end
self
end
@@ -438,10 +444,10 @@
# We will not overwrite any attributes that come pre-filled in the record.
def hydrate_model_with_default_values
default_values = get_fields
.select do |field|
- !field.computed
+ !field.computed && !field.is_a?(Avo::Fields::HeadingField)
end
.map do |field|
value = field.value
if field.type == "belongs_to"