# Flipper ActiveRecord An [ActiveRecord](https://github.com/rails/rails/tree/master/activerecord) adapter for [Flipper](https://github.com/jnunemaker/flipper). Currently, this targets Rails 4.2 because it uses real foreign keys via `add_foreign_key` and `remove_foreign_key`. For users requiring Rails 3.2 support, please see: [Flipper ActiveRecord 3.2](https://github.com/blueboxjesse/flipper-activerecord). ## Installation Add this line to your application's Gemfile: gem 'flipper-activerecord' And then execute: $ bundle Or install it yourself with: $ gem install flipper-activerecord Generate a migration to create the required database tables: $ rails g flipper:active_record ## Usage ```ruby require 'flipper/adapters/activerecord' adapter = Flipper::Adapters::ActiveRecord.new flipper = Flipper.new(adapter) # profit... ``` ## Internals Two database tables are used: `flipper_features` and `flipper_gates`. A list of all available features is stored in the `flipper_features` table: ```ruby require 'flipper/adapters/activerecord' adapter = Flipper::Adapters::ActiveRecord.new flipper = Flipper.new(adapter) # Register a few groups. Flipper.register(:admins) { |thing| thing.admin? } Flipper.register(:early_access) { |thing| thing.early_access? } # Create a user class that has flipper_id instance method. User = Struct.new(:flipper_id) flipper[:stats].enable flipper[:stats].enable flipper.group(:admins) flipper[:stats].enable User.new('25') flipper[:stats].enable User.new('90') flipper[:stats].enable User.new('180') flipper[:stats].enable flipper.random(15) flipper[:stats].enable flipper.actors(45) flipper[:awesomeness].enable flipper[:awesomeness].enable flipper.group(:admins) print 'all features: ' pp Flipper::ActiveRecord::Feature.all # Flipper::ActiveRecord::Feature Load (0.3ms) SELECT "flipper_features".* FROM "flipper_features" # [#, # #] print 'all gates: ' pp Flipper::ActiveRecord::Gate.all # Flipper::ActiveRecord::Gate Load (0.4ms) SELECT "flipper_gates".* FROM "flipper_gates" # [#, # #, # #, # #, # #, # #, # #, # #, # #] puts 'flipper get of feature' pp adapter.get(flipper[:stats]) # flipper get of feature # SQL (0.6ms) SELECT DISTINCT "flipper_features"."id" FROM "flipper_features" LEFT OUTER JOIN "flipper_gates" ON "flipper_gates"."flipper_feature_id" = "flipper_features"."id" WHERE "flipper_features"."name" = $1 LIMIT 1 [["name", "stats"]] # SQL (0.3ms) SELECT "flipper_features"."id" AS t0_r0, "flipper_features"."name" AS t0_r1, "flipper_features"."created_at" AS t0_r2, "flipper_features"."updated_at" AS t0_r3, "flipper_gates"."id" AS t1_r0, "flipper_gates"."flipper_feature_id" AS t1_r1, "flipper_gates"."name" AS t1_r2, "flipper_gates"."value" AS t1_r3, "flipper_gates"."created_at" AS t1_r4, "flipper_gates"."updated_at" AS t1_r5 FROM "flipper_features" LEFT OUTER JOIN "flipper_gates" ON "flipper_gates"."flipper_feature_id" = "flipper_features"."id" WHERE "flipper_features"."name" = $1 AND "flipper_features"."id" IN (1) [["name", "stats"]] # {:boolean=>"true", # :groups=>#, # :actors=>#, # :percentage_of_actors=>"45", # :percentage_of_random=>"15"} ``` ## Testing ``` dbcreate flipper_activerecord_test rspec ``` ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Added some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request ## Thanks Thanks to John Nunemaker for making Flipper and its adapters!