Skip to content

Commit

Permalink
Fix and improve routes and links.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashcmd committed May 11, 2012
1 parent f776013 commit 1331046
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
9 changes: 3 additions & 6 deletions app/controllers/pastes_controller.rb
Expand Up @@ -23,7 +23,7 @@ class PastesController < ApplicationController


default_search_scope :pastes default_search_scope :pastes


before_filter :find_project, :authorize before_filter :find_paste_and_project, :authorize


accept_rss_auth :index accept_rss_auth :index


Expand Down Expand Up @@ -86,12 +86,12 @@ def update
def destroy def destroy
@paste.destroy @paste.destroy
flash[:notice] = l(:notice_paste_destroyed) flash[:notice] = l(:notice_paste_destroyed)
redirect_to pastes_path(:project_id => @project.id) redirect_to :action => "index", :project_id => params[:project_id]
end end


private private


def find_project def find_paste_and_project
if params[:project_id].present? if params[:project_id].present?
@project = Project.find(params[:project_id]) @project = Project.find(params[:project_id])
@pastes = @project.pastes @pastes = @project.pastes
Expand All @@ -101,10 +101,7 @@ def find_project
if params[:id].present? if params[:id].present?
@paste = @pastes.find(params[:id]) @paste = @pastes.find(params[:id])
@project ||= @paste.project @project ||= @paste.project
else
@projects = Project.visible.has_module(:pastes)
end end
@pastes ||= Paste.for_project(@project || @projects)
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
render_404 render_404
end end
Expand Down
17 changes: 10 additions & 7 deletions app/helpers/pastes_helper.rb
Expand Up @@ -85,23 +85,28 @@ def paste_timestamp(paste)
end end
end end


def url_to_paste(action, paste = nil)
{ :controller => "pastes", :action => action,
:id => paste, :project_id => @project }
end

def link_to_paste(paste) def link_to_paste(paste)
link_to paste.title, paste link_to paste.title, paste
end end


def edit_paste_link(paste, title = l(:button_edit)) def edit_paste_link(paste, title = l(:button_edit))
link_to_if_authorized title, { :action => "edit", :id => paste }, link_to_if_authorized title, url_to_paste("edit", paste),
:class => "icon icon-edit" :class => "icon icon-edit"
end end


def delete_paste_link(paste, title = l(:button_delete)) def delete_paste_link(paste, title = l(:button_delete))
link_to_if_authorized title, { :action => "destroy", :id => paste }, link_to_if_authorized title, url_to_paste("destroy", paste),
:class => "icon icon-del", :class => "icon icon-del",
:method => :delete, :confirm => l(:text_paste_delete_confirmation) :method => :delete, :confirm => l(:text_paste_delete_confirmation)
end end


def download_paste_link(paste, title = l(:button_download)) def download_paste_link(paste, title = l(:button_download))
link_to title, { :action => "download", :id => paste }, link_to title, url_to_paste("download", paste),
:class => "icon icon-save" :class => "icon icon-save"
end end


Expand All @@ -112,14 +117,12 @@ def manage_paste_links(paste)
end end


def link_to_all_pastes def link_to_all_pastes
link_to l(:label_paste_view_all), link_to l(:label_paste_view_all), url_to_paste("index"),
{ :controller => "pastes", :action => "index", :project_id => @project },
:class => "icon icon-multiple" :class => "icon icon-multiple"
end end


def link_to_new_paste def link_to_new_paste
link_to_if_authorized l(:label_paste_new), link_to_if_authorized l(:label_paste_new), url_to_paste("new"),
{ :controller => "pastes", :action => "new", :project_id => @project },
:class => "icon icon-add" :class => "icon icon-add"
end end
end end
2 changes: 1 addition & 1 deletion app/views/pastes/_sidebar.html.erb
@@ -1,5 +1,5 @@
<% content_for :sidebar do %> <% content_for :sidebar do %>
<h3><%=l :label_paste_plural %></h3> <h3><%=l :label_paste_plural %></h3>
<p><%= link_to_new_paste %></p> <p><%= link_to_new_paste %></p>
<p><%= link_to_all_pastes %></p> <p><%= link_to_all_pastes if @paste %></p>
<% end %> <% end %>
2 changes: 1 addition & 1 deletion app/views/pastes/index.html.erb
Expand Up @@ -41,4 +41,4 @@
:key => User.current.rss_key } %> :key => User.current.rss_key } %>
<% end %> <% end %>
<%= render "sidebar" %> <%= render "sidebar" if @project %>
2 changes: 1 addition & 1 deletion app/views/pastes/show.html.erb
Expand Up @@ -34,4 +34,4 @@ table.CodeRay td.line_numbers {
<%= manage_paste_links(@paste) %> <%= manage_paste_links(@paste) %>
</p> </p>


<%= render "sidebar" %> <%= render "sidebar" if @project %>
4 changes: 3 additions & 1 deletion config/routes.rb
@@ -1,6 +1,8 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resources :pastes, :only => [:index, :show], # new paste can be only created on a project
map.resources :pastes, :except => :new,
:member => { :download => :get } :member => { :download => :get }

map.resources :pastes, :path_prefix => '/projects/:project_id', map.resources :pastes, :path_prefix => '/projects/:project_id',
:member => { :download => :get } :member => { :download => :get }
end end

0 comments on commit 1331046

Please sign in to comment.