lib/avo/base_resource.rb in avo-2.0.0 vs lib/avo/base_resource.rb in avo-2.1.0
- old
+ new
@@ -9,10 +9,11 @@
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
+ delegate :t, to: ::I18n
attr_accessor :view
attr_accessor :model
attr_accessor :reflection
attr_accessor :user
@@ -24,11 +25,11 @@
class_attribute :search_query, default: nil
class_attribute :search_query_help, default: ""
class_attribute :includes, default: []
class_attribute :model_class
class_attribute :translation_key
- class_attribute :translation_enabled
+ class_attribute :translation_enabled, default: false
class_attribute :default_view_type, default: :table
class_attribute :devise_password_optional, default: false
class_attribute :actions_loader
class_attribute :filters_loader
class_attribute :fields
@@ -36,12 +37,15 @@
class_attribute :visible_on_sidebar, default: true
class_attribute :unscoped_queries_on_index, default: false
class_attribute :resolve_query_scope
class_attribute :resolve_find_scope
class_attribute :ordering
+ class_attribute :search_hide_from_global_search, default: false
class << self
+ delegate :t, to: ::I18n
+
def grid(&block)
grid_collector = GridCollector.new
grid_collector.instance_eval(&block)
self.grid_loader = grid_collector
@@ -94,10 +98,12 @@
unless self.class.model_class.present?
if model_class.present?
self.class.model_class = model_class.base_class
end
end
+
+ self.class.translation_enabled = ::Avo::App.translation_enabled
end
def hydrate(model: nil, view: nil, user: nil, params: nil)
@view = view if view.present?
@user = user if user.present?
@@ -213,11 +219,11 @@
when :show
model_title
when :edit
model_title
when :new
- I18n.t("avo.create_new_item", item: name.downcase).upcase_first
+ t("avo.create_new_item", item: name.downcase).upcase_first
end
end
def panels
[
@@ -242,13 +248,22 @@
# generate a model class
class_name_without_resource.safe_constantize
end
+ def model_id
+ @model.send id
+ end
+
def model_title
- return @model.send title if @model.present?
+ return name if @model.nil?
+ the_title = @model.send title
+ return the_title if the_title.present?
+
+ model_id
+ rescue
name
end
def resource_description
return instance_exec(&self.class.description) if self.class.description.respond_to? :call
@@ -265,24 +280,28 @@
self.class.translation_key
end
def name
+ default = class_name_without_resource.titlecase
+
return @name if @name.present?
- return I18n.t(translation_key, count: 1).capitalize if translation_key
+ return t(translation_key, count: 1, default: default).capitalize if translation_key
- class_name_without_resource.titlecase
+ default
end
def singular_name
name
end
def plural_name
- return I18n.t(translation_key, count: 2).capitalize if translation_key
+ default = name.pluralize
- name.pluralize
+ return t(translation_key, count: 2, default: default).capitalize if translation_key
+
+ default
end
def underscore_name
return @name if @name.present?