Class: Testimonials::PromptEvent
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Testimonials::PromptEvent
- Defined in:
- app/models/testimonials/prompt_event.rb
Overview
The throttling ledger. Every auto-open is a "shown", every close without submitting is a "dismissed", every saved submission is a "submitted" — keyed by author_id for signed-in users, by a visitor cookie otherwise. eligible? reads this history so the widget never nags:
* submitted a testimonial -> never auto-prompted for one again
* submitted NPS -> not again within nps_reprompt_after
* shown or dismissed recently -> not again within reprompt_after
* shown max_prompts times -> never auto-prompted for that kind again
Explicit opens (the user clicked something) bypass all of this.
Constant Summary collapse
- KINDS =
%w[testimonial nps].freeze
- ACTIONS =
%w[shown dismissed submitted].freeze
Class Method Summary collapse
- .eligible?(kind:, author_id: nil, visitor_token: nil) ⇒ Boolean
- .record!(kind:, action:, author_id: nil, visitor_token: nil) ⇒ Object
Class Method Details
.eligible?(kind:, author_id: nil, visitor_token: nil) ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/testimonials/prompt_event.rb', line 30 def eligible?(kind:, author_id: nil, visitor_token: nil) kind = kind.to_s return false unless KINDS.include?(kind) # No identity, no history: a brand-new visitor is always eligible. return true if .blank? && visitor_token.blank? history = subject(, visitor_token).where(kind: kind) config = Testimonials.config return false if submitted_recently?(history, kind, config) return false if history.where(action: %w[shown dismissed]) .exists?(created_at: (Time.current - config.reprompt_after)..) history.where(action: 'shown').count < config.max_prompts end |
.record!(kind:, action:, author_id: nil, visitor_token: nil) ⇒ Object
23 24 25 26 27 28 |
# File 'app/models/testimonials/prompt_event.rb', line 23 def record!(kind:, action:, author_id: nil, visitor_token: nil) return if .blank? && visitor_token.blank? create!(kind: kind.to_s, action: action.to_s, author_id: .presence, visitor_token: visitor_token.presence) end |