noosfero | 2 new commits pushed to repository

Antonio Terceiro gitlab at gitlab.com
Tue Jan 27 15:50:47 BRST 2015


Antonio Terceiro pushed to refs/heads/master at <a href="https://gitlab.com/noosfero/noosfero">Noosfero / noosfero</a>

Commits:
<a href="https://gitlab.com/noosfero/noosfero/commit/0c38e980063f1500dec03679e299501cce52f86d">0c38e980</a> by Arthur Del Esposte
Adds ClassCommunityPlugin

Signed-off-by: Arthur Del Esposte <arthurmde at gmail.com>
Signed-off-by: Aurélio A. Heckert <aurelio at colivre.coop.br>

- - - - -
<a href="https://gitlab.com/noosfero/noosfero/commit/f5f1059d10148dba60f046f7c0ccffa7fba043fe">f5f1059d</a> by Antonio Terceiro
Merge branch 'AI3297-ClassCommunity' into 'master'

Ai3297 class community

Fix the reviewed code in  https://gitlab.com/noosfero/noosfero/merge_requests/348

See merge request !404

- - - - -


Changes:

=====================================
app/helpers/layout_helper.rb
=====================================
--- a/app/helpers/layout_helper.rb
+++ b/app/helpers/layout_helper.rb
@@ -9,6 +9,24 @@ module LayoutHelper
     (!profile.nil? && profile.is_on_homepage?(request.path, at page) ? " profile-homepage" : "")
   end
 
+  def html_tag_classes
+    [
+      body_classes, (
+        profile.blank? ? nil : [
+          'profile-type-is-' + profile.class.name.downcase,
+          'profile-name-is-' + profile.identifier,
+        ]
+      ), 'theme-' + current_theme,
+      @plugins.dispatch(:html_tag_classes).map do |content|
+        if content.respond_to?(:call)
+          instance_exec(&content)
+        else
+          content.html_safe
+        end
+      end
+    ].flatten.compact.join(' ')
+  end
+
   def noosfero_javascript
     plugins_javascripts = @plugins.map { |plugin| [plugin.js_files].flatten.map { |js| plugin.class.public_path(js) } }.flatten
 

=====================================
app/views/layouts/application-ng.html.erb
=====================================
--- a/app/views/layouts/application-ng.html.erb
+++ b/app/views/layouts/application-ng.html.erb
@@ -1,5 +1,5 @@
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= html_language %>" lang="<%= html_language %>">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= html_language %>" lang="<%= html_language %>" class="<%= h html_tag_classes %>">
   <head>
     <title><%= h page_title %></title>
     <%= yield(:feeds) %>

=====================================
app/views/layouts/application.html.erb
=====================================
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -1,5 +1,5 @@
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= html_language %>" lang="<%= html_language %>">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= html_language %>" lang="<%= html_language %>" class="<%= h html_tag_classes %>">
   <head>
     <title><%= h page_title %></title>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

=====================================
lib/noosfero/plugin.rb
=====================================
--- a/lib/noosfero/plugin.rb
+++ b/lib/noosfero/plugin.rb
@@ -560,6 +560,12 @@ class Noosfero::Plugin
     []
   end
 
+  # -> Adds css class to <html> tag
+  # returns = ['class1', 'class2']
+  def html_tag_classes
+    nil
+  end
+
   # -> Adds additional blocks to profiles and environments.
   # Your plugin must implements a class method called 'extra_blocks'
   # that returns a hash with the following syntax.

=====================================
plugins/classify_members/README
=====================================
--- /dev/null
+++ b/plugins/classify_members/README
@@ -0,0 +1,7 @@
+Classify Members
+Plugin that allows the association of communities with types of user profiles to classify and highlight them within the environment.
+
+ * In a school env we want to visit a profile and know it is a teacher or a student.
+ * In a enterprise env we want to view a forum comment and know it comes from a director or a lawyer.
+
+This plugin will allow the admin to select communities for classification. When the user enter in one of these communities, its profile pages comes with a community related class allowing the theme to adapt it. Also the picture block and other places where the user picture is displayed will have labels like "Teacher" or "Lawyer", also themable with css.

