From 44a57617e2b93b1a618f336014daee688e4eefc7 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Sun, 8 Jan 2012 19:55:36 -0200 Subject: [PATCH] GoogleCsePlugin - Google Custom Search Noosfero Plugin --- app/views/layouts/application-ng.rhtml | 2 +- lib/noosfero/plugin.rb | 7 +++++-- lib/noosfero/plugin/manager.rb | 7 +++++++ plugins/google_cse/controllers/google_cse_plugin_environment_controller.rb | 4 ++++ plugins/google_cse/doc/google_cse.textile | 13 +++++++++++++ plugins/google_cse/lib/google_cse_plugin.rb | 25 +++++++++++++++++++++++++ plugins/google_cse/test/functional/google_cse_plugin_environment_controller_test.rb | 20 ++++++++++++++++++++ plugins/google_cse/test/unit/google_cse_plugin_test.rb | 25 +++++++++++++++++++++++++ plugins/google_cse/views/google_cse_plugin_environment/results.rhtml | 30 ++++++++++++++++++++++++++++++ plugins/google_cse/views/search-box.rhtml | 10 ++++++++++ 10 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 plugins/google_cse/controllers/google_cse_plugin_environment_controller.rb create mode 100644 plugins/google_cse/doc/google_cse.textile create mode 100644 plugins/google_cse/lib/google_cse_plugin.rb create mode 100644 plugins/google_cse/test/functional/google_cse_plugin_environment_controller_test.rb create mode 100644 plugins/google_cse/test/unit/google_cse_plugin_test.rb create mode 100644 plugins/google_cse/views/google_cse_plugin_environment/results.rhtml create mode 100644 plugins/google_cse/views/search-box.rhtml diff --git a/app/views/layouts/application-ng.rhtml b/app/views/layouts/application-ng.rhtml index 95af2c0..c70583e 100644 --- a/app/views/layouts/application-ng.rhtml +++ b/app/views/layouts/application-ng.rhtml @@ -63,7 +63,7 @@ <%= render :file => 'account/login', :locals => { :is_thickbox => true } %> -
+ > env = Environment.find( ... ) +** >> env.settings[:google_cse_id] = '<PUT ID HERE>' +** >> env.save! +* That's all! The top search text-field of Noosfero will be used to make searchs diff --git a/plugins/google_cse/lib/google_cse_plugin.rb b/plugins/google_cse/lib/google_cse_plugin.rb new file mode 100644 index 0000000..750f725 --- /dev/null +++ b/plugins/google_cse/lib/google_cse_plugin.rb @@ -0,0 +1,25 @@ +class GoogleCsePlugin < Noosfero::Plugin + + def self.plugin_name + "GoogleCsePlugin" + end + + def self.plugin_description + _("A plugin that use the Google Custom Search as Noosfero search engine.") + end + + def google_id + context.environment.settings[:google_cse_id] + end + + def self.results_url_path + '/plugin/google_cse/results' + end + + def body_beginning + unless google_id.blank? + expanded_template('search-box.rhtml', {:selector => '#top-search, #footer-search', :plugin => self}) + end + end + +end diff --git a/plugins/google_cse/test/functional/google_cse_plugin_environment_controller_test.rb b/plugins/google_cse/test/functional/google_cse_plugin_environment_controller_test.rb new file mode 100644 index 0000000..c9860cc --- /dev/null +++ b/plugins/google_cse/test/functional/google_cse_plugin_environment_controller_test.rb @@ -0,0 +1,20 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../../controllers/google_cse_plugin_environment_controller' + +# Re-raise errors caught by the controller. +class GoogleCsePluginEnvironmentController; def rescue_action(e) raise e end; end + +class GoogleCsePluginEnvironmentControllerTest < Test::Unit::TestCase + + def setup + @controller = GoogleCsePluginEnvironmentController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + should 'get results page' do + get :results + assert_response :success + end + +end diff --git a/plugins/google_cse/test/unit/google_cse_plugin_test.rb b/plugins/google_cse/test/unit/google_cse_plugin_test.rb new file mode 100644 index 0000000..6166c20 --- /dev/null +++ b/plugins/google_cse/test/unit/google_cse_plugin_test.rb @@ -0,0 +1,25 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' + +class GoogleCsePluginTest < Test::Unit::TestCase + + def setup + @plugin = GoogleCsePlugin.new + @context = mock() + @plugin.context = @context + @env = Environment.new + @plugin.context.stubs(:environment).returns(@env) + end + + should 'get google_id from environment' do + @env.stubs(:settings).returns({:google_cse_id => 10}) + assert_equal 10, @plugin.google_id + end + + should 'not use custom search if google_cse_id isnt set' do + @env.stubs(:settings).returns({}) + assert_nil @plugin.body_beginning + @env.stubs(:settings).returns({:google_cse_id => 11}) + assert_not_nil @plugin.body_beginning + end + +end diff --git a/plugins/google_cse/views/google_cse_plugin_environment/results.rhtml b/plugins/google_cse/views/google_cse_plugin_environment/results.rhtml new file mode 100644 index 0000000..6090fdf --- /dev/null +++ b/plugins/google_cse/views/google_cse_plugin_environment/results.rhtml @@ -0,0 +1,30 @@ +<% plugin = @plugins[:google_cse] %> +
<%= _('Loading') %>
+ + diff --git a/plugins/google_cse/views/search-box.rhtml b/plugins/google_cse/views/search-box.rhtml new file mode 100644 index 0000000..c57ddde --- /dev/null +++ b/plugins/google_cse/views/search-box.rhtml @@ -0,0 +1,10 @@ + -- libgit2 0.21.2