Defect #28930 » arelsql.patch
| app/controllers/journals_controller.rb (working copy) | ||
|---|---|---|
| 31 | 31 |
def index |
| 32 | 32 |
retrieve_query |
| 33 | 33 |
if @query.valid? |
| 34 |
@journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC",
|
|
| 34 |
@journals = @query.journals(:order => Arel.sql("#{Journal.table_name}.created_on DESC"),
|
|
| 35 | 35 |
:limit => 25) |
| 36 | 36 |
end |
| 37 | 37 |
@title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) |
| app/models/message.rb (working copy) | ||
|---|---|---|
| 19 | 19 |
include Redmine::SafeAttributes |
| 20 | 20 |
belongs_to :board |
| 21 | 21 |
belongs_to :author, :class_name => 'User' |
| 22 |
acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
|
|
| 22 |
acts_as_tree :counter_cache => :replies_count, :order => Arel.sql("#{Message.table_name}.created_on ASC")
|
|
| 23 | 23 |
acts_as_attachable |
| 24 | 24 |
belongs_to :last_reply, :class_name => 'Message' |
| 25 | 25 | |
| app/models/news.rb (working copy) | ||
|---|---|---|
| 80 | 80 | |
| 81 | 81 |
# returns latest news for projects visible by user |
| 82 | 82 |
def self.latest(user = User.current, count = 5) |
| 83 |
visible(user).preload(:author, :project).order("#{News.table_name}.created_on DESC").limit(count).to_a
|
|
| 83 |
visible(user).preload(:author, :project).order(Arel.sql("#{News.table_name}.created_on DESC")).limit(count).to_a
|
|
| 84 | 84 |
end |
| 85 | 85 | |
| 86 | 86 |
private |
| app/models/query.rb (working copy) | ||
|---|---|---|
| 787 | 787 |
def group_by_sort_order |
| 788 | 788 |
if column = group_by_column |
| 789 | 789 |
order = (sort_criteria.order_for(column.name) || column.default_order || 'asc').try(:upcase) |
| 790 |
Array(column.sortable).map {|s| "#{s} #{order}"}
|
|
| 790 |
Array(column.sortable).map {|s| Arel.sql("#{s} #{order}")}
|
|
| 791 | 791 |
end |
| 792 | 792 |
end |
| 793 | 793 | |
| app/models/version.rb (working copy) | ||
|---|---|---|
| 317 | 317 | |
| 318 | 318 |
def self.fields_for_order_statement(table=nil) |
| 319 | 319 |
table ||= table_name |
| 320 |
["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
|
|
| 320 |
statements = ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
|
|
| 321 |
statements.map!{|order| Arel.sql(order) }
|
|
| 322 |
statements |
|
| 321 | 323 |
end |
| 322 | 324 | |
| 323 | 325 |
scope :sorted, lambda { order(fields_for_order_statement) }
|
| app/models/wiki_page.rb (working copy) | ||
|---|---|---|
| 26 | 26 |
has_one :content_without_text, lambda {without_text.readonly}, :class_name => 'WikiContent', :foreign_key => 'page_id'
|
| 27 | 27 | |
| 28 | 28 |
acts_as_attachable :delete_permission => :delete_wiki_pages_attachments |
| 29 |
acts_as_tree :dependent => :nullify, :order => 'title'
|
|
| 29 |
acts_as_tree :dependent => :nullify, :order => Arel.sql('title')
|
|
| 30 | 30 | |
| 31 | 31 |
acts_as_watchable |
| 32 | 32 |
acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
|
| lib/plugins/acts_as_tree/test/acts_as_tree_test.rb (working copy) | ||
|---|---|---|
| 43 | 43 |
class Mixin < ActiveRecord::Base |
| 44 | 44 |
end |
| 45 | 45 | |
| 46 |
class TreeMixin < Mixin
|
|
| 47 |
acts_as_tree :foreign_key => "parent_id", :order => "id"
|
|
| 46 |
class TreeMixin < Mixin |
|
| 47 |
acts_as_tree :foreign_key => "parent_id", :order => Arel.sql("id")
|
|
| 48 | 48 |
end |
| 49 | 49 | |
| 50 | 50 |
class TreeMixinWithoutOrder < Mixin |
| ... | ... | |
| 57 | 57 |
end |
| 58 | 58 | |
| 59 | 59 |
class TreeTest < Test::Unit::TestCase |
| 60 |
|
|
| 60 | ||
| 61 | 61 |
def setup |
| 62 | 62 |
setup_db |
| 63 | 63 |
@root1 = TreeMixin.create! |
| ... | ... | |
| 146 | 146 |
assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings |
| 147 | 147 |
assert_equal [@root1, @root2, @root3], @root2.self_and_siblings |
| 148 | 148 |
assert_equal [@root1, @root2, @root3], @root3.self_and_siblings |
| 149 |
end
|
|
| 149 |
end |
|
| 150 | 150 |
end |
| 151 | 151 | |
| 152 | 152 |
class TreeTestWithEagerLoading < Test::Unit::TestCase |
| 153 |
|
|
| 154 |
def setup
|
|
| 153 | ||
| 154 |
def setup |
|
| 155 | 155 |
teardown_db |
| 156 | 156 |
setup_db |
| 157 | 157 |
@root1 = TreeMixin.create! |
| ... | ... | |
| 160 | 160 |
@root_child2 = TreeMixin.create! :parent_id => @root1.id |
| 161 | 161 |
@root2 = TreeMixin.create! |
| 162 | 162 |
@root3 = TreeMixin.create! |
| 163 |
|
|
| 163 | ||
| 164 | 164 |
@rc1 = RecursivelyCascadedTreeMixin.create! |
| 165 |
@rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
|
|
| 165 |
@rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id |
|
| 166 | 166 |
@rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id |
| 167 | 167 |
@rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id |
| 168 | 168 |
end |
| ... | ... | |
| 170 | 170 |
def teardown |
| 171 | 171 |
teardown_db |
| 172 | 172 |
end |
| 173 |
|
|
| 173 | ||
| 174 | 174 |
def test_eager_association_loading |
| 175 | 175 |
roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id") |
| 176 |
assert_equal [@root1, @root2, @root3], roots
|
|
| 176 |
assert_equal [@root1, @root2, @root3], roots |
|
| 177 | 177 |
assert_no_queries do |
| 178 | 178 |
assert_equal 2, roots[0].children.size |
| 179 | 179 |
assert_equal 0, roots[1].children.size |
| 180 | 180 |
assert_equal 0, roots[2].children.size |
| 181 |
end
|
|
| 181 |
end |
|
| 182 | 182 |
end |
| 183 |
|
|
| 183 | ||
| 184 | 184 |
def test_eager_association_loading_with_recursive_cascading_three_levels_has_many |
| 185 | 185 |
root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
|
| 186 | 186 |
assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
|
| 187 | 187 |
end |
| 188 |
|
|
| 188 | ||
| 189 | 189 |
def test_eager_association_loading_with_recursive_cascading_three_levels_has_one |
| 190 | 190 |
root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
|
| 191 | 191 |
assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
|
| 192 | 192 |
end |
| 193 |
|
|
| 193 | ||
| 194 | 194 |
def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to |
| 195 | 195 |
leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
|
| 196 | 196 |
assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
|
| 197 |
end
|
|
| 197 |
end |
|
| 198 | 198 |
end |
| 199 | 199 | |
| 200 | 200 |
class TreeTestWithoutOrder < Test::Unit::TestCase |
| 201 |
|
|
| 202 |
def setup
|
|
| 201 | ||
| 202 |
def setup |
|
| 203 | 203 |
setup_db |
| 204 | 204 |
@root1 = TreeMixinWithoutOrder.create! |
| 205 | 205 |
@root2 = TreeMixinWithoutOrder.create! |
| ... | ... | |
| 212 | 212 |
def test_root |
| 213 | 213 |
assert [@root1, @root2].include?(TreeMixinWithoutOrder.root) |
| 214 | 214 |
end |
| 215 |
|
|
| 215 | ||
| 216 | 216 |
def test_roots |
| 217 | 217 |
assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots |
| 218 | 218 |
end |
| 219 |
end |
|
| 219 |
end |
|
| lib/redmine/helpers/gantt.rb (working copy) | ||
|---|---|---|
| 139 | 139 |
def issues |
| 140 | 140 |
@issues ||= @query.issues( |
| 141 | 141 |
:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], |
| 142 |
:order => "#{Project.table_name}.lft ASC, #{Issue.table_name}.id ASC",
|
|
| 142 |
:order => Arel.sql("#{Project.table_name}.lft ASC, #{Issue.table_name}.id ASC"),
|
|
| 143 | 143 |
:limit => @max_rows |
| 144 | 144 |
) |
| 145 | 145 |
end |
| lib/redmine/sort_criteria.rb (working copy) | ||
|---|---|---|
| 78 | 78 |
sql = self.collect do |k,o| |
| 79 | 79 |
if s = sortable_columns[k] |
| 80 | 80 |
s = [s] unless s.is_a?(Array) |
| 81 |
s.collect {|c| append_order(c, o)}
|
|
| 81 |
s.collect {|c| Arel.sql(append_order(c, o))}
|
|
| 82 | 82 |
end |
| 83 | 83 |
end.flatten.compact |
| 84 | 84 |
sql.blank? ? nil : sql |
| test/unit/query_test.rb (working copy) | ||
|---|---|---|
| 1483 | 1483 |
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
|
| 1484 | 1484 |
assert c |
| 1485 | 1485 |
assert c.sortable |
| 1486 |
issues = q.issues(:order => "#{c.sortable} ASC")
|
|
| 1486 |
issues = q.issues(:order => Arel.sql("#{c.sortable} ASC"))
|
|
| 1487 | 1487 |
values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
|
| 1488 | 1488 |
assert !values.empty? |
| 1489 | 1489 |
assert_equal values.sort, values |
| ... | ... | |
| 1494 | 1494 |
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
|
| 1495 | 1495 |
assert c |
| 1496 | 1496 |
assert c.sortable |
| 1497 |
issues = q.issues(:order => "#{c.sortable} DESC")
|
|
| 1497 |
issues = q.issues(:order => Arel.sql("#{c.sortable} DESC"))
|
|
| 1498 | 1498 |
values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
|
| 1499 | 1499 |
assert !values.empty? |
| 1500 | 1500 |
assert_equal values.sort.reverse, values |
| ... | ... | |
| 1505 | 1505 |
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' }
|
| 1506 | 1506 |
assert c |
| 1507 | 1507 |
assert c.sortable |
| 1508 |
issues = q.issues(:order => "#{c.sortable} ASC")
|
|
| 1508 |
issues = q.issues(:order => Arel.sql("#{c.sortable} ASC"))
|
|
| 1509 | 1509 |
values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact
|
| 1510 | 1510 |
assert !values.empty? |
| 1511 | 1511 |
assert_equal values.sort, values |
| ... | ... | |
| 1703 | 1703 | |
| 1704 | 1704 |
def test_issue_ids |
| 1705 | 1705 |
q = IssueQuery.new(:name => '_') |
| 1706 |
order = "issues.subject, issues.id"
|
|
| 1706 |
order = Arel.sql("issues.subject, issues.id")
|
|
| 1707 | 1707 |
issues = q.issues(:order => order) |
| 1708 | 1708 |
assert_equal issues.map(&:id), q.issue_ids(:order => order) |
| 1709 | 1709 |
end |