add shared "common" template with kind and etag
authorTom Clegg <tom@clinicalfuture.com>
Sun, 13 Jan 2013 21:34:33 +0000 (13:34 -0800)
committerTom Clegg <tom@clinicalfuture.com>
Sun, 13 Jan 2013 21:37:29 +0000 (13:37 -0800)
app/models/node.rb
config/initializers/common_api_template.rb [new file with mode: 0644]
config/initializers/kind_and_etag.rb [new file with mode: 0644]
lib/common_api_template.rb [new file with mode: 0644]
lib/kind_and_etag.rb [new file with mode: 0644]

index 8421ab2347901bba95d51365d2f65ffb037b7666..a8b183cf095f3de4e9989ed51afd428bb42944c3 100644 (file)
@@ -1,5 +1,7 @@
 class Node < ActiveRecord::Base
   include AssignUuid
+  include KindAndEtag
+  include CommonApiTemplate
   serialize :info, Hash
   before_validation :ensure_ping_secret
   after_update :dnsmasq_update
@@ -15,22 +17,13 @@ class Node < ActiveRecord::Base
               end
   @@domain = Rails.configuration.compute_node_domain rescue `hostname --domain`.strip
 
-  acts_as_api
-  api_accessible :superuser do |t|
-    t.add :uuid
-    t.add :created_by_client
-    t.add :created_by_user
-    t.add :created_at
-    t.add :modified_by_client
-    t.add :modified_by_user
-    t.add :modified_at
+  api_accessible :superuser, :extend => :common do |t|
     t.add :hostname
     t.add :domain
     t.add :ip_address
     t.add :first_ping_at
     t.add :last_ping_at
     t.add :info
-    t.add :updated_at
     t.add :status
   end
 
diff --git a/config/initializers/common_api_template.rb b/config/initializers/common_api_template.rb
new file mode 100644 (file)
index 0000000..b30bda7
--- /dev/null
@@ -0,0 +1 @@
+require 'common_api_template'
diff --git a/config/initializers/kind_and_etag.rb b/config/initializers/kind_and_etag.rb
new file mode 100644 (file)
index 0000000..ea214fd
--- /dev/null
@@ -0,0 +1 @@
+require 'kind_and_etag'
diff --git a/lib/common_api_template.rb b/lib/common_api_template.rb
new file mode 100644 (file)
index 0000000..80a08ca
--- /dev/null
@@ -0,0 +1,21 @@
+module CommonApiTemplate
+  def self.included(base)
+    base.extend(ClassMethods)
+    base.acts_as_api
+    base.api_accessible :common do |t|
+      t.add :kind
+      t.add :etag
+      t.add :uuid
+      t.add :created_by_client
+      t.add :created_by_user
+      t.add :created_at
+      t.add :modified_by_client
+      t.add :modified_by_user
+      t.add :modified_at
+      t.add :updated_at
+    end
+  end
+
+  module ClassMethods
+  end
+end
diff --git a/lib/kind_and_etag.rb b/lib/kind_and_etag.rb
new file mode 100644 (file)
index 0000000..93f69f5
--- /dev/null
@@ -0,0 +1,17 @@
+module KindAndEtag
+
+  def self.included(base)
+    base.extend(ClassMethods)
+  end
+
+  module ClassMethods
+  end
+
+  def kind
+    'orvos#' + self.class.to_s.underscore
+  end
+
+  def etag
+    Digest::MD5.hexdigest(self.inspect).to_i(16).to_s(36)
+  end
+end