Sha256: 7518edfc6426b5c50735d467d45a7f4c738a87eef7d6757512f45c7573ea349a

Contents?: true

Size: 1007 Bytes

Versions: 8

Compression:

Stored size: 1007 Bytes

Contents

module Avo
  module Fields
    class CountryField < Field
      def initialize(name, **args, &block)
        @defaults = {
          sortable: true,
          component: 'country-field',
          placeholder: '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

8 entries across 8 versions & 1 rubygems

Version Path
avo-0.3.2 lib/avo/app/fields/country_field.rb
avo-0.3.1 lib/avo/app/fields/country_field.rb
avo-0.2.5 lib/avo/app/fields/country_field.rb
avo-0.2.4 lib/avo/app/fields/country_field.rb
avo-0.2.3 lib/avo/app/fields/country_field.rb
avo-0.2.2 lib/avo/app/fields/country_field.rb
avo-0.2.1 lib/avo/app/fields/country_field.rb
avo-0.2.0 lib/avo/app/fields/country_field.rb