Note that there are some explanatory texts on larger screens.

plurals
  1. PORecord changes pend approval by a privileged user; Its like versioning combined with approvals
    text
    copied!<p>I have a requirement that certain attribute changes to records are not reflected in the user interface until those changes are approved. Further, if a change is made to an <code>approved</code> record, the user will be presented with the record as it exists before <code>approval</code>.</p> <p><strong>My first try...</strong></p> <p>was to go to a versioning plugin such as <code>paper_trail</code>, <code>acts_as_audited</code>, etc. and add an <code>approved</code> attribute to their version model. Doing so would not only give me the ability to 'rollback' through versions of the record, but also SHOULD allow me to differentiate between whether a version has been approved or not.</p> <p>I have been working down this train of thought for awhile now, and the problem I keep running into is on the user side. That is, how do I query for a collection of <code>approved</code> records? I <em>could</em> (and tried) writing some helper methods that get a collection of records, and then loop over them to find an "approved" version of the record. My primary gripe with this is how quickly the number of database hits can grow. My next attempt was to do something as follows:</p> <pre><code>Version. where(:item_type =&gt; MyModel.name, :approved =&gt; true). group(:item_type).collect do |v| # like the 'reify' method of paper_trail v.some_method_that_converts_the_version_to_a_record end </code></pre> <p>So assuming that the <code>some_method...</code> call doesn't hit the database, we kind of end up with the data we're interested in. The main problem I ran into with this method is I can't use this "finder" as a scope. That is, I can't append additional scopes to this lookup to narrow my results further. For example, my records may also have a <code>cool</code> scope that only shows records where <code>:cool =&gt; true</code>. Ideally, I would want to look up my records as <code>MyModel.approved.cool</code>, but here I guess I would have to get my collection of approved models and then loop over them for <code>cool</code> ones would would result in the very least in having a bunch of records initialized in memory for no reason.</p> <p><strong>My next try...</strong></p> <p>involved creating a special type of "pending record" that basically help "potential" changes to a record. So on the user end you would lookup whatever you wanted as you normally would. Whenever a pending record is <code>apply!</code>(ed) it would simply makes those changes to the actual record, and alls well... <em>Except</em> about 30 minutes into it I realize that it all breaks down if an "admin" wishes to go back and contribute more to his change before approving it. I guess my only option would be either to:</p> <ol> <li><p>Force the admin to approve all changes before making additional ones (that won't go over well... nor should it).</p></li> <li><p>Try to read the changes out of the "pending record" model and apply them to the existing record without saving. Something about this idea just doesn't quite sound "right".</p></li> </ol> <p>I would <strong><em>love</em></strong> someone's input on this issue. I have been wrestling with it for some time, and I just can't seem to find the way that feels right. I like to live by the "if its hard to get your head around it, you're probably doing it wrong" mantra.</p> <p>And this is kicking my tail...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload