Commit abc8b42c4808ecfe30ace5a406c40602df6d38c2
1 parent
0c8dd1ff
Exists in
master
and in
28 other branches
ActionItem4: tags management tested
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@109 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
6 changed files
with
139 additions
and
26 deletions
Show diff stats
app/controllers/manage_tags_controller.rb
@@ -4,63 +4,65 @@ require 'extended_tag.rb' | @@ -4,63 +4,65 @@ require 'extended_tag.rb' | ||
4 | class ManageTagsController < ApplicationController | 4 | class ManageTagsController < ApplicationController |
5 | # Index redirects to list action without modifing the url | 5 | # Index redirects to list action without modifing the url |
6 | def index | 6 | def index |
7 | - list | ||
8 | - render :action => 'list' | 7 | + redirect_to :action => 'list' |
9 | end | 8 | end |
10 | 9 | ||
11 | - # Lists the tags strting with the top tags or with the chidren of @parent if its provided | 10 | + # Lists the tags starting with the top tags or with the chidren of @parent if its provided |
12 | def list | 11 | def list |
13 | @parent = Tag.find(params[:parent]) if params[:parent] | 12 | @parent = Tag.find(params[:parent]) if params[:parent] |
14 | - @tags = @parent ? @parent.children : Tag.find(:all).select{|t|!t.parent} | 13 | + @tags = @parent ? @parent.children : Tag.roots |
15 | @pending_tags = Tag.find_pendings | 14 | @pending_tags = Tag.find_pendings |
16 | end | 15 | end |
17 | 16 | ||
18 | - # Prompt to data for a new tag | 17 | + # Prompt for data to a new tag |
19 | def new | 18 | def new |
20 | - @parent_tags = Tag.find_all | 19 | + @parent_tags = Tag.find(:all) |
21 | @tag = Tag.new | 20 | @tag = Tag.new |
22 | end | 21 | end |
23 | 22 | ||
24 | # Collects the data and creates a new tag with it | 23 | # Collects the data and creates a new tag with it |
25 | def create | 24 | def create |
26 | @tag = Tag.new(params[:tag]) | 25 | @tag = Tag.new(params[:tag]) |
27 | - | ||
28 | if @tag.save | 26 | if @tag.save |
29 | flash[:notice] = _('Tag was successfully created.') | 27 | flash[:notice] = _('Tag was successfully created.') |
30 | redirect_to :action => 'list' | 28 | redirect_to :action => 'list' |
31 | else | 29 | else |
30 | + @parent_tags = Tag.find(:all) | ||
32 | render :action => 'new' | 31 | render :action => 'new' |
33 | end | 32 | end |
34 | end | 33 | end |
35 | 34 | ||
36 | # Prompt for modifications on the attributes of a tag | 35 | # Prompt for modifications on the attributes of a tag |
37 | def edit | 36 | def edit |
38 | - @tag = Tag.original_find(params[:id]) | ||
39 | - @parent_tags = Tag.find_all - @tag.descendents - [@tag] | 37 | + @tag = Tag.find_with_pendings(params[:id]) |
38 | + @parent_tags = @tag.parents_candidates | ||
40 | end | 39 | end |
41 | 40 | ||
42 | # Do the modifications collected by edit | 41 | # Do the modifications collected by edit |
43 | def update | 42 | def update |
44 | - @tag = Tag.original_find(params[:id]) | 43 | + @tag = Tag.find_with_pendings(params[:id]) |
45 | if @tag.update_attributes(params[:tag]) | 44 | if @tag.update_attributes(params[:tag]) |
46 | flash[:notice] = _('Tag was successfully updated.') | 45 | flash[:notice] = _('Tag was successfully updated.') |
47 | redirect_to :action => 'list' | 46 | redirect_to :action => 'list' |
48 | else | 47 | else |
48 | + @parent_tags = @tag.parents_candidates | ||
49 | render :action => 'edit' | 49 | render :action => 'edit' |
50 | end | 50 | end |
51 | end | 51 | end |
52 | 52 | ||
53 | - # Destroy a tag | 53 | + # Destroy a tag and all its children |
54 | def destroy | 54 | def destroy |
55 | - @tag = Tag.original_find(params[:id]) | ||
56 | - @tag.destroy | 55 | + @tag = Tag.find_with_pendings(params[:id]) |
56 | + if @tag.destroy | ||
57 | + flash[:notice] = _('Tag was successfuly destroyed') | ||
58 | + end | ||
57 | redirect_to :action => 'list' | 59 | redirect_to :action => 'list' |
58 | end | 60 | end |
59 | 61 | ||
62 | + # Approve a pending tag so now ita can be used to tag things | ||
60 | def approve | 63 | def approve |
61 | - @tag = Tag.original_find(params[:id]) | ||
62 | - @tag.pending = false | ||
63 | - if @tag.save | 64 | + @tag = Tag.find_with_pendings(params[:id]) |
65 | + if @tag.update_attribute(:pending, false) | ||
64 | flash[:notice] = _('Tag was successfuly approved') | 66 | flash[:notice] = _('Tag was successfuly approved') |
65 | redirect_to :action => 'list' | 67 | redirect_to :action => 'list' |
66 | end | 68 | end |
app/views/manage_tags/_form.rhtml
1 | Name: <%= text_field 'tag', 'name' %> <br> | 1 | Name: <%= text_field 'tag', 'name' %> <br> |
2 | -Parent tag: <%= select_tag 'tag[parent_id]', ['<option value="0"></option>'] + @parent_tags.map {|pt|"<option value=\"#{pt.id}\" #{'selected="selected"' if @tag.parent == pt} >" + pt.name + '</option>'} %> <br> | 2 | +Parent tag: <%= select_tag 'tag[parent_id]', ['<option></option>'] + @parent_tags.map {|pt|"<option value=\"#{pt.id}\" #{'selected="selected"' if @tag.parent == pt} >" + pt.name + '</option>'} %> <br> |
3 | Pending: <%= check_box 'tag', 'pending' %> <br> | 3 | Pending: <%= check_box 'tag', 'pending' %> <br> |
app/views/manage_tags/edit.rhtml
1 | <h2> <%= _('Editing Tag') %> </h2> | 1 | <h2> <%= _('Editing Tag') %> </h2> |
2 | 2 | ||
3 | -<%= start_form_tag :action => 'update', :id => @tag %> | 3 | +<% form_tag :action => 'update', :id => @tag do %> |
4 | <%= render :partial => 'form' %> | 4 | <%= render :partial => 'form' %> |
5 | <%= submit_tag _('Update') %> | 5 | <%= submit_tag _('Update') %> |
6 | <%= link_to _('Cancel'), {:action => 'list'} %> | 6 | <%= link_to _('Cancel'), {:action => 'list'} %> |
7 | -<%= end_form_tag %> | 7 | +<% end %> |
app/views/manage_tags/new.rhtml
1 | <h2> <%= _('New tag') %> </h2> | 1 | <h2> <%= _('New tag') %> </h2> |
2 | 2 | ||
3 | -<%= start_form_tag :action =>'create' %> | 3 | +<% form_tag :action =>'create' do%> |
4 | <%= render :partial => 'form' %> | 4 | <%= render :partial => 'form' %> |
5 | <%= submit_tag _('Create') %> | 5 | <%= submit_tag _('Create') %> |
6 | <%= link_to _('Cancel'), {:action => 'list'} %> | 6 | <%= link_to _('Cancel'), {:action => 'list'} %> |
7 | -<%= end_form_tag %> | 7 | +<% end %> |
lib/extended_tag.rb
1 | class Tag | 1 | class Tag |
2 | - @@original_find = self.method(:find) | ||
3 | - def self.original_find(*args) | 2 | + @@original_find = self.method(:find) |
3 | + # Rename the find method to find_with_pendings that includes all tags in the search regardless if its pending or not | ||
4 | + def self.find_with_pendings(*args) | ||
4 | @@original_find.call(*args) | 5 | @@original_find.call(*args) |
5 | end | 6 | end |
6 | - | 7 | + # Redefine the find method to exclude the pending tags from the search not allowing to tag something with an unapproved tag |
7 | def self.find(*args) | 8 | def self.find(*args) |
8 | self.with_scope(:find => { :conditions => ['pending = ?', false] }) do | 9 | self.with_scope(:find => { :conditions => ['pending = ?', false] }) do |
9 | - return self.original_find(*args) | 10 | + self.find_with_pendings(*args) |
10 | end | 11 | end |
11 | end | 12 | end |
12 | 13 | ||
14 | + # Return all the tags that were suggested but not yet approved | ||
13 | def self.find_pendings | 15 | def self.find_pendings |
14 | - self.original_find(:all, :conditions => ['pending = ?', true]) | 16 | + self.find_with_pendings(:all, :conditions => ['pending = ?', true]) |
15 | end | 17 | end |
16 | 18 | ||
19 | + # All the tags that can be a new parent for this tag, that is all but itself and its descendents to avoid loops | ||
20 | + def parents_candidates | ||
21 | + Tag.find(:all) - descendents - [self] | ||
22 | + end | ||
23 | + | ||
24 | + # All tags that have this tag as its one of its ancestors | ||
17 | def descendents | 25 | def descendents |
18 | children.to_a.sum([], &:descendents) + children | 26 | children.to_a.sum([], &:descendents) + children |
19 | end | 27 | end |
20 | 28 | ||
29 | + # Test if this tag has been approved already | ||
21 | def aproved? | 30 | def aproved? |
22 | not pending? | 31 | not pending? |
23 | end | 32 | end |
@@ -0,0 +1,102 @@ | @@ -0,0 +1,102 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | +require 'manage_tags_controller' | ||
3 | + | ||
4 | +# Re-raise errors caught by the controller. | ||
5 | +class ManageTagsController; def rescue_action(e) raise e end; end | ||
6 | + | ||
7 | +class ManageTagsControllerTest < Test::Unit::TestCase | ||
8 | + fixtures :tags, :users, :blocks, :profiles, :virtual_communities, :boxes, :domains | ||
9 | + def setup | ||
10 | + @controller = ManageTagsController.new | ||
11 | + @request = ActionController::TestRequest.new | ||
12 | + @response = ActionController::TestResponse.new | ||
13 | + end | ||
14 | + | ||
15 | + # Replace this with your real tests. | ||
16 | + def test_get_index | ||
17 | + get :index | ||
18 | + assert_response :redirect | ||
19 | + assert_redirected_to :action => 'list' | ||
20 | + end | ||
21 | + | ||
22 | + def test_list | ||
23 | + get :list | ||
24 | + assert_response :success | ||
25 | + assert_template 'list' | ||
26 | + assert_not_nil assigns(:tags) | ||
27 | + assert_not_nil assigns(:pending_tags) | ||
28 | + assert_nil assigns(:parent), 'the list should not scoped' | ||
29 | + end | ||
30 | + | ||
31 | + def test_scoped_list | ||
32 | + assert_nothing_raised { Tag.find(1) } | ||
33 | + get :list, :parent => Tag.find(1) | ||
34 | + assert_response :success | ||
35 | + assert_template 'list' | ||
36 | + assert_not_nil assigns(:parent), 'the list should be scoped' | ||
37 | + assert_not_nil assigns(:tags) | ||
38 | + assert_not_nil assigns(:pending_tags) | ||
39 | + end | ||
40 | + | ||
41 | + def test_new | ||
42 | + get :new | ||
43 | + assert_response :success | ||
44 | + assert_template 'new' | ||
45 | + assert_not_nil assigns(:parent_tags) | ||
46 | + assert_not_nil assigns(:tag) | ||
47 | + end | ||
48 | + | ||
49 | + def test_create | ||
50 | + post :create, :tag => {:name => 'test_tag'} | ||
51 | + assert_response :redirect | ||
52 | + assert_redirected_to :action => 'list' | ||
53 | + assert_not_nil assigns(:tag) | ||
54 | + end | ||
55 | + | ||
56 | + def test_create_wrong | ||
57 | + post :create, :tag => {:name => ''} | ||
58 | + assert_response :success | ||
59 | + assert_template 'new' | ||
60 | + end | ||
61 | + | ||
62 | + def test_edit | ||
63 | + assert_nothing_raised { Tag.find(1) } | ||
64 | + get :edit, :id => 1 | ||
65 | + assert assigns(:tag) | ||
66 | + assert assigns(:parent_tags) | ||
67 | + end | ||
68 | + | ||
69 | + def test_update | ||
70 | + assert_nothing_raised { Tag.find(1) } | ||
71 | + post :update, :id => 1, :tag => {:name => 'altered_tag'} | ||
72 | + assert_response :redirect | ||
73 | + assert_redirected_to :action => 'list' | ||
74 | + assert assigns(:tag) | ||
75 | + end | ||
76 | + | ||
77 | + def test_update_wrong | ||
78 | + assert_nothing_raised { Tag.find(1) } | ||
79 | + post :update, :id => 1, :tag => {:name => ''} | ||
80 | + assert_response :success | ||
81 | + assert_template 'edit' | ||
82 | + assert assigns(:parent_tags) | ||
83 | + end | ||
84 | + | ||
85 | + def test_destroy | ||
86 | + assert_nothing_raised { Tag.find(1) } | ||
87 | + post :destroy, :id => 1 | ||
88 | + assert_response :redirect | ||
89 | + assert_redirected_to :action => 'list' | ||
90 | + assert_not_nil flash[:notice] | ||
91 | + assert_raise(ActiveRecord::RecordNotFound) { Tag.find(1) } | ||
92 | + end | ||
93 | + | ||
94 | + def test_approve | ||
95 | + assert_nothing_raised { Tag.find_with_pendings(4) } | ||
96 | + assert Tag.find_with_pendings(4).pending? | ||
97 | + post :approve, :id => 4 | ||
98 | + assert_response :redirect | ||
99 | + assert_redirected_to :action => 'list' | ||
100 | + assert ( not Tag.find_with_pendings(4).pending? ) | ||
101 | + end | ||
102 | +end |