Sha256: 0cf34ecd51202379fd40720d437440b331a6bd82ac88e16ab6a1c5bbc75cd73e

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# == Schema Information
#
# Table name: counter_values
#
#  id          :integer          not null, primary key
#  type        :string           indexed
#  name        :string           indexed
#  value       :integer          default(0)
#  parent_type :string           indexed => [parent_id]
#  parent_id   :integer          indexed => [parent_type]
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#
class Counter::Value < ApplicationRecord
  def self.table_name_prefix
    "counter_"
  end

  belongs_to :parent, polymorphic: true, optional: true

  validates_numericality_of :value

  def self.find_counter counter
    counter_name = if counter.is_a?(String) || counter.is_a?(Symbol)
      counter.to_s
    elsif counter.is_a?(Class) && counter.ancestors.include?(Counter::Definition)
      definition = counter.instance
      raise "Unable to find counter #{definition.name} via Counter::Value.find_counter. Use must use #{definition.model}#find_counter}" unless definition.global?

      counter.instance.record_name
    else
      counter.to_s
    end

    find_or_initialize_by name: counter_name
  end

  include Counter::Definable
  include Counter::Hooks
  include Counter::Increment
  include Counter::Reset
  include Counter::Recalculatable
  include Counter::Verifyable
  include Counter::Summable
  include Counter::Conditional
  # include Counter::Hierarchical
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
counterwise-0.1.1 app/models/counter/value.rb
counterwise-0.1.0 app/models/counter/value.rb