[Git][noosfero/noosfero][master] 3 commits: menu_block: add dynamic links to menu

Victor Costa gitlab at mg.gitlab.com
Mon Apr 17 13:24:34 BRT 2017


Victor Costa pushed to branch master at Noosfero / noosfero


Commits:
867dfb26 by Josafá Souza Jr at 2017-04-17T10:03:57-03:00
menu_block: add dynamic links to menu

- - - - -
5da2cb07 by Victor Costa at 2017-04-17T12:31:21-03:00
menu_block: fix permissions for profile links

- - - - -
6df0c892 by Victor Costa at 2017-04-17T16:24:09+00:00
Merge branch 'block-menu-create-link' into 'master'

Block menu create link

See merge request !1169
- - - - -


3 changed files:

- app/models/menu_block.rb
- app/views/blocks/menu.html.erb
- test/unit/menu_block_test.rb


Changes:

=====================================
app/models/menu_block.rb
=====================================
--- a/app/models/menu_block.rb
+++ b/app/models/menu_block.rb
@@ -1,6 +1,10 @@
 class MenuBlock < Block
 
   include SanitizeHelper
+
+  attr_accessible :enabled_links, :api_content
+  settings_items :enabled_links, type: Array, :default => []
+
   def self.description
     _('Menu Block')
   end
@@ -13,20 +17,30 @@ class MenuBlock < Block
     _('Menu Block')
   end
 
-  def enabled_links(user)
+  def available_links
     links = []
-    links << {title: _('Activities'), controller: 'profile', action: 'activities'} if display_activities?(user)
-    links << {title: _('About'), controller: 'profile', action: 'about'} if display_about?(user)
-    links << {title: _('Communities'), controller: 'memberships', action: 'index'} if display_communities?(user)
-    links << {title: _('People'), controller: 'friends', action: 'index'} if display_friends?(user)
-    links << {title: _('People'), controller: 'profile_members', action: 'index'} if display_members?(user)
-    links << {title: _('Control Panel')}.merge(owner.admin_url) if display_control_panel?(user)
+    links << {title: _('Activities'), controller: 'profile', action: 'activities', condition: -> (user) { display_activities?(user) } }
+    links << {title: _('About'), controller: 'profile', action: 'about', condition: -> (user) { display_about?(user) } }
+    links << {title: _('Communities'), controller: 'memberships', action: 'index', condition: -> (user) { display_communities?(user) } }
+    links << {title: _('People'), controller: 'friends', action: 'index', condition: -> (user) { display_friends?(user) } }
+    links << {title: _('People'), controller: 'profile_members', action: 'index', condition: -> (user) { display_members?(user) } }
+    links << {title: _('Control Panel'), condition: -> (user) { display_control_panel?(user) } }.merge(owner.admin_url)
     links
   end
 
+  def enabled_links_for(user)
+    filter_links user, enabled_links.empty? ? available_links : enabled_links
+  end
+
   def api_content(options = {})
-    links = self.enabled_links(options[:current_person])
-    links
+    {
+      enabled_items: enabled_links_for(options[:current_person]),
+      available_items: filter_links(options[:current_person], available_links)
+    }
+  end
+
+  def api_content=(values = {})
+    settings[:enabled_links] = values[:enabled_items]
   end
 
   def display_api_content_by_default?
@@ -35,15 +49,19 @@ class MenuBlock < Block
 
   protected
 
+  def filter_links(user, links)
+    links.select { |link| permission_control(link, user) }
+  end
+
   def display_control_panel?(user)
     user && user.has_permission?('edit_profile', owner)
   end
-    
+
   def display_activities?(user)
     AccessLevels.can_access?(access_level, user, owner)
   end
 
-  def access_level 
+  def access_level
     owner.person? ? AccessLevels::LEVELS[:users] : AccessLevels::LEVELS[:visitors]
   end
 
@@ -63,4 +81,14 @@ class MenuBlock < Block
     owner.community? && user && user.has_permission?(:manage_memberships, owner)
   end
 
+  def display_article?(user)
+    true
+  end
+
+  def permission_control(link, user)
+    return true if !link[:controller] || !link[:action]
+    available_link = available_links.find { |l| l[:controller] == link[:controller] && l[:action] == link[:action] }
+    return available_link[:condition].call(user)
+  end
+
 end


=====================================
app/views/blocks/menu.html.erb
=====================================
--- a/app/views/blocks/menu.html.erb
+++ b/app/views/blocks/menu.html.erb
@@ -1,9 +1,11 @@
 <%= block_title(block.title, block.subtitle) %>
 
 <ul>
-  <% block.enabled_links(user).each do |link| %>
-    <li class="<%= current_page?(:controller => link[:controller], :action => link[:action]) ? 'active' : '' %>">
-      <%= link_to link[:title], {:controller => link[:controller], :action => link[:action], :profile => block.owner.identifier} %>
-    </li>
+  <% block.enabled_links_for(user).each do |link| %>
+    <% if link[:controller] %>
+      <li class="<%= current_page?(:controller => link[:controller], :action => link[:action]) ? 'active' : '' %>">
+        <%= link_to link[:title], {:controller => link[:controller], :action => link[:action], :profile => block.owner.identifier} %>
+      </li>
+    <% end %>
   <% end %>
 </ul>


=====================================
test/unit/menu_block_test.rb
=====================================
--- a/test/unit/menu_block_test.rb
+++ b/test/unit/menu_block_test.rb
@@ -20,18 +20,18 @@ class MenuBlockTest < ActiveSupport::TestCase
 
   should 'return only about link for person when not logged in' do
     block.box = create(Box, owner: fast_create(Person))
-    links = block.enabled_links(nil)
+    links = block.enabled_links_for(nil)
     assert_equal 1, links.size
     assert_equal 'About', links.first[:title]
   end
 
   should 'return activities link for community when user has no permission' do
-    links = block.enabled_links(person)
+    links = block.enabled_links_for(person)
     assert links.detect{|link| link[:title] == 'Activities' }
   end
 
   should 'return activities link for community for visitors' do
-    links = block.enabled_links(nil)
+    links = block.enabled_links_for(nil)
     assert links.detect{|link| link[:title] == 'Activities' }
   end
 
@@ -39,19 +39,19 @@ class MenuBlockTest < ActiveSupport::TestCase
     person = fast_create(Person)
     box = create(Box, owner: person)
     block = MenuBlock.new(box: box)
-    links = block.enabled_links(nil)
+    links = block.enabled_links_for(nil)
     assert !links.detect{|link| link[:title] == 'Activities' }
   end
 
   should 'return all community links for an owner' do
     profile.add_admin(person)
-    links = block.enabled_links(person)
+    links = block.enabled_links_for(person)
     assert_equal ['Activities', 'People', 'Control Panel'], links.map { |l| l[:title] }
   end
 
   should 'return all person links for the current person' do
     block.box = create(Box, owner: person)
-    links = block.enabled_links(person)
+    links = block.enabled_links_for(person)
     assert_equal ['Activities', 'About', 'Communities', 'People', 'Control Panel'], links.map { |l| l[:title] }
   end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/b6ca76dee8d6fe1013f47af68dc0f6483e6d3a37...6df0c892b9fd9e3b26b3f189eae7dd3dbd5a9520

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/b6ca76dee8d6fe1013f47af68dc0f6483e6d3a37...6df0c892b9fd9e3b26b3f189eae7dd3dbd5a9520
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170417/b0814af6/attachment-0001.html>


More information about the Noosfero-dev mailing list