Переглянути джерело

Add a way to figure that we are on DEV

Markus Ochel 12 роки тому
батько
коміт
d6c0f2bf00
3 змінених файлів з 37 додано та 23 видалено
  1. 5 0
      site/lib/utils.coffee
  2. 16 10
      site/server/lists.coffee
  3. 16 13
      site/templates/base.html

+ 5 - 0
site/lib/utils.coffee

@@ -1,5 +1,10 @@
 moment    = require('lib/moment')
 
+exports.isDev = (req) ->
+  # We are on DEV when Host has string like
+  # '/dev.' or '/staging.' or '.local' or 'localhost'
+  !!(req.headers.Host.match(/(\/dev.|\/staging.|\.local$|localhost)/))
+
 exports.prettyDate = (date) ->
   moment.utc(date).local().format('MMM Do YYYY')
 

+ 16 - 10
site/server/lists.coffee

@@ -15,14 +15,14 @@ exports.home = (head, req) ->
   md = new Showdown.converter()
   collections = []
   blocks = {}
-  site = null
+  site = {}
 
   while row = getRow()
     doc = row.value
     if doc
       collections.push(doc) if doc.type is 'collection'
       blocks[doc.code] = doc if doc.type is 'block'
-      site ?= doc if doc.type is 'site'
+      site = doc if doc.type is 'site'
   
   collections = _.map collections, (doc) ->
     if doc.intro?
@@ -36,6 +36,7 @@ exports.home = (head, req) ->
     return doc
 
   return {
+    on_dev: utils.isDev(req)
     site: site
     title: "#{site.name}"
     content: templates.render "home.html", req,
@@ -62,7 +63,7 @@ exports.collection = (head, req) ->
   collection = null
   blocks = {}
   sponsor = null
-  site = null
+  site = {}
 
   while row = getRow()
     doc = row.doc
@@ -71,7 +72,7 @@ exports.collection = (head, req) ->
       collection ?= doc if doc.type is 'collection'
       blocks[doc.code] = doc if doc.type is 'block'
       sponsor ?= doc if doc.type is 'sponsor'
-      site ?= doc if doc.type is 'site'
+      site = doc if doc.type is 'site'
 
   if collection
     if collection.intro?
@@ -109,6 +110,7 @@ exports.collection = (head, req) ->
 
   if collection
     return {
+      on_dev: utils.isDev(req)
       site: site
       title: collection.name
       content: templates.render 'collection.html', req,
@@ -129,6 +131,7 @@ exports.collection = (head, req) ->
       code: 404
       title: '404 Not Found'
       content: templates.render '404.html', req, { host: req.headers.Host }
+      on_dev: utils.isDev(req)
     }
 
 
@@ -139,7 +142,7 @@ exports.docs = (head, req) ->
 
   md = new Showdown.converter()
   docs = []
-  site = null
+  site = {}
 
   while row = getRow()
     doc = row.doc
@@ -150,7 +153,7 @@ exports.docs = (head, req) ->
       else if doc.type is 'collection'
         # Add the collection doc to the last doc pushed
         docs[docs.length-1].collection_docs.push(doc)
-      site ?= doc if doc.type is 'site'
+      site = doc if doc.type is 'site'
 
   docs = _.map docs, (doc) ->
     if doc.intro?
@@ -164,6 +167,7 @@ exports.docs = (head, req) ->
     return doc
 
   return {
+    on_dev: utils.isDev(req)
     site: site
     title: 'Docs List'
     content: templates.render 'docs.html', req,
@@ -193,7 +197,7 @@ exports.doc = (head, req) ->
   author = null
   sponsor = null
   blocks = {}
-  site = null
+  site = {}
 
   while row = getRow()
     doc = row.doc
@@ -203,7 +207,7 @@ exports.doc = (head, req) ->
       sponsor ?= doc if doc.type is 'sponsor'
       author ?= doc if doc.type is 'author'
       blocks[doc.code] = doc if doc.type is 'block'
-      site ?= doc if doc.type is 'site'
+      site = doc if doc.type is 'site'
 
   # Let's just go back and use `doc` as the variable instead
   doc = theDoc
@@ -250,6 +254,7 @@ exports.doc = (head, req) ->
 
   if doc
     return {
+      on_dev: utils.isDev(req)
       site: site
       title: doc.title
       content: templates.render 'doc.html', req,
@@ -275,6 +280,7 @@ exports.doc = (head, req) ->
       code: 404
       title: '404 Not Found'
       content: templates.render '404.html', req, { host: req.headers.Host }
+      on_dev: utils.isDev(req)
     }
 
 
@@ -285,13 +291,13 @@ exports.rssfeed = (head, req) ->
 
   md = new Showdown.converter()
   docs = []
-  site = null
+  site = {}
 
   while row = getRow()
     doc = row.doc
     if doc
       docs.push(doc) if doc.type in settings.app.content_types
-      site ?= doc if doc.type is 'site'
+      site = doc if doc.type is 'site'
 
   docs = _.map docs, (doc) ->
     doc.intro_html = md.makeHtml(

+ 16 - 13
site/templates/base.html

@@ -91,19 +91,22 @@
     });
   </script>
 
-  {{#if site.google_analytics_code}}
-  <script type="text/javascript">
-    var _gaq = [];
-    _gaq.push(['_setAccount', '{{site.google_analytics_code}}']);
-    _gaq.push(['_setDomainName', 'auto']);
-    _gaq.push(['_trackPageview']);
-    (function() {
-      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
-      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
-      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
-    })();
-  </script>
-  {{/if}}
+  {{#unless on_dev}}
+    {{! Do the following on production }}
+    {{#if site.google_analytics_code}}
+    <script type="text/javascript">
+      var _gaq = [];
+      _gaq.push(['_setAccount', '{{site.google_analytics_code}}']);
+      _gaq.push(['_setDomainName', 'auto']);
+      _gaq.push(['_trackPageview']);
+      (function() {
+        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+      })();
+    </script>
+    {{/if}}
+  {{/unless}}
 
 </body>
 </html>