Class: RailsSettings::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rails-settings/base.rb

Constant Summary collapse

PROTECTED_KEYS =
%w[var value]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.defined_fieldsObject (readonly)

Returns the value of attribute defined_fields.



82
83
84
# File 'lib/rails-settings/base.rb', line 82

def defined_fields
  @defined_fields
end

Class Method Details

._rails_scopeObject



43
# File 'lib/rails-settings/base.rb', line 43

alias_method :_rails_scope, :scope

.cache_keyObject



64
65
66
67
68
# File 'lib/rails-settings/base.rb', line 64

def cache_key
  key_parts = ["rails-settings-cached"]
  key_parts << @cache_prefix.call if @cache_prefix
  key_parts.join("/")
end

.cache_prefix(&block) ⇒ Object



60
61
62
# File 'lib/rails-settings/base.rb', line 60

def cache_prefix(&block)
  @cache_prefix = block
end

.clear_cacheObject



34
35
36
37
# File 'lib/rails-settings/base.rb', line 34

def clear_cache
  RequestCache.reset
  Rails.cache.delete(cache_key)
end

.editable_keysObject



74
75
76
# File 'lib/rails-settings/base.rb', line 74

def editable_keys
  @defined_fields.reject(&:readonly).map(&:key)
end

.field(key, **opts) ⇒ Object



39
40
41
# File 'lib/rails-settings/base.rb', line 39

def field(key, **opts)
  _define_field(key, **opts)
end

.get_field(key) ⇒ Object



56
57
58
# File 'lib/rails-settings/base.rb', line 56

def get_field(key)
  @defined_fields.find { |field| field.key == key.to_s }.to_h || {}
end

.keysObject



70
71
72
# File 'lib/rails-settings/base.rb', line 70

def keys
  @defined_fields.map(&:key)
end

.readonly_keysObject



78
79
80
# File 'lib/rails-settings/base.rb', line 78

def readonly_keys
  @defined_fields.select(&:readonly).map(&:key)
end

.scope(*args) {|block| ... } ⇒ Object

Yields:

  • (block)


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rails-settings/base.rb', line 44

def scope(*args, &block)
  name = args.shift
  body = args.shift
  if body.respond_to?(:call)
    return _rails_scope(name, body, &block)
  end

  @scope = name.to_sym
  yield block
  @scope = nil
end

Instance Method Details

#clear_cacheObject



29
30
31
# File 'lib/rails-settings/base.rb', line 29

def clear_cache
  self.class.clear_cache
end

#valueObject

get the value field, YAML decoded



15
16
17
18
19
20
21
22
# File 'lib/rails-settings/base.rb', line 15

def value
  # rubocop:disable Security/YAMLLoad
  payload = self[:value]

  if payload.present?
    YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(payload) : YAML.load(payload)
  end
end

#value=(new_value) ⇒ Object

set the value field, YAML encoded



25
26
27
# File 'lib/rails-settings/base.rb', line 25

def value=(new_value)
  self[:value] = new_value.to_yaml
end