Method: SuperSettings::RestAPI.history

Defined in:
lib/super_settings/rest_api.rb

.history(key, limit: nil, offset: 0) ⇒ Hash?

Return the history of the setting.

Examples:

GET /setting/history

Query parameters

* key - setting key
* limit - number of history items to return
* offset - index to start fetching items from (most recent items are first)

The response format is:
{
  key: string,
  histories: [
    {
      value: object,
      changed_by: string,
      created_at: iso8601 string
    },
    ...
  ],
  previous_page_params: hash,
  next_page_params: hash
}

Parameters:

  • key (String)

    setting key

  • limit (Integer) (defaults to: nil)

    number of history items to return

  • offset (Integer) (defaults to: 0)

    index to start fetching items from (most recent items are first)

Returns:

  • (Hash, nil)

    history hash or nil if setting not found



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