Notifications (or flash messages) in an api application

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello!

As a noob in rails, i am having a problem on how to find a decent and
straightforward notifications system for my application.
Being a Api like application (talking with the client is made using JSON
/ later on XML as well), i have a problem finding a good solution to
feed my needs.

I have tried to use the rails validators, but then i have realized that
is not a good solution, as i need to send normal notifications as well
(Ex: “You have successfully bought this item” ).

After that ideea failed … I have tried to implement a “Notification”
module inside my user model as, all my notifications are user related.
However … this system has 2 instance variables : @notifications and
@api_errors. Now, the problem would be the validation of the user
action, and here might be several messages to send:

  • Stock finished
  • No money
  • No access to this feature …
  • CC invalid
    … etc

now … i can do my checks like

if false == money_requirement?
self.add_api_error “You do not have enough money”
end

if false == product.stock_requirement?
self.add_api_error …
end

But the main problem … would be now, the method of trigger the action.

if self.api_errors.size == 0

do play with the user account, stocks and other stuff …

end

what i am asking you: Is there any way to play with those notifications
and errors without passing through the database? They are more like
flashes, but the code is inside the model … so, i don’t want to fall
into an antipattern having session related info inside my model.

Thanks.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org

iQEcBAEBAgAGBQJPXGznAAoJEAa2YjEk8CRGiZMH+wTPPfDTMTyiqStGSfbueFHe
0gXcF/TGoFTKjpxYsTgZ5Ew5PgBvcZESmN8lcmw6Zt3zMiupIvy+6tFGnWdmJIbr
myJW686w/t+U5sm2GVEMUlyos454NUSR2AuMDrfF9zpCDzJ7xMJV9Bof2BZkv2EJ
YXmYKCTKsI1FskIGNBbCEg7LurFcbPSm29ixQ2e482oYt3Avbd/olqiiRSgxDEYE
qf9ob52L9IoX0tO+lvd+OyZ3/oT2z6Hx4gM1iG8ZoHCNj63FhJ22L86Sg1cKdD3X
OlvF+EguwGih5i2JRneCE3PDSP8IVRu1l70YSTkH8YvBobjORGZJ1XFlo5IRr8Y=
=z7bt
-----END PGP SIGNATURE-----

On Sun, Mar 11, 2012 at 05:14, Lupu Alexandru Emil
[email protected] wrote:

if false == money_requirement?
self.add_api_error “You do not have enough money”
end

Or should that be “Money enough, you have not. Buy, or buy not, there
is no try!”? :slight_smile: Having Yodified a lot of my checks in the dim
distant past, when working in C, and then discovering that cranking up
the warning level would flag "assignment in conditional"s for me, I’ve
since gone back to the standard, more readable, order.

Anyway…

There’s no need for an == at all when checking a boolean. Just use
“if ! money_requirement?”. Also, getting into the habit of comparing
against false, will trip you up later when you really just want to
check “falsiness”, but what you’re checking returns nil on a failure
instead of the actual value false.

what i am asking you: Is there any way to play with those
notifications and errors without passing through the database?

I’m not sure what you mean by “passing through”. As in, you want to
pass them to the user, but don’t want to record them in the database,
I presume.

Off the top of my head, what you could do is pass back a wrapper type
that includes the notices and the actual data object, and not record
the wrapper in the db, only the actual data object. Or, attach the
flash and suchlike, only after you’ve done the saving.

That strikes me as a bit of a hack, though. There’s bound to be a
more Rails-ish way to do it. Maybe this will serve as a “bump” and
someone will enlighten us. Maybe Rails is smart enough to know,
“don’t save the thing called ‘flash’”?

-Dave


Dave A.: Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote) – see www.DaveAronson.com, and blogs at
www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com