Sha256: 9b0f484b4057c613a14dfccae9a4d116e5ba7c6c15846ce8087e5606a91ae0a1

Contents?: true

Size: 1.54 KB

Versions: 20

Compression:

Stored size: 1.54 KB

Contents

module Avo
  module Fields
    class HasBaseField < BaseField
      attr_accessor :display
      attr_accessor :scope

      def initialize(id, **args, &block)
        super(id, **args, &block)

        @scope = args[:scope].present? ? args[:scope] : nil
        @display = args[:display].present? ? args[:display] : :show
        @searchable = args[:searchable] == true
      end

      def searchable
        @searchable && ::Avo::App.license.has_with_trial(:searchable_associations)
      end

      def resource
        Avo::App.get_resource_by_model_name @model.class
      end

      def turbo_frame
        "#{self.class.name.demodulize.to_s.underscore}_#{display}_#{id}"
      end

      def frame_url
        "#{@resource.record_path}/#{id}?turbo_frame=#{turbo_frame}"
      end

      # The value
      def field_value
        value.send(database_value)
      rescue
        nil
      end

      # What the user sees in the text field
      def field_label
        value.send(target_resource.class.title)
      rescue
        nil
      end

      def target_resource
        if @model._reflections[id.to_s].klass.present?
          Avo::App.get_resource_by_model_name @model._reflections[id.to_s].klass.to_s
        elsif @model._reflections[id.to_s].options[:class_name].present?
          Avo::App.get_resource_by_model_name @model._reflections[id.to_s].options[:class_name]
        else
          Avo::App.get_resource_by_name id.to_s
        end
      end

      def placeholder
        @placeholder || I18n.t("avo.choose_an_option")
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
avo-1.25.2 lib/avo/fields/has_base_field.rb
avo-2.4.1 lib/avo/fields/has_base_field.rb
avo-2.4.0 lib/avo/fields/has_base_field.rb
avo-2.3.1.pre.6 lib/avo/fields/has_base_field.rb
avo-2.3.1.pre.5 lib/avo/fields/has_base_field.rb
avo-2.3.1.pre.4 lib/avo/fields/has_base_field.rb
avo-2.3.1.pre.3 lib/avo/fields/has_base_field.rb
avo-2.3.1.pre.2 lib/avo/fields/has_base_field.rb
avo-2.3.1.pre.1 lib/avo/fields/has_base_field.rb
avo-2.3.0 lib/avo/fields/has_base_field.rb
avo-2.2.2 lib/avo/fields/has_base_field.rb
avo-2.2.1 lib/avo/fields/has_base_field.rb
avo-2.2.0 lib/avo/fields/has_base_field.rb
avo-2.1.2.pre2 lib/avo/fields/has_base_field.rb
avo-2.1.2.pre1 lib/avo/fields/has_base_field.rb
avo-1.25.1 lib/avo/fields/has_base_field.rb
avo-2.1.1 lib/avo/fields/has_base_field.rb
avo-2.1.0 lib/avo/fields/has_base_field.rb
avo-2.0.0 lib/avo/fields/has_base_field.rb
avo-1.25.0 lib/avo/fields/has_base_field.rb