summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorEtienne Massip <etienne.massip@gmail.com>2011-10-24 20:19:26 +0000
committerEtienne Massip <etienne.massip@gmail.com>2011-10-24 20:19:26 +0000
commit8f96f0c40d8032e4787f2548e0f0fd7da7e36a07 (patch)
treecb7a9ac1efdede90bd38638e4119d57e309ee7a2 /app
parent657fcd97aa1a6fc289dcb998dd994c9499112737 (diff)
downloadredmine-8f96f0c40d8032e4787f2548e0f0fd7da7e36a07.tar.gz
redmine-8f96f0c40d8032e4787f2548e0f0fd7da7e36a07.zip
Refactor : convert queries to REST resources (also fixes #9108).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7649 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r--app/controllers/queries_controller.rb58
-rw-r--r--app/helpers/queries_helper.rb41
-rw-r--r--app/views/issues/index.html.erb6
-rw-r--r--app/views/news/new.html.erb16
-rw-r--r--app/views/queries/_form.html.erb1
-rw-r--r--app/views/queries/edit.html.erb2
-rw-r--r--app/views/queries/index.html.erb6
-rw-r--r--app/views/queries/new.html.erb2
8 files changed, 71 insertions, 61 deletions
diff --git a/app/controllers/queries_controller.rb b/app/controllers/queries_controller.rb
index 0ec305e4b..241b5176c 100644
--- a/app/controllers/queries_controller.rb
+++ b/app/controllers/queries_controller.rb
@@ -17,11 +17,13 @@
class QueriesController < ApplicationController
menu_item :issues
- before_filter :find_query, :except => [:new, :index]
- before_filter :find_optional_project, :only => :new
+ before_filter :find_query, :except => [:new, :create, :index]
+ before_filter :find_optional_project, :only => [:new, :create]
accept_api_auth :index
+ include QueriesHelper
+
def index
case params[:format]
when 'xml', 'json'
@@ -41,44 +43,52 @@ class QueriesController < ApplicationController
end
def new
- @query = Query.new(params[:query])
- @query.project = params[:query_is_for_all] ? nil : @project
+ @query = Query.new
@query.user = User.current
+ @query.project = @project
@query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
+ build_query_from_params
+ end
- @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v]) if params[:fields] || params[:f]
- @query.group_by ||= params[:group_by]
- @query.column_names = params[:c] if params[:c]
+ verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
+ def create
+ @query = Query.new(params[:query])
+ @query.user = User.current
+ @query.project = params[:query_is_for_all] ? nil : @project
+ @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
+ build_query_from_params
@query.column_names = nil if params[:default_columns]
- if request.post? && params[:confirm] && @query.save
+ if @query.save
flash[:notice] = l(:notice_successful_create)
redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query
- return
+ else
+ render :action => 'new', :layout => !request.xhr?
end
- render :layout => false if request.xhr?
end
def edit
- if request.post?
- @query.filters = {}
- @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v]) if params[:fields] || params[:f]
- @query.attributes = params[:query]
- @query.project = nil if params[:query_is_for_all]
- @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
- @query.group_by ||= params[:group_by]
- @query.column_names = params[:c] if params[:c]
- @query.column_names = nil if params[:default_columns]
+ end
- if @query.save
- flash[:notice] = l(:notice_successful_update)
- redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query
- end
+ verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
+ def update
+ @query.attributes = params[:query]
+ @query.project = nil if params[:query_is_for_all]
+ @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
+ build_query_from_params
+ @query.column_names = nil if params[:default_columns]
+
+ if @query.save
+ flash[:notice] = l(:notice_successful_update)
+ redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query
+ else
+ render :action => 'edit'
end
end
+ verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
def destroy
- @query.destroy if request.post?
+ @query.destroy
redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :set_filter => 1
end
diff --git a/app/helpers/queries_helper.rb b/app/helpers/queries_helper.rb
index 2ce6a96d8..0e82eb631 100644
--- a/app/helpers/queries_helper.rb
+++ b/app/helpers/queries_helper.rb
@@ -71,30 +71,31 @@ module QueriesHelper
cond << " OR project_id = #{@project.id}" if @project
@query = Query.find(params[:query_id], :conditions => cond)
raise ::Unauthorized unless @query.visible?
- @query.project = @project
session[:query] = {:id => @query.id, :project_id => @query.project_id}
sort_clear
+ elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
+ # Give it a name, required to be valid
+ @query = Query.new(:name => "_")
+ @query.project = @project
+ build_query_from_params
+ session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
else
- if api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
- # Give it a name, required to be valid
- @query = Query.new(:name => "_")
- @query.project = @project
- if params[:fields] || params[:f]
- @query.filters = {}
- @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v])
- else
- @query.available_filters.keys.each do |field|
- @query.add_short_filter(field, params[field]) if params[field]
- end
- end
- @query.group_by = params[:group_by]
- @query.column_names = params[:c] || (params[:query] && params[:query][:column_names])
- session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
- else
- @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
- @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
- @query.project = @project
+ # retrieve from session
+ @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
+ @query ||= Query.new(:name => "_", :project_id => session[:project_id] || @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
+ end
+ end
+
+ def build_query_from_params
+ if params[:fields] || params[:f]
+ @query.filters = {}
+ @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v])
+ else
+ @query.available_filters.keys.each do |field|
+ @query.add_short_filter(field, params[field]) if params[field]
end
end
+ @query.group_by = params[:group_by] || (params[:query] && params[:query][:group_by])
+ @query.column_names = params[:c] || (params[:query] && params[:query][:column_names])
end
end
diff --git a/app/views/issues/index.html.erb b/app/views/issues/index.html.erb
index 4c2caa7ff..beff2fef6 100644
--- a/app/views/issues/index.html.erb
+++ b/app/views/issues/index.html.erb
@@ -1,7 +1,7 @@
<div class="contextual">
<% if !@query.new_record? && @query.editable_by?(User.current) %>
- <%= link_to l(:button_edit), {:controller => 'queries', :action => 'edit', :id => @query}, :class => 'icon icon-edit' %>
- <%= link_to l(:button_delete), {:controller => 'queries', :action => 'destroy', :id => @query}, :confirm => l(:text_are_you_sure), :method => :post, :class => 'icon icon-del' %>
+ <%= link_to l(:button_edit), @query.project_id ? edit_project_query_path(:project_id => @query.project, :id => @query) : edit_query_path(@query), :class => 'icon icon-edit' %>
+ <%= link_to l(:button_delete), @query.project_id ? project_query_path(:project_id => @query.project, :id => @query) : query_path(@query), :confirm => l(:text_are_you_sure), :method => :delete, :class => 'icon icon-del' %>
<% end %>
</div>
@@ -38,7 +38,7 @@
<%= link_to_function l(:button_apply), 'submit_query_form("query_form")', :class => 'icon icon-checked' %>
<%= link_to l(:button_clear), { :set_filter => 1, :project_id => @project }, :class => 'icon icon-reload' %>
<% if @query.new_record? && User.current.allowed_to?(:save_queries, @project, :global => true) %>
- <%= link_to_function l(:button_save), "$('query_form').action='#{ url_for :controller => 'queries', :action => 'new', :project_id => @project }'; submit_query_form('query_form')", :class => 'icon icon-save' %>
+ <%= link_to_function l(:button_save), "$('query_form').action='#{ @project ? new_project_query_path(@project) : new_query_path }'; submit_query_form('query_form')", :class => 'icon icon-save' %>
<% end %>
</p>
<% end %>
diff --git a/app/views/news/new.html.erb b/app/views/news/new.html.erb
index 0fbc72ec1..59589030a 100644
--- a/app/views/news/new.html.erb
+++ b/app/views/news/new.html.erb
@@ -2,13 +2,13 @@
<% labelled_tabular_form_for :news, @news, :url => project_news_index_path(@project),
:html => { :id => 'news-form' } do |f| %>
-<%= render :partial => 'news/form', :locals => { :f => f } %>
-<%= submit_tag l(:button_create) %>
-<%= link_to_remote l(:label_preview),
- { :url => preview_news_path(:project_id => @project),
- :method => 'get',
- :update => 'preview',
- :with => "Form.serialize('news-form')"
- }, :accesskey => accesskey(:preview) %>
+ <%= render :partial => 'news/form', :locals => { :f => f } %>
+ <%= submit_tag l(:button_create) %>
+ <%= link_to_remote l(:label_preview),
+ { :url => preview_news_path(:project_id => @project),
+ :method => 'get',
+ :update => 'preview',
+ :with => "Form.serialize('news-form')"
+ }, :accesskey => accesskey(:preview) %>
<% end %>
<div id="preview" class="wiki"></div>
diff --git a/app/views/queries/_form.html.erb b/app/views/queries/_form.html.erb
index 2d9d81d28..806944359 100644
--- a/app/views/queries/_form.html.erb
+++ b/app/views/queries/_form.html.erb
@@ -1,5 +1,4 @@
<%= error_messages_for 'query' %>
-<%= hidden_field_tag 'confirm', 1 %>
<div class="box">
<div class="tabular">
diff --git a/app/views/queries/edit.html.erb b/app/views/queries/edit.html.erb
index 1c99ac077..46a530f16 100644
--- a/app/views/queries/edit.html.erb
+++ b/app/views/queries/edit.html.erb
@@ -1,6 +1,6 @@
<h2><%= l(:label_query) %></h2>
-<% form_tag({:action => 'edit', :id => @query}, :onsubmit => 'selectAllOptions("selected_columns");') do %>
+<% form_tag(@query.project_id ? project_query_path(:project_id => @query.project, :id => @query) : query_path(@query), :onsubmit => 'selectAllOptions("selected_columns");', :method => :put) do %>
<%= render :partial => 'form', :locals => {:query => @query} %>
<%= submit_tag l(:button_save) %>
<% end %>
diff --git a/app/views/queries/index.html.erb b/app/views/queries/index.html.erb
index 066f30edb..b03ac6216 100644
--- a/app/views/queries/index.html.erb
+++ b/app/views/queries/index.html.erb
@@ -1,5 +1,5 @@
<div class="contextual">
-<%= link_to_if_authorized l(:label_query_new), {:controller => 'queries', :action => 'new', :project_id => @project}, :class => 'icon icon-add' %>
+<%= link_to_if_authorized l(:label_query_new), new_project_query_path(:project_id => @project), :class => 'icon icon-add' %>
</div>
<h2><%= l(:label_query_plural) %></h2>
@@ -16,8 +16,8 @@
<td align="right">
<small>
<% if query.editable_by?(User.current) %>
- <%= link_to l(:button_edit), {:controller => 'queries', :action => 'edit', :id => query}, :class => 'icon icon-edit' %>
- <%= link_to l(:button_delete), {:controller => 'queries', :action => 'destroy', :id => query}, :confirm => l(:text_are_you_sure), :method => :post, :class => 'icon icon-del' %>
+ <%= link_to l(:button_edit), edit_query_path(query), :class => 'icon icon-edit' %>
+ <%= link_to l(:button_delete), query_path(query), :confirm => l(:text_are_you_sure), :method => :delete, :class => 'icon icon-del' %>
</small>
<% end %>
</td>
diff --git a/app/views/queries/new.html.erb b/app/views/queries/new.html.erb
index a980d04ab..b5809fb88 100644
--- a/app/views/queries/new.html.erb
+++ b/app/views/queries/new.html.erb
@@ -1,6 +1,6 @@
<h2><%= l(:label_query_new) %></h2>
-<% form_tag({:action => 'new', :project_id => @query.project}, :onsubmit => 'selectAllOptions("selected_columns");') do %>
+<% form_tag(@project ? project_queries_path(:project_id => @project) : queries_path, :onsubmit => 'selectAllOptions("selected_columns");') do %>
<%= render :partial => 'form', :locals => {:query => @query} %>
<%= submit_tag l(:button_save) %>
<% end %>