Sha256: 23b4eee9c20269df7b3f7269a87d3e396e0e56bea0d9a4eb1da324cdd3a1cb93

Contents?: true

Size: 471 Bytes

Versions: 3

Compression:

Stored size: 471 Bytes

Contents

module Ahoy
  module Model
    def visitable(name = :visit, **options)
      class_eval do
        safe_options = options.dup
        safe_options[:optional] = true if Rails::VERSION::MAJOR >= 5
        belongs_to(name, class_name: "Ahoy::Visit", **safe_options)
        before_create :set_ahoy_visit
      end
      class_eval %{
        def set_ahoy_visit
          self.#{name} ||= RequestStore.store[:ahoy].try(:visit_or_create)
        end
      }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ahoy_matey-2.2.1 lib/ahoy/model.rb
ahoy_matey-2.2.0 lib/ahoy/model.rb
ahoy_matey-2.1.0 lib/ahoy/model.rb