Commit 24bf3af50ddd3736e0dc17ca712e8230db9332f1
1 parent
5189d47d
Exists in
master
and in
29 other branches
ActionItem514: caching the categories menu
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2218 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
4 changed files
with
65 additions
and
7 deletions
Show diff stats
app/controllers/admin/categories_controller.rb
@@ -23,6 +23,7 @@ class CategoriesController < AdminController | @@ -23,6 +23,7 @@ class CategoriesController < AdminController | ||
23 | if request.post? | 23 | if request.post? |
24 | begin | 24 | begin |
25 | @category.save! | 25 | @category.save! |
26 | + @saved = true | ||
26 | redirect_to :action => 'index' | 27 | redirect_to :action => 'index' |
27 | rescue Exception => e | 28 | rescue Exception => e |
28 | render :action => 'new' | 29 | render :action => 'new' |
@@ -36,6 +37,7 @@ class CategoriesController < AdminController | @@ -36,6 +37,7 @@ class CategoriesController < AdminController | ||
36 | @category = environment.categories.find(params[:id]) | 37 | @category = environment.categories.find(params[:id]) |
37 | if request.post? | 38 | if request.post? |
38 | @category.update_attributes!(params[:category]) | 39 | @category.update_attributes!(params[:category]) |
40 | + @saved = true | ||
39 | redirect_to :action => 'index' | 41 | redirect_to :action => 'index' |
40 | end | 42 | end |
41 | rescue Exception => e | 43 | rescue Exception => e |
@@ -43,10 +45,20 @@ class CategoriesController < AdminController | @@ -43,10 +45,20 @@ class CategoriesController < AdminController | ||
43 | end | 45 | end |
44 | end | 46 | end |
45 | 47 | ||
48 | + after_filter :manage_categories_menu_cache, :only => [:edit, :new] | ||
49 | + | ||
46 | post_only :remove | 50 | post_only :remove |
47 | def remove | 51 | def remove |
48 | environment.categories.find(params[:id]).destroy | 52 | environment.categories.find(params[:id]).destroy |
49 | redirect_to :action => 'index' | 53 | redirect_to :action => 'index' |
50 | end | 54 | end |
51 | 55 | ||
56 | + protected | ||
57 | + | ||
58 | + def manage_categories_menu_cache | ||
59 | + if @saved && request.post? && @category.display_in_menu? | ||
60 | + expire_fragment(:controller => 'public', :action => 'categories_menu') | ||
61 | + end | ||
62 | + end | ||
63 | + | ||
52 | end | 64 | end |
app/views/layouts/application.rhtml
@@ -94,7 +94,9 @@ | @@ -94,7 +94,9 @@ | ||
94 | { :controller=>"home" }, | 94 | { :controller=>"home" }, |
95 | :id=>"menu_link_to_envhome", | 95 | :id=>"menu_link_to_envhome", |
96 | :title=>@environment.name %> | 96 | :title=>@environment.name %> |
97 | - <%= render :file => 'shared/categories_menu' %> | 97 | + <% cache(:controller => 'public', :action => 'categories_menu') do %> |
98 | + <%= render :file => 'shared/categories_menu' %> | ||
99 | + <% end %> | ||
98 | </div><!-- id='navigation_bar' --> | 100 | </div><!-- id='navigation_bar' --> |
99 | <script type="text/javascript"> | 101 | <script type="text/javascript"> |
100 | prepareMenu('navigation_bar', { timeout: 1000 }); | 102 | prepareMenu('navigation_bar', { timeout: 1000 }); |
config/routes.rb
@@ -68,12 +68,8 @@ ActionController::Routing::Routes.draw do |map| | @@ -68,12 +68,8 @@ ActionController::Routing::Routes.draw do |map| | ||
68 | map.system 'system', :controller => 'system' | 68 | map.system 'system', :controller => 'system' |
69 | map.system 'system/:controller/:action/:id', :controller => Noosfero.pattern_for_controllers_in_directory('system') | 69 | map.system 'system/:controller/:action/:id', :controller => Noosfero.pattern_for_controllers_in_directory('system') |
70 | 70 | ||
71 | - | ||
72 | - ###################################################### | ||
73 | - ## Test controllers. | ||
74 | - ## FIXME: this should not be needed | ||
75 | - ###################################################### | ||
76 | - map.connect 'test/:controller/:action/:id' , :controller => /.*test.*/ | 71 | + # cache stuff - hack |
72 | + map.cache 'public/:action/:id', :controller => 'public' | ||
77 | 73 | ||
78 | # *content viewing* | 74 | # *content viewing* |
79 | # XXX this route must come last so other routes have priority over it. | 75 | # XXX this route must come last so other routes have priority over it. |
test/functional/categories_controller_test.rb
@@ -88,4 +88,52 @@ class CategoriesControllerTest < Test::Unit::TestCase | @@ -88,4 +88,52 @@ class CategoriesControllerTest < Test::Unit::TestCase | ||
88 | end | 88 | end |
89 | end | 89 | end |
90 | 90 | ||
91 | + should 'expire categories menu cache when some menu category is updated' do | ||
92 | + cat = Category.create!(:name => 'test category in menu', :environment => Environment.default, :display_in_menu => true) | ||
93 | + @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').once | ||
94 | + post :edit, :id => cat.id, :category => { :name => 'new name for category in menu' } | ||
95 | + end | ||
96 | + | ||
97 | + should 'not touch categories menu cache whem updated category is not in menu' do | ||
98 | + cat = Category.create!(:name => 'test category not in menu', :environment => Environment.default, :display_in_menu => false) | ||
99 | + @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never | ||
100 | + post :edit, :id => cat.id, :category => { :name => 'new name for category not in menu' } | ||
101 | + end | ||
102 | + | ||
103 | + should 'expire categories menu cache when new category is created for the menu' do | ||
104 | + @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').once | ||
105 | + post :new, :category => { :name => 'my new category for the menu', :display_in_menu => '1' } | ||
106 | + end | ||
107 | + | ||
108 | + should 'not handle cache when viewing "edit category" screen' do | ||
109 | + cat = Category.create!(:name => 'test category in menu', :environment => Environment.default, :display_in_menu => true) | ||
110 | + @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never | ||
111 | + get :edit, :id => cat.id | ||
112 | + end | ||
113 | + | ||
114 | + should 'not expire categories menu cache when new category is created, but not for the menu' do | ||
115 | + @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never | ||
116 | + post :new, :category => { :name => 'my new category for the menu', :display_in_menu => '0' } | ||
117 | + end | ||
118 | + | ||
119 | + should 'not handle cache when viewing "new category" screen' do | ||
120 | + @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never | ||
121 | + get :new | ||
122 | + end | ||
123 | + | ||
124 | + should 'not expire cache when updating fails' do | ||
125 | + cat = Category.create!(:name => 'test category in menu', :environment => Environment.default, :display_in_menu => true) | ||
126 | + @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never | ||
127 | + | ||
128 | + post :edit, :id => cat.id, :category => { :name => '' } | ||
129 | + | ||
130 | + cat.reload | ||
131 | + assert_equal 'test category in menu', cat.name | ||
132 | + end | ||
133 | + | ||
134 | + should 'not expire cache when creating fails' do | ||
135 | + @controller.expects(:expire_fragment).with(:controller => 'public', :action => 'categories_menu').never | ||
136 | + post :new, :category => { :display_in_menu => '1' } | ||
137 | + end | ||
138 | + | ||
91 | end | 139 | end |