docbrown/app/models/page_view.rb
Jeremy Friesen fe2e53cc50
Moving Experiments into AbExperiment namespace (#17532)
* Moving Experiments into AbExperiment namespace

This commit entails two major concepts:

1. Extracting logic out of a worker.
2. Cleaning the conditions in which we convert experiments.

In addition, I revisited some spec names to tidy them up for
clarification and structure.

**Extracting Logic...**

For workers, my preference is that they be a simple router to another
object.  A non-worker object, with it's initialize method, can both
better manage instance variables.  Further, workers are somewhat "flat"
in hierarchy,

In this case, having the AbExperiment be knowledgeable both in setting
the experiment and handling conversions makes (to me) organizational
sense.  For example, the AbExperiment is constructed as a wrapper to the
FieldTest gem.  But the RecordFieldTestEventWorker had knowledge of
FieldTest.

With this refactor, it does not have that knowledge.  This also provided
an opportunity to replace magic strings with constants.

**Cleaning the conditions...**

This relates directly to and closes forem/forem#17530.  I was looking at
the experiments after 2 days, and realized that there shouldn't be
results for
`user_views_pages_on_at_least_nine_different_days_within_two_weeks`;
after all the experiment started 2 days ago, how can we have results
that look at 2 weeks.

By adding the "max" function calls, we ensure a clear boundary of
"before the experiment began" versus "while the experiment is running".

Closes forem/forem#17530

* Update spec/models/ab_experiment_spec.rb

Co-authored-by: Mac Siri <krairit.siri@gmail.com>

* Apply suggestions from code review

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

Co-authored-by: Mac Siri <krairit.siri@gmail.com>
Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>
2022-05-03 21:34:12 -04:00

42 lines
1 KiB
Ruby

# @note When we destroy the related article, it's using dependent:
# :delete for the relationship. That means no before/after
# destroy callbacks will be called on this object.
class PageView < ApplicationRecord
belongs_to :user, optional: true
belongs_to :article
before_create :extract_domain_and_path
after_create_commit :record_field_test_event
private
def extract_domain_and_path
return unless referrer
parsed_url = Addressable::URI.parse(referrer)
self.domain = parsed_url.domain
self.path = parsed_url.path
end
def article_searchable_tags
article.cached_tag_list
end
def article_searchable_text
article.body_text[0..350]
end
def article_tags
article.decorate.cached_tag_list_array
end
# @see AbExperiment::GoalConversionHandler
def record_field_test_event
return if FieldTest.config["experiments"].nil?
return unless user_id
Users::RecordFieldTestEventWorker
.perform_async(user_id, AbExperiment::GoalConversionHandler::USER_CREATES_PAGEVIEW_GOAL)
end
end