=====================================
plugins/classify_members/controllers/classify_members_plugin_admin_controller.rb
=====================================
--- /dev/null
+++ b/plugins/classify_members/controllers/classify_members_plugin_admin_controller.rb
@@ -0,0 +1,12 @@
+class ClassifyMembersPluginAdminController < PluginsController
+  def index
+    @settings ||= Noosfero::Plugin::Settings.new(
+      environment, ClassifyMembersPlugin, params[:settings]
+    )
+
+    if request.post?
+      @settings.save!
+      redirect_to :controller => 'plugins', :action => 'index'
+    end
+  end
+end

=====================================
plugins/classify_members/lib/classify_members_plugin.rb
=====================================
--- /dev/null
+++ b/plugins/classify_members/lib/classify_members_plugin.rb
@@ -0,0 +1,78 @@
+class ClassifyMembersPlugin < Noosfero::Plugin
+  def self.plugin_name
+    _("Classify Members")
+  end
+
+  def self.plugin_description
+    _("Allows the association of communities with types of user profiles to classify and highlight them within the environment.")
+  end
+
+  def html_tag_classes
+    plugin = self
+    lambda do
+      if profile && profile.person?
+        plugin.find_community(profile).map do |community, community_label|
+          'member-of-' + community.identifier
+        end
+      end
+    end
+  end
+
+  def body_beginning
+    plugin = self
+    lambda do
+      if profile && profile.person?
+        javascript_tag("
+          jQuery(function(){
+            jQuery('<div class=\"cmm-member-tags\"><ul class=\"cmm-member-list\"></ul></div>').insertBefore(
+              '.profile-image-block .vcard .profile-info-options'
+            );
+          });\n" +
+          plugin.find_community(profile).map do |community, community_label|
+          "jQuery(function(){
+            jQuery('.cmm-member-list').prepend(
+              '<li>' + '#{link_to '<i></i>' + community_label,
+                {:profile => community.identifier, :controller => 'profile', :action => 'members'},
+                :class => 'member-of-' + community.identifier}' + '</li>'
+            );
+          });"
+          end.join("\n")
+        )
+      else
+        '<!-- ClassCommunityPlugin not in a profile -->'
+      end
+    end
+  end
+
+  def settings
+    @settings ||= Noosfero::Plugin::Settings.new(
+      context.environment, ClassifyMembersPlugin
+    )
+  end
+
+  def communities
+    communities = settings.communities
+
+    return [] if communities.blank?
+
+    communities.split(/\s*\n\s*/).map do |community|
+      community = community.split(/\s*:\s*/)
+      community[0] = Profile[community[0].to_s.strip]
+      community[1] = community[1].to_s.strip
+
+      if community[0].blank?
+        nil
+      else
+        community[1] = community[0].name if community[1].blank?
+        community
+      end
+    end.compact
+  end
+
+  def find_community(profile)
+    communities.map do |community|
+      profile.is_member_of?(community[0]) ? community : nil
+    end.compact
+  end
+
+end

=====================================
plugins/classify_members/test/functional/classify_members_plugin_test.rb
=====================================
--- /dev/null
+++ b/plugins/classify_members/test/functional/classify_members_plugin_test.rb
@@ -0,0 +1,45 @@
+require File.dirname(__FILE__) + '/../../../../test/test_helper'
+
+# Re-raise errors caught by the controller.
+class HomeController
+  def rescue_action(e)
+    raise e
+  end
+end
+
+class ProfileControllerTest < ActionController::TestCase
+  def setup
+    @env = Environment.default
+    @env.enable_plugin('ClassifyMembersPlugin')
+
+    @p1  = fast_create(Person, :environment_id => @env.id)
+    @p2  = fast_create(Person, :environment_id => @env.id)
+    @c1  = fast_create(Community, :environment_id => @env.id)
+    @c2  = fast_create(Community, :environment_id => @env.id)
+
+    # Register cassification communities:
+    ClassifyMembersPlugin.new(self).settings.communities = "#{@c1.identifier}: Test-Tag"
+    @env.save!
+
+    @c1.add_member @p1
+    @c2.add_member @p1
+    @c2.add_member @p2
+  end
+
+  def environment
+    @env
+  end
+
+  should 'add classification to the <html>' do
+    get :index, :profile => @p1.identifier
+
+    assert_select 'html.member-of-' + @c1.identifier
+    assert_select 'html.member-of-' + @c2.identifier, false
+  end
+
+  should 'not add classification to a non member' do
+    get :index, :profile=>@p2.identifier
+
+    assert_select 'html.member-of-' + @c1.identifier, false
+  end
+end

