Skip to content

Latest commit

 

History

History
57 lines (36 loc) · 2.2 KB

README.rdoc

File metadata and controls

57 lines (36 loc) · 2.2 KB

ActivityCounter

Counter

Figure we have an Event model that has many invitations. We want to know how many Invitations have status: accepted, rejected, pending or maybe.

To do so we define a Source module and a Cached module we will load then into classes that match these roles.

Counter Module

  This is the module were counting actions are defined. This method has the following fields:

    - :source_class
    - :source_id
       class and id gives us the source model instance.

    - :source_relation is the name of the relation that will let us know
      the collection of items.

    - :name is the name of the counter. This is the status we want to count.
    - :count is the number of items count.

There are two default counters:

all: counts all elements
new: counts new before some "status" field is updated

:default => true # it defines [:all, :new] counters

:default => [:new] # it will define :new counter only

:default => [:simple, :new] # adds :new and :simple. This last one only counts new items, it's descreased manualy by calling reset!

Custom counters by convention

The counted model, the one belonging to another, should have a status column (it can be specified with the param :status_field if it's not called 'status') that can 
be compared when is updated or the current status can be seen to decrease the counter before it's destroyed.

For this purpose three methods are created for every status:

  @invitation.status.current (current status)
  @invitation.status.before (status before update)
  @invitation.status.after (status after update)

Source class collection accessor method has method for every counter so we can access them.
  @event.invitations.pending => invitations scoped by pending status
  @event.invitations.pending.count => pending invitations count

Default :counter_cache’s

:new
   increase => it is being increased when a new item is created
   decrease => when this item is updated, the counter will decrease it
:all
   increase => it is being increased when a new item is created
   decrease => it is being decreased when an item is destroyed