lib/avo/base_resource.rb in avo-3.0.0.pre12 vs lib/avo/base_resource.rb in avo-3.0.0.pre13

- old
+ new

@@ -36,31 +36,28 @@ attr_accessor :reflection attr_accessor :user attr_accessor :record class_attribute :id, default: :id - class_attribute :title, default: :id - class_attribute :search_query, default: nil - class_attribute :search_query_help, default: "" - class_attribute :search_result_path + class_attribute :title + class_attribute :search, default: {} 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 - class_attribute :actions_loader class_attribute :scopes_loader class_attribute :filters_loader - class_attribute :grid_loader + class_attribute :view_types + class_attribute :grid_view class_attribute :visible_on_sidebar, default: true class_attribute :index_query, default: -> { query } class_attribute :find_record_method, default: -> { query.find id } - class_attribute :hide_from_global_search, default: false class_attribute :after_create_path, default: :show class_attribute :after_update_path, default: :show class_attribute :record_selector, default: true class_attribute :keep_filters_panel_open, default: false class_attribute :extra_params @@ -72,45 +69,22 @@ class << self delegate :t, to: ::I18n delegate :context, to: ::Avo::Current - def grid(&block) - grid_collector = GridCollector.new - grid_collector.instance_eval(&block) - - self.grid_loader = grid_collector - end - def action(action_class, arguments: {}) - self.actions_loader ||= Avo::Loaders::Loader.new - - action = {class: action_class, arguments: arguments} - self.actions_loader.use action + deprecated_dsl_api __method__, "actions" end def filter(filter_class, arguments: {}) - self.filters_loader ||= Avo::Loaders::Loader.new - - filter = { class: filter_class , arguments: arguments } - self.filters_loader.use filter + deprecated_dsl_api __method__, "filters" end - # This is the search_query scope - # This should be removed and passed to the search block - def scope - query_scope + def scope(scope_class) + deprecated_dsl_api __method__, "scopes" end - def scopes(*args) - self.scopes_loader ||= Avo::Loaders::Loader.new - - args.each do |scope_class| - self.scopes_loader.use scope_class - end - end - # This resolves the scope when doing "where" queries (not find queries) def query_scope authorization.apply_policy Avo::ExecutionContext.new( target: index_query, query: model_class @@ -193,10 +167,39 @@ def fields # blank fields method end + [:action, :filter, :scope].each do |entity| + plural_entity = entity.to_s.pluralize + + # def actions / def filters / def scopes + define_method plural_entity do + # blank entity method + end + + # def action / def filter / def scope + define_method entity do |entity_class, arguments: {}| + entity_loader(entity).use({class: entity_class, arguments: arguments}) + end + + # def get_actions / def get_filters / def get_scopes + define_method "get_#{plural_entity}" do + return entity_loader(entity).bag if entity_loader(entity).present? + + instance_variable_set("@#{plural_entity}_loader", Avo::Loaders::Loader.new) + send plural_entity + + 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] == entity_class.constantize }[:arguments] + end + 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? @@ -207,46 +210,10 @@ end self end - def get_grid_fields - return if self.class.grid_loader.blank? - - self.class.grid_loader.hydrate(record: @record, view: @view, resource: self) - end - - def get_filters - return [] if self.class.filters_loader.blank? - - self.class.filters_loader.bag - end - - def get_filter_arguments(filter_class) - filter = get_filters.find { |filter| filter[:class] == filter_class.constantize } - - filter[:arguments] - end - - def get_actions - return [] if self.class.actions_loader.blank? - - self.class.actions_loader.bag - end - - def get_action_arguments(action_class) - action = get_actions.find { |action| action[:class].to_s == action_class.to_s } - - action[:arguments] - end - - def get_scopes - return [] if self.class.scopes_loader.blank? - - self.class.scopes_loader.bag - end - def default_panel_name return @params[:related_name].capitalize if @params.present? && @params[:related_name].present? case @view when :show @@ -270,27 +237,31 @@ end def record_title return name if @record.nil? - the_title = @record.send title - return the_title if the_title.present? + # Get the title from the record if title is not set, try to get the name, title or label, or fallback to the id + return @record.try(:name) || @record.try(:title) || @record.try(:label) || @record.id if title.nil? - @record.id - rescue - name + # If the title is a symbol, get the value from the record else execute the block/string + case title + when Symbol + @record.send title + when Proc + Avo::ExecutionContext.new(target: title, resource: self, record: @record).handle + end end def translation_key self.class.translation_key || "avo.resource_translations.#{class_name.underscore}" end def name - default = class_name.underscore.humanize - return @name if @name.present? + default = class_name.underscore.humanize + if translation_key t(translation_key, count: 1, default: default).capitalize else default end @@ -319,13 +290,23 @@ def navigation_label plural_name.humanize end def available_view_types + if self.class.view_types.present? + return Array( + Avo::ExecutionContext.new( + target: self.class.view_types, + resource: self, + record: record + ).handle + ) + end + view_types = [:table] - view_types << :grid if get_grid_fields.present? + view_types << :grid if self.class.grid_view.present? view_types << :map if map_view.present? view_types end @@ -459,22 +440,10 @@ def records_path resources_path(resource: self) end - def label_field - get_field_definitions.find do |field| - field.as_label.present? - end - rescue - nil - end - - def label - label_field&.value || record_title - end - def avatar_field get_field_definitions.find do |field| field.as_avatar.present? end rescue @@ -495,22 +464,10 @@ 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 search_description - description_field&.value - end - def form_scope model_class.base_class.to_s.underscore.downcase end def has_record_id? @@ -538,8 +495,22 @@ { view: view, resource: self, record: record } + end + + def search_query + self.class.search.dig(:query) + end + + def fetch_search(key) + Avo::ExecutionContext.new(target: self.class.search[key], resource: self, record: record).handle + end + + private + + def entity_loader(entity) + instance_variable_get("@#{entity.to_s.pluralize}_loader") end end end