add metadata resource
authorTom Clegg <tom@clinicalfuture.com>
Sat, 5 Jan 2013 22:30:18 +0000 (14:30 -0800)
committerTom Clegg <tom@clinicalfuture.com>
Sat, 5 Jan 2013 22:31:41 +0000 (14:31 -0800)
15 files changed:
app/assets/javascripts/metadata.js.coffee [new file with mode: 0644]
app/assets/stylesheets/metadata.css.scss [new file with mode: 0644]
app/controllers/orvos/v1/metadata_controller.rb [new file with mode: 0644]
app/helpers/metadata_helper.rb [new file with mode: 0644]
app/models/metadatum.rb [new file with mode: 0644]
config/application.rb
config/initializers/assign_uuid.rb [new file with mode: 0644]
config/routes.rb
db/migrate/20130105203021_create_metadata.rb [new file with mode: 0644]
db/schema.rb
lib/assign_uuid.rb [new file with mode: 0644]
test/fixtures/metadata.yml [new file with mode: 0644]
test/functional/metadata_controller_test.rb [new file with mode: 0644]
test/unit/helpers/metadata_helper_test.rb [new file with mode: 0644]
test/unit/metadatum_test.rb [new file with mode: 0644]

diff --git a/app/assets/javascripts/metadata.js.coffee b/app/assets/javascripts/metadata.js.coffee
new file mode 100644 (file)
index 0000000..7615679
--- /dev/null
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
diff --git a/app/assets/stylesheets/metadata.css.scss b/app/assets/stylesheets/metadata.css.scss
new file mode 100644 (file)
index 0000000..72bd8b8
--- /dev/null
@@ -0,0 +1,3 @@
+// Place all the styles related to the metadata controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/orvos/v1/metadata_controller.rb b/app/controllers/orvos/v1/metadata_controller.rb
new file mode 100644 (file)
index 0000000..b7916d8
--- /dev/null
@@ -0,0 +1,2 @@
+class MetadataController < ApplicationController
+end
diff --git a/app/helpers/metadata_helper.rb b/app/helpers/metadata_helper.rb
new file mode 100644 (file)
index 0000000..300d90e
--- /dev/null
@@ -0,0 +1,2 @@
+module MetadataHelper
+end
diff --git a/app/models/metadatum.rb b/app/models/metadatum.rb
new file mode 100644 (file)
index 0000000..445c728
--- /dev/null
@@ -0,0 +1,30 @@
+class Metadatum < ActiveRecord::Base
+  serialize :info, Hash
+
+  before_validation :populate_native_target
+  include AssignUuid
+
+  def info
+    @info ||= Hash.new
+    super
+  end
+
+  protected
+
+  def populate_native_target
+    begin
+      class_name = target_kind.
+        sub(/^orvos#/,'').
+        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 f9001299114cd854dca05be454631dd43a94b81b..96e0cde90a67dba34101af5b7c794758651d705a 100644 (file)
@@ -1,6 +1,7 @@
 require File.expand_path('../boot', __FILE__)
 
 require 'rails/all'
+require 'digest'
 
 if defined?(Bundler)
   # If you precompile assets before deploying to production, use this line
@@ -48,6 +49,8 @@ module Server
     config.middleware.use Rack::Static, :urls => [ '/discovery' ], :root => "public"
 
     config.force_ssl = true
+
+    config.uuid_prefix = Digest::MD5.hexdigest('CHANGE-ME').to_i(16).to_s(36)[0..4]
   end
 
 end
diff --git a/config/initializers/assign_uuid.rb b/config/initializers/assign_uuid.rb
new file mode 100644 (file)
index 0000000..d3835db
--- /dev/null
@@ -0,0 +1 @@
+require 'assign_uuid'
index 20d1f6fb202299a022f1c42727b9d88be5f40899..b292fd38bdab77e05ef8e40e25602ec4b27bceca 100644 (file)
@@ -1,4 +1,6 @@
 Server::Application.routes.draw do
+  resources :metadata
+
   resources :collections
 
   # The priority is based upon order of creation:
diff --git a/db/migrate/20130105203021_create_metadata.rb b/db/migrate/20130105203021_create_metadata.rb
new file mode 100644 (file)
index 0000000..8e0f01b
--- /dev/null
@@ -0,0 +1,22 @@
+class CreateMetadata < ActiveRecord::Migration
+  def change
+    create_table :metadata do |t|
+      t.string :uuid
+      t.string :created_by_client
+      t.string :created_by_user
+      t.datetime :created_at
+      t.string :modified_by_client
+      t.string :modified_by_user
+      t.datetime :modified_at
+      t.string :target_uuid
+      t.string :target_kind
+      t.references :native_target, :polymorphic => true
+      t.string :metadatum_class
+      t.string :key
+      t.string :value
+      t.text :info # "unlimited length" in postgresql
+
+      t.timestamps
+    end
+  end
+end
index 9e91fca29f5383683e22e6f68aa00d249781a67a..ec8ab32196c7aaa7443d466f040578d24d057b58 100644 (file)
@@ -1,3 +1,4 @@
+# encoding: UTF-8
 # This file is auto-generated from the current state of the database. Instead
 # of editing this file, please use the migrations feature of Active Record to
 # incrementally modify your database, and then regenerate this schema definition.
@@ -10,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20121016005009) do
+ActiveRecord::Schema.define(:version => 20130105203021) do
 
   create_table "collections", :force => true do |t|
     t.string   "locator"
