Sha256: f38b56f82f99e6b74a7ffbadf1097fa860e3431e2c6c9b4ad1883dfd9c20ad57

Contents?: true

Size: 1.15 KB

Versions: 35

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

class Avo::PhotoObject
  def initialize(resource:)
    @resource = resource
  end

  delegate :record, to: :@resource
  delegate :view, to: :@resource

  def options
    @options ||= if @resource.class&.send(key).present?
      @resource.class&.send(key)
    else
      {}
    end
  end

  def value
    return unless options.fetch(:source, nil).present?

    if options[:source].is_a?(Symbol)
      record.send(options[:source])
    elsif options[:source].respond_to?(:call)
      Avo::ExecutionContext.new(target: options[:source], record:, resource: @resource, view:).handle
    end
  end

  def visible_on
    @visible_on ||= Array.wrap(options[:visible_on] || [:show, :forms])
  end

  def visible_in_current_view?
    send(:"visible_on_#{view}?")
  end

  def present?
    value.present?
  end

  def visible_on_index? = visible_in_either?(:index, :display)

  def visible_on_show? = visible_in_either?(:show, :display)

  def visible_on_edit? = visible_in_either?(:edit, :forms)

  def visible_on_new? = visible_in_either?(:new, :forms)

  private

  def visible_in_either?(*options)
    options.intersection(visible_on).present?
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
avo-3.11.3 lib/avo/photo_object.rb
avo-3.11.2 lib/avo/photo_object.rb
avo-3.11.1 lib/avo/photo_object.rb
avo-3.11.0 lib/avo/photo_object.rb
avo-3.10.10 lib/avo/photo_object.rb
avo-3.10.9 lib/avo/photo_object.rb
avo-3.10.8 lib/avo/photo_object.rb
avo-3.10.7 lib/avo/photo_object.rb
avo-3.10.6 lib/avo/photo_object.rb
avo-3.10.5 lib/avo/photo_object.rb
avo-3.10.4 lib/avo/photo_object.rb
avo-3.10.3 lib/avo/photo_object.rb
avo-3.10.2 lib/avo/photo_object.rb
avo-3.10.1 lib/avo/photo_object.rb
avo-3.10.0 lib/avo/photo_object.rb