Commit 44a57617e2b93b1a618f336014daee688e4eefc7

Authored by Joenio Costa
1 parent 8d889f08

GoogleCsePlugin - Google Custom Search Noosfero Plugin

(ActionItem2204)
app/views/layouts/application-ng.rhtml
... ... @@ -63,7 +63,7 @@
63 63 <%= render :file => 'account/login', :locals => { :is_thickbox => true } %>
64 64 </div>
65 65 </span>
66   - <form action="/search" class="search_form" method="get" class="clean">
  66 + <form action="/search" class="search_form clean" method="get" id="top-search">
67 67 <input name="query" size="15" value="<%=_('Search...')%>"
68 68 onfocus="this.form.className='focused';
69 69 if(this.value=='<%=_('Search...')%>'){this.value=''}"
... ...
lib/noosfero/plugin.rb
... ... @@ -7,6 +7,10 @@ class Noosfero::Plugin
7 7  
8 8 class << self
9 9  
  10 + def klass(dir)
  11 + (dir.to_s.camelize + 'Plugin').constantize # load the plugin
  12 + end
  13 +
10 14 def init_system
11 15 Dir.glob(File.join(Rails.root, 'config', 'plugins', '*')).select do |entry|
12 16 File.directory?(entry)
... ... @@ -18,8 +22,7 @@ class Noosfero::Plugin
18 22 path << File.join(dir, 'lib')
19 23 end
20 24  
21   - plugin_name = File.basename(dir).camelize + 'Plugin'
22   - plugin_name.constantize # load the plugin
  25 + klass(File.basename(dir))
23 26 end
24 27 end
25 28  
... ...
lib/noosfero/plugin/manager.rb
... ... @@ -22,4 +22,11 @@ class Noosfero::Plugin::Manager
22 22 end
23 23 end
24 24  
  25 + def [](name)
  26 + klass = Noosfero::Plugin.klass(name)
  27 + enabled_plugins.select do |plugin|
  28 + plugin.kind_of?(klass)
  29 + end.first
  30 + end
  31 +
25 32 end
... ...
plugins/google_cse/controllers/google_cse_plugin_environment_controller.rb 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +class GoogleCsePluginEnvironmentController < ApplicationController
  2 + append_view_path File.join(File.dirname(__FILE__) + '/../views')
  3 + no_design_blocks
  4 +end
... ...
plugins/google_cse/doc/google_cse.textile 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +h1. GoogleCsePlugin
  2 +
  3 +A plugin that use the Google Custom Search as Noosfero search engine.
  4 +
  5 +h2. Usage
  6 +
  7 +* Register a Google Custom Search ID at http://www.google.com/cse
  8 +* Set at least basic config, site list and look and fell of your Google Cse ID
  9 +* Configure Noosfero environment with this ID
  10 +** >> env = Environment.find( ... )
  11 +** >> env.settings[:google_cse_id] = '&lt;PUT ID HERE&gt;'
  12 +** >> env.save!
  13 +* That's all! The top search text-field of Noosfero will be used to make searchs
... ...
plugins/google_cse/lib/google_cse_plugin.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +class GoogleCsePlugin < Noosfero::Plugin
  2 +
  3 + def self.plugin_name
  4 + "GoogleCsePlugin"
  5 + end
  6 +
  7 + def self.plugin_description
  8 + _("A plugin that use the Google Custom Search as Noosfero search engine.")
  9 + end
  10 +
  11 + def google_id
  12 + context.environment.settings[:google_cse_id]
  13 + end
  14 +
  15 + def self.results_url_path
  16 + '/plugin/google_cse/results'
  17 + end
  18 +
  19 + def body_beginning
  20 + unless google_id.blank?
  21 + expanded_template('search-box.rhtml', {:selector => '#top-search, #footer-search', :plugin => self})
  22 + end
  23 + end
  24 +
  25 +end
