Class: Flipper::Adapters::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Flipper::Adapter
Defined in:
lib/flipper/adapters/active_record.rb

Constant Summary collapse

VALUE_TO_TEXT_WARNING =
<<-EOS
  Your database needs migrated to use the latest Flipper features.
  Run `rails generate flipper:update` and `rails db:migrate`.
EOS

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ActiveRecord

Public: Initialize a new ActiveRecord adapter instance.

name - The Symbol name for this adapter. Optional (default :active_record) feature_class - The AR class responsible for the features table. gate_class - The AR class responsible for the gates table.

Allowing the overriding of name is so you can differentiate multiple instances of this adapter from each other, if, for some reason, that is a thing you do.

Allowing the overriding of the default feature/gate classes means you can roll your own tables and what not, if you so desire.



60
61
62
63
64
65
66
# File 'lib/flipper/adapters/active_record.rb', line 60

def initialize(options = {})
  @name = options.fetch(:name, :active_record)
  @feature_class = options.fetch(:feature_class) { Feature }
  @gate_class = options.fetch(:gate_class) { Gate }

  warn VALUE_TO_TEXT_WARNING if value_not_text?
end

Instance Method Details

#add(feature) ⇒ Object

Public: Adds a feature to the set of known features.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/flipper/adapters/active_record.rb', line 74

def add(feature)
  with_connection(@feature_class) do
    @feature_class.transaction(requires_new: true) do
      begin
        # race condition, but add is only used by enable/disable which happen
        # super rarely, so it shouldn't matter in practice
        unless @feature_class.where(key: feature.key).exists?
          @feature_class.create!(key: feature.key)
        end
      rescue ::ActiveRecord::RecordNotUnique
        # already added
      end
    end
  end

  true
end

#clear(feature) ⇒ Object

Public: Clears the gate values for a feature.



104
105
106
107
# File 'lib/flipper/adapters/active_record.rb', line 104

def clear(feature)
  with_connection(@gate_class) { @gate_class.where(feature_key: feature.key).destroy_all }
  true
end

#disable(feature, gate, thing) ⇒ Object

Public: Disables a gate for a given thing.

feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.

Returns true.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/flipper/adapters/active_record.rb', line 192

def disable(feature, gate, thing)
  case gate.data_type
  when :boolean
    clear(feature)
  when :integer
    set(feature, gate, thing)
  when :json
    delete(feature, gate)
  when :set
    with_connection(@gate_class) do
      @gate_class.where(feature_key: feature.key, key: gate.key, value: thing.value).destroy_all
    end
  else
    unsupported_data_type gate.data_type
  end

  true
end

#enable(feature, gate, thing) ⇒ Object

Public: Enables a gate for a given thing.

feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to enable. thing - The Flipper::Type being enabled for the gate.

Returns true.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/flipper/adapters/active_record.rb', line 168

def enable(feature, gate, thing)
  case gate.data_type
  when :boolean
    set(feature, gate, thing, clear: true)
  when :integer
    set(feature, gate, thing)
  when :json
    set(feature, gate, thing, json: true)
  when :set
    enable_multi(feature, gate, thing)
  else
    unsupported_data_type gate.data_type
  end

  true
end

#featuresObject

Public: The set of known features.



69
70
71
# File 'lib/flipper/adapters/active_record.rb', line 69

def features
  with_connection(@feature_class) { @feature_class.distinct.pluck(:key).to_set }
end

#get(feature) ⇒ Object

Public: Gets the values for all gates for a given feature.

Returns a Hash of Flipper::Gate#key => value.



112
113
114
115
# File 'lib/flipper/adapters/active_record.rb', line 112

def get(feature)
  gates = with_connection(@gate_class) { @gate_class.where(feature_key: feature.key).pluck(:key, :value) }
  result_for_gates(feature, gates)
end

#get_allObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/flipper/adapters/active_record.rb', line 134

def get_all
  with_connection(@feature_class) do |connection|
    # query the gates from the db in a single query
    features = ::Arel::Table.new(@feature_class.table_name.to_sym)
    gates = ::Arel::Table.new(@gate_class.table_name.to_sym)
    rows_query = features.join(gates, ::Arel::Nodes::OuterJoin)
      .on(features[:key].eq(gates[:feature_key]))
      .project(features[:key].as('feature_key'), gates[:key], gates[:value])
    gates = connection.select_rows(rows_query)

    # group the gates by feature key
    grouped_gates = gates.inject({}) do |hash, (feature_key, key, value)|
      hash[feature_key] ||= []
      hash[feature_key] << [key, value]
      hash
    end

    # build up the result hash
    result = Hash.new { |hash, key| hash[key] = default_config }
    features = grouped_gates.keys.map { |key| Flipper::Feature.new(key, self) }
    features.each do |feature|
      result[feature.key] = result_for_gates(feature, grouped_gates[feature.key])
    end
    result
  end
end

#get_multi(features) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/flipper/adapters/active_record.rb', line 117

def get_multi(features)
  with_connection(@gate_class) do
    gates = @gate_class.where(feature_key: features.map(&:key)).pluck(:feature_key, :key, :value)
    grouped_gates = gates.inject({}) do |hash, (feature_key, key, value)|
      hash[feature_key] ||= []
      hash[feature_key] << [key, value]
      hash
    end

    result = {}
    features.each do |feature|
      result[feature.key] = result_for_gates(feature, grouped_gates[feature.key])
    end
    result
  end
end

#remove(feature) ⇒ Object

Public: Removes a feature from the set of known features.



93
94
95
96
97
98
99
100
101
# File 'lib/flipper/adapters/active_record.rb', line 93

def remove(feature)
  with_connection(@feature_class) do
    @feature_class.transaction do
      @feature_class.where(key: feature.key).destroy_all
      clear(feature)
    end
  end
  true
end

#unsupported_data_type(data_type) ⇒ Object

Private



212
213
214
# File 'lib/flipper/adapters/active_record.rb', line 212

def unsupported_data_type(data_type)
  raise "#{data_type} is not supported by this adapter"
end