Sha256: 469f11f92d3ef0a95b59595cc1eb2387864c8c8021303e902176298d949c929e

Contents?: true

Size: 1.16 KB

Versions: 9

Compression:

Stored size: 1.16 KB

Contents

module PresentationToggles
  extend ActiveSupport::Concern

  included do
    field :presentation_toggles, type: Hash, default: default_presentation_toggles
    validates :promotion_choice_url, presence: true, if: :promotes_something?
    validates :promotion_choice, inclusion: { in: %w(none organ_donor register_to_vote) }
  end

  def promotion_choice=(value)
    promotion_choice_key["choice"] = value
  end

  def promotion_choice_url=(value)
    promotion_choice_key['url'] = value
  end

  def promotion_choice
    choice = promotion_choice_key["choice"]
    choice.empty? ? "none" : choice
  end

  def promotes_something?
    promotion_choice != 'none'
  end

  def promotion_choice_url
    promotion_choice_key["url"]
  end

  def promotion_choice_key
    unless presentation_toggles.key? 'promotion_choice'
      presentation_toggles['promotion_choice'] = self.class.default_presentation_toggles['promotion_choice']
    end
    presentation_toggles['promotion_choice']
  end

  module ClassMethods
    def default_presentation_toggles
      {
        'promotion_choice' =>
          {
            'choice' => '',
            'url' => ''
          }
      }
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
govuk_content_models-42.0.1 lib/govuk_content_models/presentation_toggles.rb
govuk_content_models-42.0.0 lib/govuk_content_models/presentation_toggles.rb
govuk_content_models-41.1.1 lib/govuk_content_models/presentation_toggles.rb
govuk_content_models-41.1.0 lib/govuk_content_models/presentation_toggles.rb
govuk_content_models-41.0.0 lib/govuk_content_models/presentation_toggles.rb
govuk_content_models-40.0.0 lib/govuk_content_models/presentation_toggles.rb
govuk_content_models-39.0.0 lib/govuk_content_models/presentation_toggles.rb
govuk_content_models-38.0.0 lib/govuk_content_models/presentation_toggles.rb
govuk_content_models-37.0.0 lib/govuk_content_models/presentation_toggles.rb