=====================================
plugins/classify_members/test/unit/classify_members_plugin_test.rb
=====================================
--- /dev/null
+++ b/plugins/classify_members/test/unit/classify_members_plugin_test.rb
@@ -0,0 +1,46 @@
+require File.dirname(__FILE__) + '/../../../../test/test_helper'
+
+class ClassifyMembersPluginTest < ActiveSupport::TestCase
+  def setup
+    @env = fast_create(Environment)
+    @p1  = fast_create(Person, :environment_id => @env.id)
+    @c1  = fast_create(Community, :environment_id => @env.id)
+    @c2  = fast_create(Community, :environment_id => @env.id)
+    @c3  = fast_create(Community, :environment_id => @env.id)
+    @plugin = ClassifyMembersPlugin.new self
+  end
+
+  def environment
+    @env
+  end
+
+  should 'not crash for nil setting' do
+    assert_equal [], @plugin.find_community(@p1)
+  end
+
+  should 'list all classification communities' do
+    @plugin.settings.communities = "
+    #{@c1.identifier}: Tag1
+    #{@c2.identifier}
+    "
+    @env.save!
+
+    assert_equal [[@c1, 'Tag1'], [@c2, @c2.name]], @plugin.communities
+  end
+
+  should 'list the classification communities for a person' do
+    @c1.add_member @p1
+    @c2.add_member @p1
+    @p1.stubs(:is_member_of?).returns(false)
+    @p1.stubs(:is_member_of?).with(@c1).returns(true)
+    @p1.stubs(:is_member_of?).with(@c2).returns(true)
+    @plugin.settings.communities = "
+    #{@c1.identifier}: Tag1
+    #{@c2.identifier}: Tag2
+    #{@c3.identifier}: Tag3
+    "
+    @env.save!
+
+    assert_equal [[@c1, 'Tag1'], [@c2, 'Tag2']], @plugin.find_community(@p1)
+  end
+end

=====================================
plugins/classify_members/views/classify_members_plugin_admin/index.html.erb
=====================================
--- /dev/null
+++ b/plugins/classify_members/views/classify_members_plugin_admin/index.html.erb
@@ -0,0 +1,22 @@
+<h1><%= _("Classify Members Plugin's config") %></h1>
+
+<%= form_for(:settings) do |f| %>
+
+  <%= labelled_form_field _('Communities to classify people:'), f.text_area(:communities) %>
+
+  <p><%=
+    _('List of community identifiers and the applicable person label, line by line.')
+  %></p>
+
+  <fieldset>
+    <legend><%=_('Example:')%></legend>
+    <%=_('teachers: Teacher')%> <br>
+    <%=_('office-lawyers: Lawyer')%> <br>
+    <%=_('salvador-ba: Soteropolitano')%>
+  </fieldset>
+
+  <% button_bar do %>
+    <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
+  <% end %>
+
+<% end %>

=====================================
test/functional/home_controller_test.rb
=====================================
--- a/test/functional/home_controller_test.rb
+++ b/test/functional/home_controller_test.rb
@@ -130,4 +130,36 @@ class HomeControllerTest < ActionController::TestCase
     assert_no_tag :tag => 'a', :attributes => {:href => '/account/signup'}
   end
 
+  should 'add class to the <html>' do
+    get :index
+
+    # Where am i?
+    assert_select 'html.controller-home.action-home-index'
+    # What is the current layout?
+    assert_select 'html.template-default.theme-noosfero'
+  end
+
+  should 'plugins add class to the <html>' do
+    class Plugin1 < Noosfero::Plugin
+      def html_tag_classes
+        lambda { ['t1', 't2'] }
+      end
+    end
+
+    class Plugin2 < Noosfero::Plugin
+      def html_tag_classes
+        'test'
+      end
+    end
+
+    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])
+    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([Plugin1.new, Plugin2.new])
+
+    get :index
+
+    # Where am i?
+    assert_select 'html.controller-home.action-home-index'
+    # There are plugin classes?
+    assert_select 'html.t1.t2.test'
+  end
 end

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150127/db5e00ee/attachment-0001.html>


More information about the Noosfero-dev mailing list