Skip to content

Commit

Permalink
add generic_object_definition actions
Browse files Browse the repository at this point in the history
check permissions
  • Loading branch information
Jillian Tullo committed Sep 15, 2017
1 parent fdddd25 commit e4e5adb
Show file tree
Hide file tree
Showing 3 changed files with 451 additions and 8 deletions.
78 changes: 74 additions & 4 deletions app/controllers/api/generic_object_definitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ def create_resource(_type, _id, data)
end

def edit_resource(type, id, data)
id ||= data['name']
go_def = resource_search(id, type, collection_class(:generic_object_definitions))
go_def = fetch_generic_object_definition(type, id, data)
updated_data = data['resource'].try(:deep_symbolize_keys) || data.deep_symbolize_keys
go_def.update_attributes!(updated_data) if data.present?
go_def
Expand All @@ -18,13 +17,78 @@ def edit_resource(type, id, data)
end

def delete_resource(type, id, data = {})
id ||= data['name']
go_def = resource_search(id, type, collection_class(:generic_object_definitions))
go_def = fetch_generic_object_definition(type, id, data)
go_def.destroy!
rescue => err
raise BadRequestError, "Failed to delete generic object definition - #{err}"
end

def add_attributes_resource(type, id, data)
go_def = fetch_generic_object_definition(type, id, data)
attributes = data['attributes'] || data['resource']['attributes']
attributes.each do |name, attribute_type|
go_def.add_property_attribute(name, attribute_type)
end
go_def
rescue => err
raise BadRequestError, "Failed to add attributes to generic object definition - #{err}"
end

def remove_attributes_resource(type, id, data)
go_def = fetch_generic_object_definition(type, id, data)
attributes = data['attributes'] || data['resource']['attributes']
attributes.each do |name, _type|
go_def.delete_property_attribute(name)
end
go_def
rescue => err
raise BadRequestError, "Failed to remove attributes from generic object definition - #{err}"
end

def add_associations_resource(type, id, data)
go_def = fetch_generic_object_definition(type, id, data)
associations = data['associations'] || data['resource']['associations']
associations.each do |name, association_type|
go_def.add_property_association(name, association_type)
end
go_def
rescue => err
raise BadRequestError, "Failed to add attributes to object definition - #{err}"
end

def remove_associations_resource(type, id, data)
go_def = fetch_generic_object_definition(type, id, data)
associations = data['associations'] || data['resource']['associations']
associations.each do |name, _type|
go_def.delete_property_association(name)
end
go_def
rescue => err
raise BadRequestError, "Failed to add attributes to object definition - #{err}"
end

def add_methods_resource(type, id, data)
go_def = fetch_generic_object_definition(type, id, data)
methods = data['methods'] || data['resource']['methods']
methods.each do |name|
go_def.add_property_method(name)
end
go_def
rescue => err
raise BadRequestError, "Failed to add attributes to object definition - #{err}"
end

def remove_methods_resource(type, id, data)
go_def = fetch_generic_object_definition(type, id, data)
methods = data['methods'] || data['resource']['methods']
methods.each do |name|
go_def.delete_property_method(name)
end
go_def
rescue => err
raise BadRequestError, "Failed to add attributes to object definition - #{err}"
end

def self.allowed_association_types
GenericObjectDefinition::ALLOWED_ASSOCIATION_TYPES.collect do |association_type|
[Dictionary.gettext(association_type, :type => :model, :notfound => :titleize, :plural => false), association_type]
Expand All @@ -47,8 +111,14 @@ def build_generic_object_definition_options
:allowed_types => GenericObjectDefinitionsController.allowed_types
}
end

private

def fetch_generic_object_definition(type, id, data)
id ||= data['name']
resource_search(id, type, collection_class(type))
end

def resource_search(id, type, klass)
if ApplicationRecord.compressed_id?(id)
super
Expand Down
24 changes: 24 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,18 @@
:identifier: generic_object_definition_edit
- :name: delete
:identifier: generic_object_definition_delete
- :name: add_attributes
:identifier: generic_object_definition_edit
- :name: remove_attributes
:identifier: generic_object_definition_edit
- :name: add_associations
:identifier: generic_object_definition_edit
- :name: remove_associations
:identifier: generic_object_definition_edit
- :name: add_methods
:identifier: generic_object_definition_edit
- :name: remove_methods
:identifier: generic_object_definition_edit
:resource_actions:
:get:
- :name: read
Expand All @@ -851,6 +863,18 @@
:identifier: generic_object_definition_edit
- :name: delete
:identifier: generic_object_definition_delete
- :name: add_attributes
:identifier: generic_object_definition_edit
- :name: remove_attributes
:identifier: generic_object_definition_edit
- :name: add_associations
:identifier: generic_object_definition_edit
- :name: remove_associations
:identifier: generic_object_definition_edit
- :name: add_methods
:identifier: generic_object_definition_edit
- :name: remove_methods
:identifier: generic_object_definition_edit
:put:
- :name: edit
:identifier: generic_object_definition_edit
Expand Down
Loading

0 comments on commit e4e5adb

Please sign in to comment.