faustinoaq changed the topic of #amber to: Welcome to Amber Framework community! | https://amberframework.org | Developer happiness, productivity and bare metal performance | GH: https://github.com/amberframework | Docs: https://docs.amberframework.org | Gitter: https://gitter.im/amberframework/amber | IRC Logger: https://irclog.whitequark.org/amber | Amber::Server.start
FromGitter has quit [Remote host closed the connection]
FromGitter has joined #amber
_whitelogger has joined #amber
FromGitter has quit [Remote host closed the connection]
FromGitter has joined #amber
<FromGitter> <andrewc910> Jennifer orm allows you to save and skip validations like `user.save(skip_validation: true)`. Does granite support some sort of save macro/method whilst skipping all model validations?
<FromGitter> <Blacksmoke16> whats the use case for wanting to skip them?
<FromGitter> <andrewc910> And how can you detect if something is a new record vs a persisted record?
<FromGitter> <Blacksmoke16> there's a property on it, that gets set when it gets saved/fetched from the db
<FromGitter> <Blacksmoke16> iirc `.persisted?`
<FromGitter> <andrewc910> Ill play around with `#persisted`. Use case for skipping validations. I have trackable module which updates a few items when a user logins in such as `sign_in_count`. Skipping validations is what devise does for rails in this module and there really isn't a need to ensure email is unique on a persisted record.
<FromGitter> <Blacksmoke16> ah since it was already saved, no need to run validation again?
<FromGitter> <andrewc910> Yeah, the only validations i have in are ensuring the email is an email, it's unique, password is above 6 chars and if there is a new password, digest it. ⏎ ⏎ None of those are needed when i am updating `sign_in_count` or `current_ip_address`, you know?
<FromGitter> <Blacksmoke16> would have to be a feature request, logic atm is just `return false unless valid?`
<FromGitter> <Blacksmoke16> IMO it relates to, https://github.com/amberframework/granite/issues/358
<FromGitter> <Blacksmoke16> would be pretty easy feature tho, just add optional named arg to `#save`
<FromGitter> <andrewc910> The discussion in the issue is definitely over my head but it's good study material :) Agree my problem is related to it. ⏎ ⏎ Ill take a look at the implementation. If i understand it, i will make a pull request!
<FromGitter> <Blacksmoke16> should just be like ⏎ https://github.com/amberframework/granite/blob/master/src/granite/transactions.cr#L172 ⏎ `def save(*, skip_validations : Bool = false)`
<FromGitter> <Blacksmoke16> https://github.com/amberframework/granite/blob/master/src/granite/transactions.cr#L177 ⏎ `return false unless skip_validations && valid?`
<FromGitter> <andrewc910> and then add `unless skip_validations` on each of the `_before_create_` lines?
<FromGitter> <andrewc910> each of the callback lines* thats a bit more accurate
<FromGitter> <Blacksmoke16> no just need to do it in that one line within `#save`
<FromGitter> <andrewc910> So like: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/amberframework/amber?at=5e372d856f9d3d349818bf14]
<FromGitter> <andrewc910> ?
<FromGitter> <Blacksmoke16> no?
<FromGitter> <Blacksmoke16> like 177
<FromGitter> <Blacksmoke16> `return false if !skip_validations && !valid?`
<FromGitter> <Blacksmoke16> or prob could use `unless` i dont remember if it should be `&&` or `||` tho
<FromGitter> <andrewc910> Why would we return false? Wouldn't that prevent saving
<FromGitter> <Blacksmoke16> if `skip_validations` is false yea
<FromGitter> <Blacksmoke16> which would be the default
<FromGitter> <andrewc910> Oh i think that's my miscommunication then. `#save(skip_validations: true)` should still save, just skip the callbacks hence the `unless skip_validations` on each of the callback lines
<FromGitter> <Blacksmoke16> sorry to be clear `valid?` is what runs the callbacks
<FromGitter> <Blacksmoke16> `return false if !skip_validations && !valid?` would only return if `skip_validations` is `false` and `valid?` is `false`
<FromGitter> <Blacksmoke16> otherwise if you pass true for skip validations, then the expression would return and wouldnt run the validations
<FromGitter> <andrewc910> OHHHHH!!!
<FromGitter> <andrewc910> I just made this: https://play.crystal-lang.org/#/r/8hz2 ⏎ ⏎ But thats wrong. Now i see though.
<FromGitter> <Blacksmoke16> 👍
<FromGitter> <andrewc910> Does crystal short circuit with `||`s?
<FromGitter> <andrewc910> Not related, just curious on the above question
<FromGitter> <Blacksmoke16> yes
<FromGitter> <andrewc910> Thanks. I appreciate the help :)