Module: TracksVisits::ActiveRecord::Visitable::ClassMethods

Defined in:
lib/tracks_visits/visitable.rb

Instance Method Summary collapse

Instance Method Details

#tracks_visits(options = {}) ⇒ Object

Make the model visitable, i.e. count/track visits by user/account of IP.

  • Adds a has_many :visits association to the model for easy retrieval of the detailed visits.

  • Adds a has_many :visitors association to the object.

  • Adds a has_many :visits associations to the visitor class.

Options

  • :options[:visit_class] - class of the model used for the visits. Defaults to Visit. This class will be dynamically created if not already defined. If the class is predefined, it must have in it the following definitions: belongs_to :visitable, :polymorphic => true belongs_to :visitor, :class_name => 'User', :foreign_key => :visitor_id replace user with the visitor class if needed.

  • :options[:visitor_class] - class of the model that creates the visit. Defaults to User or Account - auto-detected. This class will NOT be created, so it must be defined in the app. Use the IP address to prevent multiple visits from the same client.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tracks_visits/visitable.rb', line 37

def tracks_visits(options = {})
  send :include, ::TracksVisits::ActiveRecord::Visitable::InstanceMethods
  
  # Set default class names if not given.
  options[:visitor_class_name] ||= options[:from] || Visitor::DEFAULT_CLASS_NAME
  options[:visitor_class_name] = options[:visitor_class_name].to_s.classify
  
  options[:visit_class_name] = DEFAULT_VISIT_CLASS_NAME.to_s.classify
  
  options[:visitor_class] = options[:visitor_class_name].constantize rescue nil
  
  # Assocations: Visit class (e.g. Visit).
  options[:visit_class] = begin
    options[:visit_class_name].constantize
  rescue
    # If note defined...define it!
    Object.const_set(options[:visit_class_name].to_sym, Class.new(::ActiveRecord::Base)).class_eval do
      belongs_to :visitable, :polymorphic => true
      belongs_to :visitor, :polymorphic => true
    end
    options[:visit_class_name].constantize
  end
  
  # Save the initialized options for this class.
  write_inheritable_attribute(:tracks_visits_options, options.slice(:visit_class, :visitor_class))
  class_inheritable_reader :tracks_visits_options
  
  # Assocations: Visitor class (e.g. User).
  if Object.const_defined?(options[:visitor_class].name.to_sym)
    options[:visitor_class].class_eval do
      has_many :visits,
        :foreign_key => :visitor_id,
        :class_name => options[:visit_class].name
    end
  end
  
  # Assocations: Visitable class (e.g. Page).
  self.class_eval do
    has_many options[:visit_class].name.tableize.to_sym,
      :as => :visitable,
      :dependent  => :delete_all,
      :class_name => options[:visit_class].name
      
    has_many options[:visitor_class].name.tableize.to_sym,
      :through      => options[:visit_class].name.tableize,
      :class_name   => options[:visitor_class].name
      
    # Hooks.
    before_create :init_has_visits_fields
  end
  
end

#visitable?Boolean Also known as: is_visitable?

Does this class count/track visits?

Returns:

  • (Boolean)


92
93
94
# File 'lib/tracks_visits/visitable.rb', line 92

def visitable?
  self.respond_do?(:tracks_visits_options)
end