Sha256: 1bd166d0d72dfb9ab8a264862815a31c21089c97f78bcafb5d7b8ec0d57cc98f

Contents?: true

Size: 1.99 KB

Versions: 13

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

class Avo::Views::ResourceEditComponent < Avo::ResourceComponent
  include Avo::ApplicationHelper

  def initialize(resource: nil, record: nil, actions: [], view: :edit)
    @resource = resource
    @record = record
    @actions = actions
    @view = view
  end

  def title
    @resource.default_panel_name
  end

  def back_path
    return resource_view_path if via_resource?
    return resources_path if via_index?

    if is_edit? && Avo.configuration.resource_default_view == :show # via resource show or edit page
      return helpers.resource_path(record: @resource.record, resource: @resource)
    end

    resources_path
  end

  def resources_path
    helpers.resources_path(resource: @resource)
  end

  def resource_view_path
    helpers.resource_view_path(record: association_resource.record, resource: association_resource)
  end

  def can_see_the_destroy_button?
    return super if is_edit? && Avo.configuration.resource_default_view == :edit

    false
  end

  # The save button is dependent on the edit? policy method.
  # The update? method should be called only when the user clicks the Save button so the developer gets access to the params from the form.
  def can_see_the_save_button?
    @resource.authorization.authorize_action @view, raise_exception: false
  end

  private

  def via_index?
    params[:via_view] == "index"
  end

  def is_edit?
    view.in?([:edit, :update])
  end

  def form_method
    return :put if is_edit?

    :post
  end

  def form_url
    if is_edit?
      helpers.resource_path(
        record: @resource.record,
        resource: @resource
      )
    else
      helpers.resources_path(
        resource: @resource,
        via_relation_class: params[:via_relation_class],
        via_relation: params[:via_relation],
        via_record_id: params[:via_record_id]
      )
    end
  end

  # Render :show view for read only trix fields
  def view_for(field)
    field.is_a?(Avo::Fields::TrixField) && field.is_disabled? ? :show : view
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
avo-3.0.1.beta6 app/components/avo/views/resource_edit_component.rb
avo-3.0.1.beta7 app/components/avo/views/resource_edit_component.rb
avo-3.0.1.beta5 app/components/avo/views/resource_edit_component.rb
avo-3.0.1.beta2 app/components/avo/views/resource_edit_component.rb
avo-3.0.1.beta4 app/components/avo/views/resource_edit_component.rb
avo-3.0.1.beta3 app/components/avo/views/resource_edit_component.rb
avo-3.0.0.beta1 app/components/avo/views/resource_edit_component.rb
avo-3.0.1.beta1 app/components/avo/views/resource_edit_component.rb
avo-3.0.0.pre17 app/components/avo/views/resource_edit_component.rb
avo-3.0.0.pre18 app/components/avo/views/resource_edit_component.rb
avo-3.0.0.pre14 app/components/avo/views/resource_edit_component.rb
avo-3.0.0.pre16 app/components/avo/views/resource_edit_component.rb
avo-3.0.0.pre15 app/components/avo/views/resource_edit_component.rb