lib/avo/base_resource.rb in avo-0.5.0.beta9 vs lib/avo/base_resource.rb in avo-0.5.0.beta10
- old
+ new
@@ -65,35 +65,33 @@
end
end
def get_fields(panel: nil, reflection: nil)
fields = get_field_definitions.select do |field|
- field.send("show_on_#{@view.to_s}")
+ field.send("show_on_#{@view}")
end
- .select do |field|
- field.visible?
- end
- .select do |field|
+ .select do |field|
+ field.visible?
+ end
+ .select do |field|
unless field.respond_to?(:foreign_key) &&
- reflection.present? &&
- reflection.respond_to?(:foreign_key) &&
- reflection.foreign_key == field.foreign_key
+ reflection.present? &&
+ reflection.respond_to?(:foreign_key) &&
+ reflection.foreign_key == field.foreign_key
true
end
end
if panel.present?
fields = fields.select do |field|
field.panel_name == panel
end
end
- fields = fields.map do |field|
+ fields.map do |field|
field.hydrate(model: @model, view: @view, resource: self)
end
-
- fields
end
def get_grid_fields
return if self.class.grid_loader.blank?
@@ -115,36 +113,34 @@
def default_panel_name
return @params[:related_name].capitalize if @params[:related_name].present?
case @view
when :show
- I18n.t('avo.resource_details', item: self.name.downcase, title: model_title).upcase_first
+ I18n.t("avo.resource_details", item: name.downcase, title: model_title).upcase_first
when :edit
- I18n.t('avo.update_item', item: self.name.downcase, title: model_title).upcase_first
+ I18n.t("avo.update_item", item: name.downcase, title: model_title).upcase_first
when :new
- I18n.t('avo.create_new_item', item: self.name.downcase).upcase_first
+ I18n.t("avo.create_new_item", item: name.downcase).upcase_first
end
end
def panels
- panels = [
+ [
{
name: default_panel_name,
type: :fields,
- in_panel: true,
+ in_panel: true
}
]
-
- panels
end
def model_class
return self.class.model_class if self.class.model_class.present?
return @model.class if @model.present?
- self.class.name.demodulize.chomp('Resource').safe_constantize
+ self.class.name.demodulize.chomp("Resource").safe_constantize
end
def model_title
return @model.send title if @model.present?
@@ -154,11 +150,11 @@
def name
return @name if @name.present?
return I18n.t(@translation_key, count: 1).capitalize if @translation_key
- self.class.name.demodulize.chomp('Resource').titlecase
+ self.class.name.demodulize.chomp("Resource").titlecase
end
def singular_name
name
end
@@ -189,41 +185,41 @@
def context
self.class.context
end
- def query_search(query: '', via_resource_name: , via_resource_id:, user:)
+ def query_search(via_resource_name:, via_resource_id:, user:, query: "")
# model_class = self.model
db_query = AuthorizationService.apply_policy(user, model_class)
if via_resource_name.present?
related_model = App.get_resource_by_name(via_resource_name).model
- db_query = related_model.find(via_resource_id).public_send(self.plural_name.downcase)
+ db_query = related_model.find(via_resource_id).public_send(plural_name.downcase)
end
new_query = []
- [self.search].flatten.each_with_index do |search_by, index|
- new_query.push 'or' if index != 0
+ [search].flatten.each_with_index do |search_by, index|
+ new_query.push "or" if index != 0
new_query.push "text(#{search_by}) ILIKE '%#{query}%'"
end
- db_query.where(new_query.join(' '))
+ db_query.where(new_query.join(" "))
end
def attached_file_fields
get_field_definitions.select do |field|
[Avo::Fields::FileField, Avo::Fields::FilesField].include? field.class
end
end
def fill_model(model, params)
# Map the received params to their actual fields
- fields_by_database_id = self.get_field_definitions.map { |field| [field.database_id(model).to_s, field] }.to_h
+ fields_by_database_id = get_field_definitions.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
@@ -237,58 +233,58 @@
def authorization
Avo::Services::AuthorizationService.new(user, model)
end
def file_hash
- content_to_be_hashed = ''
+ content_to_be_hashed = ""
# resource file hash
- resource_path = Rails.root.join('app', 'avo', 'resources', "#{name.underscore}.rb").to_s
+ resource_path = Rails.root.join("app", "avo", "resources", "#{name.underscore}.rb").to_s
if File.file? resource_path
content_to_be_hashed += File.read(resource_path)
end
# policy file hash
- policy_path = Rails.root.join('app', 'policies', "#{name.underscore}_policy.rb").to_s
+ policy_path = Rails.root.join("app", "policies", "#{name.underscore}_policy.rb").to_s
if File.file? policy_path
content_to_be_hashed += File.read(policy_path)
end
Digest::MD5.hexdigest(content_to_be_hashed)
end
def cache_hash(parent_model)
if parent_model.present?
- [self.model, self.file_hash, parent_model]
+ [model, file_hash, parent_model]
else
- [self.model, self.file_hash]
+ [model, file_hash]
end
end
# For :new views we're hydrating the model with the values from the resource's default attribute.
# We will not overwrite any attributes that come pre-filled in the model.
def hydrate_model_with_default_values
default_values = get_fields.select do |field|
!field.computed
end
- .map do |field|
- id = field.id
- value = field.value
+ .map do |field|
+ id = field.id
+ value = field.value
- if field.respond_to? :foreign_key
- id = field.foreign_key.to_sym
+ if field.respond_to? :foreign_key
+ id = field.foreign_key.to_sym
- reflection = @model._reflections[@params[:via_relation]]
+ reflection = @model._reflections[@params[:via_relation]]
- if reflection.present? && reflection.foreign_key.present?
- value = @params[:via_resource_id]
- end
- end
+ if reflection.present? && reflection.foreign_key.present?
+ value = @params[:via_resource_id]
+ end
+ end
- [id, value]
- end
- .to_h
- .select do |id, value|
+ [id, value]
+ end
+ .to_h
+ .select do |id, value|
value.present?
end
default_values.each do |id, value|
if @model.send(id).nil?