Sha256: ea3c4950e383b5ba7f8ca6adeace6f45177912918f3117998050e670cd7b9f15

Contents?: true

Size: 1019 Bytes

Versions: 10

Compression:

Stored size: 1019 Bytes

Contents

module Avo
  module Fields
    class CountryField < Field
      def initialize(name, **args, &block)
        @defaults = {
          sortable: true,
          component: 'country-field',
          placeholder: I18n.t('avo.choose_a_country'),
        }

        super(name, **args, &block)

        @countries = ISO3166::Country.translations.sort_by { |code, name| name }.to_h
        @display_code = args[:display_code].present? ? args[:display_code] : false
      end

      def hydrate_field(fields, model, resource, view)
        if [:show, :index].include? view
          value = fields[:computed_value] || fields[:value]

          # Just return the DB code.
          if @display_code
            return {
              value: value
            }
          end

          # Compute and get the translated value.
          return {
            value: @countries[value],
          }
        end

        {
          countries: @countries,
          display_code: @display_code
        }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
avo-0.4.10 lib/avo/app/fields/country_field.rb
avo-0.4.9 lib/avo/app/fields/country_field.rb
avo-0.4.8 lib/avo/app/fields/country_field.rb
avo-0.4.7 lib/avo/app/fields/country_field.rb
avo-0.4.6 lib/avo/app/fields/country_field.rb
avo-0.4.5 lib/avo/app/fields/country_field.rb
avo-0.4.4 lib/avo/app/fields/country_field.rb
avo-0.4.3 lib/avo/app/fields/country_field.rb
avo-0.4.2 lib/avo/app/fields/country_field.rb
avo-0.4.1 lib/avo/app/fields/country_field.rb