Class: SuperSettings::RestAPI
- Inherits:
-
Object
- Object
- SuperSettings::RestAPI
- Defined in:
- lib/super_settings/rest_api.rb
Class Method Summary collapse
-
.history(key, limit: nil, offset: 0) ⇒ Hash?
Return the history of the setting.
-
.index ⇒ Hash
Get all settings sorted by key.
-
.last_updated_at ⇒ Hash
Return the timestamp of the most recently updated setting.
-
.show(key) ⇒ Hash?
Get a setting by id.
-
.update(settings_params, changed_by = nil) ⇒ Hash
The update operation uses a transaction to atomically update all settings.
-
.updated_since(time) ⇒ Hash
Return settings that have been updated since a specified timestamp.
Class Method Details
.history(key, limit: nil, offset: 0) ⇒ Hash?
Return the history of the setting.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/super_settings/rest_api.rb', line 138 def history(key, limit: nil, offset: 0) setting = Setting.find_by_key(key) return nil unless setting offset = [offset.to_i, 0].max limit = limit.to_i fetch_limit = ((limit > 0) ? limit + 1 : nil) histories = setting.history(limit: fetch_limit, offset: offset) payload = {key: setting.key} if limit > 0 && !histories.empty? if offset > 0 previous_page_params = {key: setting.key, offset: [offset - limit, 0].max, limit: limit} end if histories.size > limit histories = histories.take(limit) next_page_params = {key: setting.key, offset: offset + limit, limit: limit} end payload[:previous_page_params] = previous_page_params if previous_page_params payload[:next_page_params] = next_page_params if next_page_params end payload[:histories] = histories.collect do |history| history_values = {value: history.value, changed_by: history.changed_by_display, created_at: history.created_at.utc.iso8601(6)} history_values[:deleted] = true if history.deleted? history_values end payload end |
.index ⇒ Hash
Get all settings sorted by key. This endpoint may be called with a REST GET request.
27 28 29 30 |
# File 'lib/super_settings/rest_api.rb', line 27 def index settings = Setting.active.reject(&:deleted?).sort_by(&:key) {settings: settings.collect(&:as_json)} end |
.last_updated_at ⇒ Hash
Return the timestamp of the most recently updated setting.
181 182 183 |
# File 'lib/super_settings/rest_api.rb', line 181 def last_updated_at {last_updated_at: Setting.last_updated_at.utc.iso8601(6)} end |
.show(key) ⇒ Hash?
Get a setting by id.
53 54 55 56 |
# File 'lib/super_settings/rest_api.rb', line 53 def show(key) setting = Setting.find_by_key(key) setting.as_json if setting && !setting.deleted? end |
.update(settings_params, changed_by = nil) ⇒ Hash
The update operation uses a transaction to atomically update all settings.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/super_settings/rest_api.rb', line 93 def update(settings_params, changed_by = nil) all_valid, settings = Setting.bulk_update(Array(settings_params), changed_by) if all_valid {success: true} else errors = {} settings.each do |setting| if setting.errors.any? errors[setting.key] = setting.errors.values.flatten end end {success: false, errors: errors} end end |
.updated_since(time) ⇒ Hash
Return settings that have been updated since a specified timestamp.
209 210 211 212 213 |
# File 'lib/super_settings/rest_api.rb', line 209 def updated_since(time) time = Coerce.time(time) settings = Setting.updated_since(time).reject(&:deleted?) {settings: settings.collect(&:as_json)} end |