lib/avo/base_resource.rb in avo-1.4.4 vs lib/avo/base_resource.rb in avo-1.4.5.pre.1
- old
+ new
@@ -9,11 +9,11 @@
attr_accessor :user
attr_accessor :params
class_attribute :id, default: :id
class_attribute :title, default: :id
- class_attribute :search, default: [:id]
+ class_attribute :search_query, default: nil
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
@@ -40,12 +40,24 @@
def filter(filter_class)
self.filters_loader ||= Avo::Loaders::Loader.new
self.filters_loader.use filter_class
end
+
+ def scope
+ authorization.apply_policy model_class
+ end
+
+ def authorization
+ Avo::Services::AuthorizationService.new Avo::App.current_user
+ end
end
+ def initialize
+ self.class.model_class = model_class
+ end
+
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?
@@ -63,11 +75,11 @@
fields = self.class.fields.map do |field|
field.hydrate(resource: self, panel_name: default_panel_name, user: user)
end
- if Avo::App.license.invalid? || Avo::App.license.lacks(:custom_fields)
+ if Avo::App.license.has_with_trial(:custom_fields)
fields = fields.reject do |field|
field.custom?
end
end
@@ -94,13 +106,13 @@
fields = fields.select do |field|
field.panel_name == panel
end
end
- fields.map do |field|
- field.hydrate(model: @model, view: @view, resource: self)
- end
+ hydrate_fields(model: @model, view: @view)
+
+ fields
end
def get_grid_fields
return if self.class.grid_loader.blank?
@@ -117,12 +129,20 @@
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[:related_name].present?
+ return @params[:related_name].capitalize if @params.present? && @params[:related_name].present?
case @view
when :show
I18n.t("avo.resource_details", item: name.downcase, title: model_title).upcase_first
when :edit
@@ -224,11 +244,15 @@
end
end
def fill_model(model, params)
# Map the received params to their actual fields
- fields_by_database_id = get_field_definitions.map { |field| [field.database_id(model).to_s, field] }.to_h
+ fields_by_database_id = get_field_definitions
+ .reject do |field|
+ field.computed
+ end
+ .map { |field| [field.database_id(model).to_s, field] }.to_h
params.each do |key, value|
field = fields_by_database_id[key]
next unless field.present?
@@ -298,8 +322,60 @@
default_values.each do |id, value|
if @model.send(id).nil?
@model.send("#{id}=", value)
end
end
+ end
+
+ def avo_path
+ "#{Avo.configuration.root_path}/resources/#{model_class.model_name.route_key}/#{model.id}"
+ end
+
+ def label_field
+ get_field_definitions.find do |field|
+ field.as_label.present?
+ end
+ rescue
+ nil
+ end
+
+ def label
+ label_field.value || model_title
+ rescue
+ model_title
+ end
+
+ def avatar_field
+ get_field_definitions.find do |field|
+ field.as_avatar.present?
+ end
+ rescue
+ nil
+ end
+
+ def avatar
+ avatar_field.to_image
+ rescue
+ nil
+ end
+
+ def avatar_type
+ avatar_field.as_avatar
+ rescue
+ nil
+ end
+
+ def description_field
+ get_field_definitions.find do |field|
+ field.as_description.present?
+ end
+ rescue
+ nil
+ end
+
+ def description
+ description_field.value
+ rescue
+ nil
end
end
end