From ce07545086402039cfd914577efb8728d0488687 Mon Sep 17 00:00:00 2001 From: Rafael Reggiani Manzo Date: Thu, 10 Mar 2016 08:28:07 -0300 Subject: [PATCH] Refactor CommunitiesBlock footer into helper/view --- app/helpers/boxes_helper.rb | 12 +++++++++++- app/models/communities_block.rb | 9 --------- app/views/blocks/communities_footer.html.erb | 17 ----------------- app/views/blocks/footers/communities.html.erb | 19 +++++++++++++++++++ test/unit/communities_block_test.rb | 65 ++++++++++++++++++++++++++++++++++++++++++++--------------------- 5 files changed, 74 insertions(+), 48 deletions(-) delete mode 100644 app/views/blocks/communities_footer.html.erb create mode 100644 app/views/blocks/footers/communities.html.erb diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb index 76c13f4..7227c3a 100644 --- a/app/helpers/boxes_helper.rb +++ b/app/helpers/boxes_helper.rb @@ -97,10 +97,20 @@ module BoxesHelper end end + def render_block_footer(block) + template_name = block.class.name.underscore.gsub('_block', '') + template_filename = "#{template_name}.html.erb" + if File.exists? Rails.root.join('app', 'views', 'blocks', 'footers', template_filename) + render :file => "blocks/footers/#{template_name}", :locals => { :block => block } + else + nil + end + end + def display_block_content(block, main_content = nil) content = block.main? ? wrap_main_content(main_content) : render_block_content(block) result = extract_block_content(content) - footer_content = extract_block_content(block.footer) + footer_content = extract_block_content(block.respond_to?(:footer) ? block.footer : render_block_footer(block)) # FIXME: this ternary conditional should be removed after all block footer methods get refatored into helpers and views unless footer_content.blank? footer_content = content_tag('div', footer_content, :class => 'block-footer-content' ) end diff --git a/app/models/communities_block.rb b/app/models/communities_block.rb index 7942613..0f2b190 100644 --- a/app/models/communities_block.rb +++ b/app/models/communities_block.rb @@ -27,15 +27,6 @@ class CommunitiesBlock < ProfileListBlock owner.profile_suggestions.of_community.enabled.limit(3).includes(:suggestion) end - def footer - owner = self.owner - suggestions = self.suggestions - return '' unless owner.kind_of?(Profile) || owner.kind_of?(Environment) - proc do - render :file => 'blocks/communities_footer', :locals => { :owner => owner, :suggestions => suggestions } - end - end - def profiles owner.communities end diff --git a/app/views/blocks/communities_footer.html.erb b/app/views/blocks/communities_footer.html.erb deleted file mode 100644 index 45a467a..0000000 --- a/app/views/blocks/communities_footer.html.erb +++ /dev/null @@ -1,17 +0,0 @@ -<% if owner.kind_of?(Profile) %> - <%= link_to s_('communities|View all'), {:profile => owner.identifier, :controller => 'profile', :action => 'communities'}, :class => 'view-all' %> -<% elsif owner.kind_of?(Environment) %> - <%= link_to s_('communities|View all'), {:controller => 'search', :action => 'communities'}, :class => 'view-all' %> -<% end %> - -<% if user && user == profile && suggestions && !suggestions.empty? %> -
-

<%= _('Some suggestions for you') %>

-
- <%= render :partial => 'shared/profile_suggestions_list', :locals => { :suggestions => suggestions, :collection => :communities_suggestions, :per_page => 3 } %> -
-
- <%= link_to _('See all suggestions'), profile.communities_suggestions_url %> -
-
-<% end %> diff --git a/app/views/blocks/footers/communities.html.erb b/app/views/blocks/footers/communities.html.erb new file mode 100644 index 0000000..79605fe --- /dev/null +++ b/app/views/blocks/footers/communities.html.erb @@ -0,0 +1,19 @@ +<% if block.owner.kind_of?(Profile) || block.owner.kind_of?(Environment) %> + <% if block.owner.kind_of?(Profile) %> + <%= link_to s_('communities|View all'), {:profile => block.owner.identifier, :controller => 'profile', :action => 'communities'}, :class => 'view-all' %> + <% elsif block.owner.kind_of?(Environment) %> + <%= link_to s_('communities|View all'), {:controller => 'search', :action => 'communities'}, :class => 'view-all' %> + <% end %> + + <% if user && user == profile && block.suggestions && !block.suggestions.empty? %> +
+

