From efb36982e05e96ac0b17f287cfed0dbf79a46c00 Mon Sep 17 00:00:00 2001 From: "James A. Rosen" Date: Sun, 13 Feb 2011 16:30:06 -0800 Subject: [PATCH] syntactic sugar for availability check [#14] --- README.md | 9 ++++--- lib/arturo.rb | 33 ++++++++++++++++++------ test/dummy_app/test/unit/feature_test.rb | 6 +++++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e8f31e1..ab72fb1 100644 --- a/README.md +++ b/README.md @@ -243,12 +243,15 @@ The latter can be used like so: 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: +can ask either Arturo.feature_enabled_for?(:foo, recipient) -This will check both whether the `:foo` feature exists and whether it is -enabled for `recipient`. +or the slightly fancier + + Arturo.foo_enabled_for?(recipient) + +Both check whether the `foo` feature exists and is enabled for `recipient`. #### Caching diff --git a/lib/arturo.rb b/lib/arturo.rb index 162efc2..7e2e46e 100644 --- a/lib/arturo.rb +++ b/lib/arturo.rb @@ -6,13 +6,30 @@ 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 + class < 100, :symbol => :foo) + recipient = stub('User', :to_s => 'Paula', :id => 12) + assert ::Arturo.foo_enabled_for?(recipient), "#{feature} should be enabled for #{recipient}" + end + def test_requires_a_symbol feature.symbol = nil assert !feature.valid?