├── .ruby-version ├── .rspec ├── spec ├── dummy_app │ ├── public │ │ ├── stylesheets │ │ │ ├── .gitkeep │ │ │ └── arturo.css │ │ └── javascripts │ │ │ ├── arturo.js │ │ │ └── jquery-1.4.3.min.js │ ├── app │ │ ├── assets │ │ │ └── config │ │ │ │ └── manifest.js │ │ ├── models │ │ │ ├── arturo │ │ │ │ └── feature.rb │ │ │ └── user.rb │ │ ├── controllers │ │ │ ├── application_controller.rb │ │ │ └── books_controller.rb │ │ └── views │ │ │ └── layouts │ │ │ └── application.html.erb │ ├── config │ │ ├── initializers │ │ │ ├── arturo_initializer.rb │ │ │ ├── backtrace_silencers.rb │ │ │ ├── session_store.rb │ │ │ └── secret_token.rb │ │ ├── database.yml │ │ ├── environment.rb │ │ ├── routes.rb │ │ ├── boot.rb │ │ ├── application.rb │ │ └── environments │ │ │ └── test.rb │ ├── config.ru │ └── db │ │ └── schema.rb ├── support │ └── prepare_database.rb ├── models │ ├── no_such_feature_spec.rb │ ├── features_helper_spec.rb │ ├── engine_spec.rb │ ├── feature_availability_spec.rb │ ├── middleware_spec.rb │ ├── whitelist_and_blacklist_spec.rb │ ├── feature_spec.rb │ └── cache_spec.rb ├── spec_helper.rb └── controllers │ ├── controller_filters_spec.rb │ ├── features_controller_non_admin_spec.rb │ └── features_controller_admin_spec.rb ├── Gemfile ├── app ├── assets │ ├── images │ │ └── colon.png │ ├── javascripts │ │ └── arturo.js │ └── stylesheets │ │ └── arturo.css ├── views │ └── arturo │ │ └── features │ │ ├── forbidden.html.erb │ │ ├── new.html.erb │ │ ├── show.html.erb │ │ ├── edit.html.erb │ │ ├── _feature.html.erb │ │ ├── _form.html.erb │ │ └── index.html.erb ├── helpers │ └── arturo │ │ └── features_helper.rb └── controllers │ └── arturo │ └── features_controller.rb ├── lib ├── arturo │ ├── version.rb │ ├── null_logger.rb │ ├── feature_factories.rb │ ├── engine.rb │ ├── no_such_feature.rb │ ├── feature_management.rb │ ├── controller_filters.rb │ ├── feature_params_support.rb │ ├── feature_availability.rb │ ├── test_support.rb │ ├── middleware.rb │ ├── special_handling.rb │ ├── feature_methods.rb │ └── feature_caching.rb ├── generators │ └── arturo │ │ ├── templates │ │ ├── arturo_customizations.css │ │ ├── feature.rb │ │ ├── migration.erb │ │ └── initializer.rb │ │ ├── feature_model_generator.rb │ │ ├── initializer_generator.rb │ │ ├── routes_generator.rb │ │ ├── assets_generator.rb │ │ └── migration_generator.rb └── arturo.rb ├── .github ├── CODEOWNERS └── workflows │ ├── codeql.yaml │ ├── publish.yml │ ├── ci.yml │ └── rails_main_testing.yml ├── .gitignore ├── gemfiles ├── common.rb ├── rails7.2.gemfile ├── rails8.0.gemfile ├── rails8.1.gemfile └── rails_main.gemfile ├── config ├── routes.rb └── locales │ └── en.yml ├── Rakefile ├── LICENSE ├── arturo.gemspec ├── CONTRIBUTING.md ├── gem-public_cert.pem ├── CHANGELOG.md └── README.md /.ruby-version: -------------------------------------------------------------------------------- 1 | 3.2 2 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --order rand 3 | -------------------------------------------------------------------------------- /spec/dummy_app/public/stylesheets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | eval_gemfile('gemfiles/rails7.2.gemfile') 2 | -------------------------------------------------------------------------------- /spec/dummy_app/app/assets/config/manifest.js: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /app/assets/images/colon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zendesk/arturo/main/app/assets/images/colon.png -------------------------------------------------------------------------------- /lib/arturo/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Arturo 3 | VERSION = '4.1.1' 4 | end 5 | -------------------------------------------------------------------------------- /spec/dummy_app/config/initializers/arturo_initializer.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'arturo' 3 | -------------------------------------------------------------------------------- /lib/generators/arturo/templates/arturo_customizations.css: -------------------------------------------------------------------------------- 1 | /* Make any customizations to the Arturo styles here */ 2 | -------------------------------------------------------------------------------- /spec/dummy_app/config/database.yml: -------------------------------------------------------------------------------- 1 | test: 2 | adapter: sqlite3 3 | database: db/test.sqlite3 4 | pool: 5 5 | timeout: 5000 6 | -------------------------------------------------------------------------------- /spec/dummy_app/config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | Rails.backtrace_cleaner.remove_silencers! 3 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # CODEOWNERS file 2 | # This file defines who should review code changes in this repository. 3 | 4 | * @zendesk/core-gem-owners 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.rvmrc 2 | /.bundle 3 | /arturo*.gem 4 | /gemfiles/*.gemfile.lock 5 | /Gemfile.lock 6 | tags 7 | **/*.sqlite3 8 | **/*.log 9 | **/tmp/**/* 10 | -------------------------------------------------------------------------------- /gemfiles/common.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | gemspec path: '../' 4 | 5 | gem 'debug' 6 | gem 'rspec-rails' 7 | gem 'factory_bot' 8 | gem 'timecop' 9 | -------------------------------------------------------------------------------- /spec/dummy_app/config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | DummyApp::Application.config.session_store :cookie_store, :key => '_dummy_app_session' 3 | -------------------------------------------------------------------------------- /app/views/arturo/features/forbidden.html.erb: -------------------------------------------------------------------------------- 1 | <%# frozen_string_literal: true %> 2 |

<%= t('.title') %>

3 | 4 | <%= arturo_flash_messages %> 5 | 6 |

<%= t('.text') %>

7 | -------------------------------------------------------------------------------- /gemfiles/rails7.2.gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | eval_gemfile 'common.rb' 4 | 5 | gem 'rails', '~> 7.2.0' 6 | gem 'responders', '~> 3.0' 7 | gem 'sqlite3', '~> 1.4' 8 | -------------------------------------------------------------------------------- /gemfiles/rails8.0.gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | eval_gemfile 'common.rb' 4 | 5 | gem 'rails', '~> 8.0.0' 6 | gem 'responders', '~> 3.0' 7 | gem 'sqlite3', '~> 2.0' 8 | -------------------------------------------------------------------------------- /gemfiles/rails8.1.gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | eval_gemfile 'common.rb' 4 | 5 | gem 'rails', '~> 8.1.0' 6 | gem 'responders', '~> 3.0' 7 | gem 'sqlite3', '~> 2.0' 8 | -------------------------------------------------------------------------------- /spec/dummy_app/config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require ::File.expand_path('../config/environment', __FILE__) 4 | run DummyApp::Application 5 | -------------------------------------------------------------------------------- /gemfiles/rails_main.gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | eval_gemfile 'common.rb' 4 | 5 | gem 'rails', github: 'rails/rails', branch: 'main' 6 | gem 'responders', '~> 3.0' 7 | gem "sqlite3", "~> 2" 8 | -------------------------------------------------------------------------------- /lib/arturo/null_logger.rb: -------------------------------------------------------------------------------- 1 | module Arturo 2 | class NullLogger 3 | %w[info debug error fatal warn].each do |level| 4 | define_method level do |_message| 5 | end 6 | end 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | Arturo::Engine.routes.draw do 3 | resources :features, :controller => 'arturo/features' 4 | put 'features', :to => 'arturo/features#update_all', :as => 'features_update_all' 5 | end 6 | -------------------------------------------------------------------------------- /lib/generators/arturo/templates/feature.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'active_record' 4 | 5 | module Arturo 6 | class Feature < ::ActiveRecord::Base 7 | include Arturo::FeatureMethods 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /spec/dummy_app/app/models/arturo/feature.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'active_record' 4 | 5 | module Arturo 6 | class Feature < ::ActiveRecord::Base 7 | include Arturo::FeatureMethods 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /spec/dummy_app/config/environment.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | # Load the rails application 3 | require File.expand_path('../application', __FILE__) 4 | 5 | # Initialize the rails application 6 | DummyApp::Application.initialize! 7 | -------------------------------------------------------------------------------- /spec/dummy_app/config/routes.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | DummyApp::Application.routes.draw do 3 | mount Arturo::Engine => "/arturo" 4 | 5 | resources :books, :only => :show do 6 | post :holds, :on => :member 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /app/views/arturo/features/new.html.erb: -------------------------------------------------------------------------------- 1 | <%# frozen_string_literal: true %> 2 |

<%= t('.title') %>

3 | 4 | <%= arturo_flash_messages %> 5 | 6 | <%= render :partial => 'form', :locals => { :feature => @feature, :legend => t('.legend') } %> 7 | -------------------------------------------------------------------------------- /app/views/arturo/features/show.html.erb: -------------------------------------------------------------------------------- 1 | <%# frozen_string_literal: true %> 2 |

<%= t('.title', :name => @feature.name) %>

3 | 4 | <%= arturo_flash_messages %> 5 | 6 |

Deployment percentage: <%= @feature.deployment_percentage %>

7 | -------------------------------------------------------------------------------- /spec/support/prepare_database.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'active_record' 3 | 4 | ActiveRecord::Base.establish_connection( 5 | adapter: 'sqlite3', 6 | database: ':memory:' 7 | ) 8 | 9 | ActiveRecord::Schema.verbose = false 10 | require 'dummy_app/db/schema.rb' 11 | -------------------------------------------------------------------------------- /app/views/arturo/features/edit.html.erb: -------------------------------------------------------------------------------- 1 | <%# frozen_string_literal: true %> 2 |

<%= t('.title', :name => @feature.name) %>

