changeset 257:3ecf99348b9f feature_80

* Enable toggling file active state through the files interface
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Fri, 04 Mar 2011 12:46:26 +0000
parents e0dc03c8d745
children ce817f379723
files app/controllers/attachments_controller.rb app/controllers/files_controller.rb app/views/attachments/toggle_active.rhtml app/views/files/index.html.erb config/routes.rb
diffstat 5 files changed, 37 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/attachments_controller.rb	Thu Mar 03 19:52:16 2011 +0000
+++ b/app/controllers/attachments_controller.rb	Fri Mar 04 12:46:26 2011 +0000
@@ -16,9 +16,11 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 class AttachmentsController < ApplicationController
+
   before_filter :find_project
   before_filter :file_readable, :read_authorize, :except => :destroy
   before_filter :delete_authorize, :only => :destroy
+  before_filter :active_authorize, :only => :toggle_active
   
   verify :method => :post, :only => :destroy
   
@@ -54,6 +56,12 @@
     redirect_to :controller => 'projects', :action => 'show', :id => @project
   end
   
+  def toggle_active
+    @attachment.active = !@attachment.active?
+    @attachment.save!
+    render :layout => false
+  end
+
 private
   def find_project
     @attachment = Attachment.find(params[:id])
@@ -77,6 +85,10 @@
     @attachment.deletable? ? true : deny_access
   end
   
+  def active_authorize
+    true
+  end
+
   def detect_content_type(attachment)
     content_type = attachment.content_type
     if content_type.blank?
--- a/app/controllers/files_controller.rb	Thu Mar 03 19:52:16 2011 +0000
+++ b/app/controllers/files_controller.rb	Fri Mar 04 12:46:26 2011 +0000
@@ -34,4 +34,5 @@
     end
     redirect_to project_files_path(@project)
   end
+
 end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/attachments/toggle_active.rhtml	Fri Mar 04 12:46:26 2011 +0000
@@ -0,0 +1,7 @@
+<%=
+file = Attachment.find(params[:id])
+active_id = "active-" + file.id.to_s
+link_to_remote image_tag(file.active? ? 'true.png' : 'false.png'),
+  :url => {:controller => 'attachments', :action => 'toggle_active', :project_id => @project.id, :id => file},
+  :update => active_id
+%>
--- a/app/views/files/index.html.erb	Thu Mar 03 19:52:16 2011 +0000
+++ b/app/views/files/index.html.erb	Fri Mar 04 12:46:26 2011 +0000
@@ -29,13 +29,19 @@
 	<% end -%>
   <% container.attachments.each do |file| %>		
   <tr class="file <%= cycle("odd", "even") %>">
-    <td class="active"><%=
-       if active_change_allowed
-         check_box_tag 'active', file.id, file.active?
-       else
-         image_tag('fav.png') if file.active?
-       end
-    %></td>
+    <td class="active">
+      <% if active_change_allowed
+           active_id = "active-" + file.id.to_s -%>
+        <div id="<%= active_id %>">
+        <%= link_to_remote image_tag(file.active? ? 'true.png' : 'false.png'),
+              :url => {:controller => 'attachments', :action => 'toggle_active', :project_id => @project.id, :id => file},
+              :update => active_id
+        %>
+        </div>
+      <% else -%>
+        <%= image_tag('fav.png') if file.active? %>
+      <% end -%>
+    </td>
     <td class="filename <%= "active" if file.active? %>"><%= link_to_attachment file, :download => true, :title => file.description %></td>
     <td class="created_on"><%= format_time(file.created_on) %></td>
     <td class="filesize"><%= number_to_human_size(file.filesize) %></td>
@@ -52,4 +58,6 @@
   </tbody>
 </table>
 
+<%= l(:text_files_active_change) if active_change_allowed %>
+
 <% html_title(l(:label_attachment_plural)) -%>
--- a/config/routes.rb	Thu Mar 03 19:52:16 2011 +0000
+++ b/config/routes.rb	Fri Mar 04 12:46:26 2011 +0000
@@ -165,6 +165,8 @@
 
   end
 
+#  map.connect 'projects/:id/files/toggle_active', :controller => 'files', :action => 'toggle_active', :conditions => {:method => :post}
+
   # Destroy uses a get request to prompt the user before the actual DELETE request
   map.project_destroy_confirm 'projects/:id/destroy', :controller => 'projects', :action => 'destroy', :conditions => {:method => :get}