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.beta17 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta18 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta19 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta15 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta16 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta14 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta12 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta13 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta10 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta11 lib/avo/fields/has_one_field.rb
avo-3.0.0.pre19 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta8 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta9 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta6 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta7 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta5 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta2 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta4 lib/avo/fields/has_one_field.rb
avo-3.0.1.beta3 lib/avo/fields/has_one_field.rb
avo-3.0.0.beta1 lib/avo/fields/has_one_field.rb