rename metadata attributes to head/tail
authorTom Clegg <tom@clinicalfuture.com>
Fri, 18 Jan 2013 01:16:28 +0000 (17:16 -0800)
committerTom Clegg <tom@clinicalfuture.com>
Fri, 18 Jan 2013 01:16:28 +0000 (17:16 -0800)
app/controllers/orvos/v1/metadata_controller.rb
app/controllers/orvos/v1/schema_controller.rb
app/models/metadatum.rb
config/routes.rb
db/migrate/20130118002239_rename_metadata_attributes.rb [new file with mode: 0644]
db/schema.rb

index 349f1e07ad1a05d8ce461bcc80b6cac6e6b28e91..6c57f3c72265cca27b9fb470662eb517d1b1ce38 100644 (file)
@@ -1,8 +1,8 @@
 class Orvos::V1::MetadataController < ApplicationController
   def index
-    if params[:target_kind] and params[:target_uuid]
-      @objects = Metadatum.where('target_kind=? and target_uuid=?',
-                                 params[:target_kind], params[:target_uuid])
+    if params[:tail_kind] and params[:tail]
+      @objects = Metadatum.where('tail_kind=? and tail=?',
+                                 params[:tail_kind], params[:tail])
     end
     super
   end
index 2aa1e8c08bde6bd3c4377c1189f2dd27cfb2a3b7..c68ce04687442a883d133bb2d88ea12fa159b7d5 100644 (file)
@@ -13,9 +13,6 @@ 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 a0cf51461ccaef3fe50b7080e239f70d25d82d8a..48f397f6bd626a9c938d20318aaa3d86c94be1f1 100644 (file)
@@ -3,78 +3,19 @@ class Metadatum < ActiveRecord::Base
   include KindAndEtag
   include CommonApiTemplate
   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
+    t.add :tail_kind
+    t.add :tail
     t.add :metadata_class
-    t.add :key
-    t.add :value
-    t.add :info
+    t.add :name
     t.add :head_kind
-    t.add :head_uuid
+    t.add :head
+    t.add :info
   end
 
   def info
     @info ||= Hash.new
     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.
-        constantize.
-        where('uuid = ?', target_uuid).
-        first.
-        id
-    rescue
-      self.native_target_type = nil
-      self.native_target_id = nil
-    end
-  end
 end
index 0bc359f32c2ecea1f389722d6a8e919167499e3d..9f313b9d5abdb29fb5d84660390e3dc4e33bcaeb 100644 (file)
@@ -77,7 +77,7 @@ Server::Application.routes.draw do
       resources :projects
       match '/schema' => 'schema#show'
       match '/nodes/:uuid/ping' => 'nodes#ping', :as => :ping_node
-      match '/metadata/:target_kind/:target_uuid' => 'metadata#index'
+      match '/metadata/:tail_kind/:tail' => 'metadata#index'
     end
   end
 
diff --git a/db/migrate/20130118002239_rename_metadata_attributes.rb b/db/migrate/20130118002239_rename_metadata_attributes.rb
new file mode 100644 (file)
index 0000000..1f7af58
--- /dev/null
@@ -0,0 +1,35 @@
+class RenameMetadataAttributes < ActiveRecord::Migration
+  def up
+    rename_column :metadata, :target_kind, :tail_kind
+    rename_column :metadata, :target_uuid, :tail
+    rename_column :metadata, :value, :head
+    rename_column :metadata, :key, :name
+    add_column :metadata, :head_kind, :string
+    add_index :metadata, :head
+    add_index :metadata, :head_kind
+    add_index :metadata, :tail
+    add_index :metadata, :tail_kind
+    Metadatum.where('head like ?', 'orvos#%').each do |m|
+      kind_uuid = m.head.match /^(orvos\#.*)\#([-0-9a-z]+)$/
+      if kind_uuid
+        m.update_attributes(head_kind: kind_uuid[1],
+                            head: kind_uuid[2])
+      end
+    end
+  end
+
+  def down
+    Metadatum.where('head_kind is not null and head_kind <> ? and head is not null', '').each do |m|
+      m.update_attributes(head: m.head_kind + '#' + m.head)
+    end
+    remove_index :metadata, :tail_kind
+    remove_index :metadata, :tail
+    remove_index :metadata, :head_kind
+    remove_index :metadata, :head
+    rename_column :metadata, :name, :key
+    remove_column :metadata, :head_kind
+    rename_column :metadata, :head, :value
+    rename_column :metadata, :tail, :target_uuid
+    rename_column :metadata, :tail_kind, :target_kind
+  end
+end
index cece25ae32e55dcfc48694a1f51396acc0f6e783..996ac186441b0715dec47e49384fca96c2c3523d 100644 (file)
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20130116215213) do
+ActiveRecord::Schema.define(:version => 20130118002239) do
 
   create_table "collections", :force => true do |t|
     t.string   "locator"
@@ -41,17 +41,22 @@ ActiveRecord::Schema.define(:version => 20130116215213) do
     t.string   "modified_by_client"
     t.string   "modified_by_user"
     t.datetime "modified_at"
-    t.string   "target_uuid"
-    t.string   "target_kind"
+    t.string   "tail"
+    t.string   "tail_kind"
     t.integer  "native_target_id"
     t.string   "native_target_type"
     t.string   "metadata_class"
-    t.string   "key"
-    t.string   "value"
+    t.string   "name"
+    t.string   "head"
     t.text     "info"
     t.datetime "updated_at"
+    t.string   "head_kind"
   end
 
+  add_index "metadata", ["head"], :name => "index_metadata_on_head"
+  add_index "metadata", ["head_kind"], :name => "index_metadata_on_head_kind"
+  add_index "metadata", ["tail"], :name => "index_metadata_on_tail"
+  add_index "metadata", ["tail_kind"], :name => "index_metadata_on_tail_kind"
   add_index "metadata", ["uuid"], :name => "index_metadata_on_uuid", :unique => true
 
   create_table "nodes", :force => true do |t|