Commit 6588d27

mo <mo.khan@gmail.com>
2018-09-23 23:52:32
install flipper.
1 parent 1e59918
app/models/concerns/flippable.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+module Flippable
+  extend ActiveSupport::Concern
+
+  def flipper_id
+    "#{self.class.name};#{to_param}"
+  end
+
+  def enable_feature(feature)
+    Flipper.enable_actor(feature.to_sym, self) unless feature_enabled?(feature)
+  end
+
+  def disable_feature(feature)
+    Flipper.disable_actor(feature.to_sym, self)
+  end
+
+  def feature_enabled?(feature)
+    Flipper.enabled?(feature.to_sym, self)
+  end
+end
app/models/application_record.rb
@@ -1,5 +1,6 @@
 # frozen_string_literal: true
 
 class ApplicationRecord < ActiveRecord::Base
+  include Flippable
   self.abstract_class = true
 end
app/views/my/audits/index.html.erb
@@ -6,6 +6,7 @@
         <thead>
           <th>Action</th>
           <th>Auditable</th>
+          <th>Changes</th>
           <th>Associated</th>
           <th>User</th>
           <th>Version</th>
@@ -16,9 +17,10 @@
         <% @audits.each do |audit| %>
           <tr>
             <td><%= audit.action %></td>
-            <td><%= audit.auditable %></td>
-            <td><%= audit.associated %></td>
-            <td><%= audit.user %></td>
+            <td><%= audit.auditable.class if audit.auditable %></td>
+            <td><%= audit.audited_changes %></td>
+            <td><%= audit.associated&.flipper_id %></td>
+            <td><%= audit.user&.email %></td>
             <td><%= audit.version %></td>
             <td><%= audit.remote_address %></td>
             <td><%= local_time_ago(audit.created_at) %></td>
config/initializers/flipper.rb
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+
+require 'flipper'
+require 'flipper/adapters/active_record'
+
+Flipper.configure do |config|
+  config.default do
+    Flipper.new(Flipper::Adapters::ActiveRecord.new)
+  end
+end
db/migrate/20180923234502_create_flipper_tables.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class CreateFlipperTables < ActiveRecord::Migration[5.2]
+  def self.up
+    create_table :flipper_features do |t|
+      t.string :key, null: false
+      t.timestamps null: false
+    end
+    add_index :flipper_features, :key, unique: true
+
+    create_table :flipper_gates do |t|
+      t.string :feature_key, null: false
+      t.string :key, null: false
+      t.string :value
+      t.timestamps null: false
+    end
+    add_index :flipper_gates, [:feature_key, :key, :value], unique: true
+  end
+
+  def self.down
+    drop_table :flipper_gates
+    drop_table :flipper_features
+  end
+end
db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2018_09_23_222720) do
+ActiveRecord::Schema.define(version: 2018_09_23_234502) do
 
   create_table "audits", force: :cascade do |t|
     t.integer "auditable_id"
@@ -57,6 +57,22 @@ ActiveRecord::Schema.define(version: 2018_09_23_222720) do
     t.index ["uuid"], name: "index_clients_on_uuid"
   end
 
+  create_table "flipper_features", force: :cascade do |t|
+    t.string "key", null: false
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+    t.index ["key"], name: "index_flipper_features_on_key", unique: true
+  end
+
+  create_table "flipper_gates", force: :cascade do |t|
+    t.string "feature_key", null: false
+    t.string "key", null: false
+    t.string "value"
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+    t.index ["feature_key", "key", "value"], name: "index_flipper_gates_on_feature_key_and_key_and_value", unique: true
+  end
+
   create_table "sessions", force: :cascade do |t|
     t.string "session_id", null: false
     t.text "data"
Gemfile
@@ -11,6 +11,8 @@ gem 'bootsnap', '>= 1.1.0', require: false
 gem 'coffee-rails', '~> 4.2'
 gem 'dotenv', '~> 2.5'
 gem 'email_validator', '~> 1.6'
+gem 'flipper', '~> 0.16'
+gem 'flipper-active_record', '~> 0.16'
 gem 'foreman', '~> 0.85'
 gem 'jbuilder', '~> 2.5'
 gem 'jwt', '~> 2.1'
Gemfile.lock
@@ -100,6 +100,10 @@ GEM
       railties (>= 3.0.0)
     ffaker (2.10.0)
     ffi (1.9.25)
+    flipper (0.16.0)
+    flipper-active_record (0.16.0)
+      activerecord (>= 3.2, < 6)
+      flipper (~> 0.16.0)
     foreman (0.85.0)
       thor (~> 0.19.1)
     globalid (0.4.1)
@@ -310,6 +314,8 @@ DEPENDENCIES
   email_validator (~> 1.6)
   factory_bot_rails (~> 4.11)
   ffaker (~> 2.10)
+  flipper (~> 0.16)
+  flipper-active_record (~> 0.16)
   foreman (~> 0.85)
   i18n-tasks (~> 0.9.24)
   jbuilder (~> 2.5)