Module: TracksVisits::ActiveRecord::Visitable::ClassMethods
- Defined in:
- lib/tracks_visits/visitable.rb
Instance Method Summary collapse
-
#tracks_visits(options = {}) ⇒ Object
Make the model visitable, i.e.
-
#visitable? ⇒ Boolean
(also: #is_visitable?)
Does this class count/track visits?.
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( = {}) send :include, ::TracksVisits::ActiveRecord::Visitable::InstanceMethods # Set default class names if not given. [:visitor_class_name] ||= [:from] || Visitor::DEFAULT_CLASS_NAME [:visitor_class_name] = [:visitor_class_name].to_s.classify [:visit_class_name] = DEFAULT_VISIT_CLASS_NAME.to_s.classify [:visitor_class] = [:visitor_class_name].constantize rescue nil # Assocations: Visit class (e.g. Visit). [:visit_class] = begin [:visit_class_name].constantize rescue # If note defined...define it! Object.const_set([:visit_class_name].to_sym, Class.new(::ActiveRecord::Base)).class_eval do belongs_to :visitable, :polymorphic => true belongs_to :visitor, :polymorphic => true end [:visit_class_name].constantize end # Save the initialized options for this class. write_inheritable_attribute(:tracks_visits_options, .slice(:visit_class, :visitor_class)) class_inheritable_reader :tracks_visits_options # Assocations: Visitor class (e.g. User). if Object.const_defined?([:visitor_class].name.to_sym) [:visitor_class].class_eval do has_many :visits, :foreign_key => :visitor_id, :class_name => [:visit_class].name end end # Assocations: Visitable class (e.g. Page). self.class_eval do has_many [:visit_class].name.tableize.to_sym, :as => :visitable, :dependent => :delete_all, :class_name => [:visit_class].name has_many [:visitor_class].name.tableize.to_sym, :through => [:visit_class].name.tableize, :class_name => [: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?
92 93 94 |
# File 'lib/tracks_visits/visitable.rb', line 92 def visitable? self.respond_do?(:tracks_visits_options) end |