Sha256: 0c2d1d95e09648c7aadcad0a63d03d8ed8def62313625a7681a5baf27faea16b

Contents?: true

Size: 1.28 KB

Versions: 12

Compression:

Stored size: 1.28 KB

Contents

# Note
# If impressionist_counter_cache_options[:counter_cache] is false(default)
# it won't even run this class
module Impressionist

  class UpdateCounters
    attr_reader :receiver, :klass

    def initialize(receiver)
      @receiver = receiver
      @klass = receiver.class
    end

    def update
      klass.
      update_counters(id, column_name => result)
    end

    private

    def result
      impressions_total - impressions_cached
    end

    # Count impressions based on unique_filter
    # default is :ip_address when unique: true
    def impressions_total
      receiver.impressionist_count filter
    end

    # Fetch impressions from a receiver's column
    def impressions_cached
      receiver.send(column_name) || 0
    end

    def filter
      {:filter => unique_filter}
    end

    # :filter gets assigned to :ip_address as default
    # One could do
    # is_impressionable :counter_cache => true,
    # :unique => :any_other_filter
    def unique_filter
      Symbol === unique ?
      unique :
      :ip_address
    end

    def unique
      cache_options[:unique]
    end

    def column_name
      cache_options[:column_name].to_s
    end

    def cache_options
      klass.
      impressionist_counter_cache_options
    end

    def id
      receiver.id
    end

  end

end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
impressionist-1.5.2 lib/impressionist/update_counters.rb
impressionist2-1.5.5 lib/impressionist/update_counters.rb
impressionist2-1.5.4 lib/impressionist/update_counters.rb
impressionist2-1.5.3 lib/impressionist/update_counters.rb
impressionist2-1.5.1 lib/impressionist/update_counters.rb
impressionist-1.5.1 lib/impressionist/update_counters.rb
impressionist-1.4.13 lib/impressionist/update_counters.rb
impressionist-1.4.12 lib/impressionist/update_counters.rb
impressionist-1.4.11 lib/impressionist/update_counters.rb
impressionist-1.4.10 lib/impressionist/update_counters.rb
impressionist-1.4.9 lib/impressionist/update_counters.rb
impressionist-1.4.8 lib/impressionist/update_counters.rb