Class: RailsSettings::Base
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- RailsSettings::Base
- Defined in:
- lib/rails-settings/base.rb
Constant Summary collapse
- PROTECTED_KEYS =
%w[var value]
Class Attribute Summary collapse
-
.defined_fields ⇒ Object
readonly
Returns the value of attribute defined_fields.
Class Method Summary collapse
- ._rails_scope ⇒ Object
- .cache_key ⇒ Object
- .cache_prefix(&block) ⇒ Object
- .clear_cache ⇒ Object
- .editable_keys ⇒ Object
- .field(key, **opts) ⇒ Object
- .get_field(key) ⇒ Object
- .keys ⇒ Object
- .readonly_keys ⇒ Object
- .scope(*args) {|block| ... } ⇒ Object
Instance Method Summary collapse
- #clear_cache ⇒ Object
-
#value ⇒ Object
get the value field, YAML decoded.
-
#value=(new_value) ⇒ Object
set the value field, YAML encoded.
Class Attribute Details
.defined_fields ⇒ Object (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_scope ⇒ Object
43 |
# File 'lib/rails-settings/base.rb', line 43 alias_method :_rails_scope, :scope |
.cache_key ⇒ Object
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_cache ⇒ Object
34 35 36 37 |
# File 'lib/rails-settings/base.rb', line 34 def clear_cache RequestCache.reset Rails.cache.delete(cache_key) end |
.editable_keys ⇒ Object
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 |
.keys ⇒ Object
70 71 72 |
# File 'lib/rails-settings/base.rb', line 70 def keys @defined_fields.map(&:key) end |
.readonly_keys ⇒ Object
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
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_cache ⇒ Object
29 30 31 |
# File 'lib/rails-settings/base.rb', line 29 def clear_cache self.class.clear_cache end |
#value ⇒ Object
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 |