module RailsConnector # @api public module Migrations # @api public module MigrationDsl # 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| attribute['name'] == attribute_name.to_s end update_obj_class(obj_class_name, attributes: attributes) end # Updates an attribute for an object class. # # @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| if attribute['name'] == attribute_name 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 # # @deprecated use {BasicObj.create Obj.create} instead def create_obj(attributes = {}) Deprecation.warn_method('create_obj', 'Obj.create') endpoint = "workspaces/#{Workspace.current.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 = "workspaces/#{Workspace.current.id}/obj_classes" CmsRestApi.post(endpoint, obj_class: attributes) 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 # # @deprecated use {BasicObj#destroy Obj#destroy} instead def delete_obj(id) Deprecation.warn_method('delete_obj', 'Obj#destroy') endpoint = "workspaces/#{Workspace.current.id}/objs/#{id}" CmsRestApi.delete(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 = "workspaces/#{Workspace.current.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 = "workspaces/#{Workspace.current.id}/obj_classes/#{id}" CmsRestApi.get(endpoint) 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 # # @deprecated use {BasicObj#update Obj#update} instead def update_obj(id, attributes = {}) Deprecation.warn_method('update_obj', 'Obj#update') endpoint = "workspaces/#{Workspace.current.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 = "workspaces/#{Workspace.current.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 # @deprecated use {BasicObj.create Obj.create} or {BasicObj#update Obj#update} instead def upload_file(file) Deprecation.warn_method("upload_file", "Obj.create or Obj#update") 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 end end end