Permalink
Browse files

updates from arturo master + fixes for rails 3.1

  • Loading branch information...
2 parents 1bbf8f3 + 17b0da1 commit 9fc67918b798cbcdccdb556dc8035a6799e49c83 @plukevdh plukevdh committed Apr 23, 2012
View
2 .rvmrc
@@ -1 +1 @@
-rvm 1.9.2
+rvm 1.9.3-p125
View
@@ -1,80 +1,97 @@
PATH
remote: .
specs:
- arturo (1.2.0)
- rails (~> 3.0)
+ arturo (1.3.0)
+ rails (>= 3.1)
GEM
remote: http://rubygems.org/
specs:
- abstract (1.0.0)
- actionmailer (3.0.5)
- actionpack (= 3.0.5)
- mail (~> 2.2.15)
- actionpack (3.0.5)
- activemodel (= 3.0.5)
- activesupport (= 3.0.5)
- builder (~> 2.1.2)
- erubis (~> 2.6.6)
- i18n (~> 0.4)
- rack (~> 1.2.1)
- rack-mount (~> 0.6.13)
- rack-test (~> 0.5.7)
- tzinfo (~> 0.3.23)
- activemodel (3.0.5)
- activesupport (= 3.0.5)
- builder (~> 2.1.2)
- i18n (~> 0.4)
- activerecord (3.0.5)
- activemodel (= 3.0.5)
- activesupport (= 3.0.5)
- arel (~> 2.0.2)
- tzinfo (~> 0.3.23)
- activeresource (3.0.5)
- activemodel (= 3.0.5)
- activesupport (= 3.0.5)
- activesupport (3.0.5)
- arel (2.0.9)
- builder (2.1.2)
- erubis (2.6.6)
- abstract (>= 1.0.0)
- factory_girl (1.3.2)
- i18n (0.5.0)
- mail (2.2.15)
- activesupport (>= 2.3.6)
+ actionmailer (3.2.3)
+ actionpack (= 3.2.3)
+ mail (~> 2.4.4)
+ actionpack (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ activerecord (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ activesupport (3.2.3)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.2)
+ builder (3.0.0)
+ erubis (2.7.0)
+ factory_girl (1.3.3)
+ hike (1.2.1)
+ i18n (0.6.0)
+ journey (1.0.3)
+ json (1.6.6)
+ mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
- mime-types (1.16)
- mocha (0.9.8)
- rake
- polyglot (0.3.1)
- rack (1.2.2)
- rack-mount (0.6.14)
- rack (>= 1.0.0)
- rack-test (0.5.7)
+ metaclass (0.0.1)
+ mime-types (1.18)
+ mocha (0.11.1)
+ metaclass (~> 0.0.1)
+ multi_json (1.3.2)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
rack (>= 1.0)
- rails (3.0.5)
- actionmailer (= 3.0.5)
- actionpack (= 3.0.5)
- activerecord (= 3.0.5)
- activeresource (= 3.0.5)
- activesupport (= 3.0.5)
+ rails (3.2.3)
+ actionmailer (= 3.2.3)
+ actionpack (= 3.2.3)
+ activerecord (= 3.2.3)
+ activeresource (= 3.2.3)
+ activesupport (= 3.2.3)
bundler (~> 1.0)
- railties (= 3.0.5)
- railties (3.0.5)
- actionpack (= 3.0.5)
- activesupport (= 3.0.5)
+ railties (= 3.2.3)
+ railties (3.2.3)
+ actionpack (= 3.2.3)
+ activesupport (= 3.2.3)
+ rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
- thor (~> 0.14.4)
- rake (0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
redgreen (1.2.2)
- sqlite3-ruby (1.3.1)
+ sprockets (2.1.2)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.6)
+ sqlite3-ruby (1.3.3)
+ sqlite3 (>= 1.3.3)
thor (0.14.6)
+ tilt (1.3.3)
timecop (0.3.5)
- treetop (1.4.9)
+ treetop (1.4.10)
+ polyglot
polyglot (>= 0.3.1)
- tzinfo (0.3.26)
+ tzinfo (0.3.33)
PLATFORMS
ruby
@@ -85,5 +102,6 @@ DEPENDENCIES
mocha
rake
redgreen (~> 1.2)
+ sqlite3 (~> 1.3)
sqlite3-ruby (~> 1.3)
timecop (~> 0.3)
View
@@ -239,6 +239,11 @@ The latter can be used like so:
widgets
end
+#### Rack Middleware
+
+ require 'arturo'
+ use Arturo::Middleware, :feature => :my_feature
+
#### Outside a Controller
If you want to check availability outside of a controller or view (really
@@ -30,8 +30,11 @@ def self.to_feature(feature_or_symbol)
end
# Create a new Feature
- def initialize(attributes = {})
- super(DEFAULT_ATTRIBUTES.merge(attributes || {}))
+ # @param [Hash, nil] attribute hash to set on new object
+ # @param [Hash, nil]
+ # @return [Arturo::Feature] newly initialized feature
+ def initialize(attributes = {}, options = {})
+ super(DEFAULT_ATTRIBUTES.merge(attributes || {}), options)
end
# @param [Object] feature_recipient a User, Account,
View
@@ -1,6 +1,6 @@
# my_rails_engine.gemspec
Gem::Specification.new do |gem|
- gem.version = '1.2.0'
+ gem.version = '1.3.0'
gem.name = 'arturo'
gem.files = Dir["lib/**/*", "app/**/*", "config/**/*"] + %w(README.md HISTORY.md)
gem.summary = "Feature sliders, wrapped up in an engine"
@@ -12,10 +12,11 @@ Gem::Specification.new do |gem|
gem.require_paths = [".", "lib"]
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
gem.specification_version = 2
- gem.add_runtime_dependency 'rails', '~> 3.0'
+ gem.add_runtime_dependency 'rails', '>= 3.1'
gem.add_development_dependency 'mocha'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'redgreen', '~> 1.2'
+ gem.add_development_dependency 'sqlite3', '~> 1.3'
gem.add_development_dependency 'sqlite3-ruby', '~> 1.3'
gem.add_development_dependency 'factory_girl', '~> 1.3'
gem.add_development_dependency 'timecop', '~> 0.3'
View
@@ -6,6 +6,7 @@ module Arturo
require 'arturo/feature_management'
require 'arturo/feature_caching'
require 'arturo/controller_filters'
+ require 'arturo/middleware'
require 'arturo/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
class <<self
View
@@ -0,0 +1,60 @@
+module Arturo
+ # A Rack middleware that requires a feature to be present. By default,
+ # checks feature availability against an `arturo.recipient` object
+ # in the `env`. If that object is missing, this middleware always fails,
+ # even if the feature is available for everyone.
+ #
+ # ## Usage
+ #
+ # use Arturo::Middleware, :feature => :foo
+ #
+ # ## Options
+ #
+ # * feature -- the name of the feature to require, as a Symbol; required
+ #
+ # * recipient -- the key in the `env` hash under which the feature
+ # recipient can be found; defaults to "arturo.recipient".
+ # * on_unavailable -- a Rack-like object
+ # (has `#call(Hash) -> [status, headers, body]`) that
+ # is called when the feature is unavailable; defaults
+ # to returning `[ 404, {}, ['Not Found'] ]`.
+ class Middleware
+
+ MISSING_FEATURE_ERROR = "Cannot create an Arturo::Middleware without a :feature"
+
+ DEFAULT_RECIPIENT_KEY = 'arturo.recipient'
+
+ DEFAULT_ON_UNAVAILABLE = lambda { |env| [ 404, {}, ['Not Found'] ] }
+
+ def initialize(app, options = {})
+ @app = app
+ @feature = options[:feature]
+ raise ArgumentError.new(MISSING_FEATURE_ERROR) unless @feature
+ @recipient_key = options[:recipient] || DEFAULT_RECIPIENT_KEY
+ @on_unavailable = options[:on_unavailable] || DEFAULT_ON_UNAVAILABLE
+ end
+
+ def call(env)
+ if enabled_for_recipient?(env)
+ @app.call(env)
+ else
+ fail(env)
+ end
+ end
+
+ private
+
+ def enabled_for_recipient?(env)
+ ::Arturo.feature_enabled_for?(@feature, recipient(env))
+ end
+
+ def recipient(env)
+ env[@recipient_key]
+ end
+
+ def fail(env)
+ @on_unavailable.call(env)
+ end
+
+ end
+end
@@ -1,4 +1,4 @@
-Arturo.class_eval do
+Arturo.instance_eval do
# Enable a feature; create it if necessary.
# For use in testing. Not auto-required on load. To load,
@@ -7,11 +7,11 @@
#
# @param [Symbol, String] name the feature name
def enable_feature!(name)
- feature = Arturo::Feature.find_by_symbol(name)
+ feature = Arturo::Feature.to_feature(name)
if feature
feature.update_attributes(:deployment_percentage => 100)
else
- Arturo::Feature.create(:symbol => name, :deployment_percentage => 100)
+ Arturo::Feature.create!(:symbol => name, :deployment_percentage => 100)
end
end
@@ -22,7 +22,7 @@ def enable_feature!(name)
#
# @param [Symbol, String] name the feature name
def disable_feature!(name)
- if (feature = Arturo::Feature.find_by_symbol(name))
+ if (feature = Arturo::Feature.to_feature(name))
feature.update_attributes(:deployment_percentage => 0)
end
end
@@ -1,14 +1,24 @@
class User
- attr_reader :name
+ attr_reader :name, :id
def initialize(options = {})
@name = options[:name]
@admin = options[:admin]
+ @id = options[:id]
end
def admin?
!!@admin
end
+ def to_s
+ name
+ end
+
+ def inspect
+ type = @admin ? 'Admin' : 'User'
+ "<Mock #{type} ##{id}: #{name}>"
+ end
+
end
@@ -15,6 +15,7 @@
require 'arturo'
require 'arturo/feature'
require 'arturo/feature_factories'
+require 'arturo/test_support'
class ActiveSupport::TestCase
def reset_translations!
@@ -15,13 +15,13 @@ def initialize
end
def bad_feature
- feature = Factory(:feature)
+ feature = Factory(:feature)
feature.deployment_percentage = 101
feature.valid?
return feature
end
end
-
+
def test_error_messages_for
output = "<ul class=\"errors\"><li class=\"error\">must be less than or equal to 100</li></ul>"
@@ -32,8 +32,10 @@ def test_error_messages_for
:virtual_path => "partial"
)
- assert_equal output, template_result.render( Context.new, {} )
- assert template_result.render( Context.new, {} ).html_safe?
-
+ result = template_result.render( Context.new, {} )
+
+ assert_equal output, result
+ assert result.html_safe?
+
end
end
Oops, something went wrong.

0 comments on commit 9fc6791

Please sign in to comment.