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 3 # Manage tags stored by the acts-as_taggable_on_steroids plugin by providing an interface to create, destroy, update and list them
4 4 class ManageTagsController < ApplicationController
... ... @@ -11,22 +11,20 @@ class ManageTagsController &lt; ApplicationController
11 11 # Lists the tags strting with the top tags or with the chidren of @parent if its provided
12 12 def list
13 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 16 end
17 17  
18 18 # Prompt to data for a new tag
19 19 def new
20   - @parent_tags = Tag.find_all.select{|t|!t.pending?}
  20 + @parent_tags = Tag.find_all
21 21 @tag = Tag.new
22 22 end
23 23  
24 24 # Collects the data and creates a new tag with it
25 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 28 if @tag.save
31 29 flash[:notice] = _('Tag was successfully created.')
32 30 redirect_to :action => 'list'
... ... @@ -37,16 +35,14 @@ class ManageTagsController &lt; ApplicationController
37 35  
38 36 # Prompt for modifications on the attributes of a tag
39 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 40 end
43 41  
44 42 # Do the modifications collected by edit
45 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 46 flash[:notice] = _('Tag was successfully updated.')
51 47 redirect_to :action => 'list'
52 48 else
... ... @@ -56,8 +52,17 @@ class ManageTagsController &lt; ApplicationController
56 52  
57 53 # Destroy a tag
58 54 def destroy
59   - @tag = Tag.find(params[:id])
  55 + @tag = Tag.original_find(params[:id])
60 56 @tag.destroy
61 57 redirect_to :action => 'list'
62 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 68 end
... ...
app/views/manage_tags/_a_tag.rhtml
... ... @@ -3,7 +3,8 @@
3 3 <%= a_tag.name %>
4 4 <%= link_to _('Edit'), {:action => 'edit', :id => a_tag } %>
5 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 9 </ul>
9 10 </li>
... ...
app/views/manage_tags/_form.rhtml
1 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 3 Pending: <%= check_box 'tag', 'pending' %> <br>
... ...
app/views/manage_tags/list.rhtml
1 1 <h2> <%= _("Listing tags") %> </h2>
2 2  
  3 +<span><%= flash[:notice] %></span>
  4 +
3 5 <ul>
4 6 <%= render :partial => 'a_tag', :collection => @tags %>
5 7 </ul>
6 8  
7   -
8 9 <h3> <%= _('Pending Tags') %> </h3>
9 10 <ul>
10 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 3 create_table :tags do |t|
4 4 t.column :name, :string
5 5 t.column :parent_id, :integer
6   - t.column :pending, :bool
  6 + t.column :pending, :boolean
7 7 end
8 8  
9 9 create_table :taggings do |t|
... ...
lib/extended_tag.rb
1 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 17 def descendents
3   - children.inject([]){|des , child| des + child.descendents << child}
  18 + children.to_a.sum([], &:descendents) + children
4 19 end
5 20  
6   - def find_tag(*args)
7   - find(*args).select{|t|!t.pending?}
  21 + def aproved?
  22 + not pending?
8 23 end
9 24  
10 25 end
... ...