Permalink
Browse files

Adding Arturo.feature_enabled_for shortcut [#11]

  • Loading branch information...
James A. Rosen
James A. Rosen committed Feb 14, 2011
1 parent a9de2ab commit 55253ddd78642b849e70d7c27c61f0bae11f93d3
Showing with 30 additions and 0 deletions.
  1. +11 −0 README.md
  2. +9 −0 lib/arturo.rb
  3. +10 −0 test/dummy_app/test/unit/feature_test.rb
View
@@ -239,6 +239,17 @@ The latter can be used like so:
widgets
end
+#### Outside a Controller
+
+If you want to check availability outside of a controller or view (really
+outside of something that has `Arturo::FeatureAvailability` mixed in), you
+can do the following:
+
+ Arturo.feature_enabled_for?(:foo, recipient)
+
+This will check both whether the `:foo` feature exists and whether it is
+enabled for `recipient`.
+
#### Caching
**Note**: Arturo does not yet have caching support. Be very careful when
View
@@ -6,4 +6,13 @@ module Arturo
require 'arturo/controller_filters'
require 'arturo/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
+ # Quick check for whether a feature is enabled for a recipient.
+ # @param [String, Symbol] feature_name
+ # @param [#id] recipient
+ # @return [true,false] whether the feature exists and is enabled for the recipient
+ def self.feature_enabled_for?(feature_name, recipient)
+ f = self::Feature.to_feature(feature_name)
+ f && f.enabled_for?(recipient)
+ end
+
end
@@ -20,6 +20,16 @@ def test_to_feature
assert_nil ::Arturo::Feature.to_feature(:does_not_exist)
end
+ def test_feature_enabled_for_existent_feature
+ feature.update_attribute(:deployment_percentage, 100)
+ recipient = stub('User', :to_s => 'Paula', :id => 12)
+ assert ::Arturo.feature_enabled_for?(feature.symbol, recipient), "#{feature} should be enabled for #{recipient}"
+ end
+
+ def test_feature_enabled_for_non_existent_feature
+ assert !::Arturo.feature_enabled_for?(:does_not_exist, 'Paula')
+ end
+
def test_requires_a_symbol
feature.symbol = nil
assert !feature.valid?

0 comments on commit 55253dd

Please sign in to comment.