Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Invoke on_assignment callback after assignment is stored in database; add global on_assignment callback #368

Merged
merged 6 commits into from Jan 29, 2022
Merged

Invoke on_assignment callback after assignment is stored in database; add global on_assignment callback #368

merged 6 commits into from Jan 29, 2022

Conversation

frostmark
Copy link
Contributor

@frostmark frostmark commented Dec 28, 2021

MVP for #367

  • Default callback configurations for on_assignment and after_assignment
Vanity.configure do |config|
  config.on_assignment = proc do |...params|
    # does something useful 
  end
end
  • Added after_assignment callback and specs
  • Minor fixes

Edit: final version of this PR does not include an after_assignment callback. Instead, the on_assignment callback's call has been moved after the assignment is preserved in the database.

…t callbacks

Added after_assignment callback and specs
Minor fixes
@frostmark
Copy link
Contributor Author

@bensheldon could you also take a look at this? 👀

@bensheldon
Copy link
Collaborator

Just to summarize what I'm seeing here.

  • Adds new global Vanity Configuration accessors to assign on_assignment and after_assignment callbacks
  • Adds a new Vanity Experiment accessor to assign an anafter_assignment callback. This callback is invoked after the assignment data is saved to the database (whereas the on_assignment callback is invoked before that save takes place)
  • The global Vanity Configuration callbacks are invoked in addition to the Experiment callbacks. My intuition of reading "Default" is that an Experiment's callbacks would override the global Configuration's callbacks.
    • What do you think of re-describing this as "Global assignment callbacks"? or...
    • Change the behavior such that the Experiment Callback overrides the global Configuration callback and only one is called?

Reading the original code, I want to suggest a completely different direction too:

  • Keep the global on_assignment (I think having either a default or additional callback sounds useful)
  • Don't create a separate after_assignment callback and instead we simply move the place where on_assigment is invoked till after it's saved in the database. Is there a functional/behavioral reason to have both callbacks? I did a little digging and didn't really see an explanation for why it should take place before save (i.e. it's not cancelable, there isn't anything subsequent that mutates the value).

@frostmark
Copy link
Contributor Author

frostmark commented Jan 28, 2022

@bensheldon

Change the behavior such that the Experiment Callback overrides the global Configuration callback and only one is called?

Yep, it's already working that way. Default callback can be overridden in Experiment

Keep the global on_assignment (I think having either a default or additional callback sounds useful)

Don't create a separate after_assignment callback and instead we simply move the place where on_assigment is invoked till after it's saved in the database. Is there a functional/behavioral reason to have both callbacks? I did a little digging and didn't really see an explanation for why it should take place before save (i.e. it's not cancelable, there isn't anything subsequent that mutates the value).

Yep, I agree that we may don't add after_assignment. I just didn't want to change the current behavior of on_assignment, but I agree that there is no need to add a new type of callback and we can move on_assigment

@frostmark frostmark marked this pull request as draft January 28, 2022 13:31
Call on_assignment after connection.ab_add_participant
@frostmark frostmark marked this pull request as ready for review January 28, 2022 13:48
@bensheldon
Copy link
Collaborator

@frostmark this looks good. Let me know when I should review.

@frostmark
Copy link
Contributor Author

frostmark commented Jan 29, 2022

@bensheldon this is ready for review

@bensheldon bensheldon changed the title Be able to configure default callbacks Add global on_assignment callback Jan 29, 2022
Copy link
Collaborator

@bensheldon bensheldon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good. Thanks for the discussion on this.

return if index == connection.ab_showing(@id, identity)
return if connection.ab_seen @id, identity, index
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent some time looking at this, because this guard clause was lifted up out of the #call_on_assignment_if_available method. I think it's safe and an improvement because it ensures re-assigning the same assignment.

@bensheldon bensheldon changed the title Add global on_assignment callback Add global on_assignment callback; invoke callback after assignment is stored in database instead of before Jan 29, 2022
@bensheldon bensheldon changed the title Add global on_assignment callback; invoke callback after assignment is stored in database instead of before Invoke on_assignment callback after assignment is stored in database; add global on_assignment callback Jan 29, 2022
@bensheldon bensheldon merged commit c7900b9 into assaf:master Jan 29, 2022
@bensheldon
Copy link
Collaborator

Released in v3.1.0

@frostmark frostmark deleted the add-default-callback-configuration-and-after-assignment-callback branch January 29, 2022 16:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants