Sha256: 163874163e58d44a00a8f41789ea2373c7df8a95e5f914f2e224ddf0110f5689

Contents?: true

Size: 1.66 KB

Versions: 23

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Avo
  module Fields
    class LocationField < BaseField
      attr_reader :stored_as

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

        @stored_as = args[:stored_as].present? ? args[:stored_as] : nil # You can pass it an array of db columns [:latitude, :longitude]
      end

      def value_as_array?
        stored_as.is_a?(Array) && stored_as.count == 2
      end

      def as_lat_long_field_id(get)
        if get == :lat
          "#{id}[#{stored_as.first}]"
        elsif get == :long
          "#{id}[#{stored_as.last}]"
        end
      end

      def as_lat_long_placeholder(get)
        if get == :lat
          "Enter #{stored_as.first}"
        elsif get == :long
          "Enter #{stored_as.last}"
        end
      end

      def as_lat_long_value(get)
        if get == :lat
          model.send(stored_as.first)
        elsif get == :long
          model.send(stored_as.last)
        end
      end

      def fill_field(model, key, value, params)
        if value_as_array?
          latitude_field, longitude_field = stored_as
          model.send("#{latitude_field}=", value[latitude_field])
          model.send("#{longitude_field}=", value[longitude_field])
          model
        else
          super(model, key, value.split(","), params)
        end
      end

      def to_permitted_param
        if value_as_array?
          [:"#{id}", "#{id}": {}]
        else
          super
        end
      end

      def value_present?
        return value.first.present? && value.second.present? if value.is_a?(Array) && value.count == 2

        value.present?
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
avo-2.35.0 lib/avo/fields/location_field.rb
avo-2.34.7.pre.1 lib/avo/fields/location_field.rb
avo-2.34.6 lib/avo/fields/location_field.rb
avo-2.34.5 lib/avo/fields/location_field.rb
avo-2.34.4 lib/avo/fields/location_field.rb
avo-2.34.4.pre.1 lib/avo/fields/location_field.rb
avo-2.34.3 lib/avo/fields/location_field.rb
avo-2.34.2 lib/avo/fields/location_field.rb
avo-2.34.1 lib/avo/fields/location_field.rb
avo-2.34.0 lib/avo/fields/location_field.rb
avo-2.33.3 lib/avo/fields/location_field.rb
avo-2.33.3.pre.2 lib/avo/fields/location_field.rb
avo-2.33.3.pre.1 lib/avo/fields/location_field.rb
avo-2.33.2 lib/avo/fields/location_field.rb
avo-2.33.1 lib/avo/fields/location_field.rb
avo-2.33.0 lib/avo/fields/location_field.rb
avo-2.32.6 lib/avo/fields/location_field.rb
avo-2.32.5 lib/avo/fields/location_field.rb
avo-2.32.4 lib/avo/fields/location_field.rb
avo-2.32.3 lib/avo/fields/location_field.rb