<%= _('Some suggestions for you') %>

+
+ <%= render :partial => 'shared/profile_suggestions_list', :locals => { :suggestions => block.suggestions, :collection => :communities_suggestions, :per_page => 3 } %> +
+
+ <%= link_to _('See all suggestions'), profile.communities_suggestions_url %> +
+
+ <% end %> +<% end %> diff --git a/test/unit/communities_block_test.rb b/test/unit/communities_block_test.rb index 3f0d884..9232777 100644 --- a/test/unit/communities_block_test.rb +++ b/test/unit/communities_block_test.rb @@ -27,14 +27,44 @@ class CommunitiesBlockTest < ActiveSupport::TestCase assert_same list, block.profiles end + should 'list non-public communities' do + user = create_user('testuser').person + + public_community = fast_create(Community, :environment_id => Environment.default.id) + public_community.add_member(user) + + private_community = fast_create(Community, :environment_id => Environment.default.id, :public_profile => false) + private_community.add_member(user) + + block = CommunitiesBlock.new + block.expects(:owner).at_least_once.returns(user) + + assert_equivalent [public_community, private_community], block.profiles + end + +end + +require 'boxes_helper' + +class CommunitiesBlockViewTest < ActionView::TestCase + include BoxesHelper + should 'support profile as block owner' do profile = Profile.new + profile.identifier = 42 + + ActionView::Base.any_instance.stubs(:user).with(anything).returns(profile) + ActionView::Base.any_instance.stubs(:profile).with(anything).returns(profile) block = CommunitiesBlock.new block.expects(:owner).returns(profile).at_least_once - self.expects(:render).with(:file => 'blocks/communities_footer', :locals => { :owner => profile, :suggestions => block.suggestions }) - instance_eval(&block.footer) + footer = render_block_footer(block) + + assert_tag_in_string footer, tag: 'a', attributes: {href: '/profile/42/communities'} + + ActionView::Base.any_instance.unstub(:user) + ActionView::Base.any_instance.unstub(:profile) end should 'support environment as block owner' do @@ -42,31 +72,24 @@ class CommunitiesBlockTest < ActiveSupport::TestCase block = CommunitiesBlock.new block.expects(:owner).returns(env).at_least_once - self.expects(:render).with(:file => 'blocks/communities_footer', :locals => { :owner => env, :suggestions => block.suggestions }) - instance_eval(&block.footer) - end - - should 'give empty footer on unsupported owner type' do - block = CommunitiesBlock.new - block.expects(:owner).returns(1).at_least_once + profile = Profile.new + profile.identifier = 42 - self.expects(:render).with(anything).never - assert_equal '', block.footer - end + ActionView::Base.any_instance.stubs(:user).with(anything).returns(profile) + ActionView::Base.any_instance.stubs(:profile).with(anything).returns(profile) - should 'list non-public communities' do - user = create_user('testuser').person + footer = render_block_footer(block) - public_community = fast_create(Community, :environment_id => Environment.default.id) - public_community.add_member(user) + assert_tag_in_string footer, tag: 'a', attributes: {href: '/search/communities'} - private_community = fast_create(Community, :environment_id => Environment.default.id, :public_profile => false) - private_community.add_member(user) + ActionView::Base.any_instance.unstub(:user) + ActionView::Base.any_instance.unstub(:profile) + end + should 'give empty footer on unsupported owner type' do block = CommunitiesBlock.new - block.expects(:owner).at_least_once.returns(user) + block.expects(:owner).returns(1).at_least_once - assert_equivalent [public_community, private_community], block.profiles + assert_equal '', render_block_footer(block) end - end -- libgit2 0.21.2