From: Tom Clegg Date: Thu, 17 Jan 2013 09:15:46 +0000 (-0800) Subject: provide metadata head_kind, head_uuid in api response X-Git-Tag: 1.1.0~3502 X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/db6e72e49f6b023b275c611c366f5eb5eadf8d09 provide metadata head_kind, head_uuid in api response if value is orvos#type#uuid; tweak "specimen" inflections --- diff --git a/app/controllers/orvos/v1/schema_controller.rb b/app/controllers/orvos/v1/schema_controller.rb index c68ce04687..2aa1e8c08b 100644 --- a/app/controllers/orvos/v1/schema_controller.rb +++ b/app/controllers/orvos/v1/schema_controller.rb @@ -13,6 +13,9 @@ class Orvos::V1::SchemaController < ApplicationController type: col.type } end end + if k.respond_to? :add_schema_columns + classes[k].concat k.add_schema_columns + end end render json: classes end diff --git a/app/models/metadatum.rb b/app/models/metadatum.rb index 7736ff9291..a0cf51461c 100644 --- a/app/models/metadatum.rb +++ b/app/models/metadatum.rb @@ -5,6 +5,11 @@ class Metadatum < ActiveRecord::Base serialize :info, Hash before_validation :populate_native_target + def self.add_schema_columns + [ { name: :head_kind, type: :string }, + { name: :head_uuid, type: :string } ] + end + api_accessible :superuser, :extend => :common do |t| t.add :target_kind t.add :target_uuid @@ -12,6 +17,8 @@ class Metadatum < ActiveRecord::Base t.add :key t.add :value t.add :info + t.add :head_kind + t.add :head_uuid end def info @@ -19,12 +26,45 @@ class Metadatum < ActiveRecord::Base super end + def head_kind + @head_kind if populate_head_object + end + + def head_uuid + @head_uuid if populate_head_object + end + protected + def populate_head_object + @head_object ||= begin + @head_kind = self.value. + sub(/^(.*)#.*/,'\1') + logger.debug @head_kind + class_name = @head_kind. + sub(/^orvos#/,''). + pluralize. + classify + logger.debug "class_name is #{class_name}" + @head_uuid = self.value.split('#').last + logger.debug "uuid is @head_uuid" + @head_object = class_name. + constantize. + where('uuid = ?', @head_uuid). + first + @head_object + rescue + @head_kind = nil + @head_uuid = nil + false + end || false unless @head_object == false + end + def populate_native_target begin class_name = target_kind. sub(/^orvos#/,''). + pluralize. classify self.native_target_type = class_name self.native_target_id = class_name. diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 7f100f3604..719262b36a 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -10,6 +10,6 @@ # end ActiveSupport::Inflector.inflections do |inflect| - inflect.plural /^(specimen)$/i, '\1s' - inflect.singular /^(specimen)s/i, '\1' + inflect.plural /^([Ss]pecimen)$/i, '\1s' + inflect.singular /^([Ss]pecimen)s?/i, '\1' end