... ...
plugins/google_cse/test/functional/google_cse_plugin_environment_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require File.dirname(__FILE__) + '/../../controllers/google_cse_plugin_environment_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class GoogleCsePluginEnvironmentController; def rescue_action(e) raise e end; end
  6 +
  7 +class GoogleCsePluginEnvironmentControllerTest < Test::Unit::TestCase
  8 +
  9 + def setup
  10 + @controller = GoogleCsePluginEnvironmentController.new
  11 + @request = ActionController::TestRequest.new
  12 + @response = ActionController::TestResponse.new
  13 + end
  14 +
  15 + should 'get results page' do
  16 + get :results
  17 + assert_response :success
  18 + end
  19 +
  20 +end
... ...
plugins/google_cse/test/unit/google_cse_plugin_test.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +
  3 +class GoogleCsePluginTest < Test::Unit::TestCase
  4 +
  5 + def setup
  6 + @plugin = GoogleCsePlugin.new
  7 + @context = mock()
  8 + @plugin.context = @context
  9 + @env = Environment.new
  10 + @plugin.context.stubs(:environment).returns(@env)
  11 + end
  12 +
  13 + should 'get google_id from environment' do
  14 + @env.stubs(:settings).returns({:google_cse_id => 10})
  15 + assert_equal 10, @plugin.google_id
  16 + end
  17 +
  18 + should 'not use custom search if google_cse_id isnt set' do
  19 + @env.stubs(:settings).returns({})
  20 + assert_nil @plugin.body_beginning
  21 + @env.stubs(:settings).returns({:google_cse_id => 11})
  22 + assert_not_nil @plugin.body_beginning
  23 + end
  24 +
  25 +end
... ...
plugins/google_cse/views/google_cse_plugin_environment/results.rhtml 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +<% plugin = @plugins[:google_cse] %>
  2 +<div id="cse" style="width: 100%;"><%= _('Loading') %></div>
  3 +<script src="//www.google.com/jsapi" type="text/javascript"></script>
  4 +<script type="text/javascript">
  5 + function parseQueryFromUrl () {
  6 + var queryParamName = "q";
  7 + var search = window.location.search.substr(1);
  8 + var parts = search.split('&');
  9 + for (var i = 0; i < parts.length; i++) {
  10 + var keyvaluepair = parts[i].split('=');
  11 + if (decodeURIComponent(keyvaluepair[0]) == queryParamName) {
  12 + return decodeURIComponent(keyvaluepair[1].replace(/\+/g, ' '));
  13 + }
  14 + }
  15 + return '';
  16 + }
  17 + google.load('search', '1', {style : google.loader.themes.MINIMALIST});
  18 + google.setOnLoadCallback(function() {
  19 + var customSearchControl = new google.search.CustomSearchControl(
  20 + '<%= plugin && plugin.google_id %>');
  21 + customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
  22 + var options = new google.search.DrawOptions();
  23 + options.enableSearchResultsOnly();
  24 + customSearchControl.draw('cse', options);
  25 + var queryFromUrl = parseQueryFromUrl();
  26 + if (queryFromUrl) {
  27 + customSearchControl.execute(queryFromUrl);
  28 + }
  29 + }, true);
  30 +</script>
... ...
plugins/google_cse/views/search-box.rhtml 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +<script type="text/javascript">
  2 + jQuery(function($) {
  3 + $('<%= locals[:selector] %>')
  4 + .attr({class: "cse-search-box", action: "<%= GoogleCsePlugin.results_url_path %>"})
  5 + .append('<input type="hidden" name="cx" value="<%= locals[:plugin].google_id %>" /><input type="hidden" name="cof" value="FORID:10" /><input type="hidden" name="ie" value="UTF-8" /><input type="hidden" name="siteurl" value="<%= context.environment.default_hostname %>">')
  6 + .children("input[name='query']")
  7 + .attr('name', 'q')
  8 + .attr('id', 'q');
  9 + });
  10 +</script>
... ...