Class: FeatureSetting::FsFeature

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/feature_setting/orm/active_record/fs_feature.rb

Constant Summary collapse

FEATURES =
{
  test: false
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defined_featuresObject



66
67
68
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 66

def defined_features
  features.keys.map(&:to_s)
end

.disable!(key) ⇒ Object



59
60
61
62
63
64
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 59

def disable!(key)
  if features.has_key?(key.to_sym)
    record = self.where(key: key, klass: klass).first
    record.update_attributes(enabled: false)
  end
end

.enable!(key) ⇒ Object



52
53
54
55
56
57
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 52

def enable!(key)
  if features.has_key?(key.to_sym)
    record = self.where(key: key, klass: klass).first
    record.update_attributes(enabled: true)
  end
end

.featuresObject



18
19
20
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 18

def features
  self.new.features
end

.init_features!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 26

def init_features!
  features.each do |key, value|
    self.create_with(key: key, enabled: value, klass: klass).find_or_create_by(klass: klass, key: key)
    define_singleton_method("#{key}_enabled?") do
      record = self.where(key: key, klass: klass).first
      record.enabled
    end
    define_singleton_method("enable_#{key}!") do
      enable!(key)
    end
    define_singleton_method("disable_#{key}!") do
      disable!(key)
    end
  end
  remove_old_features!
end

.klassObject



22
23
24
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 22

def klass
  self.new.klass
end

.remove_old_features!Object



43
44
45
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 43

def remove_old_features!
  self.where(key: all_stored_features - defined_features).destroy_all
end

.reset_features!Object



47
48
49
50
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 47

def reset_features!
  self.where(klass: klass).destroy_all
  init_features!
end

Instance Method Details

#featuresObject



9
10
11
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 9

def features
  self.class::FEATURES
end

#klassObject



13
14
15
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 13

def klass
  self.class
end