3 | 4 | <%= arturo_flash_messages %> 5 | 6 | <%= render :partial => 'form', :locals => { :feature => @feature, :legend => t('.legend', :name => @feature.name) } %> 7 | -------------------------------------------------------------------------------- /spec/dummy_app/app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | class ApplicationController < ActionController::Base 3 | protect_from_forgery 4 | 5 | layout 'application' 6 | 7 | def current_user 8 | User.new(:name => 'Freddykins', :admin => true) 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /lib/arturo/feature_factories.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | FactoryBot.define do 3 | sequence(:feature_symbol) { |n| "feature_#{n}".to_sym } 4 | 5 | factory :feature, :class => Arturo::Feature do 6 | symbol { generate(:feature_symbol) } 7 | deployment_percentage { rand(101) } 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/arturo/engine.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'arturo/middleware' 3 | require 'rails/engine' 4 | 5 | module Arturo 6 | class Engine < ::Rails::Engine 7 | root = File.expand_path("../../..", __FILE__) 8 | config.autoload_paths = ["#{root}/app/helpers", "#{root}/app/controllers"] 9 | config.eager_load_paths = [] 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /spec/dummy_app/app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | <%# frozen_string_literal: true %> 2 | 3 | 4 | 5 | DummyApp 6 | <%= stylesheet_link_tag :all, skip_pipeline: true %> 7 | <%= javascript_include_tag 'jquery-1.4.3.min', 'arturo', skip_pipeline: true %> 8 | <%= csrf_meta_tag %> 9 | 10 | 11 | 12 | <%= yield %> 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /lib/generators/arturo/feature_model_generator.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'rails/generators' 3 | 4 | module Arturo 5 | class FeatureModelGenerator < Rails::Generators::Base 6 | def self.source_root 7 | File.join(File.dirname(__FILE__), 'templates') 8 | end 9 | 10 | def copy_feature_model_file 11 | copy_file "feature.rb", "app/models/arturo/feature.rb" 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/generators/arturo/initializer_generator.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'rails/generators' 3 | 4 | module Arturo 5 | class InitializerGenerator < Rails::Generators::Base 6 | def self.source_root 7 | File.join(File.dirname(__FILE__), 'templates') 8 | end 9 | 10 | def copy_initializer_file 11 | copy_file "initializer.rb", "config/initializers/arturo_initializer.rb" 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /spec/dummy_app/config/boot.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'rubygems' 3 | 4 | # Set up gems listed in the Gemfile. 5 | gemfile = File.expand_path('../../Gemfile', __FILE__) 6 | begin 7 | ENV['BUNDLE_GEMFILE'] = gemfile 8 | require 'bundler' 9 | Bundler.setup 10 | rescue Bundler::GemNotFound => e 11 | STDERR.puts e.message 12 | STDERR.puts "Try running `bundle install`." 13 | exit! 14 | end if File.exist?(gemfile) 15 | -------------------------------------------------------------------------------- /app/views/arturo/features/_feature.html.erb: -------------------------------------------------------------------------------- 1 | <%# frozen_string_literal: true %> 2 | 3 | <%= feature.symbol %> 4 | <%= deployment_percentage_range_and_output_tags("features[#{feature.id}][deployment_percentage]", feature.deployment_percentage) %> 5 | <%= link_to t('.edit'), arturo_engine.edit_feature_path(feature), :rel => 'edit', :class => 'edit' %> 6 | 7 | -------------------------------------------------------------------------------- /lib/generators/arturo/routes_generator.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'rails/generators' 3 | 4 | module Arturo 5 | class RoutesGenerator < Rails::Generators::Base 6 | 7 | def add_mount 8 | if Arturo::Engine.respond_to?(:routes) 9 | route "mount Arturo::Engine => ''" 10 | else 11 | puts "This version of Rails doesn't support Engine-specific routing. Nothing to do." 12 | end 13 | end 14 | 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /lib/generators/arturo/templates/migration.erb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | class CreateFeatures < ActiveRecord::Migration<%= migration_version %> 4 | def self.up 5 | create_table :features do |t| 6 | t.string :symbol, null: false 7 | t.integer :deployment_percentage, null: false 8 | # Any additional fields here 9 | 10 | t.timestamps 11 | end 12 | end 13 | 14 | def self.down 15 | drop_table :features 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /spec/dummy_app/app/models/user.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | class User 3 | 4 | attr_reader :name, :id 5 | 6 | def initialize(options = {}) 7 | @name = options[:name] 8 | @admin = options[:admin] 9 | @id = options[:id] 10 | end 11 | 12 | def admin? 13 | !!@admin 14 | end 15 | 16 | def to_s 17 | name 18 | end 19 | 20 | def inspect 21 | type = @admin ? 'Admin' : 'User' 22 | "" 23 | end 24 | 25 | end 26 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yaml: -------------------------------------------------------------------------------- 1 | name: "CodeQL public repository scanning" 2 | 3 | on: 4 | push: 5 | schedule: 6 | - cron: "0 0 * * *" 7 | pull_request_target: 8 | types: [opened, synchronize, reopened] 9 | workflow_dispatch: 10 | 11 | permissions: 12 | contents: read 13 | security-events: write 14 | actions: read 15 | packages: read 16 | 17 | jobs: 18 | trigger-codeql: 19 | uses: zendesk/prodsec-code-scanning/.github/workflows/codeql_advanced_shared.yml@production 20 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/setup' 2 | require 'bundler/gem_tasks' 3 | 4 | require 'rspec/core/rake_task' 5 | RSpec::Core::RakeTask.new(:spec) 6 | task default: :spec 7 | 8 | require 'rdoc/task' 9 | Rake::RDocTask.new do |rdoc| 10 | version = File.exist?('VERSION') ? File.read('VERSION') : "" 11 | 12 | rdoc.rdoc_dir = 'rdoc' 13 | rdoc.title = "Betsy #{version}" 14 | rdoc.rdoc_files.include('README*') 15 | rdoc.rdoc_files.include('Gemfile') 16 | rdoc.rdoc_files.include('lib/**/*.rb') 17 | end 18 | -------------------------------------------------------------------------------- /spec/dummy_app/config/initializers/secret_token.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | if DummyApp::Application.config.respond_to?(:secret_key_base=) 4 | DummyApp::Application.config.secret_key_base = '2d93f8060fff84c29e7d212af5f6400626f47ebc1e16b2a2bc4d7562cfbe72d149cc8b8ce73b54f9b79c202cd2eb887000e761e3e7eb387a63fe11a4c557d253' 5 | else 6 | DummyApp::Application.config.secret_token = '2d93f8060fff84c29e7d212af5f6400626f47ebc1e16b2a2bc4d7562cfbe72d149cc8b8ce73b54f9b79c202cd2eb887000e761e3e7eb387a63fe11a4c557d253' 7 | end 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012-2013 James A. Rosen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /arturo.gemspec: -------------------------------------------------------------------------------- 1 | require_relative 'lib/arturo/version' 2 | 3 | Gem::Specification.new do |s| 4 | s.name = 'arturo' 5 | s.version = Arturo::VERSION 6 | s.authors = ['James A. Rosen'] 7 | s.email = 'james.a.rosen@gmail.com' 8 | 9 | s.summary = 'Feature sliders, wrapped up in an engine' 10 | s.homepage = 'http://github.com/zendesk/arturo' 11 | s.files = Dir['lib/**/*', 'app/**/*', 'config/**/*'] + %w(README.md CHANGELOG.md LICENSE) 12 | s.description = 'Deploy features incrementally to your users' 13 | 14 | s.license = 'APLv2' 15 | s.required_ruby_version = '>= 3.2' 16 | 17 | s.add_runtime_dependency 'activerecord', '>= 7.2' 18 | end 19 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish to RubyGems.org 2 | 3 | on: 4 | push: 5 | branches: main 6 | paths: lib/arturo/version.rb 7 | workflow_dispatch: 8 | 9 | jobs: 10 | publish: 11 | runs-on: ubuntu-latest 12 | environment: rubygems-publish 13 | if: github.repository_owner == 'zendesk' 14 | permissions: 15 | id-token: write 16 | contents: write 17 | steps: 18 | - uses: actions/checkout@v4 19 | - name: Set up Ruby 20 | uses: ruby/setup-ruby@v1 21 | with: 22 | bundler-cache: false 23 | 24 | - name: Install dependencies 25 | run: bundle install 26 | - uses: rubygems/release-gem@v1 27 | -------------------------------------------------------------------------------- /lib/arturo/no_such_feature.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Arturo 3 | 4 | # A Null-Object stand-in for a Feature. 5 | class NoSuchFeature 6 | 7 | attr_reader :symbol 8 | 9 | def initialize(symbol) 10 | raise ArgumentError.new(I18n.t('arturo.no_such_feature.symbol_required')) if symbol.nil? 11 | @symbol = symbol 12 | end 13 | 14 | def enabled_for?(feature_recipient) 15 | false 16 | end 17 | 18 | def name 19 | I18n.t('arturo.no_such_feature.name', :symbol => symbol) 20 | end 21 | 22 | alias_method :to_s, :name 23 | 24 | def inspect 25 | "" 26 | end 27 | 28 | end 29 | 30 | end 31 | -------------------------------------------------------------------------------- /spec/models/no_such_feature_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | 4 | describe Arturo::NoSuchFeature do 5 | before do 6 | reset_translations! 7 | end 8 | 9 | let(:feature) { Arturo::NoSuchFeature.new(:an_unknown_feature) } 10 | 11 | it 'is not enabled' do 12 | expect(feature.enabled_for?(nil)).to be(false) 13 | expect(feature.enabled_for?(double('User', to_s: 'Saorse', id: 12))).to be(false) 14 | end 15 | 16 | it 'requires a symbol' do 17 | expect { 18 | Arturo::NoSuchFeature.new(nil) 19 | }.to raise_error(ArgumentError) 20 | end 21 | 22 | it 'responds to to_s' do 23 | expect(feature.to_s).to include(feature.name) 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /app/assets/javascripts/arturo.js: -------------------------------------------------------------------------------- 1 | if (typeof(jQuery) === 'function') { 2 | jQuery.arturo = { 3 | agentSupportsHTML5Output: ('for' in jQuery('')), 4 | 5 | linkAndShowOutputs: function() { 6 | if (jQuery.arturo.agentSupportsHTML5Output) { 7 | jQuery('.features output,.feature_new output,.feature_edit output').each(function(i, output) { 8 | var output = jQuery(output); 9 | var input = jQuery('#' + output.attr('for')); 10 | input.change(function() { 11 | output.val(input.val()); 12 | }); 13 | output.removeClass('no_js'); 14 | }); 15 | } 16 | } 17 | }; 18 | 19 | jQuery(function() { 20 | jQuery.arturo.linkAndShowOutputs(); 21 | }); 22 | } 23 | -------------------------------------------------------------------------------- /spec/dummy_app/config/application.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require File.expand_path('../boot', __FILE__) 3 | 4 | require 'bundler/setup' 5 | require 'logger' 6 | require 'rails/all' 7 | require 'arturo/engine' 8 | 9 | Bundler.require(:default, Rails.env) if defined?(Bundler) 10 | 11 | module DummyApp 12 | class Application < Rails::Application 13 | config.encoding = "utf-8" 14 | config.filter_parameters += [:password] 15 | config.assets.precompile += %w( arturo.js ) if config.respond_to?(:assets) 16 | config.action_controller.action_on_unpermitted_parameters = :raise 17 | config.active_support.deprecation = :raise 18 | config.secret_key_base = 'dsdsdshjdshdshdshdshjdhjshjsdhjdsjhdshjds' 19 | config.i18n.enforce_available_locales = true 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /lib/generators/arturo/assets_generator.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'rails/generators' 3 | 4 | module Arturo 5 | class AssetsGenerator < Rails::Generators::Base 6 | 7 | def self.source_root 8 | File.join(File.dirname(__FILE__), 'templates') 9 | end 10 | 11 | def copy_assets 12 | copy_file 'arturo_customizations.css', 'public/stylesheets/arturo_customizations.css', :skip => true 13 | 14 | unless defined?(Sprockets) 15 | copy_file 'app/assets/stylesheets/arturo.css', 'public/stylesheets/arturo.css', :force => true 16 | copy_file 'app/assets/javascripts/arturo.js', 'public/javascripts/arturo.js' 17 | copy_file 'app/assets/images/colon.png', 'public/images/colon.png' 18 | end 19 | end 20 | 21 | end 22 | end 23 | 24 | -------------------------------------------------------------------------------- /spec/dummy_app/app/controllers/books_controller.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | class BooksController < ApplicationController 3 | 4 | require_feature :books 5 | require_feature :book_holds, :only => :holds 6 | 7 | # instead of a model: 8 | BOOKS = {} 9 | 10 | def show 11 | if (book = requested_book) 12 | render :plain => book 13 | else 14 | render :plain => 'Not Found', :status => 404 15 | end 16 | end 17 | 18 | def holds 19 | if (book = requested_book) 20 | render :plain => "Added hold on #{book}" 21 | else 22 | render :plain => 'Not Found', :status => 404 23 | end 24 | end 25 | 26 | protected 27 | 28 | def requested_book 29 | BOOKS[params[:id].to_s] 30 | end 31 | 32 | def current_user 33 | User.new(:name => "Fred") 34 | end 35 | 36 | end 37 | -------------------------------------------------------------------------------- /spec/dummy_app/public/javascripts/arturo.js: -------------------------------------------------------------------------------- 1 | if (typeof(jQuery) === 'function') { 2 | jQuery.arturo = { 3 | agentSupportsHTML5Output: ('for' in jQuery('')), 4 | 5 | linkAndShowOutputs: function() { 6 | if (jQuery.arturo.agentSupportsHTML5Output) { 7 | jQuery('.features output,.feature_new output,.feature_edit output').each(function(i, output) { 8 | var output = jQuery(output); 9 | var input = jQuery('#' + output.attr('for')); 10 | input.change(function() { 11 | console.log('input value changed to ' + input.val()); 12 | output.val(input.val()); 13 | }); 14 | output.removeClass('no_js'); 15 | }); 16 | } 17 | } 18 | }; 19 | 20 | jQuery(function() { 21 | jQuery.arturo.linkAndShowOutputs(); 22 | }); 23 | } 24 | -------------------------------------------------------------------------------- /lib/generators/arturo/migration_generator.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'rails/generators' 4 | require 'rails/generators/migration' 5 | require 'rails/generators/active_record' 6 | 7 | module Arturo 8 | class MigrationGenerator < Rails::Generators::Base 9 | include Rails::Generators::Migration 10 | 11 | def self.source_root 12 | File.join(File.dirname(__FILE__), 'templates') 13 | end 14 | 15 | def self.next_migration_number(dirname) 16 | ::ActiveRecord::Generators::Base.next_migration_number(dirname) 17 | end 18 | 19 | def create_migration_file 20 | migration_template 'migration.erb', 'db/migrate/create_features.rb', { migration_version: migration_version } 21 | end 22 | 23 | def migration_version 24 | "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | ENV['RAILS_ENV'] = 'test' 3 | require 'debug/prelude' 4 | require 'debug/config' 5 | require 'dummy_app/config/environment' 6 | require 'rspec/rails' 7 | require 'factory_bot' 8 | require 'timecop' 9 | require 'support/prepare_database' 10 | require 'arturo' 11 | require 'arturo/feature' 12 | require 'arturo/feature_factories' 13 | require 'arturo/test_support' 14 | 15 | RSpec.configure do |config| 16 | config.include ::FactoryBot::Syntax::Methods 17 | config.use_transactional_fixtures = true 18 | 19 | def reset_translations! 20 | I18n.reload! 21 | end 22 | 23 | def define_translation(key, value) 24 | hash = key.to_s.split('.').reverse.inject(value) do |value, key_part| 25 | { key_part.to_sym => value } 26 | end 27 | I18n.backend.store_translations I18n.locale, hash 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/arturo/feature_management.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Arturo 3 | 4 | # A mixin that is included by Arturo::FeaturesController and is declared 5 | # as a helper for all views. It provides a single method, 6 | # may_manage_features?, that returns whether or not the current user 7 | # may manage features. By default, it is implemented as follows: 8 | # 9 | # def may_manage_features? 10 | # current_user.present? && current_user.admin? 11 | # end 12 | # 13 | # If you would like to change this implementation, it is recommended 14 | # you do so in config/initializers/arturo_initializer.rb 15 | module FeatureManagement 16 | 17 | # @return [true,false] whether the current user may manage features 18 | def may_manage_features? 19 | current_user.present? && current_user.admin? 20 | end 21 | 22 | end 23 | 24 | end 25 | -------------------------------------------------------------------------------- /lib/generators/arturo/templates/initializer.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'arturo' 3 | require 'arturo/feature' 4 | 5 | # Configure who may manage features here. 6 | # The following is the default implementation. 7 | # Arturo::FeatureManagement.class_eval do 8 | # def may_manage_features? 9 | # current_user.present? && current_user.admin? 10 | # end 11 | # end 12 | 13 | # Configure what receives features here. 14 | # The following is the default implementation. 15 | # Arturo::FeatureAvailability.class_eval do 16 | # def feature_recipient 17 | # current_user 18 | # end 19 | # end 20 | 21 | # Whitelists and Blacklists: 22 | # 23 | # Enable feature one for all admins: 24 | # Arturo::Feature.whitelist(:feature_one) do |user| 25 | # user.admin? 26 | # end 27 | # 28 | # Disable feature two for all small accounts: 29 | # Arturo::Feature.blacklist(:feature_two) do |user| 30 | # user.account.small? 31 | # end 32 | -------------------------------------------------------------------------------- /app/views/arturo/features/_form.html.erb: -------------------------------------------------------------------------------- 1 | <%# frozen_string_literal: true %> 2 | <%= form_for(feature, :as => 'feature', :url => (feature.new_record? ? arturo_engine.features_path : arturo_engine.feature_path(feature))) do |form| %> 3 |
4 | <%= legend %> 5 | 6 | <%= form.label(:symbol) %> 7 | <%= form.text_field(:symbol, :required => 'required', :pattern => Arturo::Feature::SYMBOL_REGEX.source, :class => 'symbol') %> 8 | <%= error_messages_for_feature(feature, :symbol) %> 9 | 10 | <%= form.label(:deployment_percentage) %> 11 | <%= form.range_field(:deployment_percentage, :min => '0', :max => '100', :step => '1', :class => 'deployment_percentage') %> 12 | <%= deployment_percentage_output_tag 'feature_deployment_percentage', feature.deployment_percentage %> 13 | <%= error_messages_for_feature(feature, :deployment_percentage) %> 14 | 15 |
<%= form.submit %>
16 |
17 | <% end %> 18 | -------------------------------------------------------------------------------- /spec/controllers/controller_filters_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | 4 | describe BooksController, type: :controller do 5 | before do 6 | BooksController::BOOKS.merge!( 7 | '1' => 'The Varieties of Religious Experience', 8 | '2' => 'Jane Eyre', 9 | '3' => 'Robison Crusoe' 10 | ) 11 | create(:feature, symbol: :books, deployment_percentage: 100) 12 | create(:feature, symbol: :book_holds, deployment_percentage: 0) 13 | end 14 | 15 | it 'does not consider on_feature_disabled as an action' do 16 | expect(controller.action_methods).to_not include(:on_feature_disabled) 17 | end 18 | 19 | it 'works with a get on show' do 20 | get :show, params: { id: '2' } 21 | 22 | expect(response).to be_successful 23 | end 24 | 25 | it 'works with a post on holds' do 26 | post :holds, params: { id: '3' } 27 | 28 | expect(response).to have_http_status(:forbidden) 29 | end 30 | 31 | end 32 | -------------------------------------------------------------------------------- /lib/arturo/controller_filters.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Arturo 3 | 4 | # Adds before filters to controllers for specifying that actions 5 | # require features to be enabled for the requester. 6 | # 7 | # To configure how the controller responds when the feature is 8 | # *not* enabled, redefine #on_feature_disabled(feature_name). 9 | # It must render or raise an exception. 10 | module ControllerFilters 11 | 12 | def self.included(base) 13 | base.extend Arturo::ControllerFilters::ClassMethods 14 | end 15 | 16 | def on_feature_disabled(feature_name) 17 | render :plain => 'Forbidden', :status => 403 18 | end 19 | 20 | module ClassMethods 21 | 22 | def require_feature(name, options = {}) 23 | send(:before_action, options) do |controller| 24 | unless controller.feature_enabled?(name) 25 | controller.on_feature_disabled(name) 26 | end 27 | end 28 | end 29 | 30 | end 31 | 32 | end 33 | 34 | end 35 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | For bug reports, open an [issue](https://github.com/zendesk/arturo/issues) 2 | on GitHub. 3 | 4 | ## Getting Started 5 | 6 | 1. Install dependencies with `bundle install` 7 | 2. Run tests with `rake test` 8 | 9 | ### Releasing a new version 10 | A new version is published to RubyGems.org every time a change to `version.rb` is pushed to the `main` branch. 11 | In short, follow these steps: 12 | 1. Update `version.rb`, 13 | 2. update version in all `Gemfile.lock` files, 14 | 3. merge this change into `main`, and 15 | 4. look at [the action](https://github.com/zendesk/arturo/actions/workflows/publish.yml) for output. 16 | 17 | To create a pre-release from a non-main branch: 18 | 1. change the version in `version.rb` to something like `1.2.0.pre.1` or `2.0.0.beta.2`, 19 | 2. push this change to your branch, 20 | 3. go to [Actions → “Publish to RubyGems.org” on GitHub](https://github.com/zendesk/arturo/actions/workflows/publish.yml), 21 | 4. click the “Run workflow” button, 22 | 5. pick your branch from a dropdown. 23 | -------------------------------------------------------------------------------- /spec/dummy_app/db/schema.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | # encoding: UTF-8 3 | # This file is auto-generated from the current state of the database. Instead 4 | # of editing this file, please use the migrations feature of Active Record to 5 | # incrementally modify your database, and then regenerate this schema definition. 6 | # 7 | # Note that this schema.rb definition is the authoritative source for your 8 | # database schema. If you need to create the application database on another 9 | # system, you should be using db:schema:load, not running all the migrations 10 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 11 | # you'll amass, the slower it'll run and the greater likelihood for issues). 12 | # 13 | # It's strongly recommended to check this file into your version control system. 14 | 15 | ActiveRecord::Schema.define(:version => 20101017195547) do 16 | 17 | create_table "features", :force => true do |t| 18 | t.string "symbol", :null => false 19 | t.integer "deployment_percentage", :null => false 20 | t.datetime "created_at" 21 | t.datetime "updated_at" 22 | end 23 | 24 | end 25 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: push 4 | 5 | jobs: 6 | specs: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | ruby-version: 13 | - '3.2' 14 | - "3.3" 15 | - "3.4" 16 | gemfile: 17 | - rails7.2 18 | - rails8.0 19 | - rails8.1 20 | name: Ruby ${{ matrix.ruby-version }}, ${{ matrix.gemfile }} 21 | env: 22 | BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile 23 | steps: 24 | - uses: actions/checkout@v4 25 | - name: Set up Ruby 26 | uses: ruby/setup-ruby@v1 27 | with: 28 | ruby-version: ${{ matrix.ruby-version }} 29 | bundler-cache: true 30 | - name: RSpec 31 | run: bundle exec rspec 32 | 33 | specs_successful: 34 | name: Specs passing? 35 | needs: specs 36 | if: always() 37 | runs-on: ubuntu-latest 38 | steps: 39 | - run: | 40 | if ${{ needs.specs.result == 'success' }} 41 | then 42 | echo "All specs pass" 43 | else 44 | echo "Some specs failed" 45 | false 46 | fi 47 | -------------------------------------------------------------------------------- /lib/arturo/feature_params_support.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Arturo 3 | 4 | # Mix in to FeaturesController. Provides the logic for getting parameters 5 | # for creating/updated features out of the request. 6 | module FeatureParamsSupport 7 | 8 | module WithoutStrongParams 9 | def feature_params 10 | params[:feature] || {} 11 | end 12 | 13 | def features_params 14 | params[:features] || {} 15 | end 16 | end 17 | 18 | module WithStrongParams 19 | PERMITTED_ATTRIBUTES = [ :symbol, :deployment_percentage ] 20 | 21 | def feature_params 22 | if feature = params[:feature] 23 | feature.permit(PERMITTED_ATTRIBUTES) 24 | end 25 | end 26 | 27 | def features_params 28 | features = params[:features] 29 | features.each do |id, attributes| 30 | attributes = attributes.to_unsafe_h if attributes.respond_to?(:to_unsafe_h) 31 | features[id] = ActionController::Parameters.new(attributes).permit(*PERMITTED_ATTRIBUTES) 32 | end 33 | end 34 | end 35 | 36 | include defined?(ActionController::Parameters) ? WithStrongParams : WithoutStrongParams 37 | 38 | end 39 | 40 | end 41 | -------------------------------------------------------------------------------- /.github/workflows/rails_main_testing.yml: -------------------------------------------------------------------------------- 1 | name: Test against Rails main 2 | 3 | on: 4 | schedule: 5 | - cron: "0 0 * * *" # Run every day at 00:00 UTC 6 | workflow_dispatch: 7 | push: 8 | 9 | jobs: 10 | specs: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | fail-fast: false 14 | matrix: 15 | ruby-version: 16 | - "3.4" 17 | gemfile: 18 | - rails_main 19 | name: Ruby ${{ matrix.ruby-version }}, ${{ matrix.gemfile }} 20 | env: 21 | BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: Set up Ruby 25 | uses: ruby/setup-ruby@v1 26 | with: 27 | ruby-version: ${{ matrix.ruby-version }} 28 | bundler-cache: true 29 | - name: RSpec 30 | run: bundle exec rspec 31 | 32 | specs_successful: 33 | name: Rails Main Specs passing? 34 | needs: specs 35 | if: always() 36 | runs-on: ubuntu-latest 37 | steps: 38 | - run: | 39 | if ${{ needs.specs.result == 'success' }} 40 | then 41 | echo "All specs pass" 42 | else 43 | echo "Some specs failed" 44 | false 45 | fi 46 | -------------------------------------------------------------------------------- /lib/arturo/feature_availability.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Arturo 3 | 4 | # A mixin that provides #feature_enabled? and #if_feature_enabled 5 | # methods; to be mixed in by Controllers and Helpers. The including 6 | # class must return some "thing that has features" (e.g. a User, Person, 7 | # or Account) when Arturo.feature_recipient is bound to an instance 8 | # and called. 9 | # 10 | # @see Arturo.feature_recipient 11 | module FeatureAvailability 12 | 13 | def feature_enabled?(symbol_or_feature) 14 | feature = ::Arturo::Feature.to_feature(symbol_or_feature) 15 | return false if feature.blank? 16 | feature.enabled_for?(feature_recipient) 17 | end 18 | 19 | def if_feature_enabled(symbol_or_feature, &block) 20 | if feature_enabled?(symbol_or_feature) 21 | block.call 22 | else 23 | nil 24 | end 25 | end 26 | 27 | # By default, returns current_user. 28 | # 29 | # If you would like to change this implementation, it is recommended 30 | # you do so in config/initializers/arturo_initializer.rb 31 | # @return [Object] the recipient of features. 32 | def feature_recipient 33 | current_user 34 | end 35 | 36 | end 37 | 38 | end 39 | -------------------------------------------------------------------------------- /gem-public_cert.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDPDCCAiSgAwIBAgIBADANBgkqhkiG9w0BAQUFADBEMRYwFAYDVQQDDA1qYW1l 3 | cy5hLnJvc2VuMRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJk/IsZAEZ 4 | FgNjb20wHhcNMTMwNTAxMjIxMzMxWhcNMTQwNTAxMjIxMzMxWjBEMRYwFAYDVQQD 5 | DA1qYW1lcy5hLnJvc2VuMRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJ 6 | k/IsZAEZFgNjb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDRQLSM 7 | iwNHiF7XcbFTuLjucBG9FRxZxM3/btJuq3k3al2mPxC0Hy1GGKCZiCQBQxHzS0BT 8 | 7NmS/BWG657xCsX5PdkxOMn15LKkkRHOFDohPirUbftkSN3HQLqNORjscJ/elbB7 9 | Y22PhJmkZGbFBrOMw16CXWb6k7dYX/5D2i5CU2SNssBMALFQ4jiKZtwJwauHozSn 10 | 366rEXUc3bWvq/mzTnm34jU0cbZ9GM7QZ0rQUWHLf8hOy5UGkvkATz+JOF7Eyhi5 11 | 7NniKuw7I9uxSGtFtBHy8CoIEkHRijdIUf83yxJa7KuKAeiBRz7rrIJGSb7jSdoL 12 | v7328eQ6Hr1Zp8BtAgMBAAGjOTA3MAkGA1UdEwQCMAAwHQYDVR0OBBYEFMATrfFP 13 | 8jtg3vGVodLesPtYWn7bMAsGA1UdDwQEAwIEsDANBgkqhkiG9w0BAQUFAAOCAQEA 14 | E+LeMyTXq0vA0yY+hyAnJ8twRpsvKIMCumSWzBphjzzMsFyFe1BuoYrIkj1DgyD0 15 | VLp6XCJcsdhiVZPL+wz0iIRPAc2mBbA4QmJR6T6vcPD6XjNye/z+dFGIKscNHtyJ 16 | ocDm2dxySF61lhvEUyvF9rX6k7amDKhJ93V0EOWfACGuHISflgGi9AiY+9+0kXVk 17 | gk0uK6HyG77PA2MG7+s4wLfjVb9vP69ypabAj5TKJ02aavV35EjYVRjM6UbTA/P7 18 | /WrHsXKhYfN8xLplXXGkSt3NS8RFhQFCOIdgsXEljEvZpVxuri5ObH0zqxUNXOlq 19 | 5lEi56n3KkkKRhu1cE61gA== 20 | -----END CERTIFICATE----- 21 | -------------------------------------------------------------------------------- /app/views/arturo/features/index.html.erb: -------------------------------------------------------------------------------- 1 | <%# frozen_string_literal: true %> 2 |

<%= t('.title') %>

3 | 4 | <%= arturo_flash_messages %> 5 | 6 | <%= form_tag(arturo_engine.features_path, :method => 'put', 'data-update-path' => arturo_engine.feature_path(:id => ':id'), :remote => true) do %> 7 |
8 | <%= t('.title') %> 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | <% @features.each do |f| %> 25 | <%= render :partial => 'feature', :locals => { :feature => f } %> 26 | <% end %> 27 | <% if @features.none? %> 28 | 29 | <% end %> 30 | 31 |
<%= t('activerecord.attributes.arturo/feature.name') %><%= t('activerecord.attributes.arturo/feature.deployment_percentage') %> 
<%= link_to t('.new'), arturo_engine.new_feature_path %> <%= submit_tag %>
<%= t('.none_yet') %>
32 |
33 | <% end %> 34 | -------------------------------------------------------------------------------- /lib/arturo.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative 'arturo/null_logger' 4 | require_relative 'arturo/special_handling' 5 | require_relative 'arturo/feature_methods' 6 | require_relative 'arturo/feature_availability' 7 | require_relative 'arturo/feature_management' 8 | require_relative 'arturo/feature_caching' 9 | require_relative 'arturo/controller_filters' 10 | 11 | module Arturo 12 | class << self 13 | # Quick check for whether a feature is enabled for a recipient. 14 | # @param [String, Symbol] feature_name 15 | # @param [#id] recipient 16 | # @return [true,false] whether the feature exists and is enabled for the recipient 17 | def feature_enabled_for?(feature_name, recipient) 18 | return false if recipient.nil? 19 | 20 | f = self::Feature.to_feature(feature_name) 21 | f && f.enabled_for?(recipient) 22 | end 23 | 24 | def logger=(logger) 25 | @logger = logger 26 | end 27 | 28 | def logger 29 | @logger || NullLogger.new 30 | end 31 | end 32 | end 33 | 34 | ActiveSupport.on_load(:action_controller) do 35 | include Arturo::FeatureAvailability 36 | include Arturo::ControllerFilters 37 | if respond_to?(:helper) 38 | helper Arturo::FeatureAvailability 39 | helper Arturo::FeatureManagement 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /spec/models/features_helper_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | require 'arturo/features_helper' 4 | 5 | describe Arturo::FeaturesHelper do 6 | include ActionView::Helpers::TagHelper 7 | include Arturo::FeaturesHelper 8 | 9 | attr_accessor :output_buffer # Used by the features helper 10 | 11 | let(:bad_feature) do 12 | create(:feature).tap do |f| 13 | f.deployment_percentage = 101 14 | f.valid? 15 | end 16 | end 17 | 18 | it 'generates an error message for bad features' do 19 | expected = "
  • must be less than or equal to 100
" 20 | actual = error_messages_for_feature(bad_feature, :deployment_percentage) 21 | 22 | expect(actual).to eq(expected) 23 | expect(actual).to be_html_safe 24 | end 25 | 26 | it 'sets flash messages' do 27 | html = arturo_flash_messages( 28 | :notice => 'foo', 29 | :error => [ 'bar', 'baz' ] 30 | ) 31 | html = Nokogiri::HTML::Document.parse(html) 32 | 33 | expect(html.css('.alert.alert-arturo .close[data-dismiss="alert"]').count).to eq(3) 34 | expect(html.css('.alert-notice').text).to match(/^foo/) 35 | expect(html.css('.alert-error')[0].text).to match( /^bar/) 36 | expect(html.css('.alert-error')[1].text).to match( /^baz/) 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /spec/models/engine_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | 4 | describe Arturo::Engine do 5 | 6 | it 'includes feature availability' do 7 | expect(ActionController::Base).to be < Arturo::FeatureAvailability 8 | end 9 | 10 | it 'does not define availability methods as actions' do 11 | expect(BooksController.action_methods).to_not include('feature_enabled?') 12 | expect(BooksController.action_methods).to_not include('if_feature_enabled') 13 | expect(BooksController.action_methods).to_not include('feature_recipient') 14 | end 15 | 16 | it 'defines availability as a helper' do 17 | expect(Arturo::FeaturesController._helpers).to be < Arturo::FeatureAvailability 18 | end 19 | 20 | it 'includes filters in controllers' do 21 | expect(ActionController::Base).to be < Arturo::ControllerFilters 22 | end 23 | 24 | it 'does not define filter methods as actions' do 25 | expect(BooksController.action_methods).to_not include('on_feature_disabled') 26 | end 27 | 28 | it 'defines feature management as a helper' do 29 | expect(BooksController._helpers).to be < Arturo::FeatureManagement 30 | end 31 | 32 | it 'does not define feature management methods as actions' do 33 | expect(BooksController.action_methods).to_not include('may_manage_features?') 34 | end 35 | 36 | end 37 | -------------------------------------------------------------------------------- /spec/models/feature_availability_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | require 'arturo/features_helper' 4 | 5 | describe Arturo::FeatureAvailability do 6 | let!(:current_user) { double('CurrentUser') } 7 | let!(:helper) { double('Helper', current_user: current_user).tap { |h| h.extend described_class } } 8 | 9 | let(:feature) { create(:feature) } 10 | let(:block) { -> { 'Content that requires a feature' } } 11 | 12 | describe 'if_feature_enabled' do 13 | it 'does not call the block with non existent feature' do 14 | expect(block).to_not receive(:call) 15 | expect(helper.if_feature_enabled(:nonexistent, &block)).to be_nil 16 | end 17 | 18 | it 'uses feature recipient' do 19 | expect(feature).to receive(:enabled_for?).with(current_user) 20 | helper.if_feature_enabled(feature, &block) 21 | end 22 | 23 | it 'does not call the block with disabled feature' do 24 | allow(feature).to receive(:enabled_for?).and_return(false) 25 | expect(block).to_not receive(:call) 26 | expect(helper.if_feature_enabled(feature, &block)).to be_nil 27 | end 28 | 29 | it 'calls the block with enabled feature' do 30 | allow(feature).to receive(:enabled_for?).and_return(true) 31 | expect(block).to receive(:call).and_return('result') 32 | expect(helper.if_feature_enabled(feature, &block)).to eq('result') 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | en: 2 | activerecord: 3 | models: 4 | "arturo/feature": "Feature" 5 | attributes: 6 | "arturo/feature": 7 | symbol: "Symbol" 8 | name: "Name" 9 | deployment_percentage: "Deployment Percentage" 10 | arturo: 11 | feature: 12 | nameless: "(no name)" 13 | features: 14 | index: 15 | title: 'Features' 16 | new: 'New' 17 | none_yet: No features yet. 18 | new: 19 | title: New Feature 20 | legend: "New Feature" 21 | edit: 22 | title: "Edit Feature %{name}" 23 | legend: "Edit Feature %{name}" 24 | feature: 25 | edit: 'Edit' 26 | show: 27 | title: "Feature %{name}" 28 | forbidden: 29 | title: Forbidden 30 | text: You do not have permission to access that resource. 31 | flash: 32 | no_such_feature: "No such feature: %{id}" 33 | error_updating: "Error updating feature %{id}" 34 | updated_many: "Updated %{count} feature(s)" 35 | created: "Created %{name}" 36 | error_creating: "Sorry, there was an error creating the feature." 37 | updated: "Updated %{name}" 38 | error_updating: "Could not update %{name}: %{errors}." 39 | removed: "Removed %{name}" 40 | error_removing: "Sorry, there was an error removing %{name}" 41 | no_such_feature: 42 | name: "NoSuchFeature: %{symbol}" 43 | symbol_required: "NoSuchFeature marker objects must have a symbol." 44 | -------------------------------------------------------------------------------- /spec/controllers/features_controller_non_admin_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | require 'arturo/features_controller' 4 | 5 | describe Arturo::FeaturesController, type: :request do 6 | 7 | before do 8 | allow_any_instance_of(Arturo::FeaturesController) 9 | .to receive(:current_user).and_return(nil) 10 | end 11 | 12 | it 'returns forbidden with get on index' do 13 | get '/arturo/features' 14 | expect(response).to have_http_status(:forbidden) 15 | end 16 | 17 | it 'returns forbidden with get on new' do 18 | get '/arturo/features/new' 19 | expect(response).to have_http_status(:forbidden) 20 | end 21 | 22 | it 'returns forbidden with post on create' do 23 | post '/arturo/features', params: { feature: { deployment_percentage: '38' } } 24 | 25 | expect(response).to have_http_status(:forbidden) 26 | end 27 | 28 | it 'returns forbidden with get on show' do 29 | get '/arturo/features/1' 30 | expect(response).to have_http_status(:forbidden) 31 | end 32 | 33 | it 'returns forbidden with get on edit' do 34 | get '/arturo/features/1' 35 | expect(response).to have_http_status(:forbidden) 36 | end 37 | 38 | it 'returns forbidden with put on update' do 39 | put '/arturo/features/1', params: { feature: { deployment_percentage: '81' } } 40 | 41 | expect(response).to have_http_status(:forbidden) 42 | end 43 | 44 | it 'returns forbidden with delete on destroy' do 45 | delete '/arturo/features/1' 46 | expect(response).to have_http_status(:forbidden) 47 | end 48 | 49 | end 50 | -------------------------------------------------------------------------------- /lib/arturo/test_support.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | Arturo.instance_eval do 3 | 4 | # Enable a feature; create it if necessary. 5 | # For use in testing. Not auto-required on load. To load, 6 | # 7 | # require 'arturo/test_support' 8 | # 9 | # @param [Symbol, String] name the feature name 10 | def enable_feature!(name) 11 | if feature = Arturo::Feature.find_feature(name) 12 | feature = feature.class.find(feature.id) if feature.frozen? 13 | feature.update(:deployment_percentage => 100) 14 | else 15 | Arturo::Feature.create!(:symbol => name, :deployment_percentage => 100) 16 | end 17 | end 18 | 19 | # Disable a feature if it exists. 20 | # For use in testing. Not auto-required on load. To load, 21 | # 22 | # require 'arturo/test_support' 23 | # 24 | # @param [Symbol, String] name the feature name 25 | def disable_feature!(name) 26 | if feature = Arturo::Feature.find_feature(name) 27 | feature = feature.class.find(feature.id) if feature.frozen? 28 | feature.update(:deployment_percentage => 0) 29 | end 30 | end 31 | 32 | # Enable or disable a feature. If enabling, create it if necessary. 33 | # For use in testing. Not auto-required on load. To load, 34 | # 35 | # require 'arturo/test_support' 36 | # 37 | # @param [Symbol, String] name the feature name 38 | # @param Boolean enabled should the feature be enabled? 39 | def set_feature!(name, enabled) 40 | if enabled 41 | enable_feature!(name) 42 | else 43 | disable_feature!(name) 44 | end 45 | end 46 | 47 | end 48 | -------------------------------------------------------------------------------- /spec/dummy_app/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | DummyApp::Application.configure do 3 | # Settings specified here will take precedence over those in config/environment.rb 4 | 5 | # The test environment is used exclusively to run your application's 6 | # test suite. You never need to work with it otherwise. Remember that 7 | # your test database is "scratch space" for the test suite and is wiped 8 | # and recreated between test runs. Don't rely on the data there! 9 | config.cache_classes = true 10 | 11 | config.eager_load = false 12 | 13 | # Show full error reports and disable caching 14 | config.consider_all_requests_local = true 15 | config.action_controller.perform_caching = false 16 | 17 | # Raise exceptions instead of rendering exception templates 18 | config.action_dispatch.show_exceptions = false 19 | 20 | # Disable request forgery protection in test environment 21 | config.action_controller.allow_forgery_protection = false 22 | 23 | # Tell Action Mailer not to deliver emails to the real world. 24 | # The :test delivery method accumulates sent emails in the 25 | # ActionMailer::Base.deliveries array. 26 | config.action_mailer.delivery_method = :test 27 | 28 | # Use SQL instead of Active Record's schema dumper when creating the test database. 29 | # This is necessary if your schema can't be completely dumped by the schema dumper, 30 | # like if you have constraints or database-specific column types 31 | # config.active_record.schema_format = :sql 32 | 33 | # Print deprecation notices to the stderr 34 | config.active_support.deprecation = :stderr 35 | 36 | config.active_support.test_order = :random 37 | end 38 | -------------------------------------------------------------------------------- /spec/models/middleware_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | 4 | describe Arturo::Middleware do 5 | let(:user) { User.new(name: 'Thor', id: 5791) } 6 | let(:feature) { create(:feature) } 7 | 8 | let(:underlying_app) { ->(_env) { [ 200, {}, ['Success']] } } 9 | 10 | def arturo_app(options = {}) 11 | options[:feature] ||= feature 12 | Arturo::Middleware.new(underlying_app, options) 13 | end 14 | 15 | it 'returns 404 with no recipient' do 16 | Arturo.enable_feature! feature 17 | status, headers, body = arturo_app.call({}) 18 | expect(status).to eq(404) 19 | end 20 | 21 | it 'retursn 404 if feature is disabled' do 22 | Arturo.disable_feature! feature 23 | status, headers, body = arturo_app.call({ 'arturo.recipient' => user }) 24 | expect(status).to eq(404) 25 | end 26 | 27 | it 'passes through if feature is enabled' do 28 | Arturo.enable_feature! feature 29 | status, headers, body = arturo_app.call({ 'arturo.recipient' => user }) 30 | expect(status).to eq(200) 31 | expect(body).to eq(['Success']) 32 | end 33 | 34 | it 'uses custom on_unavailable' do 35 | fail_app = lambda { |env| [ 403, {}, [ 'Forbidden' ] ] } 36 | Arturo.disable_feature! feature 37 | status, headers, body = arturo_app(on_unavailable: fail_app).call({}) 38 | expect(status).to eq(403) 39 | end 40 | 41 | it 'works with feature recipient' do 42 | expect(feature).to receive(:enabled_for?).with(user).and_return(false) 43 | arturo_app.call({ 'arturo.recipient' => user }) 44 | end 45 | 46 | it 'works with custom feature recipient key' do 47 | expect(feature).to receive(:enabled_for?).with(user).and_return(false) 48 | arturo_app(recipient: 'warden.user').call({ 'warden.user' => user }) 49 | end 50 | 51 | end 52 | -------------------------------------------------------------------------------- /app/helpers/arturo/features_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Arturo 3 | module FeaturesHelper 4 | include ActionView::Helpers::TagHelper 5 | 6 | def arturo_flash_messages(flash = self.flash) 7 | [ :success, :notice, :error ].inject(''.html_safe) do |output, status| 8 | [* flash[status] ].each do |messages| 9 | output += arturo_flash_message(status, messages) 10 | end 11 | output 12 | end 13 | end 14 | 15 | def arturo_flash_message(status, message) 16 | content_tag(:div, :class => "alert alert-#{status} alert-arturo") do 17 | close = content_tag(:a, '×'.html_safe, :href => '#', :class => 'close', 'data-dismiss' => 'alert') 18 | content_tag(:span, message) + close 19 | end 20 | end 21 | 22 | def deployment_percentage_range_and_output_tags(name, value, options = {}) 23 | id = sanitize_to_id(name) 24 | options = { 25 | 'type' => 'range', 26 | 'name' => name, 27 | 'id' => id, 28 | 'value' => value, 29 | 'min' => '0', 30 | 'max' => '100', 31 | 'step' => '1', 32 | 'class' => 'deployment_percentage' 33 | }.update(options.stringify_keys) 34 | tag(:input, options) + deployment_percentage_output_tag(id, value) 35 | end 36 | 37 | def deployment_percentage_output_tag(id, value) 38 | content_tag(:output, value, { 'for' => id, 'class' => 'deployment_percentage no_js' }) 39 | end 40 | 41 | def error_messages_for_feature(feature, attribute) 42 | if feature.errors[attribute].any? 43 | content_tag(:ul, :class => 'errors') do 44 | feature.errors[attribute].map { |msg| content_tag(:li, msg, :class => 'error') }.join('').html_safe 45 | end 46 | else 47 | '' 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/arturo/middleware.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Arturo 3 | # A Rack middleware that requires a feature to be present. By default, 4 | # checks feature availability against an `arturo.recipient` object 5 | # in the `env`. If that object is missing, this middleware always fails, 6 | # even if the feature is available for everyone. 7 | # 8 | # ## Usage 9 | # 10 | # use Arturo::Middleware, :feature => :foo 11 | # 12 | # ## Options 13 | # 14 | # * feature -- the name of the feature to require, as a Symbol; required 15 | # 16 | # * recipient -- the key in the `env` hash under which the feature 17 | # recipient can be found; defaults to "arturo.recipient". 18 | # * on_unavailable -- a Rack-like object 19 | # (has `#call(Hash) -> [status, headers, body]`) that 20 | # is called when the feature is unavailable; defaults 21 | # to returning `[ 404, {}, ['Not Found'] ]`. 22 | class Middleware 23 | 24 | MISSING_FEATURE_ERROR = "Cannot create an Arturo::Middleware without a :feature" 25 | 26 | DEFAULT_RECIPIENT_KEY = 'arturo.recipient' 27 | 28 | DEFAULT_ON_UNAVAILABLE = lambda { |env| [ 404, {}, ['Not Found'] ] } 29 | 30 | def initialize(app, options = {}) 31 | @app = app 32 | @feature = options[:feature] 33 | raise ArgumentError.new(MISSING_FEATURE_ERROR) unless @feature 34 | @recipient_key = options[:recipient] || DEFAULT_RECIPIENT_KEY 35 | @on_unavailable = options[:on_unavailable] || DEFAULT_ON_UNAVAILABLE 36 | end 37 | 38 | def call(env) 39 | if enabled_for_recipient?(env) 40 | @app.call(env) 41 | else 42 | fail(env) 43 | end 44 | end 45 | 46 | private 47 | 48 | def enabled_for_recipient?(env) 49 | ::Arturo.feature_enabled_for?(@feature, recipient(env)) 50 | end 51 | 52 | def recipient(env) 53 | env[@recipient_key] 54 | end 55 | 56 | def fail(env) 57 | @on_unavailable.call(env) 58 | end 59 | 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /app/assets/stylesheets/arturo.css: -------------------------------------------------------------------------------- 1 | /* 2 | WARNING: 3 | 4 | Do not edit this file. Any changes you make to this file will be overwritten 5 | when you regenerate the arturo assets (which happens when you upgrade the gem). 6 | Instead, make customizations to arturo_customizations.css. 7 | */ 8 | 9 | .features code.symbol:before { content: ":"; } 10 | 11 | .features { border-collapse: collapse; } 12 | 13 | .features thead tr:last-child th { border-bottom: 1px solid; } 14 | .features tfoot tr:first-child th { border-top: 1px solid; } 15 | 16 | .features th, .features td { 17 | margin: 0; 18 | padding: 0.5em 1.5em; 19 | text-align: left; 20 | } 21 | 22 | input.deployment_percentage[type=range] { width: 200px; } 23 | 24 | output.deployment_percentage.no_js { display: none; } 25 | output.deployment_percentage { margin-left: 1em; } 26 | output.deployment_percentage:after { content: "%"; } 27 | 28 | .features a[rel=edit] { visibility: hidden; } 29 | .features tr:hover a[rel=edit] { visibility: inherit; } 30 | 31 | .features tfoot th { 32 | text-align: right; 33 | } 34 | 35 | .features tfoot th * + * { 36 | margin-left: 2em; 37 | } 38 | 39 | .feature_new label, .feature_edit label { font-weight: bold; } 40 | 41 | .feature_new label, .feature_new .errors, 42 | .feature_edit label, .feature_edit .errors { 43 | display: block; 44 | } 45 | 46 | .feature_new label + input, .feature_new label + textarea, .feature_new label + select, 47 | .feature_edit label + input, .feature_edit label + textarea, .feature_edit label + select { 48 | margin-top: 0.5em; 49 | } 50 | 51 | .feature_new input + label, .feature_new textarea + label, .feature_new select + label, 52 | .feature_edit input + label, .feature_edit textarea + label, .feature_edit select + label { 53 | margin-top: 1.5em; 54 | } 55 | 56 | .feature_new input[type=text], .feature_edit input[type=text] { padding: 0.5em; } 57 | 58 | .feature_new input.symbol, .feature_edit input.symbol { 59 | background: transparent url('/images/colon.png') no-repeat 3px 4px; 60 | font-family: "DejaVu Sans Mono", "Droid Sans Mono", "Mondale", monospace; 61 | padding-left: 9px; 62 | } 63 | 64 | .feature_new .errors, .feature_edit .errors { color: red; } 65 | .feature_new :invalid { border-color: red; } 66 | 67 | .feature_new footer, .feature_edit footer { margin-top: 2em; } 68 | -------------------------------------------------------------------------------- /spec/dummy_app/public/stylesheets/arturo.css: -------------------------------------------------------------------------------- 1 | /* 2 | WARNING: 3 | 4 | Do not edit this file. Any changes you make to this file will be overwritten 5 | when you regenerate the arturo assets (which happens when you upgrade the gem). 6 | Instead, make customizations to arturo_customizations.css. 7 | */ 8 | 9 | .features code.symbol:before { content: ":"; } 10 | 11 | .features { border-collapse: collapse; } 12 | 13 | .features thead tr:last-child th { border-bottom: 1px solid; } 14 | .features tfoot tr:first-child th { border-top: 1px solid; } 15 | 16 | .features th, .features td { 17 | margin: 0; 18 | padding: 0.5em 1.5em; 19 | text-align: left; 20 | } 21 | 22 | input.deployment_percentage[type=range] { width: 200px; } 23 | 24 | output.deployment_percentage.no_js { display: none; } 25 | output.deployment_percentage { margin-left: 1em; } 26 | output.deployment_percentage:after { content: "%"; } 27 | 28 | .features a[rel=edit] { visibility: hidden; } 29 | .features tr:hover a[rel=edit] { visibility: inherit; } 30 | 31 | .features tfoot th { 32 | text-align: right; 33 | } 34 | 35 | .features tfoot th * + * { 36 | margin-left: 2em; 37 | } 38 | 39 | .feature_new label, .feature_edit label { font-weight: bold; } 40 | 41 | .feature_new label, .feature_new .errors, 42 | .feature_edit label, .feature_edit .errors { 43 | display: block; 44 | } 45 | 46 | .feature_new label + input, .feature_new label + textarea, .feature_new label + select, 47 | .feature_edit label + input, .feature_edit label + textarea, .feature_edit label + select { 48 | margin-top: 0.5em; 49 | } 50 | 51 | .feature_new input + label, .feature_new textarea + label, .feature_new select + label, 52 | .feature_edit input + label, .feature_edit textarea + label, .feature_edit select + label { 53 | margin-top: 1.5em; 54 | } 55 | 56 | .feature_new input[type=text], .feature_edit input[type=text] { padding: 0.5em; } 57 | 58 | .feature_new input.symbol, .feature_edit input.symbol { 59 | background: transparent url('/images/colon.png') no-repeat 3px 4px; 60 | font-family: "DejaVu Sans Mono", "Droid Sans Mono", "Mondale", monospace; 61 | padding-left: 9px; 62 | } 63 | 64 | .feature_new .errors, .feature_edit .errors { color: red; } 65 | .feature_new :invalid { border-color: red; } 66 | 67 | .feature_new footer, .feature_edit footer { margin-top: 2em; } 68 | -------------------------------------------------------------------------------- /spec/models/whitelist_and_blacklist_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | 4 | describe 'Whilelist and Blacklist' do 5 | 6 | let(:feature) { create(:feature) } 7 | 8 | before do 9 | Arturo::Feature.whitelists.clear 10 | Arturo::Feature.blacklists.clear 11 | end 12 | 13 | after do 14 | Arturo::Feature.whitelists.clear 15 | Arturo::Feature.blacklists.clear 16 | end 17 | 18 | it 'overrides percent calculation with whitelist' do 19 | feature.deployment_percentage = 0 20 | Arturo::Feature.whitelist(feature.symbol) { |thing| true } 21 | expect(feature.enabled_for?(:a_thing)).to be(true) 22 | end 23 | 24 | it 'overrides percent calculation with blacklist' do 25 | feature.deployment_percentage = 100 26 | Arturo::Feature.blacklist(feature.symbol) { |thing| true } 27 | expect(feature.enabled_for?(:a_thing)).to be(false) 28 | end 29 | 30 | it 'prefers blacklist over whitelist' do 31 | Arturo::Feature.whitelist(feature.symbol) { |thing| true } 32 | Arturo::Feature.blacklist(feature.symbol) { |thing| true } 33 | expect(feature.enabled_for?(:a_thing)).to be(false) 34 | end 35 | 36 | it 'allow a whitelist or blacklist before the feature is created' do 37 | Arturo::Feature.whitelist(:does_not_exist) { |thing| thing == 'whitelisted' } 38 | Arturo::Feature.blacklist(:does_not_exist) { |thing| thing == 'blacklisted' } 39 | feature = create(:feature, symbol: :does_not_exist) 40 | expect(feature.enabled_for?('whitelisted')).to be(true) 41 | expect(feature.enabled_for?('blacklisted')).to be(false) 42 | end 43 | 44 | it 'works with global whitelisting' do 45 | feature.deployment_percentage = 0 46 | other_feature = create(:feature, deployment_percentage: 0) 47 | Arturo::Feature.whitelist { |feature, recipient| feature == other_feature } 48 | expect(feature.enabled_for?(:a_thing)).to be(false) 49 | expect(other_feature.enabled_for?(:a_thing)).to be(true) 50 | end 51 | 52 | it 'works with global blacklisting' do 53 | feature.deployment_percentage = 100 54 | other_feature = create(:feature, deployment_percentage: 100) 55 | Arturo::Feature.blacklist { |feature, recipient| feature == other_feature } 56 | expect(feature.enabled_for?(:a_thing)).to be(true) 57 | expect(other_feature.enabled_for?(:a_thing)).to be(false) 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /lib/arturo/special_handling.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'active_support' 4 | 5 | module Arturo 6 | 7 | # Adds whitelist and blacklist support to individual features by name 8 | # or for all features. Blacklists override whitelists. (In the world of 9 | # Apache, Features are "(deny,allow)".) 10 | # @example 11 | # # allow admins for some_feature: 12 | # Arturo::Feature.whitelist(:some_feature) do |user| 13 | # user.is_admin? 14 | # end 15 | # 16 | # # disallow for small accounts for another_feature: 17 | # Arturo::Feature.blacklist(:another_feature) do |user| 18 | # user.account.small? 19 | # end 20 | # 21 | # # allow large accounts access to large features: 22 | # Arturo::Feature.whitelist do |feature, user| 23 | # feature.symbol.to_s =~ /^large/ && user.account.large? 24 | # end 25 | # 26 | # Blacklists and whitelists can be defined before the feature exists 27 | # and are not persisted, so they are best defined in initializers. 28 | # This is particularly important if your application runs in several 29 | # different processes or on several servers. 30 | module SpecialHandling 31 | extend ActiveSupport::Concern 32 | 33 | class_methods do 34 | def whitelists 35 | @whitelists ||= [] 36 | end 37 | 38 | def blacklists 39 | @blacklists ||= [] 40 | end 41 | 42 | def whitelist(feature_symbol = nil, &block) 43 | whitelists << two_arg_block(feature_symbol, block) 44 | end 45 | 46 | def blacklist(feature_symbol = nil, &block) 47 | blacklists << two_arg_block(feature_symbol, block) 48 | end 49 | 50 | private 51 | 52 | def two_arg_block(symbol, block) 53 | return block if symbol.nil? 54 | lambda do |feature, recipient| 55 | feature.symbol.to_s == symbol.to_s && block.call(recipient) 56 | end 57 | end 58 | 59 | end 60 | 61 | protected 62 | 63 | def whitelisted?(feature_recipient) 64 | x_listed?(self.class.whitelists, feature_recipient) 65 | end 66 | 67 | def blacklisted?(feature_recipient) 68 | x_listed?(self.class.blacklists, feature_recipient) 69 | end 70 | 71 | def x_listed?(lists, feature_recipient) 72 | lists.any? { |block| block.call(self, feature_recipient) } 73 | end 74 | 75 | end 76 | 77 | end 78 | -------------------------------------------------------------------------------- /spec/controllers/features_controller_admin_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | require 'arturo/features_controller' 4 | 5 | describe Arturo::FeaturesController, type: :request do 6 | let!(:current_user) { double('Admin', admin?: true) } 7 | let!(:features) { 8 | [ 9 | create(:feature), 10 | create(:feature), 11 | create(:feature) 12 | ] 13 | } 14 | 15 | let(:document_root_element) { Nokogiri::HTML::Document.parse(response.body) } 16 | 17 | before do 18 | allow_any_instance_of(Arturo::FeaturesController) 19 | .to receive(:current_user) 20 | .and_return(current_user) 21 | end 22 | 23 | it 'responds to a get on index' do 24 | get '/arturo/features' 25 | expect(response).to be_successful 26 | 27 | assert_select('table tbody tr input[type=range]') 28 | assert_select("table tfoot a[href='/arturo/features/new']") 29 | assert_select('table tfoot input[type=submit]') 30 | end 31 | 32 | it 'responds to a put on update_all' do 33 | params = { 34 | features: { 35 | features.first.id => { deployment_percentage: '14' }, 36 | features.last.id => { deployment_percentage: '98' } 37 | } 38 | } 39 | 40 | put '/arturo/features', params: params 41 | 42 | expect(features.first.reload.deployment_percentage.to_s).to eq('14') 43 | expect(features.last.reload.deployment_percentage.to_s).to eq('98') 44 | expect(response).to redirect_to('/arturo/features') 45 | end 46 | 47 | it 'responds to a get on new' do 48 | get '/arturo/features/new' 49 | expect(response).to be_successful 50 | end 51 | 52 | it 'responds to a post on create' do 53 | post '/arturo/features', params: { feature: { symbol: 'anything' } } 54 | 55 | expect(Arturo::Feature.find_by_symbol('anything')).to be_present 56 | expect(response).to redirect_to('/arturo/features') 57 | end 58 | 59 | def test_get_show 60 | get "/arturo/features/#{@features.first.id}" 61 | expect(response).to be_success 62 | end 63 | 64 | def test_get_edit 65 | get "/arturo/features/#{@features.first.id}/edit" 66 | expect(response).to be_success 67 | end 68 | 69 | def test_put_update 70 | put "/arturo/features/#{@features.first.id}", params: { feature: { deployment_percentage: '2' } } 71 | 72 | expect(response).to redirect_to("/arturo/features/#{@features.first.to_param}") 73 | end 74 | 75 | def test_put_invalid_update 76 | put '/arturo/features/#{@features.first.id}', params: { feature: { deployment_percentage: '-10' } } 77 | 78 | expect(response).to be_success 79 | expect(controller.flash[:alert]) 80 | .to eq("Could not update #{@features.first.name}: Deployment Percentage must be greater than or equal to 0.") 81 | end 82 | 83 | def test_delete_destroy 84 | delete "/arturo/features/#{@features.first.id}" 85 | expect(response).to redirect_to('/arturo/features') 86 | end 87 | 88 | end 89 | -------------------------------------------------------------------------------- /app/controllers/arturo/features_controller.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'action_controller' 3 | require 'arturo/feature_params_support' 4 | 5 | # TODO: this doesn't do anything radically out of the ordinary. 6 | # Are there Rails 3 patterns/mixins/methods I can use 7 | # to clean it up a bit? 8 | module Arturo 9 | 10 | # Handles all Feature actions. Clients of the Arturo engine 11 | # should redefine Arturo::FeaturesController#may_manage_features? to 12 | # return true only for users who are permitted to manage features. 13 | class FeaturesController < ApplicationController 14 | include Arturo::FeatureManagement 15 | include Arturo::FeatureParamsSupport 16 | 17 | respond_to :html, :json, :xml 18 | 19 | before_action :require_permission 20 | before_action :load_feature, :only => [ :show, :edit, :update, :destroy ] 21 | 22 | def index 23 | @features = Arturo::Feature.all 24 | respond_with @features 25 | end 26 | 27 | def update_all 28 | updated_count = 0 29 | errors = [] 30 | features_params.each do |id, attributes| 31 | feature = Arturo::Feature.find_by_id(id) 32 | if feature.blank? 33 | errors << t('arturo.features.flash.no_such_feature', :id => id) 34 | elsif feature.update(attributes) 35 | updated_count += 1 36 | else 37 | errors << t('arturo.features.flash.error_updating', :id => id, :errors => feature.errors.full_messages.to_sentence) 38 | end 39 | end 40 | if errors.any? 41 | flash[:error] = errors 42 | else 43 | flash[:success] = t('arturo.features.flash.updated_many', :count => updated_count) 44 | end 45 | redirect_to arturo_engine.features_path 46 | end 47 | 48 | def show 49 | respond_with @feature 50 | end 51 | 52 | def new 53 | @feature = Arturo::Feature.new(feature_params) 54 | respond_with @feature 55 | end 56 | 57 | def create 58 | @feature = Arturo::Feature.new(feature_params) 59 | if @feature.save 60 | flash[:notice] = t('arturo.features.flash.created', :name => @feature.to_s) 61 | redirect_to arturo_engine.features_path 62 | else 63 | flash[:alert] = t('arturo.features.flash.error_creating', :name => @feature.to_s) 64 | render :action => 'new' 65 | end 66 | end 67 | 68 | def edit 69 | respond_with @feature 70 | end 71 | 72 | def update 73 | if @feature.update(feature_params) 74 | flash[:notice] = t('arturo.features.flash.updated', :name => @feature.to_s) 75 | redirect_to arturo_engine.feature_path(@feature) 76 | else 77 | flash[:alert] = t('arturo.features.flash.error_updating', :name => @feature.name, :errors => @feature.errors.full_messages.join("\n")) 78 | render :action => 'edit' 79 | end 80 | end 81 | 82 | def destroy 83 | if @feature.destroy 84 | flash[:notice] = t('arturo.features.flash.removed', :name => @feature.to_s) 85 | else 86 | flash[:alert] = t('arturo.features.flash.error_removing', :name => @feature.to_s) 87 | end 88 | redirect_to arturo_engine.features_path 89 | end 90 | 91 | protected 92 | 93 | def require_permission 94 | unless may_manage_features? 95 | render :action => 'forbidden', :status => 403 96 | return false 97 | end 98 | end 99 | 100 | def load_feature 101 | @feature ||= Arturo::Feature.find(params[:id]) 102 | end 103 | 104 | end 105 | 106 | end 107 | -------------------------------------------------------------------------------- /lib/arturo/feature_methods.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'active_record' 4 | require 'active_support' 5 | require 'active_support/core_ext/hash/indifferent_access' 6 | 7 | module Arturo 8 | module FeatureMethods 9 | extend ActiveSupport::Concern 10 | include Arturo::SpecialHandling 11 | 12 | SYMBOL_REGEX = /^[a-zA-z][a-zA-Z0-9_]*$/ 13 | DEFAULT_ATTRIBUTES = { :deployment_percentage => 0 }.with_indifferent_access 14 | 15 | included do 16 | attr_readonly :symbol 17 | 18 | validates_presence_of :symbol, :deployment_percentage 19 | validates_uniqueness_of :symbol, :allow_blank => true, :case_sensitive => false 20 | validates_numericality_of :deployment_percentage, 21 | :only_integer => true, 22 | :allow_blank => true, 23 | :greater_than_or_equal_to => 0, 24 | :less_than_or_equal_to => 100 25 | end 26 | 27 | class_methods do 28 | # Looks up a feature by symbol. Also accepts a Feature as input. 29 | # @param [Symbol, Arturo::Feature] feature_or_symbol a Feature or the Symbol of a Feature 30 | # @return [Arturo::Feature, Arturo::NoSuchFeature] the Feature if found, else Arturo::NoSuchFeature 31 | def to_feature(feature_or_symbol) 32 | return feature_or_symbol if feature_or_symbol.kind_of?(self) 33 | 34 | symbol = feature_or_symbol.to_sym.to_s 35 | self.where(:symbol => symbol).first || Arturo::NoSuchFeature.new(symbol) 36 | end 37 | 38 | # Looks up a feature by symbol. Also accepts a Feature as input. 39 | # @param [Symbol, Arturo::Feature] feature_or_symbol a Feature or the Symbol of a Feature 40 | # @return [Arturo::Feature, nil] the Feature if found, else nil 41 | def find_feature(feature_or_symbol) 42 | feature = to_feature(feature_or_symbol) 43 | feature.is_a?(Arturo::NoSuchFeature) ? nil : feature 44 | end 45 | 46 | def last_updated_at 47 | maximum(:updated_at) 48 | end 49 | end 50 | 51 | # Create a new Feature 52 | def initialize(*args, &block) 53 | args[0] = DEFAULT_ATTRIBUTES.merge(args[0].try(:to_h) || {}) 54 | super(*args, &block) 55 | end 56 | 57 | # @param [Object] feature_recipient a User, Account, 58 | # or other model with an #id method 59 | # @return [true,false] whether or not this feature is enabled 60 | # for feature_recipient 61 | # @see Arturo::SpecialHandling#whitelisted? 62 | # @see Arturo::SpecialHandling#blacklisted? 63 | def enabled_for?(feature_recipient) 64 | return false if feature_recipient.nil? 65 | return false if blacklisted?(feature_recipient) 66 | return true if whitelisted?(feature_recipient) 67 | passes_threshold?(feature_recipient, deployment_percentage || 0) 68 | end 69 | 70 | def name 71 | return I18n.translate("arturo.feature.nameless") if symbol.blank? 72 | 73 | I18n.translate("arturo.feature.#{symbol}", :default => symbol.to_s.titleize) 74 | end 75 | 76 | def to_s 77 | "Feature #{name}" 78 | end 79 | 80 | def to_param 81 | persisted? ? "#{id}-#{symbol.to_s.parameterize}" : nil 82 | end 83 | 84 | def inspect 85 | "" 86 | end 87 | 88 | # made public so as to allow for thresholds stored outside of the model 89 | def passes_threshold?(feature_recipient, threshold) 90 | return true if threshold == 100 91 | return false if threshold == 0 || !feature_recipient.id 92 | (((feature_recipient.id + (self.id || 1) + 17) * 13) % 100) < threshold 93 | end 94 | end 95 | end 96 | -------------------------------------------------------------------------------- /spec/models/feature_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | 4 | describe Arturo::Feature do 5 | 6 | before do 7 | reset_translations! 8 | end 9 | 10 | let(:feature) { create(:feature) } 11 | let(:bunch_of_things) do 12 | (1..2000).to_a.map do |i| 13 | double('Thing', id: i) 14 | end 15 | end 16 | 17 | it 'responds to to_feature' do 18 | expect(::Arturo::Feature.to_feature(feature)).to eq(feature) 19 | expect(::Arturo::Feature.to_feature(feature.symbol)).to eq(feature) 20 | expect(::Arturo::Feature.to_feature(:does_not_exist)).to be_a(Arturo::NoSuchFeature) 21 | end 22 | 23 | it 'responds to find_feature' do 24 | expect(::Arturo::Feature.find_feature(feature)).to eq(feature) 25 | expect(::Arturo::Feature.find_feature(feature.symbol)).to eq(feature) 26 | expect(::Arturo::Feature.find_feature(:does_not_exist)).to be_nil 27 | end 28 | 29 | it 'finds existent features with feature_enabled_for' do 30 | feature.update(:deployment_percentage => 100) 31 | recipient = double('User', to_s: 'Paula', id: 12) 32 | expect(::Arturo.feature_enabled_for?(feature.symbol, recipient)).to be(true), "#{feature} should be enabled for #{recipient}" 33 | end 34 | 35 | it 'does not finds non existent features with feature_enabled_for' do 36 | expect(::Arturo.feature_enabled_for?(:does_not_exist, 'Paula')).to be(false) 37 | end 38 | 39 | it 'does not find feature for nil recipients' do 40 | expect(::Arturo.feature_enabled_for?(feature.symbol, nil)).to be(false) 41 | expect(::Arturo.feature_enabled_for?(:does_not_exist, nil)).to be(false) 42 | end 43 | 44 | it 'requires a symbol' do 45 | feature.symbol = nil 46 | expect(feature).to_not be_valid 47 | expect(feature.errors[:symbol]).to be_present 48 | end 49 | 50 | it 'responds to last_updated_at' do 51 | Arturo::Feature.delete_all 52 | 53 | Timecop.freeze(Time.local(2008, 9, 1, 12, 0, 0)) { create(:feature) } 54 | updated_at = Time.local(2011, 9, 1, 12, 0, 0) 55 | 56 | Timecop.freeze(updated_at) { create(:feature) } 57 | expect(Arturo::Feature.last_updated_at).to eq(updated_at) 58 | end 59 | 60 | it 'responds to last_updated_at with no features' do 61 | Arturo::Feature.delete_all 62 | expect(Arturo::Feature.last_updated_at).to be_nil 63 | end 64 | 65 | # regression 66 | # @see https://github.com/zendesk/arturo/issues/7 67 | it 'does not overwrite deployment_percentage on create' do 68 | new_feature = ::Arturo::Feature.create('symbol' => :foo, 'deployment_percentage' => 37) 69 | expect(new_feature.deployment_percentage.to_s).to eq('37') 70 | end 71 | 72 | it 'requires a deployment percentage' do 73 | feature.deployment_percentage = nil 74 | expect(feature).to_not be_valid 75 | expect(feature.errors[:deployment_percentage]).to be_present 76 | end 77 | 78 | it 'has a readonly symbol' do 79 | original_symbol = feature.symbol 80 | feature.symbol = :foo_bar 81 | feature.save 82 | expect(feature.reload.symbol.to_sym).to eq(original_symbol.to_sym) 83 | end 84 | 85 | it 'has a sane default for name' do 86 | feature.symbol = :foo_bar 87 | expect(feature.name).to eq('Foo Bar') 88 | end 89 | 90 | it 'uses names with internationalization when available' do 91 | define_translation("arturo.feature.#{feature.symbol}", 'Happy Feature') 92 | expect(feature.name).to eq('Happy Feature') 93 | end 94 | 95 | it 'sets deployment percentagle to 0 by default' do 96 | expect(::Arturo::Feature.new.deployment_percentage).to eq(0) 97 | end 98 | 99 | describe 'enabled_for?' do 100 | it 'returns false if thing is nil' do 101 | feature.deployment_percentage = 100 102 | expect(feature.enabled_for?(nil)).to be(false) 103 | end 104 | 105 | it 'returns false for all things when deployment percentage is nil' do 106 | feature.deployment_percentage = 0 107 | bunch_of_things.each do |t| 108 | expect(feature.enabled_for?(t)).to be(false) 109 | end 110 | end 111 | 112 | it 'returns true for all non nil things when deployment percentage is 100' do 113 | feature.deployment_percentage = 100 114 | bunch_of_things.each do |t| 115 | expect(feature.enabled_for?(t)).to be(true) 116 | end 117 | end 118 | 119 | it 'returns true for certain accounts when deployment percentage is 50' do 120 | feature.deployment_percentage = 50 121 | { 58 => false, 61 => true, 112 => false, 116 => true }.each do |id, expected| 122 | expect(feature.enabled_for?(double('Thing', id: id))).to be(expected) 123 | end 124 | end 125 | 126 | it 'returns true for about deployment percentage percent of things' do 127 | feature.deployment_percentage = 37 128 | yes = 0 129 | bunch_of_things.each { |t| yes += 1 if feature.enabled_for?(t) } 130 | expect(yes).to be_within(0.02 * bunch_of_things.length).of(0.37 * bunch_of_things.length) 131 | end 132 | 133 | it 'returns false for things with nil id and not 100' do 134 | feature.deployment_percentage = 99 135 | expect(feature.enabled_for?(double('ThingWithNilId', id: nil))).to be(false) 136 | end 137 | 138 | it 'returns false for things with nil id and 100' do 139 | feature.deployment_percentage = 100 140 | expect(feature.enabled_for?(double('ThingWithNilId', id: nil))).to be(true) 141 | end 142 | 143 | it 'is not identical across features' do 144 | foo = create(:feature, symbol: :foo, deployment_percentage: 55) 145 | bar = create(:feature, symbol: :bar, deployment_percentage: 55) 146 | has_foo = bunch_of_things.map { |t| foo.enabled_for?(t) } 147 | has_bar = bunch_of_things.map { |t| bar.enabled_for?(t) } 148 | expect(has_bar).to_not eq(has_foo) 149 | end 150 | end 151 | 152 | it 'responds do to_s' do 153 | expect(feature.to_s).to include(feature.name) 154 | end 155 | 156 | it 'responds to to_param' do 157 | expect(feature.to_param).to match(%r{^#{feature.id}-}) 158 | end 159 | end 160 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## Unreleased 2 | 3 | Drops support for Ruby 3.0 and 3.1. 4 | 5 | Drops support for Rails 6.0 and 6.1. 6 | 7 | Adds tests against Ruby 3.4. 8 | 9 | Adds tests against Rails 7.2 and 8.0. 10 | 11 | ## v4.1.1 12 | 13 | Fixes missing indifferent_access import 14 | 15 | ## v4.1.0 16 | 17 | Removes upper boundary on ActiveRecord. 18 | 19 | Drops support for Ruby < 3.0. 20 | 21 | Drops support for Rails < 6.0. 22 | 23 | ## v4.0.1 24 | 25 | Fixes loading issues for apps not using the Rails engine. 26 | 27 | ## v4.0.0 28 | 29 | Stops loading the Rails engine automatically. If you are using the engine, you need to require it explicitly by adding `require 'arturo/engine'` to `application.rb`. 30 | 31 | Adds support for Ruby 3.3. 32 | 33 | Returns false immediately for `feature_enabled_for?` calls with `nil` recipients. 34 | 35 | ## v3.0.0 36 | 37 | Converts the Feature model into a mixin that should be used by services via a model generator. 38 | 39 | Brings back the `warm_cache!` method. 40 | 41 | Adds support for Rails 7.1. 42 | 43 | ## v2.8.0 44 | 45 | Drop support for Ruby 2.6 46 | 47 | Drop Support for Rails 5.0 & 5.1 48 | 49 | Add support for Ruby 3.2 50 | 51 | ## v2.7.0 52 | 53 | Adds ability to register cache update listeners with Arturo::FeatureCaching::AllStrategy that are called when the cache is updated 54 | 55 | ## v2.6.0 56 | 57 | Add support for Rails 7.0 58 | 59 | Add support for Ruby 3.0 & 3.1 60 | 61 | Drop support for Rails 4.2 62 | 63 | Drop support for Ruby 2.4 & 2.5 64 | 65 | ## v2.5.4 66 | 67 | Bug fix: Explicitly require rails engine to avoid errors that ::Rails::Engine cannot be found. 68 | 69 | ## v2.5.3 70 | 71 | Bug fix: Allow using Arturo with ActiveRecord, but without all of Rails. 72 | 73 | ## v2.5.2 74 | 75 | Drop support for Rails 3.2. 76 | 77 | Add support for Rails 6.1. 78 | 79 | Switch CI from Travis to GitHub Actions. 80 | 81 | ## v2.2.0 82 | 83 | Bug fix: making a feature-update request that fails strict params checks now returns a sensible error instead of throwing an exception 84 | 85 | Improvement: better failed-to-update error messages 86 | 87 | Support Matrix changes: add Rails 5.0, drop Rails 3.2, add Ruby 2.1.7, add Ruby 2.2.3, drop Ruby 1.9.3 88 | 89 | ## v2.1.0 90 | 91 | Bug fix: `Arturo::SpecialHandling` always compares symbols as strings 92 | 93 | Improvmement: Rails 4.2 compatibility 94 | 95 | Improvement: relax minitest version constraints 96 | 97 | Improvement: add `set_feature!` method to complement `enable_feature!`and `disable_feature!` 98 | 99 | ## v2.0.0 100 | 101 | Bug fix: add missing require to initializer. 102 | 103 | Improvement: Remove support for `[feature]_enabled_for?` methods. 104 | 105 | Improvement: Use more specific gem versions for development dependencies. 106 | 107 | ## v1.11.0 108 | 109 | Depreaction: `[feature]_enabled_for?` methods 110 | 111 | Bug fix: `Arturo.respond_to?` takes an optional second argument, per 112 | `Object.respond_to?`'s signature. 113 | 114 | Improvement: support Rails 4.1. 115 | 116 | Improvement: use Travis's multiple builds instead of Appraisal. 117 | 118 | ## v1.10.0 119 | 120 | Improvement: Arturo no longer declares a hard runtime dependency on Rails, but 121 | instead only on ActiveRecord. This makes it possible to use `Arturo::Feature` 122 | in non-Rails settings. Feature *management* is still expressed as a Rails engine 123 | and requires `actionpack` and other parts of Rails. 124 | 125 | ## v1.9.0 126 | 127 | Improvement: `Arturo::Feature` is defined in `lib/arturo/feature.rb` instead of 128 | `app/models/arturo/feature.rb`, which means consuming applications can load it 129 | without loading the whole engine. 130 | 131 | Improvement: `Arturo::Engine` no longer eagerly loads all engine files; instead, 132 | it uses Rails's `autoload_paths` to ensure classes are loaded as necessary. 133 | 134 | Bug fix: the route to `arturo/features_controller#update_all` is now called 135 | `features_update_all`; it had been called simply `features`, which caused 136 | conflict problems in Rails 4.0. 137 | 138 | ## v1.8.0 139 | 140 | Improvement: "All" caching strategy is now smarter about its use of the 141 | `update_at` attribute. It handles the case when a Feature's `updated_at` is 142 | `nil` and queries the database less often to figure out whether any features 143 | have changed. 144 | 145 | Bug fix: the engine's `source_root` relied on its `root_path`, which is not 146 | available on all versions of Rails. 147 | 148 | ## v1.7.0 149 | 150 | `Arturo::FeaturesHelper#error_messages_for` has been removed. This only affects 151 | people who have written their own feature-management pages that use this helper. 152 | 153 | ## v1.6.1 154 | 155 | `Arturo::FeaturesHelper#error_messages_for` has been deprecated in favor of 156 | `error_messages_for_feature` because it conflicts with a Rails and DynamicForm 157 | method. It will be removed in v1.7.0. This only affects people who have written 158 | their own feature-management pages that use this helper. 159 | 160 | ## v1.6.0 161 | 162 | Formerly, whitelists and blacklists had to be *feature-specific*: 163 | 164 | Arturo::Feature.whitelist(:foo) do |recipient| 165 | recipient.plan.has_foo? 166 | end 167 | 168 | Now whitelists and blacklists can be global. The block takes the feature 169 | as the first argument: 170 | 171 | Arturo::Feature.whitelist do |feature, recipient| 172 | recipient.plan.has?(feature.to_sym) 173 | end 174 | 175 | ## v1.5.3 176 | 177 | Set `signing_key` in gemspec only if the file exists. 178 | 179 | The `FeaturesController` docs erroneously said to override `#permitted?`. 180 | The correct method name is `may_manage_features?`. 181 | 182 | ## v1.5.2 183 | 184 | The gem is now signed. The public key is 185 | [gem-public_cert.pem](./gem-public_cert.pem). 186 | 187 | ## v1.5.1 188 | 189 | Use just ActiveRecord, not all of Rails, when defining different behavior 190 | for different versions. 191 | 192 | Unify interface of `Feature` and `NoSuchFeature` so the latter fulfills the 193 | null-object pattern. 194 | 195 | ## v1.5.0 196 | 197 | This project is now licensed under the 198 | [APLv2](https://www.apache.org/licenses/LICENSE-2.0.html). 199 | 200 | Arturo now works on Rails 3.0 and Rails 4.0. Helpers are no longer mixed into 201 | the global view, but are under the `arturo_engine` namespace, as is the 202 | convention in Rails 3.1+. 203 | 204 | The feature cache will return `NoSuchFeature` for cache misses instead of `nil`, 205 | which clients can treat like a `Feature` that is always off. 206 | 207 | Better error messages when managing features, and the addition of the 208 | `arturo_flash_messages` helper method. 209 | 210 | Add `Feature.last_updated_at` to get the most recent `updated_at` among all 211 | `Feature`s. 212 | 213 | ## v1.3.0 214 | 215 | Add `Arturo::Middleware`, which passes requests down the stack if an only if 216 | a particular feature is available. 217 | 218 | `TestSupport` methods use `Feature.to_feature`. 219 | 220 | ## v 1.1.0 - cleanup 221 | 222 | * changed `require_feature!` to `require_feature` 223 | * replaced `Arturo.permit_management` and `Arturo.feature_recipient` 224 | blocks with instance methods 225 | `Arturo::FeatureManagement.may_manage_features?` and 226 | `Arturo::FeatureAvailability.feature_recipient` 227 | 228 | ## v 1.0.0 - Initial Release 229 | 230 | * `require_feature!` controller filter 231 | * `if_feature_enabled` controller and view method 232 | * `feature_enabled?` controller and view method 233 | * CRUD for features 234 | * `Arturo.permit_management` to configure management permission 235 | * `Arturo.feature_recipient` to configure on what basis features are deployed 236 | * whitelists and blacklists 237 | -------------------------------------------------------------------------------- /lib/arturo/feature_caching.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'arturo/no_such_feature' 3 | 4 | module Arturo 5 | 6 | # To be extended by Arturo::Feature if you want to enable 7 | # in-memory caching. 8 | # NB: Arturo's feature caching only works when using 9 | # Arturo::Feature.to_feature or when using the helper methods 10 | # in Arturo and Arturo::FeatureAvailability. 11 | # NB: if you have multiple application servers, you almost certainly 12 | # want to clear this cache after each request: 13 | # 14 | # class ApplicationController < ActionController::Base 15 | # after_filter { Arturo::Feature.clear_feature_cache } 16 | # end 17 | # 18 | # Alternatively, you could redefine Arturo::Feature.feature_cache 19 | # to use a shared cache like Memcached. 20 | module FeatureCaching 21 | 22 | module PrependMethods 23 | # Wraps Arturo::Feature.to_feature with in-memory caching. 24 | def to_feature(feature_or_symbol) 25 | if !caches_features? 26 | super 27 | elsif feature_or_symbol.kind_of?(Arturo::Feature) 28 | feature_or_symbol 29 | else 30 | symbol = feature_or_symbol.to_sym 31 | feature_caching_strategy.fetch(feature_cache, symbol) { super(symbol) } 32 | end 33 | end 34 | end 35 | 36 | def self.extended(base) 37 | class << base 38 | prepend PrependMethods 39 | attr_accessor :cache_ttl, :feature_cache, :feature_caching_strategy 40 | attr_writer :extend_cache_on_failure 41 | end 42 | base.send(:after_save) do |f| 43 | f.class.feature_caching_strategy.expire(f.class.feature_cache, f.symbol.to_sym) if f.class.caches_features? 44 | end 45 | base.cache_ttl = 0 46 | base.extend_cache_on_failure = false 47 | base.feature_cache = Arturo::FeatureCaching::Cache.new 48 | base.feature_caching_strategy = AllStrategy 49 | end 50 | 51 | def extend_cache_on_failure? 52 | !!@extend_cache_on_failure 53 | end 54 | 55 | def caches_features? 56 | self.cache_ttl.to_i > 0 57 | end 58 | 59 | def warm_cache! 60 | to_feature(:fake_feature_to_force_cache_warming) 61 | end 62 | 63 | class AllStrategy 64 | class << self 65 | ## 66 | # @param cache [Arturo::Cache] cache backend 67 | # @param symbol [Symbol] arturo identifier 68 | # @return [Arturo::Feature, Arturo::NoSuchFeature] 69 | # 70 | def fetch(cache, symbol, &block) 71 | existing_features = cache.read("arturo.all") 72 | 73 | features = if cache_is_current?(cache, existing_features) 74 | existing_features 75 | else 76 | arturos_from_origin(fallback: existing_features).tap do |updated_features| 77 | update_and_extend_cache!(cache, updated_features) 78 | end 79 | end 80 | 81 | features[symbol] || Arturo::NoSuchFeature.new(symbol) 82 | end 83 | 84 | def expire(cache, symbol) 85 | cache.delete("arturo.all") 86 | end 87 | 88 | def register_cache_update_listener(&block) 89 | cache_update_listeners << block 90 | end 91 | 92 | private 93 | 94 | def cache_update_listeners 95 | @cache_update_listeners ||= [] 96 | end 97 | 98 | ## 99 | # @param fallback [Hash] features to use on database failure 100 | # @return [Hash] updated features from origin or fallback 101 | # @raise [ActiveRecord::ActiveRecordError] on database failure 102 | # without cache extension option 103 | # 104 | def arturos_from_origin(fallback:) 105 | Arturo::Feature.all.to_h { |f| [f.symbol.to_sym, f] } 106 | rescue ActiveRecord::ActiveRecordError 107 | raise unless Arturo::Feature.extend_cache_on_failure? 108 | 109 | if fallback.blank? 110 | log_empty_cache 111 | raise 112 | else 113 | log_stale_cache 114 | fallback 115 | end 116 | end 117 | 118 | ## 119 | # @return [Boolean] whether the current cache has to be updated from origin 120 | # @raise [ActiveRecord::ActiveRecordError] on database failure 121 | # without cache extension option 122 | # 123 | def cache_is_current?(cache, features) 124 | return unless features 125 | return true if cache.read("arturo.current") 126 | 127 | begin 128 | return false if origin_changed?(features) 129 | rescue ActiveRecord::ActiveRecordError 130 | raise unless Arturo::Feature.extend_cache_on_failure? 131 | 132 | if features.blank? 133 | log_empty_cache 134 | raise 135 | else 136 | log_stale_cache 137 | update_and_extend_cache!(cache, features) 138 | end 139 | 140 | return true 141 | end 142 | mark_as_current!(cache) 143 | end 144 | 145 | def formatted_log(namespace, msg) 146 | "[Arturo][#{namespace}] #{msg}" 147 | end 148 | 149 | def log_empty_cache 150 | Arturo.logger.error(formatted_log('extend_cache_on_failure', 'Fallback cache is empty')) 151 | end 152 | 153 | def log_stale_cache 154 | Arturo.logger.warn(formatted_log('extend_cache_on_failure', 'Falling back to stale cache')) 155 | end 156 | 157 | ## 158 | # @return [True] 159 | # 160 | def mark_as_current!(cache) 161 | cache.write("arturo.current", true, expires_in: Arturo::Feature.cache_ttl) 162 | end 163 | 164 | ## 165 | # The Arturo origin might return a big payload, so checking for the latest 166 | # update is a cheaper operation. 167 | # 168 | # @return [Boolean] if origin has been updated since the last cache update. 169 | # 170 | def origin_changed?(features) 171 | features.values.map(&:updated_at).compact.max != Arturo::Feature.maximum(:updated_at) 172 | end 173 | 174 | def update_and_extend_cache!(cache, features) 175 | mark_as_current!(cache) 176 | cache.write("arturo.all", features, expires_in: Arturo::Feature.cache_ttl * 10) 177 | cache_update_listeners.each(&:call) 178 | end 179 | end 180 | end 181 | 182 | class OneStrategy 183 | def self.fetch(cache, symbol, &block) 184 | if feature = cache.read("arturo.#{symbol}") 185 | feature 186 | else 187 | cache.write("arturo.#{symbol}", yield || Arturo::NoSuchFeature.new(symbol), expires_in: Arturo::Feature.cache_ttl) 188 | end 189 | end 190 | 191 | def self.expire(cache, symbol) 192 | cache.delete("arturo.#{symbol}") 193 | end 194 | end 195 | 196 | # Quack like a Rails cache. 197 | class Cache 198 | def initialize 199 | @data = {} # of the form {key => [value, expires_at or nil]} 200 | end 201 | 202 | def read(name, options = nil) 203 | value, expires_at = *@data[name] 204 | if value && (expires_at.blank? || expires_at > Time.now) 205 | value 206 | else 207 | nil 208 | end 209 | end 210 | 211 | def delete(name) 212 | @data.delete(name) 213 | end 214 | 215 | def write(name, value, options = nil) 216 | expires_at = if options && options.respond_to?(:[]) && options[:expires_in] 217 | Time.now + options.delete(:expires_in) 218 | else 219 | nil 220 | end 221 | value.freeze.tap do |val| 222 | @data[name] = [value, expires_at] 223 | end 224 | end 225 | 226 | def clear 227 | @data.clear 228 | end 229 | end 230 | 231 | end 232 | 233 | end 234 | -------------------------------------------------------------------------------- /spec/models/cache_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require 'spec_helper' 3 | require 'arturo/features_helper' 4 | 5 | describe Arturo::FeatureCaching do 6 | Arturo::Feature.extend(Arturo::FeatureCaching) 7 | 8 | class StupidCache 9 | def initialize(enabled=true) 10 | @enabled = enabled 11 | @data = {} 12 | end 13 | 14 | def read(key) 15 | @data[key] if @enabled 16 | end 17 | 18 | def delete(key) 19 | @data.delete(key) 20 | end 21 | 22 | def write(key, value, options={}) 23 | @data[key] = value 24 | end 25 | 26 | def clear 27 | @data.clear 28 | end 29 | end 30 | 31 | before do 32 | @feature = create(:feature) 33 | Arturo::Feature.cache_ttl = 30.minutes 34 | Arturo::Feature.feature_cache = Arturo::FeatureCaching::Cache.new 35 | end 36 | 37 | after do 38 | Arturo::Feature.cache_ttl = 0 # turn off for other tests 39 | Timecop.return 40 | end 41 | 42 | # Rails 4 calls all when calling maximum :/ 43 | def lock_down_maximum 44 | m = Arturo::Feature.maximum(:updated_at) 45 | allow(Arturo::Feature).to receive(:maximum).and_return(m) 46 | end 47 | 48 | [Arturo::FeatureCaching::OneStrategy, Arturo::FeatureCaching::AllStrategy].each do |strategy| 49 | describe strategy do 50 | let(:feature_method) { strategy == Arturo::FeatureCaching::OneStrategy ? :where : :all } 51 | 52 | before do 53 | Arturo::Feature.feature_caching_strategy = strategy 54 | end 55 | 56 | it 'hits db on first load' do 57 | expect(Arturo::Feature).to receive(feature_method).and_return([@feature]) 58 | 59 | Arturo::Feature.to_feature(@feature.symbol) 60 | end 61 | 62 | it 'caches missing features' do 63 | expect(Arturo::Feature).to receive(feature_method).and_return([]) 64 | 65 | expect(Arturo::Feature.to_feature(:ramen)).to be_kind_of(Arturo::NoSuchFeature) 66 | expect(Arturo::Feature.to_feature(:ramen)).to be_kind_of(Arturo::NoSuchFeature) 67 | expect(Arturo::Feature.to_feature(:ramen)).to be_kind_of(Arturo::NoSuchFeature) 68 | end 69 | 70 | it 'works with other cache backends' do 71 | Arturo::Feature.feature_cache = StupidCache.new 72 | expect(Arturo::Feature).to receive(feature_method).and_return([@feature]) 73 | 74 | Arturo::Feature.to_feature(@feature.symbol.to_sym) 75 | Arturo::Feature.to_feature(@feature.symbol) 76 | Arturo::Feature.to_feature(@feature.symbol.to_sym) 77 | Arturo::Feature.to_feature(@feature.symbol) 78 | end 79 | 80 | it 'works with inconsistent cache backend' do 81 | Arturo::Feature.feature_cache = StupidCache.new(false) 82 | expect(Arturo::Feature).to receive(feature_method).and_return([@feature]).twice 83 | 84 | Arturo::Feature.to_feature(@feature.symbol.to_sym) 85 | Arturo::Feature.to_feature(@feature.symbol.to_sym) 86 | end 87 | 88 | it 'can clear the cache' do 89 | Arturo::Feature.to_feature(@feature.symbol) 90 | Arturo::Feature.feature_cache.clear 91 | expect(Arturo::Feature).to receive(feature_method).and_return([@feature]) 92 | 93 | Arturo::Feature.to_feature(@feature.symbol) 94 | end 95 | 96 | it 'can turn off caching' do 97 | Arturo::Feature.cache_ttl = 0 98 | expect(Arturo::Feature).to receive(:where).and_return([@feature]).twice 99 | 100 | Arturo::Feature.to_feature(@feature.symbol) 101 | Arturo::Feature.to_feature(@feature.symbol) 102 | end 103 | 104 | it 'does not expire when inside cache ttl' do 105 | Arturo::Feature.to_feature(@feature.symbol) 106 | expect(Arturo::Feature).to_not receive(feature_method) 107 | 108 | Timecop.travel(Time.now + Arturo::Feature.cache_ttl - 5.seconds) 109 | Arturo::Feature.to_feature(@feature.symbol) 110 | end 111 | 112 | it 'expires when outside of cache ttl' do 113 | Arturo::Feature.to_feature(@feature.symbol) 114 | expect(Arturo::Feature).to receive(feature_method).and_return([@feature]) 115 | 116 | Timecop.travel(Time.now + Arturo::Feature.cache_ttl * 12) 117 | Arturo::Feature.to_feature(@feature.symbol) 118 | end 119 | 120 | it 'expires cache on enable or disable' do 121 | Arturo.enable_feature!(@feature.symbol) 122 | expect(Arturo::Feature.to_feature(@feature.symbol).deployment_percentage).to eq(100) 123 | 124 | Arturo.disable_feature!(@feature.symbol) 125 | expect(Arturo::Feature.to_feature(@feature.symbol).deployment_percentage).to eq(0) 126 | end 127 | end 128 | end 129 | 130 | describe Arturo::FeatureCaching::AllStrategy do 131 | before do 132 | Arturo::Feature.feature_caching_strategy = Arturo::FeatureCaching::AllStrategy 133 | end 134 | 135 | it 'caches all features in one cache' do 136 | expect(Arturo::Feature).to_not receive(:maximum) 137 | expect(Arturo::Feature).to receive(:all).and_return([]) 138 | 139 | expect(Arturo::Feature.to_feature(:ramen)) .to be_kind_of(Arturo::NoSuchFeature) 140 | expect(Arturo::Feature.to_feature(:amen)) .to be_kind_of(Arturo::NoSuchFeature) 141 | expect(Arturo::Feature.to_feature(:laymen)).to be_kind_of(Arturo::NoSuchFeature) 142 | end 143 | 144 | it 'does not expire when inside cache ttl' do 145 | Arturo::Feature.to_feature(@feature.symbol) 146 | expect(Arturo::Feature).to_not receive(:maximum) 147 | expect(Arturo::Feature).to_not receive(:all) 148 | 149 | Timecop.travel(Time.now + Arturo::Feature.cache_ttl - 5.seconds) 150 | Arturo::Feature.to_feature(@feature.symbol) 151 | end 152 | 153 | it 'expires when only feature-cache is empty' do 154 | Arturo::Feature.to_feature(@feature.symbol) 155 | expect(Arturo::Feature).to_not receive(:maximum) 156 | expect(Arturo::Feature).to receive(:all).and_return([]) 157 | 158 | Arturo::Feature.feature_cache.delete('arturo.all') 159 | Arturo::Feature.to_feature(@feature.symbol) 160 | end 161 | 162 | describe 'when outside of cache ttl and fresh' do 163 | before do 164 | Arturo::Feature.to_feature(@feature.symbol) 165 | lock_down_maximum 166 | expect(Arturo::Feature).to_not receive(:all) 167 | 168 | Timecop.travel(Time.now + Arturo::Feature.cache_ttl + 5.seconds) 169 | Arturo::Feature.to_feature(@feature.symbol) 170 | end 171 | 172 | skip('does not expire') 173 | 174 | it "does not ask for updated_at after finding out it's fresh" do 175 | expect(Arturo::Feature).to_not receive(:maximum) 176 | Arturo::Feature.to_feature(@feature.symbol) 177 | end 178 | end 179 | 180 | describe 'when outside of cache ttl and stale' do 181 | let(:listener) { proc {} } 182 | 183 | before do 184 | Arturo::Feature.to_feature(@feature.symbol) 185 | @feature.touch 186 | lock_down_maximum 187 | Arturo::Feature.feature_caching_strategy.register_cache_update_listener(&listener) 188 | Timecop.travel(Time.now + Arturo::Feature.cache_ttl + 5.seconds) 189 | end 190 | 191 | after do 192 | Timecop.return 193 | Arturo::Feature.feature_caching_strategy.send(:cache_update_listeners).clear 194 | end 195 | 196 | it 'expires' do 197 | expect(Arturo::Feature).to receive(:all).and_return([@feature]) 198 | Arturo::Feature.to_feature(@feature.symbol) 199 | end 200 | 201 | it 'triggers cache update listeners' do 202 | expect(listener).to receive(:call) 203 | Arturo::Feature.to_feature(@feature.symbol) 204 | end 205 | end 206 | 207 | it 'does not crash on nil updated_at' do 208 | @feature.class.where(id: @feature.id).update_all(updated_at: nil) 209 | create(:feature) 210 | expect { 211 | Arturo::Feature.to_feature(@feature.symbol) 212 | Timecop.travel(Time.now + Arturo::Feature.cache_ttl + 5.seconds) 213 | Arturo::Feature.to_feature(@feature.symbol) 214 | }.to_not raise_error 215 | end 216 | 217 | describe 'database errors' do 218 | before do 219 | Arturo::Feature.to_feature(@feature.symbol) 220 | @feature.touch 221 | Timecop.travel(Time.now + Arturo::Feature.cache_ttl + 5.seconds) 222 | 223 | if ActiveRecord.version < Gem::Version.new("7.2") 224 | allow(ActiveRecord::Base). 225 | to receive(:connection). 226 | and_raise(ActiveRecord::ActiveRecordError) 227 | else 228 | allow(Arturo::Feature). 229 | to receive(:with_connection). 230 | and_raise(ActiveRecord::ActiveRecordError) 231 | end 232 | end 233 | 234 | context 'with extend_cache_on_failure enabled' do 235 | before { Arturo::Feature.extend_cache_on_failure = true } 236 | 237 | context 'with error checking origin changes' do 238 | it 'does not raise error' do 239 | expect { Arturo::Feature.to_feature(@feature.symbol) }. 240 | not_to raise_error 241 | end 242 | 243 | it 'extends the cache' do 244 | expect(Arturo::Feature.feature_caching_strategy). 245 | to receive(:mark_as_current!) 246 | Arturo::Feature.to_feature(@feature.symbol) 247 | end 248 | 249 | it 'returns the cached result' do 250 | expect(Arturo::Feature.to_feature(@feature.symbol)).to eq(@feature) 251 | end 252 | 253 | context 'with a cold cache' do 254 | before do 255 | Arturo::Feature.feature_caching_strategy.expire(Arturo::Feature.feature_cache, 'all') 256 | end 257 | 258 | it 'raises error' do 259 | expect { Arturo::Feature.to_feature(@feature.symbol) }. 260 | to raise_error(ActiveRecord::ActiveRecordError) 261 | end 262 | end 263 | end 264 | 265 | context 'with error while refetching origin' do 266 | before do 267 | allow(Arturo::Feature).to receive(:origin_changed?).and_return(true) 268 | end 269 | 270 | it 'does not raise error' do 271 | expect { Arturo::Feature.to_feature(@feature.symbol) }. 272 | not_to raise_error 273 | end 274 | 275 | it 'extends the cache' do 276 | expect(Arturo::Feature.feature_caching_strategy). 277 | to receive(:mark_as_current!) 278 | Arturo::Feature.to_feature(@feature.symbol) 279 | end 280 | 281 | it 'returns the cached result' do 282 | expect(Arturo::Feature.to_feature(@feature.symbol)).to eq(@feature) 283 | end 284 | 285 | context 'with a cold cache' do 286 | before do 287 | Arturo::Feature.feature_caching_strategy.expire(Arturo::Feature.feature_cache, 'all') 288 | end 289 | 290 | it 'raises error' do 291 | expect { Arturo::Feature.to_feature(@feature.symbol) }. 292 | to raise_error(ActiveRecord::ActiveRecordError) 293 | end 294 | end 295 | end 296 | end 297 | 298 | context 'with extend_cache_on_failure disabled' do 299 | before { Arturo::Feature.extend_cache_on_failure = false } 300 | 301 | context 'with error checking origin changes' do 302 | it 'reraises the error' do 303 | expect { Arturo::Feature.to_feature(@feature.symbol) }. 304 | to raise_error(ActiveRecord::ActiveRecordError) 305 | end 306 | end 307 | 308 | context 'with error while refetching origin' do 309 | before do 310 | allow(Arturo::Feature).to receive(:origin_changed?).and_return(true) 311 | end 312 | 313 | it 'reraises the error' do 314 | expect { Arturo::Feature.to_feature(@feature.symbol) }. 315 | to raise_error(ActiveRecord::ActiveRecordError) 316 | end 317 | end 318 | end 319 | end 320 | end 321 | end 322 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What 2 | 3 | Arturo provides feature sliders for Rails. It lets you turn features on and off 4 | just like 5 | [feature flippers](https://code.flickr.net/2009/12/02/flipping-out/), 6 | but offers more fine-grained control. It supports deploying features only for 7 | a given percentage of your users and whitelisting and blacklisting users based 8 | on any criteria you can express in Ruby. 9 | 10 | The selection is deterministic. So if a user has a feature on Monday, the 11 | user will still have it on Tuesday (unless you *decrease* the feature's 12 | deployment percentage or change its white- or blacklist settings). 13 | 14 | ### A quick example 15 | 16 | Trish, a developer is working on a new feature: a live feed of recent postings 17 | in the user's city that shows up in the user's sidebar. First, she uses Arturo's 18 | view helpers to control who sees the sidebar widget: 19 | 20 | ```ERB 21 | <%# in app/views/layout/_sidebar.html.erb: %> 22 | <% if_feature_enabled(:live_postings) do %> 23 |
24 |

Recent Postings

25 |
    26 |
27 |
28 | <% end %> 29 | ``` 30 | 31 | Then Trish writes some Javascript that will poll the server for recent 32 | postings and put them in the sidebar widget: 33 | 34 | ```js 35 | // in public/javascript/live_postings.js: 36 | $(function() { 37 | var livePostingsList = $('#live_postings'); 38 | if (livePostingsList.length > 0) { 39 | var updatePostingsList = function() { 40 | livePostingsList.load('/listings/recent'); 41 | setTimeout(updatePostingsList, 30); 42 | } 43 | updatePostingsList(); 44 | } 45 | }); 46 | ``` 47 | 48 | Trish uses Arturo's Controller filters to control who has access to 49 | the feature: 50 | 51 | ```Ruby 52 | # in app/controllers/postings_controller: 53 | class PostingsController < ApplicationController 54 | require_feature :live_postings, only: :recent 55 | # ... 56 | end 57 | ``` 58 | 59 | Trish then deploys this code to production. Nobody will see the feature yet, 60 | since it's not on for anyone. (In fact, the feature doesn't yet exist 61 | in the database, which is the same as being deployed to 0% of users.) A week 62 | later, when the company is ready to start deploying the feature to a few 63 | people, the product manager, Vijay, signs in to their site and navigates 64 | to `/features`, adds a new feature called "live_postings" and sets its 65 | deployment percentage to 3%. After a few days, the operations team decides 66 | that the increase in traffic is not going to overwhelm their servers, and 67 | Vijay can bump the deployment percentage up to 50%. A few more days go by 68 | and they clean up the last few bugs they found with the "live_postings" 69 | feature and deploy it to all users. 70 | 71 | ## Installation 72 | 73 | ```Ruby 74 | gem 'arturo' 75 | ``` 76 | 77 | ## Configuration 78 | 79 | ### In Rails 80 | 81 | #### Run the generators: 82 | 83 | ``` 84 | rails g arturo:migration 85 | rails g arturo:initializer 86 | rails g arturo:routes 87 | rails g arturo:assets 88 | rails g arturo:feature_model 89 | ``` 90 | 91 | #### Run the migration: 92 | 93 | ``` 94 | rake db:migrate 95 | ``` 96 | 97 | #### Edit the generated migration as necessary 98 | 99 | #### Edit the configuration 100 | 101 | #### Edit the Feature model 102 | 103 | By default, the generated model `Arturo::Feature` inherits from `ActiveRecord::Base`. However, if you’re using multiple databases your models should inherit from an abstract class that specifies a database connection, not directly from `ActiveRecord::Base`. Update the generated model in `app/models/arturo/feature.rb` to make it use a correct database. 104 | 105 | ##### Initializer 106 | 107 | Open up the newly-generated `config/initializers/arturo_initializer.rb`. 108 | There are configuration options for the following: 109 | 110 | * logging capabilities (see [logging](#logging)) 111 | * the method that determines whether a user has permission to manage features 112 | (see [admin permissions](#adminpermissions)) 113 | * the method that returns the object that has features 114 | (e.g. User, Person, or Account; see 115 | [feature recipients](#featurerecipients)) 116 | * whitelists and blacklists for features 117 | (see [white- and blacklisting](#wblisting)) 118 | 119 | ##### CSS 120 | 121 | Open up the newly-generated `public/stylesheets/arturo_customizations.css`. 122 | You can add any overrides you like to the feature configuration page styles 123 | here. **Do not** edit `public/stylesheets/arturo.css` as that file may be 124 | overwritten in future updates to Arturo. 125 | 126 | ### In other frameworks 127 | 128 | Arturo is a Rails engine. I want to promote reuse on other frameworks by 129 | extracting key pieces into mixins, though this isn't done yet. Open an 130 | [issue](http://github.com/zendesk/arturo/issues) and I'll be happy to 131 | work with you on support for your favorite framework. 132 | 133 | ## Deep-Dive 134 | 135 | ### Logging 136 | 137 | You can provide a logger in order to inspect Arturo usage. 138 | A potential implementation for Rails would be: 139 | 140 | ```Ruby 141 | Arturo.logger = Rails.logger 142 | ``` 143 | 144 | ### Admin Permissions 145 | 146 | `Arturo::FeatureManagement#may_manage_features?` is a method that is run in 147 | the context of a Controller or View instance. It should return `true` if 148 | and only if the current user may manage permissions. The default implementation 149 | is as follows: 150 | 151 | ```Ruby 152 | current_user.present? && current_user.admin? 153 | ``` 154 | 155 | You can change the implementation in 156 | `config/initializers/arturo_initializer.rb`. A reasonable implementation 157 | might be 158 | 159 | ```Ruby 160 | Arturo.permit_management do 161 | signed_in? && current_user.can?(:manage_features) 162 | end 163 | ``` 164 | 165 | ### Feature Recipients 166 | 167 | Clients of Arturo may want to deploy new features on a per-user, per-project, 168 | per-account, or other basis. For example, it is likely Twitter deployed 169 | "#newtwitter" on a per-user basis. Conversely, Facebook -- at least in its 170 | early days -- may have deployed features on a per-university basis. It wouldn't 171 | make much sense to deploy a feature to one user of a Basecamp project but not 172 | to others, so 37Signals would probably want a per-project or per-account basis. 173 | 174 | `Arturo::FeatureAvailability#feature_recipient` is intended to support these 175 | many use cases. It is a method that returns the current "thing" (a user, account, 176 | project, university, ...) that is a member of the category that is the basis for 177 | deploying new features. It should return an `Object` that responds to `#id`. 178 | 179 | The default implementation simply returns `current_user`. Like 180 | `Arturo::FeatureManagement#may_manage_features?`, this method can be configured 181 | in `config/initializers/arturo_initializer.rb`. If you want to deploy features 182 | on a per-account basis, a reasonable implementation might be 183 | 184 | ```Ruby 185 | Arturo.feature_recipient do 186 | current_account 187 | end 188 | ``` 189 | 190 | or 191 | 192 | ```Ruby 193 | Arturo.feature_recipient do 194 | current_user.account 195 | end 196 | ``` 197 | 198 | If the block returns `nil`, the feature will be disabled. 199 | 200 | ### Whitelists & Blacklists 201 | 202 | Whitelists and blacklists allow you to control exactly which users or accounts 203 | will have a feature. For example, if all premium users should have the 204 | `:awesome` feature, place the following in 205 | `config/initializers/arturo_initializer.rb`: 206 | 207 | ```Ruby 208 | Arturo::Feature.whitelist(:awesome) do |user| 209 | user.account.premium? 210 | end 211 | ``` 212 | 213 | If, on the other hand, no users on the free plan should have the 214 | `:awesome` feature, place the following in 215 | `config/initializers/arturo_initializer.rb`: 216 | 217 | ```Ruby 218 | Arturo::Feature.blacklist(:awesome) do |user| 219 | user.account.free? 220 | end 221 | ``` 222 | 223 | If you want to whitelist or blacklist large groups of features at once, you 224 | can move the feature argument into the block: 225 | 226 | ```Ruby 227 | Arturo::Feature.whitelist do |feature, user| 228 | user.account.has?(feature.to_sym) 229 | end 230 | ``` 231 | 232 | ### Feature Conditionals 233 | 234 | All that configuration is just a waste of time if Arturo didn't modify the 235 | behavior of your application based on feature availability. There are a few 236 | ways to do so. 237 | 238 | #### Controller Filters 239 | 240 | If an action should only be available to those with a feature enabled, 241 | use a before filter. The following will raise a 403 Forbidden error for 242 | every action within `BookHoldsController` that is invoked by a user who 243 | does not have the `:hold_book` feature. 244 | 245 | ```Ruby 246 | class BookHoldsController < ApplicationController 247 | require_feature :hold_book 248 | end 249 | ``` 250 | 251 | `require_feature` accepts as a second argument a `Hash` that it passes on 252 | to `before_action`, so you can use `:only` and `:except` to specify exactly 253 | which actions are filtered. 254 | 255 | If you want to customize the page that is rendered on 403 Forbidden 256 | responses, put the view in 257 | `RAILS_ROOT/app/views/arturo/features/forbidden.html.erb`. Rails will 258 | check there before falling back on Arturo's forbidden page. 259 | 260 | #### Conditional Evaluation 261 | 262 | Both controllers and views have access to the `if_feature_enabled?` and 263 | `feature_enabled?` methods. The former is used like so: 264 | 265 | ```ERB 266 | <% if_feature_enabled?(:reserve_table) %> 267 | <%= link_to 'Reserve a table', new_restaurant_reservation_path(:restaurant_id => @restaurant) %> 268 | <% end %> 269 | ``` 270 | 271 | The latter can be used like so: 272 | 273 | ```Ruby 274 | def widgets_for_sidebar 275 | widgets = [] 276 | widgets << twitter_widget if feature_enabled?(:twitter_integration) 277 | ... 278 | widgets 279 | end 280 | ``` 281 | 282 | #### Rack Middleware 283 | 284 | ```Ruby 285 | require 'arturo' 286 | use Arturo::Middleware, feature: :my_feature 287 | ``` 288 | 289 | #### Outside a Controller 290 | 291 | If you want to check availability outside of a controller or view (really 292 | outside of something that has `Arturo::FeatureAvailability` mixed in), you 293 | can ask either 294 | 295 | ```Ruby 296 | Arturo.feature_enabled_for?(:foo, recipient) 297 | ``` 298 | 299 | or the slightly fancier 300 | 301 | ```Ruby 302 | Arturo.foo_enabled_for?(recipient) 303 | ``` 304 | 305 | Both check whether the `foo` feature exists and is enabled for `recipient`. 306 | 307 | #### Caching 308 | 309 | **Note**: Arturo has support for caching `Feature` lookups, but doesn't yet 310 | integrate with Rails's caching. This means you should be very careful when 311 | caching actions or pages that involve feature detection as you will get 312 | strange behavior when a user who has access to a feature requests a page 313 | just after one who does not (and vice versa). 314 | 315 | To enable caching `Feature` lookups, mix `Arturo::FeatureCaching` into 316 | `Arturo::Feature` and set the `cache_ttl`. This is best done in an 317 | initializer: 318 | 319 | ```Ruby 320 | Arturo::Feature.extend(Arturo::FeatureCaching) 321 | Arturo::Feature.cache_ttl = 10.minutes 322 | ``` 323 | 324 | You can also warm the cache on startup: 325 | 326 | ```Ruby 327 | Arturo::Feature.warm_cache! 328 | ``` 329 | 330 | This will pre-fetch all `Feature`s and put them in the cache. 331 | 332 | To use the current cache state when you can't fetch updates from origin: 333 | 334 | ```Ruby 335 | Arturo::Feature.extend_cache_on_failure = true 336 | ``` 337 | 338 | The following is the **intended** support for integration with view caching: 339 | 340 | Both the `require_feature` before filter and the `if_feature_enabled` block 341 | evaluation automatically append a string based on the feature's 342 | `last_modified` timestamp to cache keys that Rails generates. Thus, you don't 343 | have to worry about expiring caches when you increase a feature's deployment 344 | percentage. See `Arturo::CacheSupport` for more information. 345 | 346 | ## The Name 347 | 348 | Arturo gets its name from 349 | [Professor Maximillian Arturo](http://en.wikipedia.org/wiki/Maximillian_Arturo) 350 | on [Sliders](http://en.wikipedia.org/wiki/Sliders). 351 | 352 | ## Quality Metrics 353 | 354 | [![Build Status](https://github.com/zendesk/arturo/workflows/CI/badge.svg)](https://github.com/zendesk/arturo/actions?query=workflow%3ACI) 355 | 356 | [![Code Quality](https://codeclimate.com/github/zendesk/arturo.png)](https://codeclimate.com/github/zendesk/arturo) 357 | -------------------------------------------------------------------------------- /spec/dummy_app/public/javascripts/jquery-1.4.3.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery JavaScript Library v1.4.3 3 | * http://jquery.com/ 4 | * 5 | * Copyright 2010, John Resig 6 | * Dual licensed under the MIT or GPL Version 2 licenses. 7 | * http://jquery.org/license 8 | * 9 | * Includes Sizzle.js 10 | * http://sizzlejs.com/ 11 | * Copyright 2010, The Dojo Foundation 12 | * Released under the MIT, BSD, and GPL Licenses. 13 | * 14 | * Date: Thu Oct 14 23:10:06 2010 -0400 15 | */ 16 | (function(E,A){function U(){return false}function ba(){return true}function ja(a,b,d){d[0].type=a;return c.event.handle.apply(b,d)}function Ga(a){var b,d,e=[],f=[],h,k,l,n,s,v,B,D;k=c.data(this,this.nodeType?"events":"__events__");if(typeof k==="function")k=k.events;if(!(a.liveFired===this||!k||!k.live||a.button&&a.type==="click")){if(a.namespace)D=RegExp("(^|\\.)"+a.namespace.split(".").join("\\.(?:.*\\.)?")+"(\\.|$)");a.liveFired=this;var H=k.live.slice(0);for(n=0;nd)break;a.currentTarget=f.elem;a.data=f.handleObj.data; 18 | a.handleObj=f.handleObj;D=f.handleObj.origHandler.apply(f.elem,arguments);if(D===false||a.isPropagationStopped()){d=f.level;if(D===false)b=false}}return b}}function Y(a,b){return(a&&a!=="*"?a+".":"")+b.replace(Ha,"`").replace(Ia,"&")}function ka(a,b,d){if(c.isFunction(b))return c.grep(a,function(f,h){return!!b.call(f,h,f)===d});else if(b.nodeType)return c.grep(a,function(f){return f===b===d});else if(typeof b==="string"){var e=c.grep(a,function(f){return f.nodeType===1});if(Ja.test(b))return c.filter(b, 19 | e,!d);else b=c.filter(b,e)}return c.grep(a,function(f){return c.inArray(f,b)>=0===d})}function la(a,b){var d=0;b.each(function(){if(this.nodeName===(a[d]&&a[d].nodeName)){var e=c.data(a[d++]),f=c.data(this,e);if(e=e&&e.events){delete f.handle;f.events={};for(var h in e)for(var k in e[h])c.event.add(this,h,e[h][k],e[h][k].data)}}})}function Ka(a,b){b.src?c.ajax({url:b.src,async:false,dataType:"script"}):c.globalEval(b.text||b.textContent||b.innerHTML||"");b.parentNode&&b.parentNode.removeChild(b)} 20 | function ma(a,b,d){var e=b==="width"?a.offsetWidth:a.offsetHeight;if(d==="border")return e;c.each(b==="width"?La:Ma,function(){d||(e-=parseFloat(c.css(a,"padding"+this))||0);if(d==="margin")e+=parseFloat(c.css(a,"margin"+this))||0;else e-=parseFloat(c.css(a,"border"+this+"Width"))||0});return e}function ca(a,b,d,e){if(c.isArray(b)&&b.length)c.each(b,function(f,h){d||Na.test(a)?e(a,h):ca(a+"["+(typeof h==="object"||c.isArray(h)?f:"")+"]",h,d,e)});else if(!d&&b!=null&&typeof b==="object")c.isEmptyObject(b)? 21 | e(a,""):c.each(b,function(f,h){ca(a+"["+f+"]",h,d,e)});else e(a,b)}function S(a,b){var d={};c.each(na.concat.apply([],na.slice(0,b)),function(){d[this]=a});return d}function oa(a){if(!da[a]){var b=c("<"+a+">").appendTo("body"),d=b.css("display");b.remove();if(d==="none"||d==="")d="block";da[a]=d}return da[a]}function ea(a){return c.isWindow(a)?a:a.nodeType===9?a.defaultView||a.parentWindow:false}var u=E.document,c=function(){function a(){if(!b.isReady){try{u.documentElement.doScroll("left")}catch(i){setTimeout(a, 22 | 1);return}b.ready()}}var b=function(i,r){return new b.fn.init(i,r)},d=E.jQuery,e=E.$,f,h=/^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,k=/\S/,l=/^\s+/,n=/\s+$/,s=/\W/,v=/\d/,B=/^<(\w+)\s*\/?>(?:<\/\1>)?$/,D=/^[\],:{}\s]*$/,H=/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,w=/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,G=/(?:^|:|,)(?:\s*\[)+/g,M=/(webkit)[ \/]([\w.]+)/,g=/(opera)(?:.*version)?[ \/]([\w.]+)/,j=/(msie) ([\w.]+)/,o=/(mozilla)(?:.*? rv:([\w.]+))?/,m=navigator.userAgent,p=false, 23 | q=[],t,x=Object.prototype.toString,C=Object.prototype.hasOwnProperty,P=Array.prototype.push,N=Array.prototype.slice,R=String.prototype.trim,Q=Array.prototype.indexOf,L={};b.fn=b.prototype={init:function(i,r){var y,z,F;if(!i)return this;if(i.nodeType){this.context=this[0]=i;this.length=1;return this}if(i==="body"&&!r&&u.body){this.context=u;this[0]=u.body;this.selector="body";this.length=1;return this}if(typeof i==="string")if((y=h.exec(i))&&(y[1]||!r))if(y[1]){F=r?r.ownerDocument||r:u;if(z=B.exec(i))if(b.isPlainObject(r)){i= 24 | [u.createElement(z[1])];b.fn.attr.call(i,r,true)}else i=[F.createElement(z[1])];else{z=b.buildFragment([y[1]],[F]);i=(z.cacheable?z.fragment.cloneNode(true):z.fragment).childNodes}return b.merge(this,i)}else{if((z=u.getElementById(y[2]))&&z.parentNode){if(z.id!==y[2])return f.find(i);this.length=1;this[0]=z}this.context=u;this.selector=i;return this}else if(!r&&!s.test(i)){this.selector=i;this.context=u;i=u.getElementsByTagName(i);return b.merge(this,i)}else return!r||r.jquery?(r||f).find(i):b(r).find(i); 25 | else if(b.isFunction(i))return f.ready(i);if(i.selector!==A){this.selector=i.selector;this.context=i.context}return b.makeArray(i,this)},selector:"",jquery:"1.4.3",length:0,size:function(){return this.length},toArray:function(){return N.call(this,0)},get:function(i){return i==null?this.toArray():i<0?this.slice(i)[0]:this[i]},pushStack:function(i,r,y){var z=b();b.isArray(i)?P.apply(z,i):b.merge(z,i);z.prevObject=this;z.context=this.context;if(r==="find")z.selector=this.selector+(this.selector?" ": 26 | "")+y;else if(r)z.selector=this.selector+"."+r+"("+y+")";return z},each:function(i,r){return b.each(this,i,r)},ready:function(i){b.bindReady();if(b.isReady)i.call(u,b);else q&&q.push(i);return this},eq:function(i){return i===-1?this.slice(i):this.slice(i,+i+1)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},slice:function(){return this.pushStack(N.apply(this,arguments),"slice",N.call(arguments).join(","))},map:function(i){return this.pushStack(b.map(this,function(r,y){return i.call(r, 27 | y,r)}))},end:function(){return this.prevObject||b(null)},push:P,sort:[].sort,splice:[].splice};b.fn.init.prototype=b.fn;b.extend=b.fn.extend=function(){var i=arguments[0]||{},r=1,y=arguments.length,z=false,F,I,K,J,fa;if(typeof i==="boolean"){z=i;i=arguments[1]||{};r=2}if(typeof i!=="object"&&!b.isFunction(i))i={};if(y===r){i=this;--r}for(;r0)){if(q){for(var r=0;i=q[r++];)i.call(u,b);q=null}b.fn.triggerHandler&&b(u).triggerHandler("ready")}}},bindReady:function(){if(!p){p=true;if(u.readyState==="complete")return setTimeout(b.ready, 29 | 1);if(u.addEventListener){u.addEventListener("DOMContentLoaded",t,false);E.addEventListener("load",b.ready,false)}else if(u.attachEvent){u.attachEvent("onreadystatechange",t);E.attachEvent("onload",b.ready);var i=false;try{i=E.frameElement==null}catch(r){}u.documentElement.doScroll&&i&&a()}}},isFunction:function(i){return b.type(i)==="function"},isArray:Array.isArray||function(i){return b.type(i)==="array"},isWindow:function(i){return i&&typeof i==="object"&&"setInterval"in i},isNaN:function(i){return i== 30 | null||!v.test(i)||isNaN(i)},type:function(i){return i==null?String(i):L[x.call(i)]||"object"},isPlainObject:function(i){if(!i||b.type(i)!=="object"||i.nodeType||b.isWindow(i))return false;if(i.constructor&&!C.call(i,"constructor")&&!C.call(i.constructor.prototype,"isPrototypeOf"))return false;for(var r in i);return r===A||C.call(i,r)},isEmptyObject:function(i){for(var r in i)return false;return true},error:function(i){throw i;},parseJSON:function(i){if(typeof i!=="string"||!i)return null;i=b.trim(i); 31 | if(D.test(i.replace(H,"@").replace(w,"]").replace(G,"")))return E.JSON&&E.JSON.parse?E.JSON.parse(i):(new Function("return "+i))();else b.error("Invalid JSON: "+i)},noop:function(){},globalEval:function(i){if(i&&k.test(i)){var r=u.getElementsByTagName("head")[0]||u.documentElement,y=u.createElement("script");y.type="text/javascript";if(b.support.scriptEval)y.appendChild(u.createTextNode(i));else y.text=i;r.insertBefore(y,r.firstChild);r.removeChild(y)}},nodeName:function(i,r){return i.nodeName&&i.nodeName.toUpperCase()=== 32 | r.toUpperCase()},each:function(i,r,y){var z,F=0,I=i.length,K=I===A||b.isFunction(i);if(y)if(K)for(z in i){if(r.apply(i[z],y)===false)break}else for(;F";a=u.createDocumentFragment();a.appendChild(d.firstChild);c.support.checkClone=a.cloneNode(true).cloneNode(true).lastChild.checked;c(function(){var s=u.createElement("div"); 40 | s.style.width=s.style.paddingLeft="1px";u.body.appendChild(s);c.boxModel=c.support.boxModel=s.offsetWidth===2;if("zoom"in s.style){s.style.display="inline";s.style.zoom=1;c.support.inlineBlockNeedsLayout=s.offsetWidth===2;s.style.display="";s.innerHTML="
";c.support.shrinkWrapBlocks=s.offsetWidth!==2}s.innerHTML="
t
";var v=s.getElementsByTagName("td");c.support.reliableHiddenOffsets=v[0].offsetHeight=== 41 | 0;v[0].style.display="";v[1].style.display="none";c.support.reliableHiddenOffsets=c.support.reliableHiddenOffsets&&v[0].offsetHeight===0;s.innerHTML="";u.body.removeChild(s).style.display="none"});a=function(s){var v=u.createElement("div");s="on"+s;var B=s in v;if(!B){v.setAttribute(s,"return;");B=typeof v[s]==="function"}return B};c.support.submitBubbles=a("submit");c.support.changeBubbles=a("change");a=b=d=f=h=null}})();c.props={"for":"htmlFor","class":"className",readonly:"readOnly",maxlength:"maxLength", 42 | cellspacing:"cellSpacing",rowspan:"rowSpan",colspan:"colSpan",tabindex:"tabIndex",usemap:"useMap",frameborder:"frameBorder"};var pa={},Oa=/^(?:\{.*\}|\[.*\])$/;c.extend({cache:{},uuid:0,expando:"jQuery"+c.now(),noData:{embed:true,object:"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",applet:true},data:function(a,b,d){if(c.acceptData(a)){a=a==E?pa:a;var e=a.nodeType,f=e?a[c.expando]:null,h=c.cache;if(!(e&&!f&&typeof b==="string"&&d===A)){if(e)f||(a[c.expando]=f=++c.uuid);else h=a;if(typeof b==="object")if(e)h[f]= 43 | c.extend(h[f],b);else c.extend(h,b);else if(e&&!h[f])h[f]={};a=e?h[f]:h;if(d!==A)a[b]=d;return typeof b==="string"?a[b]:a}}},removeData:function(a,b){if(c.acceptData(a)){a=a==E?pa:a;var d=a.nodeType,e=d?a[c.expando]:a,f=c.cache,h=d?f[e]:e;if(b){if(h){delete h[b];d&&c.isEmptyObject(h)&&c.removeData(a)}}else if(d&&c.support.deleteExpando)delete a[c.expando];else if(a.removeAttribute)a.removeAttribute(c.expando);else if(d)delete f[e];else for(var k in a)delete a[k]}},acceptData:function(a){if(a.nodeName){var b= 44 | c.noData[a.nodeName.toLowerCase()];if(b)return!(b===true||a.getAttribute("classid")!==b)}return true}});c.fn.extend({data:function(a,b){if(typeof a==="undefined")return this.length?c.data(this[0]):null;else if(typeof a==="object")return this.each(function(){c.data(this,a)});var d=a.split(".");d[1]=d[1]?"."+d[1]:"";if(b===A){var e=this.triggerHandler("getData"+d[1]+"!",[d[0]]);if(e===A&&this.length){e=c.data(this[0],a);if(e===A&&this[0].nodeType===1){e=this[0].getAttribute("data-"+a);if(typeof e=== 45 | "string")try{e=e==="true"?true:e==="false"?false:e==="null"?null:!c.isNaN(e)?parseFloat(e):Oa.test(e)?c.parseJSON(e):e}catch(f){}else e=A}}return e===A&&d[1]?this.data(d[0]):e}else return this.each(function(){var h=c(this),k=[d[0],b];h.triggerHandler("setData"+d[1]+"!",k);c.data(this,a,b);h.triggerHandler("changeData"+d[1]+"!",k)})},removeData:function(a){return this.each(function(){c.removeData(this,a)})}});c.extend({queue:function(a,b,d){if(a){b=(b||"fx")+"queue";var e=c.data(a,b);if(!d)return e|| 46 | [];if(!e||c.isArray(d))e=c.data(a,b,c.makeArray(d));else e.push(d);return e}},dequeue:function(a,b){b=b||"fx";var d=c.queue(a,b),e=d.shift();if(e==="inprogress")e=d.shift();if(e){b==="fx"&&d.unshift("inprogress");e.call(a,function(){c.dequeue(a,b)})}}});c.fn.extend({queue:function(a,b){if(typeof a!=="string"){b=a;a="fx"}if(b===A)return c.queue(this[0],a);return this.each(function(){var d=c.queue(this,a,b);a==="fx"&&d[0]!=="inprogress"&&c.dequeue(this,a)})},dequeue:function(a){return this.each(function(){c.dequeue(this, 47 | a)})},delay:function(a,b){a=c.fx?c.fx.speeds[a]||a:a;b=b||"fx";return this.queue(b,function(){var d=this;setTimeout(function(){c.dequeue(d,b)},a)})},clearQueue:function(a){return this.queue(a||"fx",[])}});var qa=/[\n\t]/g,ga=/\s+/,Pa=/\r/g,Qa=/^(?:href|src|style)$/,Ra=/^(?:button|input)$/i,Sa=/^(?:button|input|object|select|textarea)$/i,Ta=/^a(?:rea)?$/i,ra=/^(?:radio|checkbox)$/i;c.fn.extend({attr:function(a,b){return c.access(this,a,b,true,c.attr)},removeAttr:function(a){return this.each(function(){c.attr(this, 48 | a,"");this.nodeType===1&&this.removeAttribute(a)})},addClass:function(a){if(c.isFunction(a))return this.each(function(s){var v=c(this);v.addClass(a.call(this,s,v.attr("class")))});if(a&&typeof a==="string")for(var b=(a||"").split(ga),d=0,e=this.length;d-1)return true;return false}, 51 | val:function(a){if(!arguments.length){var b=this[0];if(b){if(c.nodeName(b,"option")){var d=b.attributes.value;return!d||d.specified?b.value:b.text}if(c.nodeName(b,"select")){var e=b.selectedIndex;d=[];var f=b.options;b=b.type==="select-one";if(e<0)return null;var h=b?e:0;for(e=b?e+1:f.length;h=0;else if(c.nodeName(this,"select")){var B=c.makeArray(v);c("option",this).each(function(){this.selected= 53 | c.inArray(c(this).val(),B)>=0});if(!B.length)this.selectedIndex=-1}else this.value=v}})}});c.extend({attrFn:{val:true,css:true,html:true,text:true,data:true,width:true,height:true,offset:true},attr:function(a,b,d,e){if(!a||a.nodeType===3||a.nodeType===8)return A;if(e&&b in c.attrFn)return c(a)[b](d);e=a.nodeType!==1||!c.isXMLDoc(a);var f=d!==A;b=e&&c.props[b]||b;if(a.nodeType===1){var h=Qa.test(b);if((b in a||a[b]!==A)&&e&&!h){if(f){b==="type"&&Ra.test(a.nodeName)&&a.parentNode&&c.error("type property can't be changed"); 54 | if(d===null)a.nodeType===1&&a.removeAttribute(b);else a[b]=d}if(c.nodeName(a,"form")&&a.getAttributeNode(b))return a.getAttributeNode(b).nodeValue;if(b==="tabIndex")return(b=a.getAttributeNode("tabIndex"))&&b.specified?b.value:Sa.test(a.nodeName)||Ta.test(a.nodeName)&&a.href?0:A;return a[b]}if(!c.support.style&&e&&b==="style"){if(f)a.style.cssText=""+d;return a.style.cssText}f&&a.setAttribute(b,""+d);if(!a.attributes[b]&&a.hasAttribute&&!a.hasAttribute(b))return A;a=!c.support.hrefNormalized&&e&& 55 | h?a.getAttribute(b,2):a.getAttribute(b);return a===null?A:a}}});var X=/\.(.*)$/,ha=/^(?:textarea|input|select)$/i,Ha=/\./g,Ia=/ /g,Ua=/[^\w\s.|`]/g,Va=function(a){return a.replace(Ua,"\\$&")},sa={focusin:0,focusout:0};c.event={add:function(a,b,d,e){if(!(a.nodeType===3||a.nodeType===8)){if(c.isWindow(a)&&a!==E&&!a.frameElement)a=E;if(d===false)d=U;var f,h;if(d.handler){f=d;d=f.handler}if(!d.guid)d.guid=c.guid++;if(h=c.data(a)){var k=a.nodeType?"events":"__events__",l=h[k],n=h.handle;if(typeof l=== 56 | "function"){n=l.handle;l=l.events}else if(!l){a.nodeType||(h[k]=h=function(){});h.events=l={}}if(!n)h.handle=n=function(){return typeof c!=="undefined"&&!c.event.triggered?c.event.handle.apply(n.elem,arguments):A};n.elem=a;b=b.split(" ");for(var s=0,v;k=b[s++];){h=f?c.extend({},f):{handler:d,data:e};if(k.indexOf(".")>-1){v=k.split(".");k=v.shift();h.namespace=v.slice(0).sort().join(".")}else{v=[];h.namespace=""}h.type=k;if(!h.guid)h.guid=d.guid;var B=l[k],D=c.event.special[k]||{};if(!B){B=l[k]=[]; 57 | if(!D.setup||D.setup.call(a,e,v,n)===false)if(a.addEventListener)a.addEventListener(k,n,false);else a.attachEvent&&a.attachEvent("on"+k,n)}if(D.add){D.add.call(a,h);if(!h.handler.guid)h.handler.guid=d.guid}B.push(h);c.event.global[k]=true}a=null}}},global:{},remove:function(a,b,d,e){if(!(a.nodeType===3||a.nodeType===8)){if(d===false)d=U;var f,h,k=0,l,n,s,v,B,D,H=a.nodeType?"events":"__events__",w=c.data(a),G=w&&w[H];if(w&&G){if(typeof G==="function"){w=G;G=G.events}if(b&&b.type){d=b.handler;b=b.type}if(!b|| 58 | typeof b==="string"&&b.charAt(0)==="."){b=b||"";for(f in G)c.event.remove(a,f+b)}else{for(b=b.split(" ");f=b[k++];){v=f;l=f.indexOf(".")<0;n=[];if(!l){n=f.split(".");f=n.shift();s=RegExp("(^|\\.)"+c.map(n.slice(0).sort(),Va).join("\\.(?:.*\\.)?")+"(\\.|$)")}if(B=G[f])if(d){v=c.event.special[f]||{};for(h=e||0;h=0){a.type= 60 | f=f.slice(0,-1);a.exclusive=true}if(!d){a.stopPropagation();c.event.global[f]&&c.each(c.cache,function(){this.events&&this.events[f]&&c.event.trigger(a,b,this.handle.elem)})}if(!d||d.nodeType===3||d.nodeType===8)return A;a.result=A;a.target=d;b=c.makeArray(b);b.unshift(a)}a.currentTarget=d;(e=d.nodeType?c.data(d,"handle"):(c.data(d,"__events__")||{}).handle)&&e.apply(d,b);e=d.parentNode||d.ownerDocument;try{if(!(d&&d.nodeName&&c.noData[d.nodeName.toLowerCase()]))if(d["on"+f]&&d["on"+f].apply(d,b)=== 61 | false){a.result=false;a.preventDefault()}}catch(h){}if(!a.isPropagationStopped()&&e)c.event.trigger(a,b,e,true);else if(!a.isDefaultPrevented()){e=a.target;var k,l=f.replace(X,""),n=c.nodeName(e,"a")&&l==="click",s=c.event.special[l]||{};if((!s._default||s._default.call(d,a)===false)&&!n&&!(e&&e.nodeName&&c.noData[e.nodeName.toLowerCase()])){try{if(e[l]){if(k=e["on"+l])e["on"+l]=null;c.event.triggered=true;e[l]()}}catch(v){}if(k)e["on"+l]=k;c.event.triggered=false}}},handle:function(a){var b,d,e; 62 | d=[];var f,h=c.makeArray(arguments);a=h[0]=c.event.fix(a||E.event);a.currentTarget=this;b=a.type.indexOf(".")<0&&!a.exclusive;if(!b){e=a.type.split(".");a.type=e.shift();d=e.slice(0).sort();e=RegExp("(^|\\.)"+d.join("\\.(?:.*\\.)?")+"(\\.|$)")}a.namespace=a.namespace||d.join(".");f=c.data(this,this.nodeType?"events":"__events__");if(typeof f==="function")f=f.events;d=(f||{})[a.type];if(f&&d){d=d.slice(0);f=0;for(var k=d.length;f-1?c.map(a.options,function(e){return e.selected}).join("-"):"";else if(a.nodeName.toLowerCase()==="select")d=a.selectedIndex;return d},Z=function(a,b){var d=a.target,e,f;if(!(!ha.test(d.nodeName)||d.readOnly)){e=c.data(d,"_change_data");f=va(d);if(a.type!=="focusout"||d.type!=="radio")c.data(d,"_change_data",f);if(!(e===A||f===e))if(e!=null||f){a.type="change";a.liveFired= 71 | A;return c.event.trigger(a,b,d)}}};c.event.special.change={filters:{focusout:Z,beforedeactivate:Z,click:function(a){var b=a.target,d=b.type;if(d==="radio"||d==="checkbox"||b.nodeName.toLowerCase()==="select")return Z.call(this,a)},keydown:function(a){var b=a.target,d=b.type;if(a.keyCode===13&&b.nodeName.toLowerCase()!=="textarea"||a.keyCode===32&&(d==="checkbox"||d==="radio")||d==="select-multiple")return Z.call(this,a)},beforeactivate:function(a){a=a.target;c.data(a,"_change_data",va(a))}},setup:function(){if(this.type=== 72 | "file")return false;for(var a in V)c.event.add(this,a+".specialChange",V[a]);return ha.test(this.nodeName)},teardown:function(){c.event.remove(this,".specialChange");return ha.test(this.nodeName)}};V=c.event.special.change.filters;V.focus=V.beforeactivate}u.addEventListener&&c.each({focus:"focusin",blur:"focusout"},function(a,b){function d(e){e=c.event.fix(e);e.type=b;return c.event.trigger(e,null,e.target)}c.event.special[b]={setup:function(){sa[b]++===0&&u.addEventListener(a,d,true)},teardown:function(){--sa[b]=== 73 | 0&&u.removeEventListener(a,d,true)}}});c.each(["bind","one"],function(a,b){c.fn[b]=function(d,e,f){if(typeof d==="object"){for(var h in d)this[b](h,e,d[h],f);return this}if(c.isFunction(e)||e===false){f=e;e=A}var k=b==="one"?c.proxy(f,function(n){c(this).unbind(n,k);return f.apply(this,arguments)}):f;if(d==="unload"&&b!=="one")this.one(d,e,f);else{h=0;for(var l=this.length;h0?this.bind(b,d,e):this.trigger(b)};if(c.attrFn)c.attrFn[b]=true});E.attachEvent&&!E.addEventListener&&c(E).bind("unload",function(){for(var a in c.cache)if(c.cache[a].handle)try{c.event.remove(c.cache[a].handle.elem)}catch(b){}}); 78 | (function(){function a(g,j,o,m,p,q){p=0;for(var t=m.length;p0){C=x;break}}x=x[g]}m[p]=C}}}var d=/((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,e=0,f=Object.prototype.toString,h=false,k=true;[0,0].sort(function(){k=false;return 0});var l=function(g,j,o,m){o=o||[];var p=j=j||u;if(j.nodeType!==1&&j.nodeType!==9)return[];if(!g||typeof g!=="string")return o;var q=[],t,x,C,P,N=true,R=l.isXML(j),Q=g,L;do{d.exec("");if(t=d.exec(Q)){Q=t[3];q.push(t[1]);if(t[2]){P=t[3]; 80 | break}}}while(t);if(q.length>1&&s.exec(g))if(q.length===2&&n.relative[q[0]])x=M(q[0]+q[1],j);else for(x=n.relative[q[0]]?[j]:l(q.shift(),j);q.length;){g=q.shift();if(n.relative[g])g+=q.shift();x=M(g,x)}else{if(!m&&q.length>1&&j.nodeType===9&&!R&&n.match.ID.test(q[0])&&!n.match.ID.test(q[q.length-1])){t=l.find(q.shift(),j,R);j=t.expr?l.filter(t.expr,t.set)[0]:t.set[0]}if(j){t=m?{expr:q.pop(),set:D(m)}:l.find(q.pop(),q.length===1&&(q[0]==="~"||q[0]==="+")&&j.parentNode?j.parentNode:j,R);x=t.expr?l.filter(t.expr, 81 | t.set):t.set;if(q.length>0)C=D(x);else N=false;for(;q.length;){t=L=q.pop();if(n.relative[L])t=q.pop();else L="";if(t==null)t=j;n.relative[L](C,t,R)}}else C=[]}C||(C=x);C||l.error(L||g);if(f.call(C)==="[object Array]")if(N)if(j&&j.nodeType===1)for(g=0;C[g]!=null;g++){if(C[g]&&(C[g]===true||C[g].nodeType===1&&l.contains(j,C[g])))o.push(x[g])}else for(g=0;C[g]!=null;g++)C[g]&&C[g].nodeType===1&&o.push(x[g]);else o.push.apply(o,C);else D(C,o);if(P){l(P,p,o,m);l.uniqueSort(o)}return o};l.uniqueSort=function(g){if(w){h= 82 | k;g.sort(w);if(h)for(var j=1;j0};l.find=function(g,j,o){var m;if(!g)return[];for(var p=0,q=n.order.length;p":function(g,j){var o=typeof j==="string",m,p=0,q=g.length;if(o&&!/\W/.test(j))for(j=j.toLowerCase();p=0))o||m.push(t);else if(o)j[q]=false;return false},ID:function(g){return g[1].replace(/\\/g,"")},TAG:function(g){return g[1].toLowerCase()},CHILD:function(g){if(g[1]==="nth"){var j=/(-?)(\d*)n((?:\+|-)?\d*)/.exec(g[2]==="even"&&"2n"||g[2]==="odd"&&"2n+1"||!/\D/.test(g[2])&&"0n+"+g[2]||g[2]);g[2]=j[1]+(j[2]||1)-0;g[3]=j[3]-0}g[0]=e++;return g},ATTR:function(g,j,o, 89 | m,p,q){j=g[1].replace(/\\/g,"");if(!q&&n.attrMap[j])g[1]=n.attrMap[j];if(g[2]==="~=")g[4]=" "+g[4]+" ";return g},PSEUDO:function(g,j,o,m,p){if(g[1]==="not")if((d.exec(g[3])||"").length>1||/^\w/.test(g[3]))g[3]=l(g[3],null,null,j);else{g=l.filter(g[3],j,o,true^p);o||m.push.apply(m,g);return false}else if(n.match.POS.test(g[0])||n.match.CHILD.test(g[0]))return true;return g},POS:function(g){g.unshift(true);return g}},filters:{enabled:function(g){return g.disabled===false&&g.type!=="hidden"},disabled:function(g){return g.disabled=== 90 | true},checked:function(g){return g.checked===true},selected:function(g){return g.selected===true},parent:function(g){return!!g.firstChild},empty:function(g){return!g.firstChild},has:function(g,j,o){return!!l(o[3],g).length},header:function(g){return/h\d/i.test(g.nodeName)},text:function(g){return"text"===g.type},radio:function(g){return"radio"===g.type},checkbox:function(g){return"checkbox"===g.type},file:function(g){return"file"===g.type},password:function(g){return"password"===g.type},submit:function(g){return"submit"=== 91 | g.type},image:function(g){return"image"===g.type},reset:function(g){return"reset"===g.type},button:function(g){return"button"===g.type||g.nodeName.toLowerCase()==="button"},input:function(g){return/input|select|textarea|button/i.test(g.nodeName)}},setFilters:{first:function(g,j){return j===0},last:function(g,j,o,m){return j===m.length-1},even:function(g,j){return j%2===0},odd:function(g,j){return j%2===1},lt:function(g,j,o){return jo[3]-0},nth:function(g,j,o){return o[3]- 92 | 0===j},eq:function(g,j,o){return o[3]-0===j}},filter:{PSEUDO:function(g,j,o,m){var p=j[1],q=n.filters[p];if(q)return q(g,o,j,m);else if(p==="contains")return(g.textContent||g.innerText||l.getText([g])||"").indexOf(j[3])>=0;else if(p==="not"){j=j[3];o=0;for(m=j.length;o=0}},ID:function(g,j){return g.nodeType===1&&g.getAttribute("id")===j},TAG:function(g,j){return j==="*"&&g.nodeType===1||g.nodeName.toLowerCase()=== 94 | j},CLASS:function(g,j){return(" "+(g.className||g.getAttribute("class"))+" ").indexOf(j)>-1},ATTR:function(g,j){var o=j[1];o=n.attrHandle[o]?n.attrHandle[o](g):g[o]!=null?g[o]:g.getAttribute(o);var m=o+"",p=j[2],q=j[4];return o==null?p==="!=":p==="="?m===q:p==="*="?m.indexOf(q)>=0:p==="~="?(" "+m+" ").indexOf(q)>=0:!q?m&&o!==false:p==="!="?m!==q:p==="^="?m.indexOf(q)===0:p==="$="?m.substr(m.length-q.length)===q:p==="|="?m===q||m.substr(0,q.length+1)===q+"-":false},POS:function(g,j,o,m){var p=n.setFilters[j[2]]; 95 | if(p)return p(g,o,j,m)}}},s=n.match.POS,v=function(g,j){return"\\"+(j-0+1)},B;for(B in n.match){n.match[B]=RegExp(n.match[B].source+/(?![^\[]*\])(?![^\(]*\))/.source);n.leftMatch[B]=RegExp(/(^(?:.|\r|\n)*?)/.source+n.match[B].source.replace(/\\(\d+)/g,v))}var D=function(g,j){g=Array.prototype.slice.call(g,0);if(j){j.push.apply(j,g);return j}return g};try{Array.prototype.slice.call(u.documentElement.childNodes,0)}catch(H){D=function(g,j){var o=j||[],m=0;if(f.call(g)==="[object Array]")Array.prototype.push.apply(o, 96 | g);else if(typeof g.length==="number")for(var p=g.length;m";var o=u.documentElement;o.insertBefore(g,o.firstChild);if(u.getElementById(j)){n.find.ID=function(m,p,q){if(typeof p.getElementById!=="undefined"&&!q)return(p=p.getElementById(m[1]))?p.id===m[1]||typeof p.getAttributeNode!=="undefined"&&p.getAttributeNode("id").nodeValue===m[1]?[p]:A:[]};n.filter.ID=function(m,p){var q=typeof m.getAttributeNode!=="undefined"&&m.getAttributeNode("id");return m.nodeType===1&&q&&q.nodeValue===p}}o.removeChild(g); 99 | o=g=null})();(function(){var g=u.createElement("div");g.appendChild(u.createComment(""));if(g.getElementsByTagName("*").length>0)n.find.TAG=function(j,o){var m=o.getElementsByTagName(j[1]);if(j[1]==="*"){for(var p=[],q=0;m[q];q++)m[q].nodeType===1&&p.push(m[q]);m=p}return m};g.innerHTML="";if(g.firstChild&&typeof g.firstChild.getAttribute!=="undefined"&&g.firstChild.getAttribute("href")!=="#")n.attrHandle.href=function(j){return j.getAttribute("href",2)};g=null})();u.querySelectorAll&& 100 | function(){var g=l,j=u.createElement("div");j.innerHTML="

";if(!(j.querySelectorAll&&j.querySelectorAll(".TEST").length===0)){l=function(m,p,q,t){p=p||u;if(!t&&!l.isXML(p))if(p.nodeType===9)try{return D(p.querySelectorAll(m),q)}catch(x){}else if(p.nodeType===1&&p.nodeName.toLowerCase()!=="object"){var C=p.id,P=p.id="__sizzle__";try{return D(p.querySelectorAll("#"+P+" "+m),q)}catch(N){}finally{if(C)p.id=C;else p.removeAttribute("id")}}return g(m,p,q,t)};for(var o in g)l[o]=g[o]; 101 | j=null}}();(function(){var g=u.documentElement,j=g.matchesSelector||g.mozMatchesSelector||g.webkitMatchesSelector||g.msMatchesSelector,o=false;try{j.call(u.documentElement,":sizzle")}catch(m){o=true}if(j)l.matchesSelector=function(p,q){try{if(o||!n.match.PSEUDO.test(q))return j.call(p,q)}catch(t){}return l(q,null,null,[p]).length>0}})();(function(){var g=u.createElement("div");g.innerHTML="
";if(!(!g.getElementsByClassName||g.getElementsByClassName("e").length=== 102 | 0)){g.lastChild.className="e";if(g.getElementsByClassName("e").length!==1){n.order.splice(1,0,"CLASS");n.find.CLASS=function(j,o,m){if(typeof o.getElementsByClassName!=="undefined"&&!m)return o.getElementsByClassName(j[1])};g=null}}})();l.contains=u.documentElement.contains?function(g,j){return g!==j&&(g.contains?g.contains(j):true)}:function(g,j){return!!(g.compareDocumentPosition(j)&16)};l.isXML=function(g){return(g=(g?g.ownerDocument||g:0).documentElement)?g.nodeName!=="HTML":false};var M=function(g, 103 | j){for(var o=[],m="",p,q=j.nodeType?[j]:j;p=n.match.PSEUDO.exec(g);){m+=p[0];g=g.replace(n.match.PSEUDO,"")}g=n.relative[g]?g+"*":g;p=0;for(var t=q.length;p0)for(var h=d;h0},closest:function(a, 105 | b){var d=[],e,f,h=this[0];if(c.isArray(a)){var k={},l,n=1;if(h&&a.length){e=0;for(f=a.length;e-1:c(h).is(e))d.push({selector:l,elem:h,level:n})}h=h.parentNode;n++}}return d}k=$a.test(a)?c(a,b||this.context):null;e=0;for(f=this.length;e-1:c.find.matchesSelector(h,a)){d.push(h);break}else{h=h.parentNode;if(!h|| 106 | !h.ownerDocument||h===b)break}d=d.length>1?c.unique(d):d;return this.pushStack(d,"closest",a)},index:function(a){if(!a||typeof a==="string")return c.inArray(this[0],a?c(a):this.parent().children());return c.inArray(a.jquery?a[0]:a,this)},add:function(a,b){var d=typeof a==="string"?c(a,b||this.context):c.makeArray(a),e=c.merge(this.get(),d);return this.pushStack(!d[0]||!d[0].parentNode||d[0].parentNode.nodeType===11||!e[0]||!e[0].parentNode||e[0].parentNode.nodeType===11?e:c.unique(e))},andSelf:function(){return this.add(this.prevObject)}}); 107 | c.each({parent:function(a){return(a=a.parentNode)&&a.nodeType!==11?a:null},parents:function(a){return c.dir(a,"parentNode")},parentsUntil:function(a,b,d){return c.dir(a,"parentNode",d)},next:function(a){return c.nth(a,2,"nextSibling")},prev:function(a){return c.nth(a,2,"previousSibling")},nextAll:function(a){return c.dir(a,"nextSibling")},prevAll:function(a){return c.dir(a,"previousSibling")},nextUntil:function(a,b,d){return c.dir(a,"nextSibling",d)},prevUntil:function(a,b,d){return c.dir(a,"previousSibling", 108 | d)},siblings:function(a){return c.sibling(a.parentNode.firstChild,a)},children:function(a){return c.sibling(a.firstChild)},contents:function(a){return c.nodeName(a,"iframe")?a.contentDocument||a.contentWindow.document:c.makeArray(a.childNodes)}},function(a,b){c.fn[a]=function(d,e){var f=c.map(this,b,d);Wa.test(a)||(e=d);if(e&&typeof e==="string")f=c.filter(e,f);f=this.length>1?c.unique(f):f;if((this.length>1||Ya.test(e))&&Xa.test(a))f=f.reverse();return this.pushStack(f,a,Za.call(arguments).join(","))}}); 109 | c.extend({filter:function(a,b,d){if(d)a=":not("+a+")";return b.length===1?c.find.matchesSelector(b[0],a)?[b[0]]:[]:c.find.matches(a,b)},dir:function(a,b,d){var e=[];for(a=a[b];a&&a.nodeType!==9&&(d===A||a.nodeType!==1||!c(a).is(d));){a.nodeType===1&&e.push(a);a=a[b]}return e},nth:function(a,b,d){b=b||1;for(var e=0;a;a=a[d])if(a.nodeType===1&&++e===b)break;return a},sibling:function(a,b){for(var d=[];a;a=a.nextSibling)a.nodeType===1&&a!==b&&d.push(a);return d}});var xa=/ jQuery\d+="(?:\d+|null)"/g, 110 | $=/^\s+/,ya=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,za=/<([\w:]+)/,ab=/\s]+\/)>/g,O={option:[1,""],legend:[1,"
","
"],thead:[1,"","
"],tr:[2,"","
"],td:[3,"","
"],col:[2,"","
"], 111 | area:[1,"",""],_default:[0,"",""]};O.optgroup=O.option;O.tbody=O.tfoot=O.colgroup=O.caption=O.thead;O.th=O.td;if(!c.support.htmlSerialize)O._default=[1,"div
","
"];c.fn.extend({text:function(a){if(c.isFunction(a))return this.each(function(b){var d=c(this);d.text(a.call(this,b,d.text()))});if(typeof a!=="object"&&a!==A)return this.empty().append((this[0]&&this[0].ownerDocument||u).createTextNode(a));return c.text(this)},wrapAll:function(a){if(c.isFunction(a))return this.each(function(d){c(this).wrapAll(a.call(this, 112 | d))});if(this[0]){var b=c(a,this[0].ownerDocument).eq(0).clone(true);this[0].parentNode&&b.insertBefore(this[0]);b.map(function(){for(var d=this;d.firstChild&&d.firstChild.nodeType===1;)d=d.firstChild;return d}).append(this)}return this},wrapInner:function(a){if(c.isFunction(a))return this.each(function(b){c(this).wrapInner(a.call(this,b))});return this.each(function(){var b=c(this),d=b.contents();d.length?d.wrapAll(a):b.append(a)})},wrap:function(a){return this.each(function(){c(this).wrapAll(a)})}, 113 | unwrap:function(){return this.parent().each(function(){c.nodeName(this,"body")||c(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,true,function(a){this.nodeType===1&&this.appendChild(a)})},prepend:function(){return this.domManip(arguments,true,function(a){this.nodeType===1&&this.insertBefore(a,this.firstChild)})},before:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,false,function(b){this.parentNode.insertBefore(b,this)});else if(arguments.length){var a= 114 | c(arguments[0]);a.push.apply(a,this.toArray());return this.pushStack(a,"before",arguments)}},after:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,false,function(b){this.parentNode.insertBefore(b,this.nextSibling)});else if(arguments.length){var a=this.pushStack(this,"after",arguments);a.push.apply(a,c(arguments[0]).toArray());return a}},remove:function(a,b){for(var d=0,e;(e=this[d])!=null;d++)if(!a||c.filter(a,[e]).length){if(!b&&e.nodeType===1){c.cleanData(e.getElementsByTagName("*")); 115 | c.cleanData([e])}e.parentNode&&e.parentNode.removeChild(e)}return this},empty:function(){for(var a=0,b;(b=this[a])!=null;a++)for(b.nodeType===1&&c.cleanData(b.getElementsByTagName("*"));b.firstChild;)b.removeChild(b.firstChild);return this},clone:function(a){var b=this.map(function(){if(!c.support.noCloneEvent&&!c.isXMLDoc(this)){var d=this.outerHTML,e=this.ownerDocument;if(!d){d=e.createElement("div");d.appendChild(this.cloneNode(true));d=d.innerHTML}return c.clean([d.replace(xa,"").replace(cb,'="$1">').replace($, 116 | "")],e)[0]}else return this.cloneNode(true)});if(a===true){la(this,b);la(this.find("*"),b.find("*"))}return b},html:function(a){if(a===A)return this[0]&&this[0].nodeType===1?this[0].innerHTML.replace(xa,""):null;else if(typeof a==="string"&&!Aa.test(a)&&(c.support.leadingWhitespace||!$.test(a))&&!O[(za.exec(a)||["",""])[1].toLowerCase()]){a=a.replace(ya,"<$1>");try{for(var b=0,d=this.length;b0||e.cacheable||this.length>1?l.cloneNode(true):l)}k.length&&c.each(k,Ka)}return this}});c.buildFragment=function(a,b,d){var e,f,h;b=b&&b[0]?b[0].ownerDocument||b[0]:u;if(a.length===1&&typeof a[0]==="string"&&a[0].length<512&&b===u&&!Aa.test(a[0])&&(c.support.checkClone|| 120 | !Ba.test(a[0]))){f=true;if(h=c.fragments[a[0]])if(h!==1)e=h}if(!e){e=b.createDocumentFragment();c.clean(a,b,e,d)}if(f)c.fragments[a[0]]=h?e:1;return{fragment:e,cacheable:f}};c.fragments={};c.each({appendTo:"append",prependTo:"prepend",insertBefore:"before",insertAfter:"after",replaceAll:"replaceWith"},function(a,b){c.fn[a]=function(d){var e=[];d=c(d);var f=this.length===1&&this[0].parentNode;if(f&&f.nodeType===11&&f.childNodes.length===1&&d.length===1){d[b](this[0]);return this}else{f=0;for(var h= 121 | d.length;f0?this.clone(true):this).get();c(d[f])[b](k);e=e.concat(k)}return this.pushStack(e,a,d.selector)}}});c.extend({clean:function(a,b,d,e){b=b||u;if(typeof b.createElement==="undefined")b=b.ownerDocument||b[0]&&b[0].ownerDocument||u;for(var f=[],h=0,k;(k=a[h])!=null;h++){if(typeof k==="number")k+="";if(k){if(typeof k==="string"&&!bb.test(k))k=b.createTextNode(k);else if(typeof k==="string"){k=k.replace(ya,"<$1>");var l=(za.exec(k)||["",""])[1].toLowerCase(),n=O[l]||O._default, 122 | s=n[0],v=b.createElement("div");for(v.innerHTML=n[1]+k+n[2];s--;)v=v.lastChild;if(!c.support.tbody){s=ab.test(k);l=l==="table"&&!s?v.firstChild&&v.firstChild.childNodes:n[1]===""&&!s?v.childNodes:[];for(n=l.length-1;n>=0;--n)c.nodeName(l[n],"tbody")&&!l[n].childNodes.length&&l[n].parentNode.removeChild(l[n])}!c.support.leadingWhitespace&&$.test(k)&&v.insertBefore(b.createTextNode($.exec(k)[0]),v.firstChild);k=v.childNodes}if(k.nodeType)f.push(k);else f=c.merge(f,k)}}if(d)for(h=0;f[h];h++)if(e&& 123 | c.nodeName(f[h],"script")&&(!f[h].type||f[h].type.toLowerCase()==="text/javascript"))e.push(f[h].parentNode?f[h].parentNode.removeChild(f[h]):f[h]);else{f[h].nodeType===1&&f.splice.apply(f,[h+1,0].concat(c.makeArray(f[h].getElementsByTagName("script"))));d.appendChild(f[h])}return f},cleanData:function(a){for(var b,d,e=c.cache,f=c.event.special,h=c.support.deleteExpando,k=0,l;(l=a[k])!=null;k++)if(!(l.nodeName&&c.noData[l.nodeName.toLowerCase()]))if(d=l[c.expando]){if((b=e[d])&&b.events)for(var n in b.events)f[n]? 124 | c.event.remove(l,n):c.removeEvent(l,n,b.handle);if(h)delete l[c.expando];else l.removeAttribute&&l.removeAttribute(c.expando);delete e[d]}}});var Ca=/alpha\([^)]*\)/i,db=/opacity=([^)]*)/,eb=/-([a-z])/ig,fb=/([A-Z])/g,Da=/^-?\d+(?:px)?$/i,gb=/^-?\d/,hb={position:"absolute",visibility:"hidden",display:"block"},La=["Left","Right"],Ma=["Top","Bottom"],W,ib=u.defaultView&&u.defaultView.getComputedStyle,jb=function(a,b){return b.toUpperCase()};c.fn.css=function(a,b){if(arguments.length===2&&b===A)return this; 125 | return c.access(this,a,b,true,function(d,e,f){return f!==A?c.style(d,e,f):c.css(d,e)})};c.extend({cssHooks:{opacity:{get:function(a,b){if(b){var d=W(a,"opacity","opacity");return d===""?"1":d}else return a.style.opacity}}},cssNumber:{zIndex:true,fontWeight:true,opacity:true,zoom:true,lineHeight:true},cssProps:{"float":c.support.cssFloat?"cssFloat":"styleFloat"},style:function(a,b,d,e){if(!(!a||a.nodeType===3||a.nodeType===8||!a.style)){var f,h=c.camelCase(b),k=a.style,l=c.cssHooks[h];b=c.cssProps[h]|| 126 | h;if(d!==A){if(!(typeof d==="number"&&isNaN(d)||d==null)){if(typeof d==="number"&&!c.cssNumber[h])d+="px";if(!l||!("set"in l)||(d=l.set(a,d))!==A)try{k[b]=d}catch(n){}}}else{if(l&&"get"in l&&(f=l.get(a,false,e))!==A)return f;return k[b]}}},css:function(a,b,d){var e,f=c.camelCase(b),h=c.cssHooks[f];b=c.cssProps[f]||f;if(h&&"get"in h&&(e=h.get(a,true,d))!==A)return e;else if(W)return W(a,b,f)},swap:function(a,b,d){var e={},f;for(f in b){e[f]=a.style[f];a.style[f]=b[f]}d.call(a);for(f in b)a.style[f]= 127 | e[f]},camelCase:function(a){return a.replace(eb,jb)}});c.curCSS=c.css;c.each(["height","width"],function(a,b){c.cssHooks[b]={get:function(d,e,f){var h;if(e){if(d.offsetWidth!==0)h=ma(d,b,f);else c.swap(d,hb,function(){h=ma(d,b,f)});return h+"px"}},set:function(d,e){if(Da.test(e)){e=parseFloat(e);if(e>=0)return e+"px"}else return e}}});if(!c.support.opacity)c.cssHooks.opacity={get:function(a,b){return db.test((b&&a.currentStyle?a.currentStyle.filter:a.style.filter)||"")?parseFloat(RegExp.$1)/100+"": 128 | b?"1":""},set:function(a,b){var d=a.style;d.zoom=1;var e=c.isNaN(b)?"":"alpha(opacity="+b*100+")",f=d.filter||"";d.filter=Ca.test(f)?f.replace(Ca,e):d.filter+" "+e}};if(ib)W=function(a,b,d){var e;d=d.replace(fb,"-$1").toLowerCase();if(!(b=a.ownerDocument.defaultView))return A;if(b=b.getComputedStyle(a,null)){e=b.getPropertyValue(d);if(e===""&&!c.contains(a.ownerDocument.documentElement,a))e=c.style(a,d)}return e};else if(u.documentElement.currentStyle)W=function(a,b){var d,e,f=a.currentStyle&&a.currentStyle[b], 129 | h=a.style;if(!Da.test(f)&&gb.test(f)){d=h.left;e=a.runtimeStyle.left;a.runtimeStyle.left=a.currentStyle.left;h.left=b==="fontSize"?"1em":f||0;f=h.pixelLeft+"px";h.left=d;a.runtimeStyle.left=e}return f};if(c.expr&&c.expr.filters){c.expr.filters.hidden=function(a){var b=a.offsetHeight;return a.offsetWidth===0&&b===0||!c.support.reliableHiddenOffsets&&(a.style.display||c.css(a,"display"))==="none"};c.expr.filters.visible=function(a){return!c.expr.filters.hidden(a)}}var kb=c.now(),lb=/)<[^<]*)*<\/script>/gi, 130 | mb=/^(?:select|textarea)/i,nb=/^(?:color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,ob=/^(?:GET|HEAD|DELETE)$/,Na=/\[\]$/,T=/\=\?(&|$)/,ia=/\?/,pb=/([?&])_=[^&]*/,qb=/^(\w+:)?\/\/([^\/?#]+)/,rb=/%20/g,sb=/#.*$/,Ea=c.fn.load;c.fn.extend({load:function(a,b,d){if(typeof a!=="string"&&Ea)return Ea.apply(this,arguments);else if(!this.length)return this;var e=a.indexOf(" ");if(e>=0){var f=a.slice(e,a.length);a=a.slice(0,e)}e="GET";if(b)if(c.isFunction(b)){d= 131 | b;b=null}else if(typeof b==="object"){b=c.param(b,c.ajaxSettings.traditional);e="POST"}var h=this;c.ajax({url:a,type:e,dataType:"html",data:b,complete:function(k,l){if(l==="success"||l==="notmodified")h.html(f?c("
").append(k.responseText.replace(lb,"")).find(f):k.responseText);d&&h.each(d,[k.responseText,l,k])}});return this},serialize:function(){return c.param(this.serializeArray())},serializeArray:function(){return this.map(function(){return this.elements?c.makeArray(this.elements):this}).filter(function(){return this.name&& 132 | !this.disabled&&(this.checked||mb.test(this.nodeName)||nb.test(this.type))}).map(function(a,b){var d=c(this).val();return d==null?null:c.isArray(d)?c.map(d,function(e){return{name:b.name,value:e}}):{name:b.name,value:d}}).get()}});c.each("ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".split(" "),function(a,b){c.fn[b]=function(d){return this.bind(b,d)}});c.extend({get:function(a,b,d,e){if(c.isFunction(b)){e=e||d;d=b;b=null}return c.ajax({type:"GET",url:a,data:b,success:d,dataType:e})}, 133 | getScript:function(a,b){return c.get(a,null,b,"script")},getJSON:function(a,b,d){return c.get(a,b,d,"json")},post:function(a,b,d,e){if(c.isFunction(b)){e=e||d;d=b;b={}}return c.ajax({type:"POST",url:a,data:b,success:d,dataType:e})},ajaxSetup:function(a){c.extend(c.ajaxSettings,a)},ajaxSettings:{url:location.href,global:true,type:"GET",contentType:"application/x-www-form-urlencoded",processData:true,async:true,xhr:function(){return new E.XMLHttpRequest},accepts:{xml:"application/xml, text/xml",html:"text/html", 134 | script:"text/javascript, application/javascript",json:"application/json, text/javascript",text:"text/plain",_default:"*/*"}},ajax:function(a){var b=c.extend(true,{},c.ajaxSettings,a),d,e,f,h=b.type.toUpperCase(),k=ob.test(h);b.url=b.url.replace(sb,"");b.context=a&&a.context!=null?a.context:b;if(b.data&&b.processData&&typeof b.data!=="string")b.data=c.param(b.data,b.traditional);if(b.dataType==="jsonp"){if(h==="GET")T.test(b.url)||(b.url+=(ia.test(b.url)?"&":"?")+(b.jsonp||"callback")+"=?");else if(!b.data|| 135 | !T.test(b.data))b.data=(b.data?b.data+"&":"")+(b.jsonp||"callback")+"=?";b.dataType="json"}if(b.dataType==="json"&&(b.data&&T.test(b.data)||T.test(b.url))){d=b.jsonpCallback||"jsonp"+kb++;if(b.data)b.data=(b.data+"").replace(T,"="+d+"$1");b.url=b.url.replace(T,"="+d+"$1");b.dataType="script";var l=E[d];E[d]=function(m){f=m;c.handleSuccess(b,w,e,f);c.handleComplete(b,w,e,f);if(c.isFunction(l))l(m);else{E[d]=A;try{delete E[d]}catch(p){}}v&&v.removeChild(B)}}if(b.dataType==="script"&&b.cache===null)b.cache= 136 | false;if(b.cache===false&&h==="GET"){var n=c.now(),s=b.url.replace(pb,"$1_="+n);b.url=s+(s===b.url?(ia.test(b.url)?"&":"?")+"_="+n:"")}if(b.data&&h==="GET")b.url+=(ia.test(b.url)?"&":"?")+b.data;b.global&&c.active++===0&&c.event.trigger("ajaxStart");n=(n=qb.exec(b.url))&&(n[1]&&n[1]!==location.protocol||n[2]!==location.host);if(b.dataType==="script"&&h==="GET"&&n){var v=u.getElementsByTagName("head")[0]||u.documentElement,B=u.createElement("script");if(b.scriptCharset)B.charset=b.scriptCharset;B.src= 137 | b.url;if(!d){var D=false;B.onload=B.onreadystatechange=function(){if(!D&&(!this.readyState||this.readyState==="loaded"||this.readyState==="complete")){D=true;c.handleSuccess(b,w,e,f);c.handleComplete(b,w,e,f);B.onload=B.onreadystatechange=null;v&&B.parentNode&&v.removeChild(B)}}}v.insertBefore(B,v.firstChild);return A}var H=false,w=b.xhr();if(w){b.username?w.open(h,b.url,b.async,b.username,b.password):w.open(h,b.url,b.async);try{if(b.data!=null&&!k||a&&a.contentType)w.setRequestHeader("Content-Type", 138 | b.contentType);if(b.ifModified){c.lastModified[b.url]&&w.setRequestHeader("If-Modified-Since",c.lastModified[b.url]);c.etag[b.url]&&w.setRequestHeader("If-None-Match",c.etag[b.url])}n||w.setRequestHeader("X-Requested-With","XMLHttpRequest");w.setRequestHeader("Accept",b.dataType&&b.accepts[b.dataType]?b.accepts[b.dataType]+", */*; q=0.01":b.accepts._default)}catch(G){}if(b.beforeSend&&b.beforeSend.call(b.context,w,b)===false){b.global&&c.active--===1&&c.event.trigger("ajaxStop");w.abort();return false}b.global&& 139 | c.triggerGlobal(b,"ajaxSend",[w,b]);var M=w.onreadystatechange=function(m){if(!w||w.readyState===0||m==="abort"){H||c.handleComplete(b,w,e,f);H=true;if(w)w.onreadystatechange=c.noop}else if(!H&&w&&(w.readyState===4||m==="timeout")){H=true;w.onreadystatechange=c.noop;e=m==="timeout"?"timeout":!c.httpSuccess(w)?"error":b.ifModified&&c.httpNotModified(w,b.url)?"notmodified":"success";var p;if(e==="success")try{f=c.httpData(w,b.dataType,b)}catch(q){e="parsererror";p=q}if(e==="success"||e==="notmodified")d|| 140 | c.handleSuccess(b,w,e,f);else c.handleError(b,w,e,p);d||c.handleComplete(b,w,e,f);m==="timeout"&&w.abort();if(b.async)w=null}};try{var g=w.abort;w.abort=function(){w&&g.call&&g.call(w);M("abort")}}catch(j){}b.async&&b.timeout>0&&setTimeout(function(){w&&!H&&M("timeout")},b.timeout);try{w.send(k||b.data==null?null:b.data)}catch(o){c.handleError(b,w,null,o);c.handleComplete(b,w,e,f)}b.async||M();return w}},param:function(a,b){var d=[],e=function(h,k){k=c.isFunction(k)?k():k;d[d.length]=encodeURIComponent(h)+ 141 | "="+encodeURIComponent(k)};if(b===A)b=c.ajaxSettings.traditional;if(c.isArray(a)||a.jquery)c.each(a,function(){e(this.name,this.value)});else for(var f in a)ca(f,a[f],b,e);return d.join("&").replace(rb,"+")}});c.extend({active:0,lastModified:{},etag:{},handleError:function(a,b,d,e){a.error&&a.error.call(a.context,b,d,e);a.global&&c.triggerGlobal(a,"ajaxError",[b,a,e])},handleSuccess:function(a,b,d,e){a.success&&a.success.call(a.context,e,d,b);a.global&&c.triggerGlobal(a,"ajaxSuccess",[b,a])},handleComplete:function(a, 142 | b,d){a.complete&&a.complete.call(a.context,b,d);a.global&&c.triggerGlobal(a,"ajaxComplete",[b,a]);a.global&&c.active--===1&&c.event.trigger("ajaxStop")},triggerGlobal:function(a,b,d){(a.context&&a.context.url==null?c(a.context):c.event).trigger(b,d)},httpSuccess:function(a){try{return!a.status&&location.protocol==="file:"||a.status>=200&&a.status<300||a.status===304||a.status===1223}catch(b){}return false},httpNotModified:function(a,b){var d=a.getResponseHeader("Last-Modified"),e=a.getResponseHeader("Etag"); 143 | if(d)c.lastModified[b]=d;if(e)c.etag[b]=e;return a.status===304},httpData:function(a,b,d){var e=a.getResponseHeader("content-type")||"",f=b==="xml"||!b&&e.indexOf("xml")>=0;a=f?a.responseXML:a.responseText;f&&a.documentElement.nodeName==="parsererror"&&c.error("parsererror");if(d&&d.dataFilter)a=d.dataFilter(a,b);if(typeof a==="string")if(b==="json"||!b&&e.indexOf("json")>=0)a=c.parseJSON(a);else if(b==="script"||!b&&e.indexOf("javascript")>=0)c.globalEval(a);return a}});if(E.ActiveXObject)c.ajaxSettings.xhr= 144 | function(){if(E.location.protocol!=="file:")try{return new E.XMLHttpRequest}catch(a){}try{return new E.ActiveXObject("Microsoft.XMLHTTP")}catch(b){}};c.support.ajax=!!c.ajaxSettings.xhr();var da={},tb=/^(?:toggle|show|hide)$/,ub=/^([+\-]=)?([\d+.\-]+)(.*)$/,aa,na=[["height","marginTop","marginBottom","paddingTop","paddingBottom"],["width","marginLeft","marginRight","paddingLeft","paddingRight"],["opacity"]];c.fn.extend({show:function(a,b,d){if(a||a===0)return this.animate(S("show",3),a,b,d);else{a= 145 | 0;for(b=this.length;a=0;e--)if(d[e].elem===this){b&&d[e](true);d.splice(e,1)}});b||this.dequeue();return this}});c.each({slideDown:S("show",1),slideUp:S("hide",1),slideToggle:S("toggle",1),fadeIn:{opacity:"show"},fadeOut:{opacity:"hide"}},function(a,b){c.fn[a]=function(d,e,f){return this.animate(b, 150 | d,e,f)}});c.extend({speed:function(a,b,d){var e=a&&typeof a==="object"?c.extend({},a):{complete:d||!d&&b||c.isFunction(a)&&a,duration:a,easing:d&&b||b&&!c.isFunction(b)&&b};e.duration=c.fx.off?0:typeof e.duration==="number"?e.duration:e.duration in c.fx.speeds?c.fx.speeds[e.duration]:c.fx.speeds._default;e.old=e.complete;e.complete=function(){e.queue!==false&&c(this).dequeue();c.isFunction(e.old)&&e.old.call(this)};return e},easing:{linear:function(a,b,d,e){return d+e*a},swing:function(a,b,d,e){return(-Math.cos(a* 151 | Math.PI)/2+0.5)*e+d}},timers:[],fx:function(a,b,d){this.options=b;this.elem=a;this.prop=d;if(!b.orig)b.orig={}}});c.fx.prototype={update:function(){this.options.step&&this.options.step.call(this.elem,this.now,this);(c.fx.step[this.prop]||c.fx.step._default)(this)},cur:function(){if(this.elem[this.prop]!=null&&(!this.elem.style||this.elem.style[this.prop]==null))return this.elem[this.prop];var a=parseFloat(c.css(this.elem,this.prop));return a&&a>-1E4?a:0},custom:function(a,b,d){function e(h){return f.step(h)} 152 | this.startTime=c.now();this.start=a;this.end=b;this.unit=d||this.unit||"px";this.now=this.start;this.pos=this.state=0;var f=this;a=c.fx;e.elem=this.elem;if(e()&&c.timers.push(e)&&!aa)aa=setInterval(a.tick,a.interval)},show:function(){this.options.orig[this.prop]=c.style(this.elem,this.prop);this.options.show=true;this.custom(this.prop==="width"||this.prop==="height"?1:0,this.cur());c(this.elem).show()},hide:function(){this.options.orig[this.prop]=c.style(this.elem,this.prop);this.options.hide=true; 153 | this.custom(this.cur(),0)},step:function(a){var b=c.now(),d=true;if(a||b>=this.options.duration+this.startTime){this.now=this.end;this.pos=this.state=1;this.update();this.options.curAnim[this.prop]=true;for(var e in this.options.curAnim)if(this.options.curAnim[e]!==true)d=false;if(d){if(this.options.overflow!=null&&!c.support.shrinkWrapBlocks){var f=this.elem,h=this.options;c.each(["","X","Y"],function(l,n){f.style["overflow"+n]=h.overflow[l]})}this.options.hide&&c(this.elem).hide();if(this.options.hide|| 154 | this.options.show)for(var k in this.options.curAnim)c.style(this.elem,k,this.options.orig[k]);this.options.complete.call(this.elem)}return false}else{a=b-this.startTime;this.state=a/this.options.duration;b=this.options.easing||(c.easing.swing?"swing":"linear");this.pos=c.easing[this.options.specialEasing&&this.options.specialEasing[this.prop]||b](this.state,a,0,1,this.options.duration);this.now=this.start+(this.end-this.start)*this.pos;this.update()}return true}};c.extend(c.fx,{tick:function(){for(var a= 155 | c.timers,b=0;b-1;e={};var s={};if(n)s=f.position();k=n?s.top:parseInt(k,10)||0;l=n?s.left:parseInt(l,10)||0;if(c.isFunction(b))b=b.call(a,d,h);if(b.top!=null)e.top=b.top-h.top+k;if(b.left!=null)e.left=b.left-h.left+l;"using"in b?b.using.call(a, 163 | e):f.css(e)}};c.fn.extend({position:function(){if(!this[0])return null;var a=this[0],b=this.offsetParent(),d=this.offset(),e=Fa.test(b[0].nodeName)?{top:0,left:0}:b.offset();d.top-=parseFloat(c.css(a,"marginTop"))||0;d.left-=parseFloat(c.css(a,"marginLeft"))||0;e.top+=parseFloat(c.css(b[0],"borderTopWidth"))||0;e.left+=parseFloat(c.css(b[0],"borderLeftWidth"))||0;return{top:d.top-e.top,left:d.left-e.left}},offsetParent:function(){return this.map(function(){for(var a=this.offsetParent||u.body;a&&!Fa.test(a.nodeName)&& 164 | c.css(a,"position")==="static";)a=a.offsetParent;return a})}});c.each(["Left","Top"],function(a,b){var d="scroll"+b;c.fn[d]=function(e){var f=this[0],h;if(!f)return null;if(e!==A)return this.each(function(){if(h=ea(this))h.scrollTo(!a?e:c(h).scrollLeft(),a?e:c(h).scrollTop());else this[d]=e});else return(h=ea(f))?"pageXOffset"in h?h[a?"pageYOffset":"pageXOffset"]:c.support.boxModel&&h.document.documentElement[d]||h.document.body[d]:f[d]}});c.each(["Height","Width"],function(a,b){var d=b.toLowerCase(); 165 | c.fn["inner"+b]=function(){return this[0]?parseFloat(c.css(this[0],d,"padding")):null};c.fn["outer"+b]=function(e){return this[0]?parseFloat(c.css(this[0],d,e?"margin":"border")):null};c.fn[d]=function(e){var f=this[0];if(!f)return e==null?null:this;if(c.isFunction(e))return this.each(function(h){var k=c(this);k[d](e.call(this,h,k[d]()))});return c.isWindow(f)?f.document.compatMode==="CSS1Compat"&&f.document.documentElement["client"+b]||f.document.body["client"+b]:f.nodeType===9?Math.max(f.documentElement["client"+ 166 | b],f.body["scroll"+b],f.documentElement["scroll"+b],f.body["offset"+b],f.documentElement["offset"+b]):e===A?parseFloat(c.css(f,d)):this.css(d,typeof e==="string"?e:e+"px")}})})(window); 167 | --------------------------------------------------------------------------------