provide metadata head_kind, head_uuid in api response
authorTom Clegg <tom@clinicalfuture.com>
Thu, 17 Jan 2013 09:15:46 +0000 (01:15 -0800)
committerTom Clegg <tom@clinicalfuture.com>
Thu, 17 Jan 2013 09:16:01 +0000 (01:16 -0800)
if value is orvos#type#uuid; tweak "specimen" inflections

app/controllers/orvos/v1/schema_controller.rb
app/models/metadatum.rb
config/initializers/inflections.rb

index c68ce04687442a883d133bb2d88ea12fa159b7d5..2aa1e8c08bde6bd3c4377c1189f2dd27cfb2a3b7 100644 (file)
@@ -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
index 7736ff9291eb9f2903da251d46a1657425660619..a0cf51461ccaef3fe50b7080e239f70d25d82d8a 100644 (file)
@@ -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.
index 7f100f3604cf546edb431648de8d335811e2b1e4..719262b36a0df6f44e024b7d3a621960cb00cf4a 100644 (file)
@@ -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