[noosfero/noosfero][master] 2 commits: fixes for google_analytics_plugin

Antonio Terceiro gitlab at gitlab.com
Tue Apr 14 16:16:59 BRT 2015


Antonio Terceiro pushed to master at Noosfero / noosfero


Commits:
9fd165ce by Daniela Feitosa at 2015-04-14T15:33:30Z
fixes for google_analytics_plugin

- allow mass-assignment
- Replaced .rhtml by .html.erb
- updated script code

- - - - -
20d51a81 by Antonio Terceiro at 2015-04-14T19:16:33Z
Merge branch 'ga_plugin' into 'master'

fixes for google_analytics_plugin

The plugin wasn't working

Fixes:
- allow mass-assignment
- Replaced .rhtml by .html.erb
- updated script code

See merge request !544

- - - - -


7 changed files:

- plugins/google_analytics/lib/ext/profile.rb
- plugins/google_analytics/lib/google_analytics_plugin.rb
- + plugins/google_analytics/test/functional/profile_editor_controller_test.rb
- plugins/google_analytics/test/unit/google_analytics_plugin_test.rb
- plugins/google_analytics/views/profile-editor-extras.rhtml → plugins/google_analytics/views/profile-editor-extras.html.erb
- + plugins/google_analytics/views/tracking-code.html.erb
- − plugins/google_analytics/views/tracking-code.rhtml


Changes:

=====================================
plugins/google_analytics/lib/ext/profile.rb
=====================================
--- a/plugins/google_analytics/lib/ext/profile.rb
+++ b/plugins/google_analytics/lib/ext/profile.rb
@@ -2,4 +2,9 @@ require_dependency 'profile'
 
 class Profile
   settings_items :google_analytics_profile_id
+  attr_accessible :google_analytics_profile_id
+
+  descendants.each do |descendant|
+    descendant.attr_accessible :google_analytics_profile_id
+  end
 end

=====================================
plugins/google_analytics/lib/google_analytics_plugin.rb
=====================================
--- a/plugins/google_analytics/lib/google_analytics_plugin.rb
+++ b/plugins/google_analytics/lib/google_analytics_plugin.rb
@@ -19,12 +19,15 @@ class GoogleAnalyticsPlugin < Noosfero::Plugin
 
   def head_ending
     unless profile_id.blank?
-      expanded_template('tracking-code.rhtml',{:profile_id => profile_id})
+      expanded_template('tracking-code.html.erb',{:profile_id => profile_id})
     end
   end
 
   def profile_editor_extras
-    expanded_template('profile-editor-extras.rhtml',{:profile_id => profile_id})
+    analytics_id = profile_id
+    lambda {
+      render :file => 'profile-editor-extras', :locals => { :profile_id => analytics_id }
+    }
   end
 
 end

=====================================
plugins/google_analytics/test/functional/profile_editor_controller_test.rb
=====================================
--- /dev/null
+++ b/plugins/google_analytics/test/functional/profile_editor_controller_test.rb
@@ -0,0 +1,31 @@
+require 'test_helper'
+require 'profile_editor_controller'
+
+# Re-raise errors caught by the controller.
+class ProfileEditorController; def rescue_action(e) raise e end; end
+
+class ProfileEditorControllerTest < ActionController::TestCase
+
+  def setup
+    @controller = ProfileEditorController.new
+    @request    = ActionController::TestRequest.new
+    @response   = ActionController::TestResponse.new
+    @profile = create_user('default_user').person
+    login_as(@profile.identifier)
+    Environment.default.enable_plugin(GoogleAnalyticsPlugin.name)
+  end
+
+  attr_accessor :profile
+
+  should 'add extra fields to profile editor info and settings' do
+    get :edit, :profile => profile.identifier
+    assert_tag_in_string @response.body, :tag => 'label', :content => /Google Analytics/,  :attributes => { :for => 'profile_data_google_analytics_profile_id' }
+    assert_tag_in_string @response.body, :tag => 'input', :attributes => { :id => 'profile_data_google_analytics_profile_id' }
+  end
+
+  should 'save code filled in on field' do
+    post :edit, :profile => profile.identifier, :profile_data => {:google_analytics_profile_id => 12345678}
+    assert_equal '12345678', Person.find(profile.id).google_analytics_profile_id
+  end
+
+end

=====================================
plugins/google_analytics/test/unit/google_analytics_plugin_test.rb
=====================================
--- a/plugins/google_analytics/test/unit/google_analytics_plugin_test.rb
+++ b/plugins/google_analytics/test/unit/google_analytics_plugin_test.rb
@@ -27,11 +27,6 @@ class GoogleAnalyticsPluginTest < ActiveSupport::TestCase
     assert_equal 'content', @plugin.head_ending
   end
 
-  should 'add extra fields to profile editor info and settings' do
-    assert_tag_in_string @plugin.profile_editor_extras,
-      :tag => 'input', :attributes => {:id => 'profile_data_google_analytics_profile_id', :value => 10}
-  end
-
   should 'extends Profile with attr google_analytics_profile_id' do
     assert_respond_to Profile.new, :google_analytics_profile_id
   end

=====================================
plugins/google_analytics/views/profile-editor-extras.rhtml → plugins/google_analytics/views/profile-editor-extras.html.erb
=====================================
--- a/plugins/google_analytics/views/profile-editor-extras.rhtml
+++ b/plugins/google_analytics/views/profile-editor-extras.html.erb
@@ -1,5 +1,3 @@
-<% extend ApplicationHelper %>
-
 <h2><%= c_('Statistics') %></h2>
 <%= labelled_form_field(_('Google Analytics Profile ID'), text_field(:profile_data, :google_analytics_profile_id, :value => profile_id)) %>
 <%= link_to(_('See how to configure statistics for your profile'), '/doc/plugins/google_analytics', :target => '_blank') %>

=====================================
plugins/google_analytics/views/tracking-code.html.erb
=====================================
--- /dev/null
+++ b/plugins/google_analytics/views/tracking-code.html.erb
@@ -0,0 +1,9 @@
+<script>
+  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+
+  ga('create', '<%= escape_javascript locals[:profile_id] %>', 'auto');
+  ga('send', 'pageview');
+</script>

=====================================
plugins/google_analytics/views/tracking-code.rhtml deleted
=====================================
--- a/plugins/google_analytics/views/tracking-code.rhtml
+++ /dev/null
@@ -1,10 +0,0 @@
-<script type="text/javascript">
-  var _gaq = _gaq || [];
-  _gaq.push(['_setAccount', '<%= escape_javascript locals[:profile_id] %>']);
-  _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>


View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/a1459c79bfe2f67f9eee286fd8e054cf87e2c7cf...20d51a818ccf8fe47ac7bd83249486a394df3c6d
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150414/279b1bc8/attachment-0001.html>


More information about the Noosfero-dev mailing list