add generic #show page for orvos objects and use it for Log and User
authorTom Clegg <tom@clinicalfuture.com>
Thu, 7 Feb 2013 17:52:28 +0000 (12:52 -0500)
committerTom Clegg <tom@clinicalfuture.com>
Thu, 7 Feb 2013 17:52:28 +0000 (12:52 -0500)
app/models/orvos_base.rb
app/views/application/_orvos_object.html.erb [new file with mode: 0644]
app/views/logs/show.html.erb
app/views/users/show.html.erb [new file with mode: 0644]

index 9a02001c9b8b8159aefdd68fb0a199ff9b26cc8e..ed6a6a59b35559b62cb2b0c41854d6a8fea64867 100644 (file)
@@ -1,5 +1,34 @@
 class OrvosBase < ActiveRecord::Base
   self.abstract_class = true
+  attr_accessor :attribute_sortkey
+
+  def self.uuid_infix_object_kind
+    @@uuid_infix_object_kind ||= {
+      '4zz18' => 'orvos#collection',
+      'tpzed' => 'orvos#user',
+      'ozdt8' => 'orvos#api_client',
+      '57u5n' => 'orvos#log'
+    }
+  end
+
+  def initialize
+    super
+    @attribute_sortkey ||= {
+      'id' => nil,
+      'uuid' => '000',
+      'owner' => '001',
+      'created_at' => '002',
+      'modified_at' => '003',
+      'modified_by_user' => '004',
+      'modified_by_client' => '005',
+      'tail_kind' => '100',
+      'tail_uuid' => '100',
+      'head_kind' => '101',
+      'head_uuid' => '101',
+      'info' => 'zzz-000',
+      'updated_at' => 'zzz-999'
+    }
+  end
 
   def self.columns
     return @columns unless @columns.nil?
@@ -140,6 +169,29 @@ class OrvosBase < ActiveRecord::Base
     super.forget_uuid!
   end
 
+  def attributes_for_display
+    self.attributes.reject { |k,v|
+      attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
+    }.sort_by { |k,v|
+      attribute_sortkey[k] or k
+    }
+  end
+
+  def self.resource_class_for_uuid(uuid, attr_name=nil, object=nil)
+    resource_class = nil
+    if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
+      return 'orvos#collection'
+    end
+    uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
+      resource_class ||= $orvos_api_client.
+        kind_class(self.uuid_infix_object_kind[re[1]])
+    end
+    if object and attr_name and attr_name.match /_uuid$/
+      resource_class ||= $orvos_api_client.kind_class(object.attributes[attr.sub(/_uuid$/, '_kind')])
+    end
+    resource_class
+  end
+
   protected
 
   def forget_uuid!
diff --git a/app/views/application/_orvos_object.html.erb b/app/views/application/_orvos_object.html.erb
new file mode 100644 (file)
index 0000000..0fa5566
--- /dev/null
@@ -0,0 +1,33 @@
+<table class="table topalign">
+  <thead>
+  </thead>
+  <tbody>
+    <% @object.attributes_for_display.each do |attr, attrvalue| %>
+    <% if attrvalue.is_a? Hash then attrvalue.each do |infokey, infocontent| %>
+    <tr>
+      <td><%= infokey %></td>
+      <td>
+        <tt>
+          <% if infocontent.is_a? Array then infocontent.each do |message| %>
+          <%= message %><br />
+          <% end; elsif infocontent.is_a? String %>
+          <%= infocontent %>
+          <% else %>
+          <%= infocontent.inspect %>
+          <% end %>
+        </tt>
+      </td>
+    </tr>
+    <% end %>
+    <% elsif attrvalue.is_a? String or attrvalue.respond_to? :to_s %>
+    <tr>
+      <td><%= attr %></td>
+      <% if attrvalue.is_a? String and (resource_class = OrvosBase::resource_class_for_uuid(attrvalue, attr, @object)) %>
+      <td><%= link_to "#{resource_class.to_s} #{attrvalue}", { controller: resource_class.to_s.camelize(:lower).pluralize, action: 'show', id: attrvalue } %></td>
+      <% else %>
+      <td><%= attrvalue %></td>
+      <% end %>
+    </tr>
+    <% end; end %>
+  </tbody>
+</table>
index 818177f2fb64d3eb6bbe94fd7d347d7820cfda81..68f5fa9a549ca3c88602de47015223ed711475f1 100644 (file)
@@ -1,30 +1 @@
-<table class="table topalign">
-  <thead>
-  </thead>
-  <tbody>
-    <% %w(event_type summary created_at modified_at info).each do |attr| %>
-    <% attrvalue = @object.send attr %>
-    <% if attrvalue.is_a? Hash then attrvalue.each do |infokey, infocontent| %>
-    <tr>
-      <td><%= infokey %></td>
-      <td>
-        <tt>
-          <% if infocontent.is_a? Array then infocontent.each do |message| %>
-          <%= message %><br />
-          <% end; elsif infocontent.is_a? String %>
-          <%= infocontent %>
-          <% else %>
-          <%= infocontent.inspect %>
-          <% end %>
-        </tt>
-      </td>
-    </tr>
-    <% end %>
-    <% elsif attrvalue.is_a? String or attrvalue.respond_to? :to_s %>
-    <tr>
-      <td><%= attr %></td>
-      <td><%= attrvalue %></td>
-    </tr>
-    <% end; end %>
-  </tbody>
-</table>
+<%= render :partial => 'application/orvos_object' %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb
new file mode 100644 (file)
index 0000000..68f5fa9
--- /dev/null
@@ -0,0 +1 @@
+<%= render :partial => 'application/orvos_object' %>