Sha256: 5407928c35ac76964cccaa16bea8e4da87ff3b3b885a0509f6815c8871c8a6ad

Contents?: true

Size: 1.04 KB

Versions: 26

Compression:

Stored size: 1.04 KB

Contents

module Avo
  module Fields
    class HasOneField < HasBaseField
      attr_accessor :relation_method

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

        super(id, **args, &block)

        @placeholder ||= I18n.t "avo.choose_an_option"

        @relation_method = name.to_s.parameterize.underscore
      end

      def label
        value.send(target_resource.title)
      end

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

      def fill_field(model, key, value, params)
        if value.blank?
          related_record = nil
        else
          related_class = model.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

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

        model
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
avo-3.0.1.beta1 lib/avo/fields/has_one_field.rb
avo-3.0.0.pre17 lib/avo/fields/has_one_field.rb
avo-3.0.0.pre18 lib/avo/fields/has_one_field.rb
avo-3.0.0.pre14 lib/avo/fields/has_one_field.rb
avo-3.0.0.pre16 lib/avo/fields/has_one_field.rb
avo-3.0.0.pre15 lib/avo/fields/has_one_field.rb