Class: Flipper::Adapters::ActiveRecord

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

Defined Under Namespace

Classes: Feature, Gate

Instance Attribute Summary collapse

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.



35
36
37
38
39
# File 'lib/flipper/adapters/active_record.rb', line 35

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

Instance Attribute Details

#nameObject (readonly)

Public: The name of the adapter.



21
22
23
# File 'lib/flipper/adapters/active_record.rb', line 21

def name
  @name
end

Instance Method Details

#add(feature) ⇒ Object

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



47
48
49
50
51
52
53
# File 'lib/flipper/adapters/active_record.rb', line 47

def add(feature)
  attributes = {key: feature.key}
  # race condition, but add is only used by enable/disable which happen
  # super rarely, so it shouldn't matter in practice
  @feature_class.where(attributes).first || @feature_class.create!(attributes)
  true
end

#clear(feature) ⇒ Object

Public: Clears the gate values for a feature.



65
66
67
68
# File 'lib/flipper/adapters/active_record.rb', line 65

def clear(feature)
  @gate_class.delete_all(feature_key: feature.key)
  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.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/flipper/adapters/active_record.rb', line 133

def disable(feature, gate, thing)
  case gate.data_type
  when :boolean
    clear(feature)
  when :integer
    @gate_class.create!({
      feature_key: feature.key,
      key: gate.key,
      value: thing.value.to_s,
    })
  when :set
    @gate_class.delete_all(feature_key: feature.key, key: gate.key, value: thing.value)
  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 disable. thing - The Flipper::Type being disabled for the gate.

Returns true.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/flipper/adapters/active_record.rb', line 105

def enable(feature, gate, thing)
  case gate.data_type
  when :boolean, :integer
    @gate_class.create!({
      feature_key: feature.key,
      key: gate.key,
      value: thing.value.to_s,
    })
  when :set
    @gate_class.create!({
      feature_key: feature.key,
      key: gate.key,
      value: thing.value.to_s,
    })
  else
    unsupported_data_type gate.data_type
  end

  true
end

#featuresObject

Public: The set of known features.



42
43
44
# File 'lib/flipper/adapters/active_record.rb', line 42

def features
  @feature_class.select(:key).map(&: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.



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

def get(feature)
  result = {}

  db_gates = @gate_class.where(feature_key: feature.key)

  feature.gates.each do |gate|
    result[gate.key] = case gate.data_type
    when :boolean
      if db_gate = db_gates.detect { |db_gate| db_gate.key == gate.key.to_s }
        db_gate.value
      end
    when :integer
      if db_gate = db_gates.detect { |db_gate| db_gate.key == gate.key.to_s }
        db_gate.value
      end
    when :set
      db_gates.select { |db_gate| db_gate.key == gate.key.to_s }.map(&:value).to_set
    else
      unsupported_data_type gate.data_type
    end
  end

  result
end

#remove(feature) ⇒ Object

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



56
57
58
59
60
61
62
# File 'lib/flipper/adapters/active_record.rb', line 56

def remove(feature)
  @feature_class.transaction do
    @feature_class.delete_all(key: feature.key)
    clear(feature)
  end
  true
end

#unsupported_data_type(data_type) ⇒ Object

Private



153
154
155
# File 'lib/flipper/adapters/active_record.rb', line 153

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