Commit f5f1059d10148dba60f046f7c0ccffa7fba043fe

Authored by Antonio Terceiro
2 parents 342be5f0 0c38e980

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
app/helpers/layout_helper.rb
... ... @@ -9,6 +9,24 @@ module LayoutHelper
9 9 (!profile.nil? && profile.is_on_homepage?(request.path,@page) ? " profile-homepage" : "")
10 10 end
11 11  
  12 + def html_tag_classes
  13 + [
  14 + body_classes, (
  15 + profile.blank? ? nil : [
  16 + 'profile-type-is-' + profile.class.name.downcase,
  17 + 'profile-name-is-' + profile.identifier,
  18 + ]
  19 + ), 'theme-' + current_theme,
  20 + @plugins.dispatch(:html_tag_classes).map do |content|
  21 + if content.respond_to?(:call)
  22 + instance_exec(&content)
  23 + else
  24 + content.html_safe
  25 + end
  26 + end
  27 + ].flatten.compact.join(' ')
  28 + end
  29 +
12 30 def noosfero_javascript
13 31 plugins_javascripts = @plugins.map { |plugin| [plugin.js_files].flatten.map { |js| plugin.class.public_path(js) } }.flatten
14 32  
... ...
app/views/layouts/application-ng.html.erb
1 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2   -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= html_language %>" lang="<%= html_language %>">
  2 +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= html_language %>" lang="<%= html_language %>" class="<%= h html_tag_classes %>">
3 3 <head>
4 4 <title><%= h page_title %></title>
5 5 <%= yield(:feeds) %>
... ...
app/views/layouts/application.html.erb
1 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2   -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= html_language %>" lang="<%= html_language %>">
  2 +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= html_language %>" lang="<%= html_language %>" class="<%= h html_tag_classes %>">
3 3 <head>
4 4 <title><%= h page_title %></title>
5 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
... ...
lib/noosfero/plugin.rb
... ... @@ -560,6 +560,12 @@ class Noosfero::Plugin
560 560 []
561 561 end
562 562  
  563 + # -> Adds css class to <html> tag
  564 + # returns = ['class1', 'class2']
  565 + def html_tag_classes
  566 + nil
  567 + end
  568 +
563 569 # -> Adds additional blocks to profiles and environments.
564 570 # Your plugin must implements a class method called 'extra_blocks'
565 571 # that returns a hash with the following syntax.
... ...
plugins/classify_members/README 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +Classify Members
  2 +Plugin that allows the association of communities with types of user profiles to classify and highlight them within the environment.
  3 +
  4 + * In a school env we want to visit a profile and know it is a teacher or a student.
  5 + * In a enterprise env we want to view a forum comment and know it comes from a director or a lawyer.
  6 +
  7 +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 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +class ClassifyMembersPluginAdminController < PluginsController
  2 + def index
  3 + @settings ||= Noosfero::Plugin::Settings.new(
  4 + environment, ClassifyMembersPlugin, params[:settings]
  5 + )
  6 +
  7 + if request.post?
  8 + @settings.save!
  9 + redirect_to :controller => 'plugins', :action => 'index'
  10 + end
  11 + end
  12 +end
