Impressionist Ruby Library

repository·master·Indexed 23 days ago

https://github.com/charlotte-ruby/impressionist

A Ruby library for tracking impressions (views/hits) of models. It supports ActiveRecord, Mongoid, and MongoMapper ORMs, providing mechanisms for counter caches and unique impression tracking via IP address or custom filters. Features include the is_impressionable method for model configuration and support for polymorphic relationships to an impressions table.

Tokens
1K
Snippets
2
Records
10
Agent score
81%

What's inside Impressionist

  1. Configure Impressionist for Mongoid ORM

    master

    If you are using Mongoid instead of ActiveRecord, generate the initializer with the Mongoid flag and configure the ORM setting.

    1. Run the generator:
    rails g impressionist --orm mongoid
    1. Update the generated config/initializer/impressionist.rb file:
    Impressionist.setup do |config|
      config.orm = :mongoid
    end
    rails g impressionist --orm mongoid
    
    # config/initializer/impressionist.rb
    Impressionist.setup do |config|
      config.orm = :mongoid
    end
  2. Migrate from v1.5.1 to v1.5.2

    master

    Version 1.5.2 introduces a new params column to the impressions table. If you are upgrading from version 1.5.1, you must manually add this column and its associated index via a migration before performing the gem upgrade to avoid errors.

    add_column :impressions, :params, :text
    
    add_index :impressions, [:impressionable_type, :impressionable_id, :params], :name => "poly_params_request_index", :unique => false
  3. Install Impressionist in a Rails application

    master

    To install Impressionist, add the gem to your Gemfile based on your Rails version, install with Bundler, generate the migration, and run it.

    For Rails 6:

    gem 'impressionist'

    For Rails 5 or lower:

    gem 'impressionist', '~>1.6.1'

    Then run:

    bundle install
    rails g impressionist
    rake db:migrate
  4. Configure counter cache for impressions

    master

    To enable automatic updating of impression counters on your model, use the is_impressionable method with the counter_cache: true option.

    When counter_cache is enabled, Impressionist will use the UpdateCounters logic to calculate the difference between total impressions and the currently cached value, then update the specified column on your model.

    Unique Impression Filtering

    You can control how 'unique' impressions are counted using the unique option:

    • Default behavior: If you set unique: true, the system defaults to filtering by :ip_address to count unique impressions.
    • Custom filter: You can provide a specific column name (e.g., :session_id) to unique: to define what constitutes a unique impression.
    • Disable uniqueness: Set unique: false to use :all as the filter, effectively counting every impression regardless of uniqueness.
  5. Make a model impressionable with is_impressionable

    master

    To track impressions on a model, include the Impressionist::IsImpressionable module and call the is_impressionable class method. This sets up a has_many association to an impressions table using a polymorphic :as => :impressionable relationship. By default, impressions are deleted when the parent model is destroyed (dependent: :delete_all).

    You can pass an options hash to is_impressionable to configure how impression data is cached.

  6. Configure impressionable models with is_impressionable

    master
    Call is_impressionable(options={}) within your MongoMapper document to set up the impression tracking relationship. This method automatically defines a many :impressions relationship with :as => :impressionable and sets :dependent => :delete_all to ensure impressions are cleaned up when the parent document is deleted. You can pass an options hash to configure the impression cache.