##// END OF EJS Templates
Fixed that sorting time entries by custom field raises a SQL error (#14366)....
Jean-Philippe Lang -
r11812:21fc903c0424
parent child
Show More
@@ -43,10 +43,10 class TimelogController < ApplicationController
43
43
44 def index
44 def index
45 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
45 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
46 scope = time_entry_scope
47
46
48 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
47 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
49 sort_update(@query.sortable_columns)
48 sort_update(@query.sortable_columns)
49 scope = time_entry_scope(:order => sort_clause)
50
50
51 respond_to do |format|
51 respond_to do |format|
52 format.html {
52 format.html {
@@ -55,7 +55,6 class TimelogController < ApplicationController
55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
56 @entries = scope.all(
56 @entries = scope.all(
57 :include => [:project, :activity, :user, {:issue => :tracker}],
57 :include => [:project, :activity, :user, {:issue => :tracker}],
58 :order => sort_clause,
59 :limit => @entry_pages.per_page,
58 :limit => @entry_pages.per_page,
60 :offset => @entry_pages.offset
59 :offset => @entry_pages.offset
61 )
60 )
@@ -68,15 +67,13 class TimelogController < ApplicationController
68 @offset, @limit = api_offset_and_limit
67 @offset, @limit = api_offset_and_limit
69 @entries = scope.all(
68 @entries = scope.all(
70 :include => [:project, :activity, :user, {:issue => :tracker}],
69 :include => [:project, :activity, :user, {:issue => :tracker}],
71 :order => sort_clause,
72 :limit => @limit,
70 :limit => @limit,
73 :offset => @offset
71 :offset => @offset
74 )
72 )
75 }
73 }
76 format.atom {
74 format.atom {
77 entries = scope.all(
75 entries = scope.reorder("#{TimeEntry.table_name}.created_on DESC").all(
78 :include => [:project, :activity, :user, {:issue => :tracker}],
76 :include => [:project, :activity, :user, {:issue => :tracker}],
79 :order => "#{TimeEntry.table_name}.created_on DESC",
80 :limit => Setting.feeds_limit.to_i
77 :limit => Setting.feeds_limit.to_i
81 )
78 )
82 render_feed(entries, :title => l(:label_spent_time))
79 render_feed(entries, :title => l(:label_spent_time))
@@ -84,8 +81,7 class TimelogController < ApplicationController
84 format.csv {
81 format.csv {
85 # Export all entries
82 # Export all entries
86 @entries = scope.all(
83 @entries = scope.all(
87 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
84 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}]
88 :order => sort_clause
89 )
85 )
90 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
86 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
91 }
87 }
@@ -296,8 +292,8 private
296 end
292 end
297
293
298 # Returns the TimeEntry scope for index and report actions
294 # Returns the TimeEntry scope for index and report actions
299 def time_entry_scope
295 def time_entry_scope(options={})
300 scope = TimeEntry.visible.where(@query.statement)
296 scope = @query.results_scope(options)
301 if @issue
297 if @issue
302 scope = scope.on_issue(@issue)
298 scope = scope.on_issue(@issue)
303 elsif @project
299 elsif @project
@@ -100,6 +100,15 class TimeEntryQuery < Query
100 @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
100 @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
101 end
101 end
102
102
103 def results_scope(options={})
104 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
105
106 TimeEntry.visible.
107 where(statement).
108 order(order_option).
109 joins(joins_for_order_statement(order_option.join(',')))
110 end
111
103 # Accepts :from/:to params as shortcut filters
112 # Accepts :from/:to params as shortcut filters
104 def build_from_params(params)
113 def build_from_params(params)
105 super
114 super
@@ -559,6 +559,24 class TimelogControllerTest < ActionController::TestCase
559 assert_select "td.#{field_name}", :text => 'CF Value'
559 assert_select "td.#{field_name}", :text => 'CF Value'
560 end
560 end
561
561
562 def test_index_with_time_entry_custom_field_sorting
563 field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
564 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
565 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
566 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
567 field_name = "cf_#{field.id}"
568
569 get :index, :c => ["hours", field_name], :sort => field_name
570 assert_response :success
571 assert_include field_name.to_sym, assigns(:query).column_names
572 assert_select "th a.sort", :text => 'String Field'
573
574 # Make sure that values are properly sorted
575 values = assigns(:entries).map {|e| e.custom_field_value(field)}.compact
576 assert_equal 3, values.size
577 assert_equal values.sort, values
578 end
579
562 def test_index_atom_feed
580 def test_index_atom_feed
563 get :index, :project_id => 1, :format => 'atom'
581 get :index, :project_id => 1, :format => 'atom'
564 assert_response :success
582 assert_response :success
General Comments 0
You need to be logged in to leave comments. Login now