From 7d1cef867e1325f623638366faeb5387a9261b0a Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Mon, 15 Jun 2015 17:17:20 -0300 Subject: [PATCH] initial commit Noosfero Gravatar Provider Plugin --- controllers/gravatar_provider_plugin_controller.rb | 13 +++++++++++++ db/migrate/20150611070000_create_gravatar_provider_plugin_user_emails_hashes_table.rb | 25 +++++++++++++++++++++++++ lib/ext/user.rb | 15 +++++++++++++++ lib/gravatar_provider_plugin.rb | 15 +++++++++++++++ lib/gravatar_provider_plugin/user_email_hash.rb | 8 ++++++++ public/images/nophoto.png | Bin 0 -> 1163 bytes test/functional/gravatar_provider_plugin_controller_test.rb | 24 ++++++++++++++++++++++++ test/unit/user_email_hash_test.rb | 28 ++++++++++++++++++++++++++++ 8 files changed, 128 insertions(+), 0 deletions(-) create mode 100644 controllers/gravatar_provider_plugin_controller.rb create mode 100644 db/migrate/20150611070000_create_gravatar_provider_plugin_user_emails_hashes_table.rb create mode 100644 lib/ext/user.rb create mode 100644 lib/gravatar_provider_plugin.rb create mode 100644 lib/gravatar_provider_plugin/user_email_hash.rb create mode 100644 public/images/nophoto.png create mode 100644 test/functional/gravatar_provider_plugin_controller_test.rb create mode 100644 test/unit/user_email_hash_test.rb diff --git a/controllers/gravatar_provider_plugin_controller.rb b/controllers/gravatar_provider_plugin_controller.rb new file mode 100644 index 0000000..0590100 --- /dev/null +++ b/controllers/gravatar_provider_plugin_controller.rb @@ -0,0 +1,13 @@ +class GravatarProviderPluginController < PublicController + append_view_path File.join(File.dirname(__FILE__) + '/../views') + def h + begin + user_hash = GravatarProviderPlugin::UserEmailHash.where(email_md5_hash: params[:id]).first + redirect_to profile_icon(user_hash.user.person, :thumb) + rescue Exception => e + Rails.logger.error "GravatarProviderPlugin:: Cannot find profile image for hash [#{params[:id]}]" + redirect_to '/plugins/gravatar_provider/images/nophoto.png' + end + end + +end diff --git a/db/migrate/20150611070000_create_gravatar_provider_plugin_user_emails_hashes_table.rb b/db/migrate/20150611070000_create_gravatar_provider_plugin_user_emails_hashes_table.rb new file mode 100644 index 0000000..8a7925b --- /dev/null +++ b/db/migrate/20150611070000_create_gravatar_provider_plugin_user_emails_hashes_table.rb @@ -0,0 +1,25 @@ +class CreateGravatarProviderPluginUserEmailsHashesTable < ActiveRecord::Migration + def self.up + create_table :gravatar_provider_plugin_user_email_hashes do |t| + t.belongs_to :user + t.string :email_md5_hash + end + add_index( + :gravatar_provider_plugin_user_email_hashes, + [:email_md5_hash], + name: 'index_gravatar_provider_plugin_md5_hash' + ) + user_count = User.count + puts "Migrating #{user_count} user to table gravatar_provider_plugin_user_email_hashes..." + User.all.each do |user| + GravatarProviderPlugin::UserEmailHash.create! do |user_email_hash| + user_email_hash.user = user + user_email_hash.email_md5_hash = Digest::MD5.hexdigest(user.email) + end + end + puts "Migration gravatar_provider_plugin_user_email_hashes completed" + end + def self.down + drop_table :gravatar_provider_plugin_user_email_hashes + end +end diff --git a/lib/ext/user.rb b/lib/ext/user.rb new file mode 100644 index 0000000..06a1bd7 --- /dev/null +++ b/lib/ext/user.rb @@ -0,0 +1,15 @@ +require_dependency 'user' +require 'digest/md5' + +User.class_eval do + + after_create :insert_email_hash + + def insert_email_hash + GravatarProviderPlugin::UserEmailHash.create! do |user_email_hash| + user_email_hash.user = self + user_email_hash.email_md5_hash = Digest::MD5.hexdigest(self.email) + end + end + +end diff --git a/lib/gravatar_provider_plugin.rb b/lib/gravatar_provider_plugin.rb new file mode 100644 index 0000000..2ed7c1b --- /dev/null +++ b/lib/gravatar_provider_plugin.rb @@ -0,0 +1,15 @@ +class GravatarProviderPlugin < Noosfero::Plugin + + def self.plugin_name + "GravatarProviderPlugin" + end + + def self.plugin_description + _("A plugin that enable noosfero as a gravatar server") + end + + def stylesheet? + true + end + +end diff --git a/lib/gravatar_provider_plugin/user_email_hash.rb b/lib/gravatar_provider_plugin/user_email_hash.rb new file mode 100644 index 0000000..34329ec --- /dev/null +++ b/lib/gravatar_provider_plugin/user_email_hash.rb @@ -0,0 +1,8 @@ +class GravatarProviderPlugin::UserEmailHash < Noosfero::Plugin::ActiveRecord + belongs_to :user + + attr_accessible :email_md5_hash + + validates_presence_of :user, :email_md5_hash + +end diff --git a/public/images/nophoto.png b/public/images/nophoto.png new file mode 100644 index 0000000..5ce8620 Binary files /dev/null and b/public/images/nophoto.png differ diff --git a/test/functional/gravatar_provider_plugin_controller_test.rb b/test/functional/gravatar_provider_plugin_controller_test.rb new file mode 100644 index 0000000..1a27980 --- /dev/null +++ b/test/functional/gravatar_provider_plugin_controller_test.rb @@ -0,0 +1,24 @@ +require "test_helper" + +class GravatarProviderPluginControllerTest < ActionController::TestCase + fixtures :profiles, :environments, :users, :roles, :domains + + include ApplicationHelper + + def setup + user = create_user_full('testinguser') + @person = user.person + @user_email_hash = GravatarProviderPlugin::UserEmailHash.where(user_id: user.id).first + end + + def current_theme + 'default' + end + + def test_index_should_redirect_to_user_image + get :h, {id: @user_email_hash.email_md5_hash} + assert_redirected_to profile_icon(@person, :thumb) + end + + +end diff --git a/test/unit/user_email_hash_test.rb b/test/unit/user_email_hash_test.rb new file mode 100644 index 0000000..e1e02fa --- /dev/null +++ b/test/unit/user_email_hash_test.rb @@ -0,0 +1,28 @@ +require "test_helper" + +class UserEmailHashTest < ActiveSupport::TestCase + fixtures :profiles, :environments, :users, :roles, :domains + + def setup + @user_email_hash = GravatarProviderPlugin::UserEmailHash.new + end + + should 'not save without user' do + assert !@user_email_hash.save + end + + should 'require user' do + @user_email_hash.save + assert @user_email_hash.errors[:user].present? + end + + should 'require email_md5_hash' do + @user_email_hash.save + assert @user_email_hash.errors[:email_md5_hash].present? + end + + should 'have user_email_hash record crerated after user creation' do + user = create_user_full 'mytestuser' + assert GravatarProviderPlugin::UserEmailHash.where(user_id: user.id).count.eql?(1) + end +end -- libgit2 0.21.2