Module: Ahoy::Model

Defined in:
lib/ahoy/model.rb

Instance Method Summary collapse

Instance Method Details

#visitable(name = :visit, **options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/ahoy/model.rb', line 3

def visitable(name = :visit, **options)
  class_eval do
    belongs_to(name, class_name: "Ahoy::Visit", optional: true, **options)
    before_create :set_ahoy_visit
  end
  class_eval %{
    def set_ahoy_visit
      self.#{name} ||= Ahoy.instance.try(:visit_or_create)
    end
  }
end