Sha256: a826aa719301c25cdb50aa3f64936a166066c00da2d2183060f1691dd4aec15a

Contents?: true

Size: 1.52 KB

Versions: 33

Compression:

Stored size: 1.52 KB

Contents

class LinkDecisionHelper
  ALLOWED_TYPES = [
    NAVBAR_LOGGED_IN = :n_logged_in,
    NAVBAR_LOGGED_OUT = :n_logged_out
  ]

  DEFAULT_TITLE = 'Default Title'
  DEFAULT_URL = '/' # root

  class NotOnAllowListError < StandardError; end;

  def self.clear_type!(type:)
    raise "Unexpected type [#{type}]. Expected #{ALLOWED_TYPES}" unless ALLOWED_TYPES.include?(type)
    Rails.application.config.public_send("#{type}=", [])
  end

  def initialize(title:, url:, type:, display: true, default_type: nil, config: nil, _blank: false)
    raise NotOnAllowListError, "Unexpected type [#{type}]. Expected #{ALLOWED_TYPES}" unless ALLOWED_TYPES.include?(type)

    @config = config
    @type = type
    @url = url
    @title = title
    @display = display
    @_blank = _blank

    if default_type && ALLOWED_TYPES.include?(default_type)
      assign_default!
    elsif default_type && !ALLOWED_TYPES.include?(default_type)
      raise NotOnAllowListError, 'unexpected default value'
    end
  end

  def assign!(index: -1)
    config.public_send(@type).insert(index, self)
  end

  def url
    get_value(@url)
  end

  def title
    get_value(@title)
  end

  def blank?
    @_blank
  end

  def display?(current_user)
    get_value(@display, current_user)
  end

  private

  def get_value(thing, *args)
    thing.is_a?(Proc) ? thing.call(*args) : thing
  end

  def assign_default!
    case @type
    when NAVBAR_LOGGED_IN
      @url = DEFAULT_URL
      @title = DEFAULT_TITLE
    end
  end

  def config
    @config ||= Rails.application.config
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
rails_base-0.60.0 lib/link_decision_helper.rb
rails_base-0.58.0 lib/link_decision_helper.rb
rails_base-0.57.0 lib/link_decision_helper.rb
rails_base-0.56.0 lib/link_decision_helper.rb
rails_base-0.55.0 lib/link_decision_helper.rb
rails_base-0.54.0 lib/link_decision_helper.rb
rails_base-0.53.1 lib/link_decision_helper.rb
rails_base-0.53.0 lib/link_decision_helper.rb
rails_base-0.52.3 lib/link_decision_helper.rb
rails_base-0.52.1 lib/link_decision_helper.rb
rails_base-0.52.0 lib/link_decision_helper.rb
rails_base-0.51.1 lib/link_decision_helper.rb
rails_base-0.51.0 lib/link_decision_helper.rb