diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 0af5f192a497b268099368f44d9ea556dcf60ff5..929b928acb78d3f8c52d3be5949ce932044d19fd 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -45,7 +45,7 @@ class IssuesController < ApplicationController def index retrieve_query - sort_init 'id', 'desc' + sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h})) if @query.valid? @@ -471,6 +471,7 @@ private @query = Query.find(params[:query_id], :conditions => cond) @query.project = @project session[:query] = {:id => @query.id, :project_id => @query.project_id} + sort_clear else if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) # Give it a name, required to be valid diff --git a/app/helpers/sort_helper.rb b/app/helpers/sort_helper.rb index d4d8f721ac5308abcdc45a19ee5e006163b132f8..7b7fbc8a3ee2e4ce0b73c75192ea83c7ea12ce60 100644 --- a/app/helpers/sort_helper.rb +++ b/app/helpers/sort_helper.rb @@ -69,6 +69,11 @@ module SortHelper normalize! end + def criteria=(arg) + @criteria = arg + normalize! + end + def to_param @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',') end @@ -102,24 +107,42 @@ module SortHelper @criteria.first && @criteria.first.last end + def empty? + @criteria.empty? + end + private def normalize! - @criteria = @criteria.collect {|s| [s.first, (s.last == false || s.last == 'desc') ? false : true]} + @criteria ||= [] + @criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]} @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria @criteria.slice!(3) self end end + + def sort_name + controller_name + '_' + action_name + '_sort' + end - # Initializes the default sort column (default_key) and sort order - # (default_order). - # - # - default_key is a column attribute name. - # - default_order is 'asc' or 'desc'. + # Initializes the default sort. + # Examples: + # + # sort_init 'name' + # sort_init 'id', 'desc' + # sort_init ['name', ['id', 'desc']] + # sort_init [['name', 'desc'], ['id', 'desc']] # - def sort_init(default_key, default_order='asc') - @sort_default = "#{default_key}:#{default_order}" + def sort_init(*args) + case args.size + when 1 + @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]] + when 2 + @sort_default = [[args.first, args.last]] + else + raise ArgumentError + end end # Updates the sort state. Call this in the controller prior to calling @@ -127,13 +150,18 @@ module SortHelper # - criteria can be either an array or a hash of allowed keys # def sort_update(criteria) - sort_name = controller_name + '_' + action_name + '_sort' - @sort_criteria = SortCriteria.new @sort_criteria.available_criteria = criteria - @sort_criteria.from_param(params[:sort] || session[sort_name] || @sort_default) + @sort_criteria.from_param(params[:sort] || session[sort_name]) + @sort_criteria.criteria = @sort_default if @sort_criteria.empty? session[sort_name] = @sort_criteria.to_param end + + # Clears the sort criteria session data + # + def sort_clear + session[sort_name] = nil + end # Returns an SQL sort clause corresponding to the current sort state. # Use this to sort the controller's table items collection. @@ -188,13 +216,6 @@ module SortHelper # # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> # - # Renders: - # - # <th title="Sort by contact ID" width="40"> - # <a href="/contact/list?sort_order=desc&sort_key=id">Id</a> - # <img alt="Sort_asc" src="/images/sort_asc.png" /> - # </th> - # def sort_header_tag(column, options = {}) caption = options.delete(:caption) || column.to_s.humanize default_order = options.delete(:default_order) || 'asc' diff --git a/app/models/query.rb b/app/models/query.rb index 5c9fad5e5243635a55b26977b837f1a7ceaaac78..99ebb757ea200752b11b0a2d566a218a96b7e784 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -28,6 +28,11 @@ class QueryColumn def caption l("field_#{name}") end + + # Returns true if the column is sortable, otherwise false + def sortable? + !sortable.nil? + end end class QueryCustomFieldColumn < QueryColumn @@ -52,6 +57,7 @@ class Query < ActiveRecord::Base belongs_to :user serialize :filters serialize :column_names + serialize :sort_criteria, Array attr_protected :project_id, :user_id @@ -261,6 +267,27 @@ class Query < ActiveRecord::Base column_names.nil? || column_names.empty? end + def sort_criteria=(arg) + c = [] + if arg.is_a?(Hash) + arg = arg.keys.sort.collect {|k| arg[k]} + end + c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']} + write_attribute(:sort_criteria, c) + end + + def sort_criteria + read_attribute(:sort_criteria) || [] + end + + def sort_criteria_key(arg) + sort_criteria && sort_criteria[arg] && sort_criteria[arg].first + end + + def sort_criteria_order(arg) + sort_criteria && sort_criteria[arg] && sort_criteria[arg].last + end + def project_statement project_clauses = [] if project && !@project.descendants.active.empty? diff --git a/app/views/queries/_form.rhtml b/app/views/queries/_form.rhtml index 8da264032b0e54cde8e96e0704903038cced2047..7c227a9f69cc50b5a332d41520daddbb79701a72 100644 --- a/app/views/queries/_form.rhtml +++ b/app/views/queries/_form.rhtml @@ -25,5 +25,14 @@ <%= render :partial => 'queries/filters', :locals => {:query => query}%> </fieldset> +<fieldset><legend><%= l(:label_sort) %></legend> +<% 3.times do |i| %> +<%= i+1 %>: <%= select_tag("query[sort_criteria][#{i}][]", + options_for_select([[]] + query.available_columns.select(&:sortable?).collect {|column| [column.caption, column.name.to_s]}, @query.sort_criteria_key(i))) %> + <%= select_tag("query[sort_criteria][#{i}][]", + options_for_select([[], [l(:label_ascending), 'asc'], [l(:label_descending), 'desc']], @query.sort_criteria_order(i))) %><br /> +<% end %> +</fieldset> + <%= render :partial => 'queries/columns', :locals => {:query => query}%> </div> diff --git a/config/locales/bg.yml b/config/locales/bg.yml index 0a6bca015736e2d4fc2a32d2145f3ccee2e4b60c..0179cb26160f3aeea7bf62a01bc1ece1d88a9d3f 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -778,3 +778,6 @@ bg: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/ca.yml b/config/locales/ca.yml index 15cca4e0c9c7d3e7e40ad89d92b74a808a03be7b..e62a936d6f5e4173a3aa6856050a9e9d09865c7f 100644 --- a/config/locales/ca.yml +++ b/config/locales/ca.yml @@ -779,3 +779,6 @@ ca: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/cs.yml b/config/locales/cs.yml index 0795b078497c23e3a268fe2409c9912e361d3f9c..577758532e8e723c3bbd9eb252eab0ec897e7743 100644 --- a/config/locales/cs.yml +++ b/config/locales/cs.yml @@ -783,3 +783,6 @@ cs: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/da.yml b/config/locales/da.yml index e27c4a055337fe897dcc80944bf51a808e20e8c2..76323520f0ff0416ecfd026c5f0c46d0ad8bf8cf 100644 --- a/config/locales/da.yml +++ b/config/locales/da.yml @@ -811,3 +811,6 @@ da: setting_per_page_options: Objects per page options mail_body_reminder: "{{count}} issue(s) that are assigned to you are due in the next {{days}} days:" field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/de.yml b/config/locales/de.yml index 44329474b7cf6fdeb9cd41297c2026bcb5b9d1a2..95a6f6c38c665a93a2a1d653f59acb784509e587 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -810,3 +810,6 @@ de: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/en.yml b/config/locales/en.yml index 47dcc0e4e66a3928ab235770b39b3f0d3046c177..e0cce3cbea8c6edb2d655b2bec4898e780bc85fb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -662,6 +662,9 @@ en: label_issue_watchers: Watchers label_example: Example label_display: Display + label_sort: Sort + label_ascending: Ascending + label_descending: Descending button_login: Login button_submit: Submit diff --git a/config/locales/es.yml b/config/locales/es.yml index 78f3bbd5fcee895cd20f01366dda3fbb3fabfe51..259d29489e5c1baaa9425d2a806045d1532974d8 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -831,3 +831,6 @@ es: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/fi.yml b/config/locales/fi.yml index d9f3c03d0dbf53b2f3ea776905b065bdfc8cfc2f..1ae6bc75c15b35fe1c2a315686435a907caf95d9 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -821,3 +821,6 @@ fi: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 9af234cc1c0fd4420bed1ef7dd85b33a6ead7661..72a1023a0a8d31b8c19a4400c511e4110005f923 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -694,6 +694,9 @@ fr: label_issue_watchers: Observateurs label_example: Exemple label_display: Affichage + label_sort: Tri + label_ascending: Croissant + label_descending: Décroissant button_login: Connexion button_submit: Soumettre diff --git a/config/locales/gl.yml b/config/locales/gl.yml index d15448885ee9286d54c3ab963fbcb4a192f87114..65f37de853bd0457c8d1936daa3ae08a745a00ad 100644 --- a/config/locales/gl.yml +++ b/config/locales/gl.yml @@ -810,3 +810,6 @@ gl: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/he.yml b/config/locales/he.yml index 661fca9045b42c84fdd6e503570d4f5096f81872..e674cc16063ec7c6e697a3d4a87652e34743ead2 100644 --- a/config/locales/he.yml +++ b/config/locales/he.yml @@ -793,3 +793,6 @@ he: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/hu.yml b/config/locales/hu.yml index a1456c3ae461702f9967ec4eb3b8bf87572e12a3..d1fe5a045be8448b20c8a59c13131f133c5f1993 100644 --- a/config/locales/hu.yml +++ b/config/locales/hu.yml @@ -816,3 +816,6 @@ field_identity_url: OpenID URL label_login_with_open_id_option: bejelentkezés OpenID használatával field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/it.yml b/config/locales/it.yml index f0c008f80f66df99343f4f71e16536623c1aa688..d10084ce2d6ded487c890aa2cb312f8f2bdb7777 100644 --- a/config/locales/it.yml +++ b/config/locales/it.yml @@ -796,3 +796,6 @@ it: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/ja.yml b/config/locales/ja.yml index 3753bb3ec146e61a9fb88e1e2439979977158cd5..c82074b0f8f2cce6727cfa4a6619c5056a3fd38f 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -809,3 +809,6 @@ ja: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/ko.yml b/config/locales/ko.yml index c60744f94db33c21d4cccd95aa966cd2ebf2615c..30d04324fa5648db82004668e543be61f1ae4ef4 100644 --- a/config/locales/ko.yml +++ b/config/locales/ko.yml @@ -840,3 +840,6 @@ ko: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/lt.yml b/config/locales/lt.yml index 09bf3fe7af1d0176f94fa5404f8eda3a6bcb9b90..a8bea412f391b55aa1c3dd51a78f9a88b716f0f6 100644 --- a/config/locales/lt.yml +++ b/config/locales/lt.yml @@ -821,3 +821,6 @@ lt: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 644d6f9317f94c75c01e54dfd5f6fd460c431528..89e9b8346119d48508bae0ea2f30a2b0162e2774 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -766,3 +766,6 @@ nl: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/no.yml b/config/locales/no.yml index f0da35cea3862b1cfe48e6ef060bb7f2557c44d4..1b670ad6e1d44b8f77ef30f264050b60412f490e 100644 --- a/config/locales/no.yml +++ b/config/locales/no.yml @@ -783,3 +783,6 @@ field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/pl.yml b/config/locales/pl.yml index 595705d5119b73a30582effff82158d74695783c..6f263aaf4c069799e1c9bf1068c9f5bb3b87712f 100644 --- a/config/locales/pl.yml +++ b/config/locales/pl.yml @@ -814,3 +814,6 @@ pl: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index 861881dfcd03f1edc0f18bc0d3f3c77bacef93e3..daeeb9fd431282b476dc53223ac31b183bf373a7 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -816,3 +816,6 @@ pt-BR: field_identity_url: OpenID URL label_login_with_open_id_option: ou use o OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/pt.yml b/config/locales/pt.yml index 4f248edd106a2e0f9d09c33f890c9eba9994d29c..fb44479780a65195ac0eb1e0e9f1b8bd6fbe7bae 100644 --- a/config/locales/pt.yml +++ b/config/locales/pt.yml @@ -802,3 +802,6 @@ pt: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/ro.yml b/config/locales/ro.yml index 3e2214d67db09f265b3c1ec52e6151c89003fab9..30fbc3a940f63c13f857c297598511b95ba1d09f 100644 --- a/config/locales/ro.yml +++ b/config/locales/ro.yml @@ -823,3 +823,6 @@ ro: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/ru.yml b/config/locales/ru.yml index c9788c37c70d1979755061e8882870a70a72e5e9..fcb62b35e8e1054a4ccb6e52fee61ab31b9d88ab 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -909,3 +909,6 @@ ru: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/sk.yml b/config/locales/sk.yml index b0ff847a7acb9428440a7c860a7099f1feec4213..fb75161f1656998994cc4beeab594c1f95373741 100644 --- a/config/locales/sk.yml +++ b/config/locales/sk.yml @@ -782,3 +782,6 @@ sk: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/sl.yml b/config/locales/sl.yml index 98c8995e37ef945a0916b56966b92439d53d01fa..249e47fbb9133afa5df58cacde75306980310381 100644 --- a/config/locales/sl.yml +++ b/config/locales/sl.yml @@ -780,3 +780,6 @@ sl: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/sr.yml b/config/locales/sr.yml index 80a290678138131675025bd2561e371c39127b60..a4b66a0589163353b9ffec6e3f326891f4317895 100644 --- a/config/locales/sr.yml +++ b/config/locales/sr.yml @@ -804,3 +804,6 @@ field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/sv.yml b/config/locales/sv.yml index 9b5adcd4178e8307a6891ed5e3c08dd5d1be0e72..0ed8e8d22085298bf71ed630c120e9c28805ceb3 100644 --- a/config/locales/sv.yml +++ b/config/locales/sv.yml @@ -838,3 +838,6 @@ sv: enumeration_doc_categories: Dokumentkategorier enumeration_activities: Aktiviteter (tidsuppföljning) field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/th.yml b/config/locales/th.yml index 06d46c5a416bb163246086ba5b614172b53ab506..147c59bfcfc8f6b78c20973882ff8d43cd435e71 100644 --- a/config/locales/th.yml +++ b/config/locales/th.yml @@ -781,3 +781,6 @@ th: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/tr.yml b/config/locales/tr.yml index d240cae3c58d395d0495c003dcf32fbd61c5aeb5..de46119407caf112a3ad2dd6c55f16ca26f31c73 100644 --- a/config/locales/tr.yml +++ b/config/locales/tr.yml @@ -817,3 +817,6 @@ tr: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/uk.yml b/config/locales/uk.yml index 08ca568afbcee29f4b35c1e3a3b74df958ededd7..4eda5113648e98aee5abf4a3467e25ff4d75799e 100644 --- a/config/locales/uk.yml +++ b/config/locales/uk.yml @@ -780,3 +780,6 @@ uk: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/vi.yml b/config/locales/vi.yml index 375f03b58e5b717b94526021e970e458ef022073..decba375ff21c884cee43cecec0f380711a7a16e 100644 --- a/config/locales/vi.yml +++ b/config/locales/vi.yml @@ -850,3 +850,6 @@ vi: field_identity_url: OpenID URL label_login_with_open_id_option: or login with OpenID field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/zh-TW.yml b/config/locales/zh-TW.yml index 1b9387b2b1ce603aac261f5a0762c8b577a54a29..f1cd072fe2209e5bd69fae4c45536673e16e291f 100644 --- a/config/locales/zh-TW.yml +++ b/config/locales/zh-TW.yml @@ -888,3 +888,6 @@ enumeration_doc_categories: 文件分類 enumeration_activities: 活動 (時間追蹤) field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/config/locales/zh.yml b/config/locales/zh.yml index dd7de569ed8616445d30d8edf8952275f33481b3..ee6671a2738a64970c38a71a3712630f9aa824b1 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -813,3 +813,6 @@ zh: enumeration_doc_categories: 文档类别 enumeration_activities: 活动(时间跟踪) field_content: Content + label_descending: Descending + label_sort: Sort + label_ascending: Ascending diff --git a/db/migrate/20090312172426_add_queries_sort_criteria.rb b/db/migrate/20090312172426_add_queries_sort_criteria.rb new file mode 100644 index 0000000000000000000000000000000000000000..743ed42ff77bdecc0b83059aced16f8ba290b6f0 --- /dev/null +++ b/db/migrate/20090312172426_add_queries_sort_criteria.rb @@ -0,0 +1,9 @@ +class AddQueriesSortCriteria < ActiveRecord::Migration + def self.up + add_column :queries, :sort_criteria, :text + end + + def self.down + remove_column :queries, :sort_criteria + end +end diff --git a/test/fixtures/queries.yml b/test/fixtures/queries.yml index f1202272907e4803fbda71de8e247de201df9c4f..a274ce350a5f9d3025f4febb6feebf2f3f95a7cf 100644 --- a/test/fixtures/queries.yml +++ b/test/fixtures/queries.yml @@ -67,3 +67,23 @@ queries_004: user_id: 2 column_names: +queries_005: + id: 5 + project_id: + is_public: true + name: Open issues by priority and tracker + filters: | + status_id: + :values: + - "1" + :operator: o + + user_id: 1 + column_names: + sort_criteria: | + --- + - - priority + - desc + - - tracker + - asc + \ No newline at end of file diff --git a/test/functional/queries_controller_test.rb b/test/functional/queries_controller_test.rb index de08b424596f0db5ecb16380ee06f3a71a8732ba..1951aff8f175d4369f79f49d66bb3c38131d7b20 100644 --- a/test/functional/queries_controller_test.rb +++ b/test/functional/queries_controller_test.rb @@ -111,6 +111,22 @@ class QueriesControllerTest < Test::Unit::TestCase assert q.valid? end + def test_new_with_sort + @request.session[:user_id] = 1 + post :new, + :confirm => '1', + :default_columns => '1', + :operators => {"status_id" => "o"}, + :values => {"status_id" => ["1"]}, + :query => {:name => "test_new_with_sort", + :is_public => "1", + :sort_criteria => {"0" => ["due_date", "desc"], "1" => ["tracker", ""]}} + + query = Query.find_by_name("test_new_with_sort") + assert_not_nil query + assert_equal [['due_date', 'desc'], ['tracker', 'asc']], query.sort_criteria + end + def test_get_edit_global_public_query @request.session[:user_id] = 1 get :edit, :id => 4 @@ -202,6 +218,19 @@ class QueriesControllerTest < Test::Unit::TestCase :disabled => 'disabled' } end + def test_get_edit_sort_criteria + @request.session[:user_id] = 1 + get :edit, :id => 5 + assert_response :success + assert_template 'edit' + assert_tag :tag => 'select', :attributes => { :name => 'query[sort_criteria][0][]' }, + :child => { :tag => 'option', :attributes => { :value => 'priority', + :selected => 'selected' } } + assert_tag :tag => 'select', :attributes => { :name => 'query[sort_criteria][0][]' }, + :child => { :tag => 'option', :attributes => { :value => 'desc', + :selected => 'selected' } } + end + def test_destroy @request.session[:user_id] = 2 post :destroy, :id => 1 diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index d568604c3509f489a03e23cf8a1fab93ecedf67b..80112b77918d9926436b0246417b6cc22eb03be5 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -195,6 +195,31 @@ class QueryTest < Test::Unit::TestCase assert q.has_column?(c) end + def test_default_sort + q = Query.new + assert_equal [], q.sort_criteria + end + + def test_set_sort_criteria_with_hash + q = Query.new + q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']} + assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria + end + + def test_set_sort_criteria_with_array + q = Query.new + q.sort_criteria = [['priority', 'desc'], 'tracker'] + assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria + end + + def test_create_query_with_sort + q = Query.new(:name => 'Sorted') + q.sort_criteria = [['priority', 'desc'], 'tracker'] + assert q.save + q.reload + assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria + end + def test_sort_by_string_custom_field_asc q = Query.new c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }