features_toggle 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +37 -0
  4. data/app/assets/javascripts/feature_toggle/application.js +12 -0
  5. data/app/assets/javascripts/feature_toggle/features.js +15 -0
  6. data/app/assets/stylesheets/feature_toggle/application.css +13 -0
  7. data/app/assets/stylesheets/feature_toggle/features.css +124 -0
  8. data/app/controllers/feature_toggle/application_controller.rb +9 -0
  9. data/app/controllers/feature_toggle/features_controller.rb +17 -0
  10. data/app/helpers/feature_toggle/application_helper.rb +20 -0
  11. data/app/helpers/feature_toggle/features_helper.rb +7 -0
  12. data/app/models/feature_toggle/feature.rb +4 -0
  13. data/app/views/feature_toggle/features/_feature.html.erb +18 -0
  14. data/app/views/feature_toggle/features/index.html.erb +23 -0
  15. data/config/routes.rb +8 -0
  16. data/db/migrate/20160222162323_create_feature_toggle_features.rb +13 -0
  17. data/lib/feature_toggle.rb +31 -0
  18. data/lib/feature_toggle/engine.rb +15 -0
  19. data/lib/feature_toggle/syntax.rb +54 -0
  20. data/lib/feature_toggle/syntax/feature.rb +17 -0
  21. data/lib/feature_toggle/syntax/shared.rb +20 -0
  22. data/lib/feature_toggle/syntax/state.rb +17 -0
  23. data/lib/feature_toggle/version.rb +3 -0
  24. data/lib/tasks/feature_toggle_tasks.rake +4 -0
  25. data/test/controllers/feature_toggle/features_controller_test.rb +13 -0
  26. data/test/dummy/README.rdoc +28 -0
  27. data/test/dummy/Rakefile +6 -0
  28. data/test/dummy/app/assets/javascripts/application.js +13 -0
  29. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  30. data/test/dummy/app/controllers/application_controller.rb +5 -0
  31. data/test/dummy/app/helpers/application_helper.rb +2 -0
  32. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  33. data/test/dummy/bin/bundle +3 -0
  34. data/test/dummy/bin/rails +4 -0
  35. data/test/dummy/bin/rake +4 -0
  36. data/test/dummy/bin/setup +29 -0
  37. data/test/dummy/config.ru +4 -0
  38. data/test/dummy/config/application.rb +26 -0
  39. data/test/dummy/config/boot.rb +5 -0
  40. data/test/dummy/config/database.yml +25 -0
  41. data/test/dummy/config/environment.rb +5 -0
  42. data/test/dummy/config/environments/development.rb +41 -0
  43. data/test/dummy/config/environments/production.rb +79 -0
  44. data/test/dummy/config/environments/test.rb +42 -0
  45. data/test/dummy/config/initializers/assets.rb +11 -0
  46. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  47. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  48. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  49. data/test/dummy/config/initializers/inflections.rb +16 -0
  50. data/test/dummy/config/initializers/mime_types.rb +4 -0
  51. data/test/dummy/config/initializers/session_store.rb +3 -0
  52. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  53. data/test/dummy/config/locales/en.yml +23 -0
  54. data/test/dummy/config/routes.rb +4 -0
  55. data/test/dummy/config/secrets.yml +22 -0
  56. data/test/dummy/log/development.log +0 -0
  57. data/test/dummy/public/404.html +67 -0
  58. data/test/dummy/public/422.html +67 -0
  59. data/test/dummy/public/500.html +66 -0
  60. data/test/dummy/public/favicon.ico +0 -0
  61. data/test/feature_toggle_test.rb +7 -0
  62. data/test/fixtures/feature_toggle/features.yml +11 -0
  63. data/test/integration/navigation_test.rb +8 -0
  64. data/test/models/feature_toggle/feature_test.rb +9 -0
  65. data/test/test_helper.rb +21 -0
  66. metadata +178 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 001cdabf19c99f78ad7af9c3205aa8492cbf929d
4
+ data.tar.gz: b649f567270a24adcaf6d930b822bff815df907f
5
+ SHA512:
6
+ metadata.gz: b90910058d8ed7a7be54298af555237bdc38c247c37d63ee7a71a7df68ddc7178a8bda512f3af0cb840db8c47cf3129c7609d2d4bf5d8402b7bd5cd1489082b9
7
+ data.tar.gz: 4968878d44820f9eb8d98d497cc4de88c9ad51ea3a1b1a1209a637f7f2c49be6e28189cbd405b807afb177ef3b160bd9f31bfd1c16909750c09658c295942ebb
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Tien Le
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'FeatureToggle'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,12 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
@@ -0,0 +1,15 @@
1
+ $(function() {
2
+ $(document).on('click', '.feature-table .checkbox', function(e) {
3
+ var $el = $(e.target);
4
+
5
+ $.ajax({
6
+ type: "POST",
7
+ url: window.toggleFeaturePath,
8
+ data: {
9
+ "feature": $el.data('feature-name'),
10
+ "state": $el.data('state-name'),
11
+ "enable": $el.is(':checked')
12
+ }
13
+ });
14
+ });
15
+ });
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ */
@@ -0,0 +1,124 @@
1
+ .checkbox {
2
+ position: relative;
3
+ top: -0.375rem;
4
+ margin: 0 1rem 0 0;
5
+ cursor: pointer;
6
+ }
7
+ .checkbox:before {
8
+ -webkit-transition: all 0.3s ease-in-out;
9
+ -moz-transition: all 0.3s ease-in-out;
10
+ transition: all 0.3s ease-in-out;
11
+ content: "";
12
+ position: absolute;
13
+ left: 0;
14
+ top: -0.225rem;
15
+ z-index: 1;
16
+ width: 2rem;
17
+ height: 2rem;
18
+ border: 2px solid #e2e2e2;
19
+ }
20
+ .checkbox:checked:before {
21
+ -webkit-transform: rotate(-45deg);
22
+ -moz-transform: rotate(-45deg);
23
+ -ms-transform: rotate(-45deg);
24
+ -o-transform: rotate(-45deg);
25
+ transform: rotate(-45deg);
26
+ height: 1rem;
27
+ border-color: #009688;
28
+ border-top-style: none;
29
+ border-right-style: none;
30
+ }
31
+ .checkbox:after {
32
+ content: "";
33
+ position: absolute;
34
+ top: -0.225rem;
35
+ left: 0;
36
+ width: 2.0rem;
37
+ height: 2.0rem;
38
+ background: #fff;
39
+ cursor: pointer;
40
+ }
41
+
42
+ .button--round {
43
+ -webkit-transition: 0.3s background ease-in-out;
44
+ -moz-transition: 0.3s background ease-in-out;
45
+ transition: 0.3s background ease-in-out;
46
+ width: 2rem;
47
+ height: 2rem;
48
+ background: #5677fc;
49
+ border-radius: 50%;
50
+ box-shadow: 0 0.125rem 0.3125rem 0 rgba(0, 0, 0, 0.25);
51
+ color: #fff;
52
+ text-decoration: none;
53
+ text-align: center;
54
+ }
55
+ .button--round i {
56
+ font-size: 1rem;
57
+ line-height: 220%;
58
+ vertical-align: middle;
59
+ }
60
+ .button--round:hover {
61
+ background: #3b50ce;
62
+ }
63
+
64
+ .button--sticky {
65
+ position: fixed;
66
+ right: 2rem;
67
+ top: 16rem;
68
+ }
69
+
70
+ .content {
71
+ -webkit-animation-duration: 0.4s;
72
+ animation-duration: 0.4s;
73
+ -webkit-animation-fill-mode: both;
74
+ animation-fill-mode: both;
75
+ -webkit-animation-name: slideUp;
76
+ animation-name: slideUp;
77
+ -webkit-animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
78
+ animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
79
+ }
80
+
81
+ @-webkit-keyframes slideUp {
82
+ 0% {
83
+ -webkit-transform: translateY(6.25rem);
84
+ transform: translateY(6.25rem);
85
+ }
86
+ 100% {
87
+ -webkit-transform: translateY(0);
88
+ transform: translateY(0);
89
+ }
90
+ }
91
+ @keyframes slideUp {
92
+ 0% {
93
+ -webkit-transform: translateY(6.25rem);
94
+ transform: translateY(6.25rem);
95
+ }
96
+ 100% {
97
+ -webkit-transform: translateY(0);
98
+ transform: translateY(0);
99
+ }
100
+ }
101
+
102
+ .feature-table {
103
+ display: table;
104
+ border-collapse: collapse;
105
+
106
+ }
107
+ .feature-table p.desc {
108
+ font-size: small;
109
+ color: gray;
110
+ }
111
+ .feature-table-row { display: table-row; }
112
+ .feature-table-cell {
113
+ display: table-cell;
114
+ padding: 10px;
115
+ border: 1px solid #ddd;
116
+ -webkit-box-sizing: border-box;
117
+ -moz-box-sizing: border-box;
118
+ box-sizing: border-box;
119
+ }
120
+ .feature-table-cell-tl {
121
+ border-top: 0;
122
+ border-left: 0;
123
+ }
124
+
@@ -0,0 +1,9 @@
1
+ module FeatureToggle
2
+ class ApplicationController < ::ApplicationController
3
+ layout (FeatureToggle.configuration.layout || 'application')
4
+
5
+ if FeatureToggle.configuration.authentication_method
6
+ before_filter FeatureToggle.configuration.authentication_method
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ require_dependency "feature_toggle/application_controller"
2
+
3
+ module FeatureToggle
4
+ class FeaturesController < ApplicationController
5
+ def index
6
+ @features = Feature.all.to_a
7
+ end
8
+
9
+ def toggle
10
+ feature = Feature.where(feature: params[:feature], state: params[:state]).first_or_initialize
11
+ feature.enable = (params[:enable] == 'true')
12
+ feature.save
13
+
14
+ render nothing: true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module FeatureToggle
2
+ module ApplicationHelper
3
+ def method_missing(method, *args, &block)
4
+ if (method.to_s.end_with?('_path') || method.to_s.end_with?('_url')) && main_app.respond_to?(method)
5
+ main_app.send(method, *args)
6
+ else
7
+ super
8
+ end
9
+ end
10
+
11
+ def turn_on_feature?(feature, state)
12
+ @features.find do |f|
13
+ f.feature == feature.name.to_s &&
14
+ f.state == state.name.to_s &&
15
+ f.enable?
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ module FeatureToggle
2
+ module FeaturesHelper
3
+ def feature?(name)
4
+ FeatureToggle.on?(name, self)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module FeatureToggle
2
+ class Feature < ActiveRecord::Base
3
+ end
4
+ end
@@ -0,0 +1,18 @@
1
+ <div class='feature-table-row'>
2
+ <div class='feature-table-cell'>
3
+ <strong><%= feature.name %></strong>
4
+ <p class='desc'><%= feature.desc %></p>
5
+ </div>
6
+
7
+ <% FeatureToggle.states.each do |state| %>
8
+ <div class='feature-table-cell'>
9
+ <input type="checkbox" class="checkbox" data-feature-name="<%= feature.name %>" data-state-name="<%= state.name %>" value="1" <%= 'checked' if turn_on_feature?(feature, state) %>>&nbsp;</input>
10
+ </div>
11
+ <% end %>
12
+ </div>
13
+
14
+ <% if feature.childrens && feature.childrens.size > 0 %>
15
+ <% feature.childrens.each do |feature| %>
16
+ <%= render partial: 'feature', locals: { feature: feature } %>
17
+ <% end %>
18
+ <% end %>
@@ -0,0 +1,23 @@
1
+ <%= javascript_include_tag "feature_toggle/features" %>
2
+ <%= stylesheet_link_tag "feature_toggle/features" %>
3
+
4
+ <div class='feature-table'>
5
+ <div class='feature-table-row'>
6
+ <div class='feature-table-cell feature-table-cell-tl'>&nbsp;</div>
7
+
8
+ <% FeatureToggle.states.each do |state| %>
9
+ <div class='feature-table-cell'>
10
+ <strong><%= state.name %></strong>
11
+ <p class='desc'><%= state.desc %></p>
12
+ </div>
13
+ <% end %>
14
+ </div>
15
+
16
+ <% FeatureToggle.features.each do |feature| %>
17
+ <%= render partial: 'feature', locals: { feature: feature } %>
18
+ <% end %>
19
+ </div>
20
+
21
+ <script>
22
+ window.toggleFeaturePath = "<%= toggle_features_path %>";
23
+ </script>
data/config/routes.rb ADDED
@@ -0,0 +1,8 @@
1
+ FeatureToggle::Engine.routes.draw do
2
+ resources :features, only: [:index] do
3
+ collection do
4
+ post :toggle
5
+ end
6
+ end
7
+ root :to => "features#index"
8
+ end
@@ -0,0 +1,13 @@
1
+ class CreateFeatureToggleFeatures < ActiveRecord::Migration
2
+ def change
3
+ create_table :feature_toggle_features do |t|
4
+ t.string :feature
5
+ t.string :state
6
+ t.boolean :enable, default: false
7
+
8
+ t.timestamps null: false
9
+ end
10
+
11
+ add_index :feature_toggle_features, :feature
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ require "feature_toggle/engine"
2
+ require 'feature_toggle/syntax'
3
+ require 'ostruct'
4
+
5
+ module FeatureToggle
6
+ extend Syntax
7
+
8
+ def self.setup(&block)
9
+ yield configuration
10
+ end
11
+
12
+ def self.states
13
+ @states ||= []
14
+ end
15
+
16
+ def self.features
17
+ @features ||= []
18
+ end
19
+
20
+ def self.configuration
21
+ @config ||= OpenStruct.new
22
+ end
23
+
24
+ def self.on?(feature, state_context)
25
+ enable_state_names = Feature.where(feature: feature, enable: true).pluck(:state)
26
+ enable_states = states.select {|state| enable_state_names.include?(state.name.to_s) }
27
+ enable_states.any? do |state|
28
+ state_context.instance_eval &state.value
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ module FeatureToggle
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace FeatureToggle
4
+
5
+ initializer "feature_toggle", before: :load_config_initializers do |app|
6
+ config.paths["db/migrate"].expanded.each do |expanded_path|
7
+ Rails.application.config.paths["db/migrate"] << expanded_path
8
+ end
9
+ end
10
+
11
+ initializer :assets do |config|
12
+ Rails.application.config.assets.precompile += %w{ feature_toggle/features.css feature_toggle/features.js }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ require 'feature_toggle/syntax/feature'
2
+ require 'feature_toggle/syntax/state'
3
+
4
+ module FeatureToggle
5
+ module Syntax
6
+ def define(&block)
7
+ DSL.run(block)
8
+ end
9
+
10
+ class DSL
11
+ def self.run(block)
12
+ new.instance_eval(&block)
13
+ end
14
+
15
+ def feature(name, &block)
16
+ self.features << Feature.new(name, block)
17
+ end
18
+
19
+ def state(name, &block)
20
+ self.states << State.new(name, block)
21
+ end
22
+
23
+ protected
24
+
25
+ def states
26
+ FeatureToggle.states
27
+ end
28
+
29
+ def features
30
+ FeatureToggle.features
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ #FeatureToggle.define do
37
+ #feature :employee_file do
38
+ #desc"Access employee file"
39
+
40
+ #feature :create
41
+ #feature :delete
42
+ #end
43
+
44
+ #state :premium,
45
+ #desc "Premium user"
46
+ #value { current_user.paid? }
47
+ #end
48
+
49
+ #state :free do
50
+ #desc "Trial user"
51
+
52
+ #current_user.free?
53
+ #end
54
+ #end