lib/avo/base_resource.rb in avo-1.6.3.pre.3 vs lib/avo/base_resource.rb in avo-1.6.4.pre.1

- old
+ new

@@ -85,35 +85,24 @@ fields end def get_fields(panel: nil, reflection: nil) - fields = get_field_definitions + fields = get_field_definitions.select do |field| + field.send("show_on_#{@view}") + end .select do |field| - field.send("show_on_#{@view}") - end + field.visible? + end .select do |field| - field.visible? - end - .select do |field| - # Strip out the reflection field in index queries with a parent association. - if reflection.present? && - reflection.options.present? && - field.respond_to?(:polymorphic_as) && - field.polymorphic_as.to_s == reflection.options[:as].to_s - next - end - # if !field.respond_to?(:polymorphic_as) && - # field.respond_to?(:foreign_key) && - # reflection.present? && - # reflection.respond_to?(:foreign_key) && - # reflection.foreign_key != field.foreign_key - # next - # end - + unless field.respond_to?(: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 @@ -259,21 +248,18 @@ # Map the received params to their actual fields fields_by_database_id = get_field_definitions .reject do |field| field.computed end - .map do |field| - [field.database_id(model).to_s, field] - end - .to_h + .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? - model = field.fill_field model, key, value, params + model = field.fill_field model, key, value end model end @@ -307,35 +293,32 @@ end end # 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 + default_values = get_fields.select do |field| + !field.computed + end .map do |field| id = field.id value = field.value - if field.type == "belongs_to" + if field.respond_to? :foreign_key id = field.foreign_key.to_sym reflection = @model._reflections[@params[:via_relation]] - if field.polymorphic_as.present? && field.types.map(&:to_s).include?(@params["via_relation_class"]) - value = @params["via_relation_class"].safe_constantize.find(@params[:via_resource_id]) - elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s + if reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s value = @params[:via_resource_id] end end [id, value] end .to_h .select do |id, value| - value.present? - end + value.present? + end default_values.each do |id, value| if @model.send(id).nil? @model.send("#{id}=", value) end