Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Commit

Permalink
bump version to 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhue committed Jan 13, 2018
1 parent 61338ac commit 31ff8f2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,98 @@ end

### Tracking

You can track views for ActiveRecord objects from your controllers:

```ruby
class ArticleController < ApplicationController

def index
@articles = Article.all
ahoy_view @articles
end

def show
@article = Article.find params[:id]
ahoy_view @article
end

end
```

**Note:** This will create an `Ahoy::Event` object for every record passed.

The `current_visit` object, if present, will be automatically associated with every visit.

You are also able to associate other records with views. [Learn more](#ahoy_viewer).

#### Types

You can pass an array of `types` to `ahoy_view`. Types allow you to track multiple types of visits. The `:types` option defaults to `[:visit]`.

```ruby
ahoy_view @article, types: [:view, :visitor, :returnee, :unique_visitor, :unique_returnee]
```

Here is a list of available types:

* `:visit` stores every call to `ahoy_view` for an object.

* `:visitor` stores only one call per visit to `ahoy_view`.

* `:returnee` ...

* `:unique_visitor` ...

* `:unique_returnee` ...

### `ahoy_viewable`

Add `ahoy_viewable` to an ActiveRecord class.

```ruby
class Article < ApplicationRecord
ahoy_viewable
end

a = Article.first

# All belonging Ahoy::Event objects that are a view
a.ahoy_views

# All viewer records that have a view object
a.ahoy_viewers

# Scope to order Article records by views
Article.trending

# Whether or not a is one of the 5 "most trending" articles
a.trending? 5
```

### `ahoy_viewer`

Add `ahoy_viewer` to an ActiveRecord class.

```ruby
class User < ApplicationRecord
ahoy_viewer
end

u = User.first

# All belonging Ahoy::Event objects that are a view
u.ahoy_visits

# All records that this user has taken a look at
u.ahoy_viewed
```

Here is how to associate a viewer with a view:

```ruby
ahoy_view @article, viewer: current_user
```

---

## To Do
Expand Down
24 changes: 16 additions & 8 deletions lib/ahoy/views/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@ module View

extend ActiveSupport::Concern

def ahoy_view objects, types = [:view]
if objects.kind_of? Array
objects.each do |object|
track_ahoy_view object, types
def ahoy_view objects, options = {}
defaults = {
types: [:view],
viewer: nil
}
options = defaults.merge! options

if current_visit
if objects.kind_of? Array
objects.each do |object|
track_ahoy_view object, options[:types], options[:viewer]
end
else
track_ahoy_view objects, options[:types], options[:viewer]
end
else
track_ahoy_view objects, types
end
end

private

def track_ahoy_view objects, types
def track_ahoy_view object, types, viewer
types.each do |name|
ahoy.track name, model: objects
ahoy.track name, visited: object, visitor: viewer, visit: current_visit
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/ahoy/views/views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ def process_view
if self.visited && self.name
case self.name
when 'visitor'
return false if self.class.visitors.visited_in_session.where(visited_id: visited.id, visited_type: visited.class.name).any?
return false if self.class.visitors.visited_in_session(self.visit).where(visited_id: visited.id, visited_type: visited.class.name).any?
when 'returnee'
return false if self.class.returnees.visited_in_session.where(visited_id: visited.id, visited_type: visited.class.name).any? && !self.class.visited.where(visited_id: visited.id, visited_type: visited.class.name).any?
return false if self.class.returnees.visited_in_session(self.visit).where(visited_id: visited.id, visited_type: visited.class.name).any? && !self.class.visited(self.visitor).where(visited_id: visited.id, visited_type: visited.class.name).any?
when 'unique_visitor'
return false if self.class.visitors.visited.where(visited_id: visited.id, visited_type: visited.class.name).any?
return false if self.class.visitors.visited(self.visitor).where(visited_id: visited.id, visited_type: visited.class.name).any?
when 'unique_returnee'
return false if self.class.returnees.visited.where(visited_id: visited.id, visited_type: visited.class.name).any? && !self.class.visited.where(visited_id: visited.id, visited_type: visited.class.name).any?
return false if self.class.returnees.visited(self.visitor).where(visited_id: visited.id, visited_type: visited.class.name).any? && !self.class.visited(self.visitor).where(visited_id: visited.id, visited_type: visited.class.name).any?
end
end
end
Expand Down

0 comments on commit 31ff8f2

Please sign in to comment.