Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new default_validations to Operations #751

Merged
merged 1 commit into from
Oct 17, 2021
Merged

Added new default_validations to Operations #751

merged 1 commit into from
Oct 17, 2021

Conversation

jwoertink
Copy link
Member

Fixes #645

The original issue was that our polymorphic before_save callback would run validations before any that you would set in your own SaveOperation. This prevented you from being able to assign your association in your SaveOperation.

This PR adds a new functionality to operations (SaveOperation, Operation, and DeleteOperation) to set your own default_validations. It works similar to before_save in that it takes a block, and you can run some code in there. When the operations call valid? it will run any required validations (as it always has), as well as any default validations you might need. This means you can also abstract them to a module to include them in several different operations if you need.

module DefaultThingValidations
  macro included
    default_validations do
      validate_required thing
    end
  end
end

class SaveThing1 < Thing1::SaveOperation
  include DefaultThingValidations
  before_save do
    # this is called before the default validations
    # so you can do any setup you need
    thing.value = "thing 1"
  end
end

class SaveThing2 < Thing2::SaveOperation
  include DefaultThingValidations
  before_save do
    thing.value = "thing 2"
  end
end

@paulcsmith
Copy link
Member

I think this is a goo idea to fix this issue. One thing that may be helpful is to add something to the docs and/org guides to explain when to use this. I read through: https://luckyframework.org/guides/database/callbacks-and-validations#default-validations and I wasn't sure when to use this over before_save until I came to this PR. What do you think?

Definitely not a blocker, but just something I was thinking when I was going through the docs

@jwoertink
Copy link
Member Author

For sure. I'll make an issue on the site to better explain this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

polymorphic validator runs before before_save methods
2 participants