module SmartAnswer
  module Question
    class CountrySelect < MultipleChoice
      def initialize(name, options = {}, &block)
        @include_uk = options.delete(:include_uk)
        @use_legacy_data = options.delete(:use_legacy_data)
        options = country_list
        super(name, options, &block)
      end

      def options
        country_list
      end

      def country_list
        @countries ||= load_countries
      end

      def valid_option?(option)
        options.map{|v| v.slug}.include? (option.to_s)
      end

    private

      def load_countries
        countries = @use_legacy_data ? LegacyCountry.all : WorldLocation.all
        if @include_uk
          countries
        else
          countries.reject {|c| c.slug == 'united-kingdom' }
        end
      end
    end
  end
end

module SmartAnswer
  module Question
    class CountrySelect < Base
      def initialize(name, options = {}, &block)
        @include_uk = options.delete(:include_uk)
        @use_legacy_data = options.delete(:use_legacy_data)
        super(name, options, &block)
      end

      def options
        country_list
      end

      def country_list
        @countries ||= load_countries
      end

      def valid_option?(option)
        options.map{|v| v.slug}.include? (option.to_s)
      end

      def parse_input(raw_input)
        raise SmartAnswer::InvalidResponse, "Illegal option #{raw_input} for #{name}", caller unless valid_option?(raw_input)
        super
      end

    private

      def load_countries
        countries = @use_legacy_data ? LegacyCountry.all : WorldLocation.all
        if @include_uk
          countries
        else
          countries.reject {|c| c.slug == 'united-kingdom' }
        end
      end
    end
  end
end

Fix the CountrySelect initialize to not load options
Fix the CountrySelect initialize to not load options

This was unnecessary and seemed to be incorrect, maybe a hangover from
inheriting from MultipleChoice.  This change means it doesn't need to
load the countries from the API on initialise, which means it doesn't
need to load them when registering with Panopticon... which is nice.

It no longer inherits form MultipleChoice as there was no real reason to.
