Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the 'right' data structure to turn app features on/off based on 'type' of account?
    text
    copied!<p>My app has 10 features that are enabled/disabled depending upon which of the 3 'types' of account a user has. </p> <p>Currently, I have 10 methods (one per feature) along the lines of:</p> <pre><code>def is_FEATURENAME_enabled case currentuser.accounttype when "A", "C" # account types allow to see that feature return true else return false end end </code></pre> <p>Then, in each place where I potentially disable a feature, I do</p> <pre><code>if foo.is_SOMEFEATURE_enable do stuff to enable that feature end </code></pre> <p>It works. It's not that hard to maintain. But there should be a better way. I suspect the right solution is to define some sort of structure (hash? I dunno) in one place that maps enabled features to accounttypes, then have a single method that I call something like:</p> <pre><code>if foo.is_feature_enabled(:FEATURENAME) do stuff to enable feature end </code></pre> <p>where the method <code>is_feature_enabled</code> looks at <code>currentuser.accountype</code> and checks the mapping structure to see if the identified feature is enabled.</p> <p>And I suspect the DRY way to define that mapping (given I have WAY more features than account types) is to list all the features ONCE then for each feature list the accounttypes that have access to that feature (not the other way around). That way when I add a new feature I only have to edit ONE line in the mapping. Something like:</p> <pre><code>FeatureA: usertype1 FeatureB: usertype1, usertype3 FeatureC: usertype2 ... </code></pre> <p>seems more logical and easier to maintain than:</p> <pre><code>usertype1: FeatureA, FeatureB, FeatureD, FeatureG usertype2: FeatureC, FeatureD usertype3: FeatureB, FeatureD, FeatureG, FeatureH </code></pre> <p>Any suggestions would be appreciated, and instructive for learning The Right Way to do stuff in ruby.</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