How to track custom events in paper_trail? - ruby

I am using paper_trail for audit trail. Along with create, update and delete events I want to track few custom events like view(record), sent(email) etc. How can we introduce such custom events while auditing a model?

I have found a tweak to add custom events in paper_trail managed Versions:
Version.create(item_type: "Campaign", item_id: campaign.id, event: "Sent")
Maybe this is not right solution, but it helped me to achieve the goal. I would like to explore paper_trail more to find a better solution.

Following the paper trail flow, and having paper trail hooked to your touch events:
record.paper_trail_event = 'notified'
record.touch
If you don't want to have that hook in place you can:
record.versions.create!(event: 'notified')
The main problem with the second approach is that it won't apply any of the PaperTrail scoped params, nor any other dynamic field you may have defined for that model PaperTrail config.
You will need to set those manually. For the request.whodunnit it would be like:
record.versions.create!(event: 'notified', whodunnit: current_user.id)

See simple hit counter for page views in rails as an answer to the first part of your question. As for tracking sent mails, you may want to use Observer pattern.
In any case all these events are outside of paper_trail domain. While paper_trail simply creates versions of your model during data changing, what you need is to observe custom event and write to DB all necessary information about that event.

Related

Removing dynamic template events in Meteor

So, we all know that you can add events post-render by calling Template.templateName.events() and passing in new events. My question is: How do I remove them? I've found that I'm adding them how I like, but I end up with several of the same event, that all fire, and it's causing all sorts of problems. Is there a specific place that meteor stores the actual events? Where I could clear them out?
Template events should not be called several times but used once for static event definitions for this template that every instance of the template will listen to.
The documentation is not very helpful here. However, if you need dynamic template events you are still in good hands using the classic addEventListener or jQuery on and use bind to bind them to the template instance.
Don't forget to remove them when required but at least in Template#onDestroyed
I found that Template.templateName.__eventMaps = [] did the trick. I run this before creating a new instance of my template, and therefore only get one set of event handlers. woo!

how to delete alarm in lightning (in js)

I am banging my head looking at the code for ... quite some time.
I have a lightning event, created from ics (including an alarm).
I want to delete the alarm after something has occurred. I found calItemBase has mAlarms. But how to delete a single alarm? (there should be only one). What is the proper value of mAlarms if there is no alarm?
What to do with mAlarmLastAck and other properties?
My workaround is to recreate from ical without the alarm, but then the user looses categories and other things he set for the event in the UI.
Many thanks,
Klaus
A summary of the methods intended to be public for an item can be seen here: http://mxr.mozilla.org/comm-central/source/calendar/base/public/calIItemBase.idl
Specifically, there is a deleteAlarm method. Example:
var alarms = item.getAlarms({});
item.deleteAlarm(alarms[0]);
If you are sure you want to delete all alarms, you can also use the clearAlarms method.
item.clearAlarms();

How to create an empty activerecord object in rails 4?

I am trying to develop a Rails application for QuickBase for which there is no adapter. I checked online for the QuickBase adapter but it is not working as it is for a very old version of rails and the author told me that he no longer supports it.
I want to be able to use the Active Record and the associated concepts of a typical rails application, but intervene and modify how the create, update and show actions work. I have created the application to create a new record successfully. However, when I want to be able to edit the record, I have first modified the edit action to go to my own database and get the data into an active record object.
To create an empty Active Record I said $user = User.new
Then I populated all the attributes with the data from my custom database. But the form still shows the Create User button, instead of Update User button. Apparently, I guess this is because the persist? method is returning false as this is a "new" object. But I know this is not a new one. So how do I influence the #user object to think it is a persistent one?
Alternatively, is there a way I can create the blank #user object without the new function?
I have checked all over the place, but couldn't find any clue on how to accomplish this. Thank you so much for your help.
You can try calling #user.disconnect! after your call #user = User.new, which will keep ActiveRecord from trying to write to the database, but I'm not sure that will solve your problem.
The larger problem is that you're trying to fit a square peg into a round hole here. The entire point of ActiveRecord is to abstract the connection to a database. So without a database, what's the point?
I think your best solution would be to write your own QB adapter. It may not be as difficult as you think, since you already seem to know how to read/write to the database.
You can read more about how to do that here: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/AbstractAdapter.html

How to show feedback/error messages in a backbone application

I'm working on a simple CRUD proof of concept with Rails/Backbone/JST templating. I've been able to find a lot of examples up to this point. But after much searching and reading, I've yet to find a good example of how to handle these scenarios:
info message: new item successfully added to list (shown on list screen)
info message: item successfully deleted from list
error message: problem with field(s) entry
field level error message: problem with entry
The Backbone objects are:
Collection (of "post" Models) -> Model ("post" object) -> List/Edit/New Views (and a JST template for each of these views)
So, I'm looking for a high level description of how I should organize my code and templates to achieve the level of messaging desired. I already have a handle on how to perform my validation routine on the form inputs whenever they change. But not sure what do with the error messages now that I have them.
Here is the approach I'm considering. Not sure if it's a good one:
Create a "Message" Model, which maps to a "View", which is a sub-view (if that's possible) on my existing views. This view/model can display page level messages and errors in the first three scenarios I mention above. Not sure if it's feasible to have a "sub-view" and how to handle the templating for that. But if it's possible, the parent templates could include the "message" sub-template. The message view could show/hide the sub-template based on the state of the message model. Feasible? Stupid?
For the fourth scenario, the model validation will return an error object with specific messages per each erroneous field each time a "model.set" is called by form field changes. I don't want to interrupt the "model.set" but I do want to display the error message(s) next to each field. I want to know how to factor my edit/new template and Post model/view in such a way that I don't violate the MVC pattern. I.e. I don't want to put references to DOM elements in the wrong plage.
Sorry if this is vague. If you're inclined to help, let me know what code snippets could be helpful (or other details) and I'll provide them.
You create a global eventbus. When ever an error appears trigger an event. Your view that should show the message listen to the events on this eventbus. Doing so, your error message view dont needs to know all of your collection and vice versa. The eventbus is simple:
var eventBus = _.extend({}, Backbone.Events);
Add it to your collection and trigger it when ever add was called:
var myCollection = Backbone.Collection.extend({
initialize: function([],eventbus){
this.bind('add', function(obj){eventbus.trigger('added', obj)}
}
})
Take also a look at the article: http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

Get list of currently dispatched events

I know this question may seem weird but I'd like to get a list of currently dispatched events.
The thing is that I am a lazy man and I would like to check if the 'checkout_cart_add_product_complete' has been fired without creating an observer for it.
So the idea is to get an array of all dispatched events and do an in_array on it :)
I thought that Mage::getEvents()->getAllEvents() would throw some info but it just returns an empty array.
I also digged a bit in lib/Varien/Event files and folders but didn't manage to be successful at creating an observer programmatically. Yep, I know, why being simple while one can be complicated ? :)
So this main question (getting a list of dispatched events) hides another (for the pure knowledge) wich would be "how to create an observer programmatically".
What do you think?
Thanks a lot!
Take a look at dispatchEvent and you'll see that events are only loaded from the assorted config.xml files, via SimpleXML. I cannot see any way to intercept this except to override Mage_Core_Model_App.
Of course there cannot be an event-dispatched-event, that would create an infinite loop, so there is no way to observe all events.
If you need to see events for development my advice would be to set a breakpoint in dispatchEvent with your debugger, that way you get to see not only the event names but also the objects passed as parameters too. I've tried other ways before but this was most convenient for me.
I need to do the same and I think it's possible to trick magento by the function getEventConfig in Mage_Core_Model_Config. You could force him to add automatically a default observer.

Resources