lib/avo/base_resource.rb in avo-1.18.2 vs lib/avo/base_resource.rb in avo-1.19.0

- old
+ new

@@ -2,10 +2,18 @@ class BaseResource extend ActiveSupport::DescendantsTracker extend FieldsCollector extend HasContext + include ActionView::Helpers::UrlHelper + + delegate :view_context, to: 'Avo::App' + delegate :main_app, to: :view_context + delegate :avo, to: :view_context + delegate :resource_path, to: :view_context + delegate :resources_path, to: :view_context + attr_accessor :view attr_accessor :model attr_accessor :user attr_accessor :params @@ -72,11 +80,13 @@ Avo::Services::AuthorizationService.new Avo::App.current_user end end def initialize - self.class.model_class = model_class.base_class + unless self.class.model_class.present? + self.class.model_class = model_class.base_class + end end def hydrate(model: nil, view: nil, user: nil, params: nil) @view = view if view.present? @user = user if user.present? @@ -254,14 +264,10 @@ view_types << :grid if get_grid_fields.present? view_types end - def route_key - model_class.model_name.route_key - end - def context self.class.context end def attached_file_fields @@ -356,11 +362,31 @@ @model.send("#{id}=", value) end end end - def avo_path - "#{Avo::App.root_path}/resources/#{model_class.model_name.route_key}/#{model.id}" + def route_key + model_class.model_name.route_key + 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->medie_items, Fish->fish + def model_key + model_class.model_name.plural + end + + def singular_model_key + model_class.model_name.singular + end + + def record_path + resource_path(model: model, resource: self) + end + + def records_path + resources_path(resource: self) end def label_field get_field_definitions.find do |field| field.as_label.present?