diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb index 0b72b5d..82fe50d 100644 --- a/app/helpers/layout_helper.rb +++ b/app/helpers/layout_helper.rb @@ -8,6 +8,24 @@ module LayoutHelper (!profile.nil? && profile.is_on_homepage?(request.path,@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 diff --git a/app/views/layouts/application-ng.html.erb b/app/views/layouts/application-ng.html.erb index cf046e5..6afe146 100644 --- a/app/views/layouts/application-ng.html.erb +++ b/app/views/layouts/application-ng.html.erb @@ -1,5 +1,5 @@ - + <%= h page_title %> <%= yield(:feeds) %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 6fe955a..dc95b04 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,5 +1,5 @@ - + <%= h page_title %> diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index 6d44a2d..8f45610 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -554,6 +554,12 @@ class Noosfero::Plugin [] end + # -> Adds css class to 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. diff --git a/plugins/classify_members/README b/plugins/classify_members/README new file mode 100644 index 0000000..01cb6da --- /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. diff --git a/plugins/classify_members/controllers/classify_members_plugin_admin_controller.rb b/plugins/classify_members/controllers/classify_members_plugin_admin_controller.rb new file mode 100644 index 0000000..b7048a4 --- /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 diff --git a/plugins/classify_members/lib/classify_members_plugin.rb b/plugins/classify_members/lib/classify_members_plugin.rb new file mode 100644 index 0000000..6745b17 --- /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('
').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( + '
  • ' + '#{link_to '' + community_label, + {:profile => community.identifier, :controller => 'profile', :action => 'members'}, + :class => 'member-of-' + community.identifier}' + '
  • ' + ); + });" + end.join("\n") + ) + else + '' + 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 diff --git a/plugins/classify_members/test/functional/classify_members_plugin_test.rb b/plugins/classify_members/test/functional/classify_members_plugin_test.rb new file mode 100644 index 0000000..9901c23 --- /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 ' 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 diff --git a/plugins/classify_members/test/unit/classify_members_plugin_test.rb b/plugins/classify_members/test/unit/classify_members_plugin_test.rb new file mode 100644 index 0000000..26344bd --- /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 diff --git a/plugins/classify_members/views/classify_members_plugin_admin/index.html.erb b/plugins/classify_members/views/classify_members_plugin_admin/index.html.erb new file mode 100644 index 0000000..69ca4a0 --- /dev/null +++ b/plugins/classify_members/views/classify_members_plugin_admin/index.html.erb @@ -0,0 +1,22 @@ +

    <%= _("Classify Members Plugin's config") %>

    + +<%= form_for(:settings) do |f| %> + + <%= labelled_form_field _('Communities to classify people:'), f.text_area(:communities) %> + +

    <%= + _('List of community identifiers and the applicable person label, line by line.') + %>

    + +
    + <%=_('Example:')%> + <%=_('teachers: Teacher')%>
    + <%=_('office-lawyers: Lawyer')%>
    + <%=_('salvador-ba: Soteropolitano')%> +
    + + <% button_bar do %> + <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %> + <% end %> + +<% end %> diff --git a/test/functional/home_controller_test.rb b/test/functional/home_controller_test.rb index 855c61e..7fe6702 100644 --- 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 ' 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 ' 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 -- libgit2 0.21.2