make resource_class_for_uuid more convenient and robust
authorTom Clegg <tom@clinicalfuture.com>
Thu, 7 Feb 2013 17:56:55 +0000 (12:56 -0500)
committerTom Clegg <tom@clinicalfuture.com>
Thu, 7 Feb 2013 17:59:41 +0000 (12:59 -0500)
app/helpers/application_helper.rb
app/models/orvos_base.rb
app/views/application/_orvos_object.html.erb

index a0d693c704c21ada815fa05c728cc60b5ec534cb..7106a140edaf76b460695e07dbb7c3e66fe4149f 100644 (file)
@@ -12,4 +12,12 @@ module ApplicationHelper
     end
     cooked = raw + cooked
   end
+
+  def link_to_if_orvos_object(attrvalue, attr, object)
+    if (resource_class = OrvosBase::resource_class_for_uuid(attrvalue, attr, object))
+      link_to "#{resource_class.to_s} #{attrvalue}", { controller: resource_class.to_s.camelize(:lower).pluralize, action: 'show', id: attrvalue }
+    else
+      attrvalue
+    end
+  end
 end
index ed6a6a59b35559b62cb2b0c41854d6a8fea64867..e42039bced4bff0eaec6556a8afdc56f6a4536d6 100644 (file)
@@ -178,10 +178,13 @@ class OrvosBase < ActiveRecord::Base
   end
 
   def self.resource_class_for_uuid(uuid, attr_name=nil, object=nil)
-    resource_class = nil
+    unless uuid.is_a? String
+      return nil
+    end
     if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
       return 'orvos#collection'
     end
+    resource_class = nil
     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]])
index 0fa55665c73d3853292868c5f0fcde3b09ce8fcd..d0922c9f5cccfef6f7b117fced79812a5f6d5223 100644 (file)
     <% 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 %>
+      <td><%= link_to_if_orvos_object attrvalue, attr, @object %></td>
     </tr>
     <% end; end %>
   </tbody>