module RailsConnector # @api public module Migrations # @api public module MigrationDsl # Creates a CMS attribute. # # @example Create "test" Attribute # # create_attribute(name: 'test', type: 'string') # # @param attributes [Hash] The attributes and their values of the new # CMS attribute. # # @return nothing # @deprecated Please use local attributes. Create attributes using {#create_obj_class} or # {#update_obj_class}. # @api public def create_attribute(attributes = {}) warn_deprecated __callee__, 'create_obj_class', 'update_obj_class' endpoint = "revisions/#{Workspace.current.revision_id}/attributes" CmsRestApi.post(endpoint, attribute: attributes) end # Adds an attribute to an object class. # # @example Add "test" attribute to object class "Foo. # # add_attribute_to('Foo', { name: 'test', type: 'string' }) # # @param obj_class_name [String] The class name of the object class. # @param params [Hash] The definition of the new attribute. # # @return nothing def add_attribute_to(obj_class_name, params) attributes = get_obj_class(obj_class_name)['attributes'] attributes << params update_obj_class(obj_class_name, attributes: attributes) end # Deletes an attribute from an object class. # # @example Delete "test" attribute from object class "Foo. # # delete_attribute_from('Foo', 'test') # # @param obj_class_name [String] The class name of the object class. # @param attribute_name [String] The name of the attribute that should be # deleted. # # @return nothing def delete_attribute_from(obj_class_name, attribute_name) attributes = get_obj_class(obj_class_name)['attributes'] attributes = attributes.delete_if do |attribute| # handle global and local attributes name = attribute.is_a?(String) ? attribute : attribute['name'] name == attribute_name.to_s end update_obj_class(obj_class_name, attributes: attributes) end # Updates an attribute for an object class. Turns local into global # attribute if found. # # @example Update "test" attribute for object class "Foo. # # update_attribute_for('Foo', 'test', { title: 'New Title' }) # # @param obj_class_name [String] The class name of the object class. # @param attribute_name [String] The name of the attribute that should be # updated. # @param params [Hash] Hash of updated attributes. # # @return nothing def update_attribute_for(obj_class_name, attribute_name, params) attribute_name = attribute_name.to_s attributes = get_obj_class(obj_class_name)['attributes'] found = false attributes.each_with_index do |attribute, index| name = attribute.is_a?(String) ? attribute : attribute['name'] if name == attribute_name if attribute.is_a?(String) attribute = get_attribute(attribute) end attributes[index] = attribute.merge(params.stringify_keys) update_obj_class(obj_class_name, attributes: attributes) found = true break end end unless found raise ArgumentError.new("Object class '#{obj_class_name}' does not have attribute '#{attribute_name}'.") end end # Creates a CMS object. # # @example Create "/test" Object # # create_obj(_path: '/test', _obj_class: 'Test') # # @param attributes [Hash] The attributes and their values of the new # CMS object. # # @return nothing # @api public def create_obj(attributes = {}) endpoint = "revisions/#{Workspace.current.revision_id}/objs" CmsRestApi.post(endpoint, obj: attributes) end # Creates a CMS object class. # # @example Create "Test" Object Class # # create_obj_class(name: 'Test', type: 'publication') # # @param attributes [Hash] The attributes and their values of the new # CMS object class. # # @return nothing # @api public def create_obj_class(attributes = {}) endpoint = "revisions/#{Workspace.current.revision_id}/obj_classes" CmsRestApi.post(endpoint, obj_class: attributes) end # Deletes a CMS attribute. # # @example Delete "test" Attribute # # delete_attribute('test') # # @param id [String] The ID of the CMS attribute. # # @return nothing # @deprecated Please use local attributes. Delete attributes using {#update_obj_class}. # @api public def delete_attribute(id) warn_deprecated __callee__, 'update_obj_class' endpoint = "revisions/#{Workspace.current.revision_id}/attributes/#{id}" CmsRestApi.delete(endpoint) end # Deletes a CMS object with the given id. # # @example Deletes "a1b2c3" Object # # delete_obj('a1b2c3') # # @param id [String] The ID of the CMS object. # # @return nothing # @api public def delete_obj(id) endpoint = "revisions/#{Workspace.current.revision_id}/objs/#{id}" CmsRestApi.delete(endpoint) end # Fetches all attribute attributes and their values. # # @example Get all attributes for the attribute "test" # # get_attribute('test') # # @param id [String] The ID of the attribute. # # @return [Hash] a hash with attributes and their values. # @deprecated Please use local attributes. Get attributes using {#get_obj_class}. # @api public def get_attribute(id) warn_deprecated __callee__, 'get_obj_class' endpoint = "revisions/#{Workspace.current.revision_id}/attributes/#{id}" CmsRestApi.get(endpoint) end # Fetches all object attributes and their values. # # @example Get all attributes for the obj with id "abc123" # # get_obj('abc123') # # @param id [String] The ID of the CMS object. # # @return [Hash] a hash with attributes and their values. # @api public def get_obj(id) endpoint = "revisions/#{Workspace.current.revision_id}/objs/#{id}" CmsRestApi.get(endpoint) end # Fetches all object class attributes and their values. # # @example Get all attributes for the object class "Test" # # get_obj_class('Test') # # @param id [String] The ID of the object class. # # @return [Hash] a hash with attributes and their values. # @api public def get_obj_class(id) endpoint = "revisions/#{Workspace.current.revision_id}/obj_classes/#{id}" CmsRestApi.get(endpoint) end # Updates a CMS attribute. # # @example Update the title of the "test" Attribute # # update_attribute(title: 'Test Title') # # @param id [String] The ID of the attribute. # @param attributes [Hash] The updated attributes and their values. # # @return nothing # @deprecated Please use local attributes. Update attributes using {#update_obj_class}. # @api public def update_attribute(id, attributes = {}) warn_deprecated __callee__, 'update_obj_class' endpoint = "revisions/#{Workspace.current.revision_id}/attributes/#{id}" CmsRestApi.put(endpoint, attribute: attributes) end # Updates a CMS object. # # @example Update the title of the "a1b2c3" Object # # update_attribute('a1b2c3', title: 'Test Title') # # @param id [String] The ID of the CMS object. # @param attributes [Hash] The updated attributes and their values. # # @return nothing # @api public def update_obj(id, attributes = {}) endpoint = "revisions/#{Workspace.current.revision_id}/objs/#{id}" CmsRestApi.put(endpoint, obj: attributes) end # Updates a CMS object class. # # @example Update the title of the "Test" Object Class # # update_obj_class('Test', title: 'Test Title') # # @param id [String] The ID of the CMS object class. # @param attributes [Hash] The updated attributes and their values. # # @return nothing # @api public def update_obj_class(id, attributes = {}) endpoint = "revisions/#{Workspace.current.revision_id}/obj_classes/#{id}" CmsRestApi.put(endpoint, obj_class: attributes) end # Uploads a file to the content store. # # @example Upload "image.png" and store it in an "Image" CMS object. # # filename = 'image.png' # file = File.new(filename) # blob = upload_file(file) # create_obj(_path: "/resources/images/#{filename}", _obj_class: 'Image', blob: blob) # # @param file [IO] The file IO object that gets uploaded. # # @return The blob of the uploaded file that can be stored on a CMS object. # @api public def upload_file(file) upload_permission = RailsConnector::CmsRestApi.get('blobs/upload_permission') fields = upload_permission['fields'].map { |name, value| [name, value] } fields << [:file, file] RestClient.post(upload_permission['url'], fields) upload_permission['blob'] end private def warn_deprecated(method, *substitutes) warn "[DEPRECATION] '#{method}' is deprecated. Use '#{substitutes.join("' or '")}' instead." end end end end