Commit adb6d89d5f41326826466a8c1258cd3a48905da9

Authored by Marcos Pereira
1 parent 11a81060

Refactoring for community_track plugin

- Remove html from community_track models
- Extracts update_categories to categories helper
app/controllers/my_profile/cms_controller.rb
... ... @@ -3,6 +3,7 @@ class CmsController < MyProfileController
3 3 protect 'edit_profile', :profile, :only => [:set_home_page]
4 4  
5 5 include ArticleHelper
  6 + include CategoriesHelper
6 7  
7 8 def search_tags
8 9 arg = params[:term].downcase
... ... @@ -256,12 +257,7 @@ class CmsController < MyProfileController
256 257  
257 258 def update_categories
258 259 @object = params[:id] ? @profile.articles.find(params[:id]) : Article.new
259   - @categories = @toplevel_categories = environment.top_level_categories
260   - if params[:category_id]
261   - @current_category = Category.find(params[:category_id])
262   - @categories = @current_category.children
263   - end
264   - render :template => 'shared/update_categories', :locals => { :category => @current_category, :object_name => 'article' }
  260 + render_categories 'article'
265 261 end
266 262  
267 263 def search_communities_to_publish
... ...
app/controllers/my_profile/profile_design_controller.rb
... ... @@ -6,6 +6,7 @@ class ProfileDesignController < BoxOrganizerController
6 6  
7 7 before_filter :protect_uneditable_block, :only => [:save]
8 8 before_filter :protect_fixed_block, :only => [:move_block]
  9 + include CategoriesHelper
9 10  
10 11 def protect_uneditable_block
11 12 block = boxes_holder.blocks.find(params[:id].gsub(/^block-/, ''))
... ... @@ -69,12 +70,7 @@ class ProfileDesignController < BoxOrganizerController
69 70  
70 71 def update_categories
71 72 @object = params[:id] ? @profile.blocks.find(params[:id]) : Block.new
72   - @categories = @toplevel_categories = environment.top_level_categories
73   - if params[:category_id]
74   - @current_category = Category.find(params[:category_id])
75   - @categories = @current_category.children
76   - end
77   - render :template => 'shared/update_categories', :locals => { :category => @current_category, :object_name => 'block' }
  73 + render_categories 'block'
78 74 end
79 75  
80 76 end
... ...
app/controllers/my_profile/profile_editor_controller.rb
... ... @@ -9,6 +9,7 @@ class ProfileEditorController < MyProfileController
9 9 before_filter :check_user_can_edit_header_footer, :only => [:header_footer]
10 10 helper_method :has_welcome_page
11 11 helper CustomFieldsHelper
  12 + include CategoriesHelper
12 13  
13 14 def index
14 15 @pending_tasks = Task.to(profile).pending.without_spam.select{|i| user.has_permission?(i.permission, profile)}
... ... @@ -60,12 +61,7 @@ class ProfileEditorController < MyProfileController
60 61  
61 62 def update_categories
62 63 @object = profile
63   - @categories = @toplevel_categories = environment.top_level_categories
64   - if params[:category_id]
65   - @current_category = Category.find(params[:category_id])
66   - @categories = @current_category.children
67   - end
68   - render :template => 'shared/update_categories', :locals => { :category => @current_category, :object_name => 'profile_data' }
  64 + render_categories 'profile_data'
69 65 end
70 66  
71 67 def header_footer
... ...
app/helpers/categories_helper.rb
... ... @@ -34,4 +34,15 @@ module CategoriesHelper
34 34 {:id => category_id ? "select-category-#{category_id}-link" : nil, :remote => true, :class => html_class}.merge(html_options)
35 35 end
36 36  
  37 + def render_categories object_name
  38 + @toplevel_categories = environment.top_level_categories
  39 + if params[:category_id]
  40 + @current_category = Category.find(params[:category_id])
  41 + @categories = @current_category.children
  42 + else
  43 + @categories = @toplevel_categories
  44 + end
  45 + render :template => 'shared/update_categories', :locals => { :category => @current_category, :object_name => object_name }
  46 + end
  47 +
37 48 end
... ...
app/models/block.rb
... ... @@ -4,8 +4,6 @@ class Block < ActiveRecord::Base
4 4 :visualization_format, :language, :display_user,
5 5 :box, :edit_modes, :move_modes, :mirror
6 6  
7   - # to be able to generate HTML
8   - include ActionView::Helpers::UrlHelper
9 7 include ActionView::Helpers::TagHelper
10 8  
11 9 # Block-specific stuff
... ...
plugins/community_track/lib/community_track_plugin/track_list_block.rb
... ... @@ -62,26 +62,10 @@ class CommunityTrackPlugin::TrackListBlock < Block
62 62 tracks
63 63 end
64 64  
65   - def content(args={})
66   - block = self
67   - proc do
68   - instance_eval(&block.set_seed(true))
69   - render :file => 'blocks/track_list', :locals => {:block => block}
70   - end
71   - end
72   -
73 65 def has_page?(page, per_page=limit)
74 66 return (page-1) * per_page < count_tracks
75 67 end
76 68  
77   - def footer
78   - block = self
79   - return nil if !has_page?(2)
80   - proc do
81   - render :partial => 'blocks/track_list_more', :locals => {:block => block, :page => 2, :per_page => block.limit}
82   - end
83   - end
84   -
85 69 def self.expire_on
86 70 { :profile => [:article, :category], :environment => [:article, :category] }
87 71 end
... ...
plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb
... ... @@ -34,16 +34,6 @@ class TrackListBlockTest &lt; ActiveSupport::TestCase
34 34 assert_equal @block.limit, @block.tracks.to_a.size
35 35 end
36 36  
37   - should 'return more link if has more tracks to show' do
38   - @block.limit.times { |i| create_track("track#{i}", profile) }
39   - assert @block.footer
40   - end
41   -
42   - should 'do not return more link if there is no more tracks to show' do
43   - (@block.limit-1).times { |i| create_track("track#{i}", profile) }
44   - refute @block.footer
45   - end
46   -
47 37 should 'count all tracks' do
48 38 @block.owner.articles.destroy_all
49 39 tracks_to_insert = @block.limit + 1
... ...
plugins/community_track/views/blocks/_track_list_more.html.erb
... ... @@ -6,7 +6,7 @@
6 6 </div>
7 7 <% else %>
8 8 <div class="more">
9   - <%= link_to_remote(c_('More'), :url => {:id => block.id, :controller => 'community_track_plugin_public', :action => 'view_tracks', :page => page, :per_page => per_page, :force_same_page => force_same_page}, :method => :get) %>
  9 + <%= link_to c_('More'), :id => block.id, :controller => 'community_track_plugin_public', :action => 'view_tracks', :page => page, :per_page => per_page, :force_same_page => force_same_page, :remote => true %>
10 10 </div>
11 11 <% end %>
12 12 </div>
... ...
plugins/community_track/views/blocks/footers/track_list.html.erb 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +<% if block.has_page?(2)%>
  2 + <%=render :partial => 'blocks/track_list_more', :locals => {:block => block, :page => 2, :per_page => block.limit} %>
  3 +<% end%>
... ...