Sha256: d488e718f9bf734d99b225b6eb2576e604500bc24051d4f765ccec99aaddb76c

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

class DashboardOrderGroup
  include EnumField::DefineEnum

  attr_reader :code, :slug, :disabled

  def initialize(code, slug = nil, disabled = false)
    @code = code.to_sym
    @slug = (slug || @code).to_s
    @disabled = disabled
  end

  def title
    I18n.t(code, scope: [:account, :dashboard, :type_title])
  end

  def disabled?
    @disabled
  end

  def notify?(notifications)
    case @code
    when :processing
      (notifications.order_messages > 0) || (notifications.translator_assigned > 0)
    when :completed
      notifications.order_completed > 0
    when :canceled
      notifications.order_canceled > 0
    when :payments
      (notifications.order_full_refund > 0) || (notifications.order_partial_refund > 0)
    end
  end

  define_enum do |builder|
    builder.member :processing, object: new(:processing, '')
    builder.member :completed,  object: new(:completed, :completed)
    builder.member :canceled,   object: new(:canceled, :canceled)
    builder.member :payments,   object: new(:payments, :payments)
  end

  all.map(&:code).each do |code|
    define_method "#{code}?" do
      @code == code
    end
  end

  class << self
    def available_slugs
      DashboardOrderGroup.all.map(&:slug).select(&:present?)
    end

    def enabled
      all.reject(&:disabled?)
    end

    def find_by_slug(slug)
      all.select { |group| group.slug == slug }.first || first
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translation_cms-0.1.5 app/models/enums/dashboard_order_group.rb