@@ -29,4 +30,23 @@ ActiveRecord::Schema.define(:version => 20121016005009) do
     t.datetime "updated_at"
   end
 
+  create_table "metadata", :force => true do |t|
+    t.string   "uuid"
+    t.string   "created_by_client"
+    t.string   "created_by_user"
+    t.datetime "created_at"
+    t.string   "modified_by_client"
+    t.string   "modified_by_user"
+    t.datetime "modified_at"
+    t.string   "target_uuid"
+    t.string   "target_kind"
+    t.integer  "native_target_id"
+    t.string   "native_target_type"
+    t.string   "metadatum_class"
+    t.string   "key"
+    t.string   "value"
+    t.text     "info"
+    t.datetime "updated_at"
+  end
+
 end
diff --git a/lib/assign_uuid.rb b/lib/assign_uuid.rb
new file mode 100644 (file)
index 0000000..b470d8e
--- /dev/null
@@ -0,0 +1,29 @@
+module AssignUuid
+
+  def self.included(base)
+    base.extend(ClassMethods)
+    base.validates_presence_of :uuid, :if => :respond_to_uuid?
+    base.validates_uniqueness_of :uuid, :if => :respond_to_uuid?
+    base.before_validation :assign_uuid
+  end
+
+  module ClassMethods
+    def uuid_prefix
+      Digest::MD5.hexdigest(self.to_s).to_i(16).to_s(36)[0..4]
+    end
+  end
+
+  protected
+
+  def respond_to_uuid?
+    self.respond_to? :uuid
+  end
+
+  def assign_uuid
+    return true if !self.respond_to_uuid?
+    self.uuid ||= [Server::Application.config.uuid_prefix,
+                   self.class.uuid_prefix,
+                   rand(2**256).to_s(36)[0..14]].
+      join '-'
+  end
+end
diff --git a/test/fixtures/metadata.yml b/test/fixtures/metadata.yml
new file mode 100644 (file)
index 0000000..c63aac0
--- /dev/null
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+# This model initially had no columns defined.  If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+#  column: value
diff --git a/test/functional/metadata_controller_test.rb b/test/functional/metadata_controller_test.rb
new file mode 100644 (file)
index 0000000..ec39c56
--- /dev/null
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class MetadataControllerTest < ActionController::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end
diff --git a/test/unit/helpers/metadata_helper_test.rb b/test/unit/helpers/metadata_helper_test.rb
new file mode 100644 (file)
index 0000000..d50e16c
--- /dev/null
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class MetadataHelperTest < ActionView::TestCase
+end
diff --git a/test/unit/metadatum_test.rb b/test/unit/metadatum_test.rb
new file mode 100644 (file)
index 0000000..1fd95db
--- /dev/null
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class MetadatumTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end