lib/scrivito/workspace.rb in scrivito_sdk-1.1.1 vs lib/scrivito/workspace.rb in scrivito_sdk-1.2.0.rc1

- old
+ new

@@ -17,25 +17,26 @@ # Set the workspace to use for subsequent workspace operations. # @api public # @param [Scrivito::Workspace] workspace def self.current=(workspace) - @current = workspace + Thread.current[:scrivito_current_workspace] = workspace end def self.current_using_proc=(workspace_proc) - @current = workspace_proc + Thread.current[:scrivito_current_workspace] = workspace_proc end # Returns the currently used workspace. # @api public # @return [Scrivito::Workspace] def self.current - if @current.respond_to? :call - @current = @current.call + workspace = Thread.current[:scrivito_current_workspace] + if workspace.respond_to?(:call) + Thread.current[:scrivito_current_workspace] = workspace.call else - @current ||= published + Thread.current[:scrivito_current_workspace] ||= published end end # Returns all workspaces. # @api public @@ -55,15 +56,15 @@ def self.published find("published") end def self.published_with_fallback - cached_workspace_data = CmsBackend.instance.find_workspace_data_from_cache('published') + cached_workspace_data = CmsBackend.find_workspace_data_from_cache('published') if cached_workspace_data workspace_data = begin - CmsBackend.instance.find_workspace_data_by_id('published', 0.5) + CmsBackend.find_workspace_data_by_id('published', 0.5) rescue => e warn_backend_not_available(e.message) cached_workspace_data end @@ -78,11 +79,11 @@ # @param [String] id # @return [Scrivito::Workspace] # @raise [Scrivito::ResourceNotFound] def self.find(id) cache.fetch(id) do - workspace_data = CmsBackend.instance.find_workspace_data_by_id(id) + workspace_data = CmsBackend.find_workspace_data_by_id(id) from_workspace_data(id, workspace_data) end end @@ -95,37 +96,35 @@ def self.find_by_title(title) all.detect { |workspace| workspace.title == title } end # - # Find a workspace by its id or title and set it as the currently used workspace. + # Find a workspace by its title or ID and set it as the currently used workspace. # # @api public # - # @param [String] id_or_title id or title of the workspace + # @param [String] title_or_id title or id of the workspace # @raise [Scrivito::ResourceNotFound] - # @return [void] + # @note This method is intended to be used in the Rails console. Please avoid using it in + # application code. # # @example - # Scrivito::Workspace.use("6a75fe694eeeb093") - # Scrivito::Workspace.current.id - # # => "6a75fe694eeeb093" - # # Scrivito::Workspace.use("my working copy") # Scrivito::Workspace.current.title # # => "my working copy" # - # # raises Scrivito::ResourceNotFound: + # Scrivito::Workspace.use("6a75fe694eeeb093") + # Scrivito::Workspace.current.id + # # => "6a75fe694eeeb093" + # + # # Raises Scrivito::ResourceNotFound: # Scrivito::Workspace.use("missing") # - def self.use(id_or_title) - self.current = if id_or_title =~ /^[a-z0-9]{16}$/ - find(id_or_title) - else - find_by_title(id_or_title) or - raise ResourceNotFound, "Could not find #{self} with title #{id_or_title}" - end + def self.use(title_or_id) + self.current = find_by_title(title_or_id) || find(title_or_id) + rescue ResourceNotFound + raise ResourceNotFound, %{Could not find #{self} with title or ID "#{title_or_id}"} end delegate :content_state_id, :base_content_state_id, :content_state, :base_revision_id, :base_content_state, to: :data @@ -194,15 +193,15 @@ reload if [:post, :put, :delete].include?(verb) response end def create_obj(attributes) - CmsBackend.instance.create_obj(id, attributes).tap { reload } + CmsBackend.create_obj(id, attributes).tap { reload } end def update_obj(obj_id, attributes) - CmsBackend.instance.update_obj(id, obj_id, attributes).tap { reload } + CmsBackend.update_obj(id, obj_id, attributes).tap { reload } end def task_unaware_api_request(verb, path, payload = nil) CmsRestApi.task_unaware_request(verb, "#{backend_url}#{path}", payload) end @@ -374,10 +373,10 @@ @revision = nil @cache_key = nil end def fetch_workspace_data - CmsBackend.instance.find_workspace_data_by_id(id) + CmsBackend.find_workspace_data_by_id(id) end def backend_url "/workspaces/#{id}" end