Sha256: 7659070acd0602bdc50bda08ba7cdf729016fb8f897d3dc41d058eac6bbf90ab

Contents?: true

Size: 1.04 KB

Versions: 89

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 :new, :edit

        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 resource
        Avo::App.get_resource_by_model_name @model.class
      end

      def frame_url
        Avo::Services::URIService.parse(@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_model = nil
        else
          related_class = model.class.reflections[name.to_s.downcase].class_name
          related_model = related_class.safe_constantize.find value
        end

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

        model
      end
    end
  end
end

Version data entries

89 entries across 89 versions & 1 rubygems

Version Path
avo-2.29.0 lib/avo/fields/has_one_field.rb
avo-2.28.3.pre.pr1646 lib/avo/fields/has_one_field.rb
avo-2.28.2.pre.pr1642 lib/avo/fields/has_one_field.rb
avo-2.28.1.pre.pr1642 lib/avo/fields/has_one_field.rb
avo-2.28.0 lib/avo/fields/has_one_field.rb
avo-2.27.2.pre.pr1606 lib/avo/fields/has_one_field.rb
avo-2.27.1 lib/avo/fields/has_one_field.rb
avo-2.27.0 lib/avo/fields/has_one_field.rb
avo-2.26.3.pre.pr1601 lib/avo/fields/has_one_field.rb
avo-2.26.2.pre.pr1579 lib/avo/fields/has_one_field.rb
avo-2.26.1.pr1584.pre.1 lib/avo/fields/has_one_field.rb
avo-2.26.0 lib/avo/fields/has_one_field.rb
avo-2.25.0 lib/avo/fields/has_one_field.rb
avo-2.24.1 lib/avo/fields/has_one_field.rb
avo-2.24.0 lib/avo/fields/has_one_field.rb
avo-2.23.3.pre.1.pr1529 lib/avo/fields/has_one_field.rb
avo-2.23.2 lib/avo/fields/has_one_field.rb
avo-2.23.1 lib/avo/fields/has_one_field.rb
avo-2.23.0 lib/avo/fields/has_one_field.rb
avo-2.22.0 lib/avo/fields/has_one_field.rb