... ...
plugins/classify_members/lib/classify_members_plugin.rb 0 → 100644
... ... @@ -0,0 +1,78 @@
  1 +class ClassifyMembersPlugin < Noosfero::Plugin
  2 + def self.plugin_name
  3 + _("Classify Members")
  4 + end
  5 +
  6 + def self.plugin_description
  7 + _("Allows the association of communities with types of user profiles to classify and highlight them within the environment.")
  8 + end
  9 +
  10 + def html_tag_classes
  11 + plugin = self
  12 + lambda do
  13 + if profile && profile.person?
  14 + plugin.find_community(profile).map do |community, community_label|
  15 + 'member-of-' + community.identifier
  16 + end
  17 + end
  18 + end
  19 + end
  20 +
  21 + def body_beginning
  22 + plugin = self
  23 + lambda do
  24 + if profile && profile.person?
  25 + javascript_tag("
  26 + jQuery(function(){
  27 + jQuery('<div class=\"cmm-member-tags\"><ul class=\"cmm-member-list\"></ul></div>').insertBefore(
  28 + '.profile-image-block .vcard .profile-info-options'
  29 + );
  30 + });\n" +
  31 + plugin.find_community(profile).map do |community, community_label|
  32 + "jQuery(function(){
  33 + jQuery('.cmm-member-list').prepend(
  34 + '<li>' + '#{link_to '<i></i>' + community_label,
  35 + {:profile => community.identifier, :controller => 'profile', :action => 'members'},
  36 + :class => 'member-of-' + community.identifier}' + '</li>'
  37 + );
  38 + });"
  39 + end.join("\n")
  40 + )
  41 + else
  42 + '<!-- ClassCommunityPlugin not in a profile -->'
  43 + end
  44 + end
  45 + end
  46 +
  47 + def settings
  48 + @settings ||= Noosfero::Plugin::Settings.new(
  49 + context.environment, ClassifyMembersPlugin
  50 + )
  51 + end
  52 +
  53 + def communities
  54 + communities = settings.communities
  55 +
  56 + return [] if communities.blank?
  57 +
  58 + communities.split(/\s*\n\s*/).map do |community|
  59 + community = community.split(/\s*:\s*/)
  60 + community[0] = Profile[community[0].to_s.strip]
  61 + community[1] = community[1].to_s.strip
  62 +
  63 + if community[0].blank?
  64 + nil
  65 + else
  66 + community[1] = community[0].name if community[1].blank?
  67 + community
  68 + end
  69 + end.compact
  70 + end
  71 +
  72 + def find_community(profile)
  73 + communities.map do |community|
  74 + profile.is_member_of?(community[0]) ? community : nil
  75 + end.compact
  76 + end
  77 +
  78 +end
... ...
plugins/classify_members/test/functional/classify_members_plugin_test.rb 0 → 100644
... ... @@ -0,0 +1,45 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +
  3 +# Re-raise errors caught by the controller.
  4 +class HomeController
  5 + def rescue_action(e)
  6 + raise e
  7 + end
  8 +end
  9 +
  10 +class ProfileControllerTest < ActionController::TestCase
  11 + def setup
  12 + @env = Environment.default
  13 + @env.enable_plugin('ClassifyMembersPlugin')
  14 +
  15 + @p1 = fast_create(Person, :environment_id => @env.id)
  16 + @p2 = fast_create(Person, :environment_id => @env.id)
  17 + @c1 = fast_create(Community, :environment_id => @env.id)
  18 + @c2 = fast_create(Community, :environment_id => @env.id)
  19 +
  20 + # Register cassification communities:
  21 + ClassifyMembersPlugin.new(self).settings.communities = "#{@c1.identifier}: Test-Tag"
  22 + @env.save!
  23 +
  24 + @c1.add_member @p1
  25 + @c2.add_member @p1
  26 + @c2.add_member @p2
  27 + end
  28 +
  29 + def environment
  30 + @env
  31 + end
  32 +
  33 + should 'add classification to the <html>' do
  34 + get :index, :profile => @p1.identifier
  35 +
  36 + assert_select 'html.member-of-' + @c1.identifier
  37 + assert_select 'html.member-of-' + @c2.identifier, false
  38 + end
  39 +
  40 + should 'not add classification to a non member' do
  41 + get :index, :profile=>@p2.identifier
  42 +
  43 + assert_select 'html.member-of-' + @c1.identifier, false
  44 + end
  45 +end
