Sha256: 0ba88717389f38ca6015e21d8485e6621805cb3e9ad9be97a3e3bbfae8bf1a07

Contents?: true

Size: 1.94 KB

Versions: 35

Compression:

Stored size: 1.94 KB

Contents

module Scrivito

class Revision < Struct.new(:id, :workspace, :base)
  def initialize(options)
    super(*options.values_at(:id, :workspace, :base))
  end

  def content_state_id
    base ? workspace.base_content_state_id : workspace.content_state_id
  end

  def content_state_node
    @node ||= Backend::ContentStateNode.new(content_state_id)
  end

  # returns a list of raw rest-api hashes, one for each given id
  def obj_mget_request(ids)
    internal_obj_mget(ids, [])
  end

  # performs given search-query, returns the search-hits as a list of obj-ids
  def obj_search_request(query)
    if base
      raise Scrivito::InternalError, "search not possible for base revision"
    end

    internal_obj_search(query, [])
  end

  def api_request(verb, path, payload = nil)
    CmsRestApi.public_send(verb, "revisions/#{id}#{path}", payload)
  end

  def base?
    !!base
  end

  private

  def internal_obj_search(query, result, continuation=nil, tentative=false)
    response = workspace.api_request(:get, "/objs/search",
      query: query,
      options: {include_deleted: true},
      size: 100,
      continuation: continuation,
      consistent_with: content_state_id
    )

    cur_result = response['results'].map { |obj_data| obj_data['id'] }
    result += cur_result
    continuation = response['continuation']
    tentative ||= !!response['tentative']

    if continuation
      internal_obj_search(query, result, continuation, tentative)
    else
      [result.uniq, tentative]
    end
  end

  def internal_obj_mget(ids, result)
    request_options = [:get, "/objs/mget", ids: ids, include_deleted: true]

    if base
      response = api_request(*request_options)
    else
      response = workspace.api_request(*request_options)
    end

    current_result = result + response["results"]

    if response["results"].size < ids.size
      internal_obj_mget(ids.drop(response["results"].size), current_result)
    else
      current_result
    end
  end
end

end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
scrivito_sdk-1.18.0 app/cms/scrivito/revision.rb
scrivito_sdk-1.18.0.rc1 app/cms/scrivito/revision.rb
scrivito_sdk-1.17.0 app/cms/scrivito/revision.rb
scrivito_sdk-1.17.0.rc3 app/cms/scrivito/revision.rb
scrivito_sdk-1.17.0.rc2 app/cms/scrivito/revision.rb
scrivito_sdk-1.17.0.rc1 app/cms/scrivito/revision.rb
scrivito_sdk-1.16.0 app/cms/scrivito/revision.rb
scrivito_sdk-1.16.0.rc2 app/cms/scrivito/revision.rb
scrivito_sdk-1.16.0.rc1 app/cms/scrivito/revision.rb
scrivito_sdk-1.15.0 app/cms/scrivito/revision.rb
scrivito_sdk-1.15.0.rc2 app/cms/scrivito/revision.rb
scrivito_sdk-1.15.0.rc1 app/cms/scrivito/revision.rb
scrivito_sdk-1.14.0 app/cms/scrivito/revision.rb
scrivito_sdk-1.14.0.rc3 app/cms/scrivito/revision.rb
scrivito_sdk-1.14.0.rc2 app/cms/scrivito/revision.rb
scrivito_sdk-1.14.0.rc1 app/cms/scrivito/revision.rb
scrivito_sdk-1.13.0 app/cms/scrivito/revision.rb
scrivito_sdk-1.13.0.rc2 app/cms/scrivito/revision.rb
scrivito_sdk-1.13.0.rc1 app/cms/scrivito/revision.rb
scrivito_sdk-1.12.0 app/cms/scrivito/revision.rb