Sha256: eaf90f20b0083e888a600be5dda05201318432cf372a70e5ded0d522201ad704

Contents?: true

Size: 1 KB

Versions: 43

Compression:

Stored size: 1 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
        field_label
      end

      def frame_url
        Avo::Services::URIService.parse(field_resource.record_path)
          .append_paths(id, value.id)
          .append_query(query_params)
          .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

43 entries across 43 versions & 1 rubygems

Version Path
avo-3.5.8 lib/avo/fields/has_one_field.rb
avo-3.5.7 lib/avo/fields/has_one_field.rb
avo-3.5.6 lib/avo/fields/has_one_field.rb
avo-3.5.6.beta1 lib/avo/fields/has_one_field.rb
avo-3.5.5 lib/avo/fields/has_one_field.rb
avo-3.5.4 lib/avo/fields/has_one_field.rb
avo-3.5.3 lib/avo/fields/has_one_field.rb
avo-3.5.2 lib/avo/fields/has_one_field.rb
avo-3.5.1 lib/avo/fields/has_one_field.rb
avo-3.5.0 lib/avo/fields/has_one_field.rb
avo-3.4.4 lib/avo/fields/has_one_field.rb
avo-3.4.3 lib/avo/fields/has_one_field.rb
avo-3.4.2 lib/avo/fields/has_one_field.rb
avo-3.4.1 lib/avo/fields/has_one_field.rb
avo-3.4.0 lib/avo/fields/has_one_field.rb
avo-3.3.6 lib/avo/fields/has_one_field.rb
avo-3.3.5 lib/avo/fields/has_one_field.rb
avo-3.3.4 lib/avo/fields/has_one_field.rb
avo-3.3.3 lib/avo/fields/has_one_field.rb
avo-3.3.2 lib/avo/fields/has_one_field.rb