Sha256: 6d64f46c06ca0079c73c7bf87ab011150446c1ff7d9cd2f7cc7767061ac117af

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module Avo
  module Fields
    class HasOneField < FrameBaseField
      attr_reader :attach_fields,
        :attach_scope

      def initialize(id, **args, &block)
        hide_on :forms

        super(id, **args, &block)

        @placeholder ||= I18n.t "avo.choose_an_option"
        @attach_fields = args[:attach_fields]
        @attach_scope = args[:attach_scope]
      end

      def label
        field_label
      end

      def frame_url
        Avo::Services::URIService.parse(field_resource.record_path)
          .append_paths(id, value.to_param)
          .append_query(query_params)
          .to_s
      end

      def fill_field(record, key, value, params)
        if value.blank?
          related_record = nil
        else
          related_class = record.class.reflections[name.to_s.downcase].class_name
          related_resource = Avo.resource_manager.get_resource_by_model_class(related_class)
          related_record = related_resource.find_record value
        end

        record.public_send(:"#{key}=", related_record)

        record
      end

      def is_searchable? = false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
avo-3.16.4 lib/avo/fields/has_one_field.rb
avo-3.16.3 lib/avo/fields/has_one_field.rb