lib/avo/base_resource.rb in avo-3.10.2 vs lib/avo/base_resource.rb in avo-3.10.3

- old
+ new

@@ -46,10 +46,13 @@ class_attribute :id, default: :id class_attribute :title class_attribute :search, default: {} class_attribute :includes, default: [] + class_attribute :attachments, default: [] + class_attribute :single_includes, default: [] + class_attribute :single_attachments, 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 :scopes_loader @@ -148,15 +151,15 @@ # 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 - model_class.model_name.plural + @model_key ||= model_class.model_name.plural end def class_name - to_s.demodulize + @class_name ||= to_s.demodulize end def route_key class_name.underscore.pluralize end @@ -168,11 +171,11 @@ def translation_key @translation_key || "avo.resource_translations.#{class_name.underscore}" end def name - name_from_translation_key(count: 1, default: class_name.underscore.humanize) + @name ||= name_from_translation_key(count: 1, default: class_name.underscore.humanize) end alias_method :singular_name, :name def plural_name name_from_translation_key(count: 2, default: name.pluralize) @@ -202,22 +205,38 @@ def navigation_label plural_name.humanize end def find_record(id, query: nil, params: nil) + query ||= find_scope # If no record is given we'll use the default + + if single_includes.present? + query = query.includes(*single_includes) + end + + if single_attachments.present? + single_attachments.each do |attachment| + query = query.send(:"with_attached_#{attachment}") + end + end + Avo::ExecutionContext.new( target: find_record_method, - query: query || find_scope, # If no record is given we'll use the default + query: query, id: id, params: params ).handle end def search_query search.dig(:query) end + def search_results_count + search.dig(:results_count) + end + def fetch_search(key, record: nil) # self.class.fetch_search Avo::ExecutionContext.new(target: search[key], resource: self, record: record).handle end end @@ -446,11 +465,10 @@ end def file_hash content_to_be_hashed = "" - file_name = self.class.underscore_name.tr(" ", "_") resource_path = Rails.root.join("app", "avo", "resources", "#{file_name}.rb").to_s if File.file? resource_path content_to_be_hashed += File.read(resource_path) end @@ -459,9 +477,13 @@ if File.file? policy_path content_to_be_hashed += File.read(policy_path) end Digest::MD5.hexdigest(content_to_be_hashed) + end + + def file_name + @file_name ||= self.class.underscore_name.tr(" ", "_") end def cache_hash(parent_record) result = [record, file_hash]