Commit f28c05e6c67810dc718d5f55c4f3b2b73f89825e

Authored by MoisesMachado
1 parent c9c34f4d

ActionItem4: redefining find for tags to filter pending tags


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@84 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/manage_tags_controller.rb
1 -require 'extended_tag' 1 +require 'extended_tag.rb'
2 2
3 # Manage tags stored by the acts-as_taggable_on_steroids plugin by providing an interface to create, destroy, update and list them 3 # Manage tags stored by the acts-as_taggable_on_steroids plugin by providing an interface to create, destroy, update and list them
4 class ManageTagsController < ApplicationController 4 class ManageTagsController < ApplicationController
@@ -11,22 +11,20 @@ class ManageTagsController &lt; ApplicationController @@ -11,22 +11,20 @@ class ManageTagsController &lt; ApplicationController
11 # Lists the tags strting with the top tags or with the chidren of @parent if its provided 11 # Lists the tags strting with the top tags or with the chidren of @parent if its provided
12 def list 12 def list
13 @parent = Tag.find(params[:parent]) if params[:parent] 13 @parent = Tag.find(params[:parent]) if params[:parent]
14 - @tags = @parent ? @parent.children.select{|t|!t.pending} : Tag.find_all.select{|t|!t.pending?}  
15 - @pending_tags = Tag.find_all.select(&:pending?) 14 + @tags = @parent ? @parent.children : Tag.find(:all).select{|t|!t.parent}
  15 + @pending_tags = Tag.find_pendings
16 end 16 end
17 17
18 # Prompt to data for a new tag 18 # Prompt to data for a new tag
19 def new 19 def new
20 - @parent_tags = Tag.find_all.select{|t|!t.pending?} 20 + @parent_tags = Tag.find_all
21 @tag = Tag.new 21 @tag = Tag.new
22 end 22 end
23 23
24 # Collects the data and creates a new tag with it 24 # Collects the data and creates a new tag with it
25 def create 25 def create
26 - @tag = Tag.new  
27 - @tag.name = params[:tag][:name]  
28 - @tag.parent = Tag.find(params[:parent_id].to_i) if params[:parent_id] != "0"  
29 - @tag.pending = params[:tag][:pending] if params[:tag][:pending] 26 + @tag = Tag.new(params[:tag])
  27 +
30 if @tag.save 28 if @tag.save
31 flash[:notice] = _('Tag was successfully created.') 29 flash[:notice] = _('Tag was successfully created.')
32 redirect_to :action => 'list' 30 redirect_to :action => 'list'
@@ -37,16 +35,14 @@ class ManageTagsController &lt; ApplicationController @@ -37,16 +35,14 @@ class ManageTagsController &lt; ApplicationController
37 35
38 # Prompt for modifications on the attributes of a tag 36 # Prompt for modifications on the attributes of a tag
39 def edit 37 def edit
40 - @tag = Tag.find(params[:id])  
41 - @parent_tags = Tag.find_all.select{|t|!t.pending?} - @tag.descendents - [@tag] 38 + @tag = Tag.original_find(params[:id])
  39 + @parent_tags = Tag.find_all - @tag.descendents - [@tag]
42 end 40 end
43 41
44 # Do the modifications collected by edit 42 # Do the modifications collected by edit
45 def update 43 def update
46 - @tag = Tag.find(params[:id])  
47 - @tag.name = params[:tag][:name]  
48 - @tag.parent = params[:parent_id] != "0" ? Tag.find(params[:parent_id].to_i) : nil  
49 - if @tag.save 44 + @tag = Tag.original_find(params[:id])
  45 + if @tag.update_attributes(params[:tag])
50 flash[:notice] = _('Tag was successfully updated.') 46 flash[:notice] = _('Tag was successfully updated.')
51 redirect_to :action => 'list' 47 redirect_to :action => 'list'
52 else 48 else
@@ -56,8 +52,17 @@ class ManageTagsController &lt; ApplicationController @@ -56,8 +52,17 @@ class ManageTagsController &lt; ApplicationController
56 52
57 # Destroy a tag 53 # Destroy a tag
58 def destroy 54 def destroy
59 - @tag = Tag.find(params[:id]) 55 + @tag = Tag.original_find(params[:id])
60 @tag.destroy 56 @tag.destroy
61 redirect_to :action => 'list' 57 redirect_to :action => 'list'
62 end 58 end
  59 +
  60 + def approve
  61 + @tag = Tag.original_find(params[:id])
  62 + @tag.pending = false
  63 + if @tag.save
  64 + flash[:notice] = _('Tag was successfuly approved')
  65 + redirect_to :action => 'list'
  66 + end
  67 + end
63 end 68 end
app/views/manage_tags/_a_tag.rhtml
@@ -3,7 +3,8 @@ @@ -3,7 +3,8 @@
3 <%= a_tag.name %> 3 <%= a_tag.name %>
4 <%= link_to _('Edit'), {:action => 'edit', :id => a_tag } %> 4 <%= link_to _('Edit'), {:action => 'edit', :id => a_tag } %>
5 <%= link_to _('Destroy'), {:action => 'destroy', :id => a_tag} %> 5 <%= link_to _('Destroy'), {:action => 'destroy', :id => a_tag} %>
6 -<%= link_to _('Filter by this tag'), {:action => 'list', :parent => a_tag} %><ul>  
7 - <%= render :partial => 'a_tag', :collection => a_tag.children %> 6 +<%= link_to _('Filter by this tag'), {:action => 'list', :parent => a_tag} %>
  7 +<%= link_to _('Approve tag'), {:action => 'approve', :id => a_tag} if a_tag.pending? %> <ul>
  8 + <%= render :partial => 'a_tag', :collection => a_tag.children.select{|t|!t.pending?} %>
8 </ul> 9 </ul>
9 </li> 10 </li>
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 '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 value="0"></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/list.rhtml
1 <h2> <%= _("Listing tags") %> </h2> 1 <h2> <%= _("Listing tags") %> </h2>
2 2
  3 +<span><%= flash[:notice] %></span>
  4 +
3 <ul> 5 <ul>
4 <%= render :partial => 'a_tag', :collection => @tags %> 6 <%= render :partial => 'a_tag', :collection => @tags %>
5 </ul> 7 </ul>
6 8
7 -  
8 <h3> <%= _('Pending Tags') %> </h3> 9 <h3> <%= _('Pending Tags') %> </h3>
9 <ul> 10 <ul>
10 <%= render :partial => 'a_tag', :collection => @pending_tags %> 11 <%= render :partial => 'a_tag', :collection => @pending_tags %>
db/migrate/004_acts_as_taggable_migration.rb
@@ -3,7 +3,7 @@ class ActsAsTaggableMigration &lt; ActiveRecord::Migration @@ -3,7 +3,7 @@ class ActsAsTaggableMigration &lt; ActiveRecord::Migration
3 create_table :tags do |t| 3 create_table :tags do |t|
4 t.column :name, :string 4 t.column :name, :string
5 t.column :parent_id, :integer 5 t.column :parent_id, :integer
6 - t.column :pending, :bool 6 + t.column :pending, :boolean
7 end 7 end
8 8
9 create_table :taggings do |t| 9 create_table :taggings do |t|
lib/extended_tag.rb
1 class Tag 1 class Tag
  2 + @@original_find = self.method(:find)
  3 + def self.original_find(*args)
  4 + @@original_find.call(*args)
  5 + end
  6 +
  7 + def self.find(*args)
  8 + self.with_scope(:find => { :conditions => ['pending = ?', false] }) do
  9 + return self.original_find(*args)
  10 + end
  11 + end
  12 +
  13 + def self.find_pendings
  14 + self.original_find(:all, :conditions => ['pending = ?', true])
  15 + end
  16 +
2 def descendents 17 def descendents
3 - children.inject([]){|des , child| des + child.descendents << child} 18 + children.to_a.sum([], &:descendents) + children
4 end 19 end
5 20
6 - def find_tag(*args)  
7 - find(*args).select{|t|!t.pending?} 21 + def aproved?
  22 + not pending?
8 end 23 end
9 24
10 end 25 end