Permalink
Browse files

Added vero_javascript_tag view helper.

  • Loading branch information...
1 parent 67aa6a6 commit c11a2ed2337b2935bdf7923f820653f1d7579944 @jylamont jylamont committed Jul 19, 2012
Showing with 102 additions and 6 deletions.
  1. +1 −0 .rspec
  2. +1 −1 Gemfile.lock
  3. +3 −1 lib/vero.rb
  4. +0 −1 lib/vero/app.rb
  5. +9 −2 lib/vero/config.rb
  6. +8 −0 lib/vero/railtie.rb
  7. +1 −1 lib/vero/trackable.rb
  8. +38 −0 lib/vero/view_helpers.rb
  9. +41 −0 spec/lib/view_helpers_spec.rb
View
1 .rspec
@@ -0,0 +1 @@
+--colour
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- vero (0.1.2)
+ vero (0.1.3)
delayed_job
delayed_job_active_record
delayed_job_mongoid
View
@@ -12,4 +12,6 @@ module Vero
if defined? Mongoid
require 'delayed_job_mongoid'
-end
+end
+
+require 'vero/railtie' if defined?(Rails)
View
@@ -7,7 +7,6 @@ def self.init(&block)
if block_given?
block.call(self.config)
- @@config.configured = true
end
end
View
@@ -1,15 +1,18 @@
module Vero
class Config
attr_writer :domain
- attr_accessor :api_key, :secret, :development_mode, :async, :configured, :disabled
+ attr_accessor :api_key, :secret, :development_mode, :async, :disabled
def initialize
self.disabled = false
- self.configured = false
self.development_mode = !Rails.env.production?
self.async = true
end
+ def config_params
+ options = {api_key: self.api_key, secret: self.secret}
+ end
+
def request_params
temp = {}
temp_auth_token = self.auth_token
@@ -27,5 +30,9 @@ def auth_token
return if api_key.blank? || secret.blank?
Base64::encode64("#{api_key}:#{secret}").gsub(/[\n ]/, '')
end
+
+ def configured
+ !api_key.blank? && !secret.blank?
+ end
end
end
View
@@ -0,0 +1,8 @@
+require 'vero/view_helpers'
+module Vero
+ class Railtie < Rails::Railtie
+ initializer "vero.view_helpers" do
+ ActionView::Base.send :include, ViewHelpers
+ end
+ end
+end
View
@@ -79,7 +79,7 @@ def post_later(url, params)
def validate_configured!
unless Vero::App.configured?
- raise "You must configure the 'vero' gem. Visit https://bitbucket.org/semblance/vero/overview for more details."
+ raise "You must configure the 'vero' gem. Visit https://github.com/semblancesystems/vero for more details."
end
end
View
@@ -0,0 +1,38 @@
+module Vero
+ module ViewHelpers
+ def vero_javascript_tag(method = :default)
+ return "" unless Vero::App.configured?
+
+ unless [:default, :mixpanel, :kissmetrics].include?(method)
+ method = :default
+ end
+
+ method_name = method.to_s + "_vero_javascript_tag"
+ self.send(method_name.to_sym, Vero::App.config.config_params)
+ end
+
+ private
+ def default_vero_javascript_tag(options = {})
+ content_tag :script, {type: "text/javascript"} do
+ result = "var _veroq = _veroq || [];" +
+ "setTimeout(function(){if(typeof window.Semblance==\"undefined\"){console.log(\"Vero did not load in time.\");for(var i=0;i<_veroq.length;i++){a=_veroq[i];if(a.length==3&&typeof a[2]==\"function\")a[2](null,false);}}},3000);" +
+ "_veroq.push(['init', {" +
+ options_to_string(options) +
+ "}]);"
+ result.html_safe
+ end.html_safe
+ end
+
+ def mixpanel_vero_javascript_tag(options = {})
+ end
+
+ def kissmetrics_vero_javascript_tag(options = {})
+ end
+
+ def options_to_string(options)
+ options = {} unless options.kind_of?(Hash)
+ result = options.keys.collect { |k| "#{k}: \"#{options[k]}\"" }
+ result.join(", ")
+ end
+ end
+end
@@ -0,0 +1,41 @@
+require 'spec_helper'
+require 'action_view'
+require 'active_support'
+
+include Vero::ViewHelpers
+include ActionView::Helpers
+include ActionView::Context
+
+describe Vero::ViewHelpers do
+ before :each do
+ Vero::App.reset!
+ end
+
+ describe :vero_javascript_tag do
+ it "should return an empty string if Vero::App is not properly configured" do
+ Vero::ViewHelpers.vero_javascript_tag.should == ""
+
+ Vero::App.init {}
+ Vero::ViewHelpers.vero_javascript_tag.should == ""
+ end
+
+ context "Vero::App has been properly configured" do
+ before :each do
+ @api_key = "abcd1234"
+ @api_secret = "secret"
+ @api_dev_mode = false
+
+ Vero::App.init do |c|
+ c.api_key = @api_key
+ c.secret = @api_secret
+ c.development_mode = @api_dev_mode
+ end
+ end
+
+ it "should return a properly formatted javascript snippet" do
+ result = "<script type=\"text/javascript\">var _veroq = _veroq || [];setTimeout(function(){if(typeof window.Semblance==\"undefined\"){console.log(\"Vero did not load in time.\");for(var i=0;i<_veroq.length;i++){a=_veroq[i];if(a.length==3&&typeof a[2]==\"function\")a[2](null,false);}}},3000);_veroq.push(['init', {api_key: \"#{@api_key}\", secret: \"#{@api_secret}\"}]);</script>"
+ Vero::ViewHelpers.vero_javascript_tag.should == result
+ end
+ end
+ end
+end

0 comments on commit c11a2ed

Please sign in to comment.