... ...
plugins/classify_members/test/unit/classify_members_plugin_test.rb 0 → 100644
... ... @@ -0,0 +1,46 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +
  3 +class ClassifyMembersPluginTest < ActiveSupport::TestCase
  4 + def setup
  5 + @env = fast_create(Environment)
  6 + @p1 = fast_create(Person, :environment_id => @env.id)
  7 + @c1 = fast_create(Community, :environment_id => @env.id)
  8 + @c2 = fast_create(Community, :environment_id => @env.id)
  9 + @c3 = fast_create(Community, :environment_id => @env.id)
  10 + @plugin = ClassifyMembersPlugin.new self
  11 + end
  12 +
  13 + def environment
  14 + @env
  15 + end
  16 +
  17 + should 'not crash for nil setting' do
  18 + assert_equal [], @plugin.find_community(@p1)
  19 + end
  20 +
  21 + should 'list all classification communities' do
  22 + @plugin.settings.communities = "
  23 + #{@c1.identifier}: Tag1
  24 + #{@c2.identifier}
  25 + "
  26 + @env.save!
  27 +
  28 + assert_equal [[@c1, 'Tag1'], [@c2, @c2.name]], @plugin.communities
  29 + end
  30 +
  31 + should 'list the classification communities for a person' do
  32 + @c1.add_member @p1
  33 + @c2.add_member @p1
  34 + @p1.stubs(:is_member_of?).returns(false)
  35 + @p1.stubs(:is_member_of?).with(@c1).returns(true)
  36 + @p1.stubs(:is_member_of?).with(@c2).returns(true)
  37 + @plugin.settings.communities = "
  38 + #{@c1.identifier}: Tag1
  39 + #{@c2.identifier}: Tag2
  40 + #{@c3.identifier}: Tag3
  41 + "
  42 + @env.save!
  43 +
  44 + assert_equal [[@c1, 'Tag1'], [@c2, 'Tag2']], @plugin.find_community(@p1)
  45 + end
  46 +end
... ...
plugins/classify_members/views/classify_members_plugin_admin/index.html.erb 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +<h1><%= _("Classify Members Plugin's config") %></h1>
  2 +
  3 +<%= form_for(:settings) do |f| %>
  4 +
  5 + <%= labelled_form_field _('Communities to classify people:'), f.text_area(:communities) %>
  6 +
  7 + <p><%=
  8 + _('List of community identifiers and the applicable person label, line by line.')
  9 + %></p>
  10 +
  11 + <fieldset>
  12 + <legend><%=_('Example:')%></legend>
  13 + <%=_('teachers: Teacher')%> <br>
  14 + <%=_('office-lawyers: Lawyer')%> <br>
  15 + <%=_('salvador-ba: Soteropolitano')%>
  16 + </fieldset>
  17 +
  18 + <% button_bar do %>
  19 + <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
  20 + <% end %>
  21 +
  22 +<% end %>
... ...
test/functional/home_controller_test.rb
... ... @@ -130,4 +130,36 @@ class HomeControllerTest &lt; ActionController::TestCase
130 130 assert_no_tag :tag => 'a', :attributes => {:href => '/account/signup'}
131 131 end
132 132  
  133 + should 'add class to the <html>' do
  134 + get :index
  135 +
  136 + # Where am i?
  137 + assert_select 'html.controller-home.action-home-index'
  138 + # What is the current layout?
  139 + assert_select 'html.template-default.theme-noosfero'
  140 + end
  141 +
  142 + should 'plugins add class to the <html>' do
  143 + class Plugin1 < Noosfero::Plugin
  144 + def html_tag_classes
  145 + lambda { ['t1', 't2'] }
  146 + end
  147 + end
  148 +
  149 + class Plugin2 < Noosfero::Plugin
  150 + def html_tag_classes
  151 + 'test'
  152 + end
  153 + end
  154 +
  155 + Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])
  156 + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([Plugin1.new, Plugin2.new])
  157 +
  158 + get :index
  159 +
  160 + # Where am i?
  161 + assert_select 'html.controller-home.action-home-index'
  162 + # There are plugin classes?
  163 + assert_select 'html.t1.t2.test'
  164 + end
133 165 end
... ...