Defect #28930 » arelsql.patch
| app/controllers/journals_controller.rb (working copy) | ||
|---|---|---|
|
def index
|
||
|
retrieve_query
|
||
|
if @query.valid?
|
||
|
@journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC",
|
||
|
@journals = @query.journals(:order => Arel.sql("#{Journal.table_name}.created_on DESC"),
|
||
|
:limit => 25)
|
||
|
end
|
||
|
@title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
|
||
| app/models/message.rb (working copy) | ||
|---|---|---|
|
include Redmine::SafeAttributes
|
||
|
belongs_to :board
|
||
|
belongs_to :author, :class_name => 'User'
|
||
|
acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
|
||
|
acts_as_tree :counter_cache => :replies_count, :order => Arel.sql("#{Message.table_name}.created_on ASC")
|
||
|
acts_as_attachable
|
||
|
belongs_to :last_reply, :class_name => 'Message'
|
||
| app/models/news.rb (working copy) | ||
|---|---|---|
|
# returns latest news for projects visible by user
|
||
|
def self.latest(user = User.current, count = 5)
|
||
|
visible(user).preload(:author, :project).order("#{News.table_name}.created_on DESC").limit(count).to_a
|
||
|
visible(user).preload(:author, :project).order(Arel.sql("#{News.table_name}.created_on DESC")).limit(count).to_a
|
||
|
end
|
||
|
private
|
||
| app/models/query.rb (working copy) | ||
|---|---|---|
|
def group_by_sort_order
|
||
|
if column = group_by_column
|
||
|
order = (sort_criteria.order_for(column.name) || column.default_order || 'asc').try(:upcase)
|
||
|
Array(column.sortable).map {|s| "#{s} #{order}"}
|
||
|
Array(column.sortable).map {|s| Arel.sql("#{s} #{order}")}
|
||
|
end
|
||
|
end
|
||
| app/models/version.rb (working copy) | ||
|---|---|---|
|
def self.fields_for_order_statement(table=nil)
|
||
|
table ||= table_name
|
||
|
["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
|
||
|
statements = ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
|
||
|
statements.map!{|order| Arel.sql(order) }
|
||
|
statements
|
||
|
end
|
||
|
scope :sorted, lambda { order(fields_for_order_statement) }
|
||
| app/models/wiki_page.rb (working copy) | ||
|---|---|---|
|
has_one :content_without_text, lambda {without_text.readonly}, :class_name => 'WikiContent', :foreign_key => 'page_id'
|
||
|
acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
|
||
|
acts_as_tree :dependent => :nullify, :order => 'title'
|
||
|
acts_as_tree :dependent => :nullify, :order => Arel.sql('title')
|
||
|
acts_as_watchable
|
||
|
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) | ||
|---|---|---|
|
class Mixin < ActiveRecord::Base
|
||
|
end
|
||
|
class TreeMixin < Mixin
|
||
|
acts_as_tree :foreign_key => "parent_id", :order => "id"
|
||
|
class TreeMixin < Mixin
|
||
|
acts_as_tree :foreign_key => "parent_id", :order => Arel.sql("id")
|
||
|
end
|
||
|
class TreeMixinWithoutOrder < Mixin
|
||
| ... | ... | |
|
end
|
||
|
class TreeTest < Test::Unit::TestCase
|
||
|
|
||
|
def setup
|
||
|
setup_db
|
||
|
@root1 = TreeMixin.create!
|
||
| ... | ... | |
|
assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
|
||
|
assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
|
||
|
assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
class TreeTestWithEagerLoading < Test::Unit::TestCase
|
||
|
|
||
|
def setup
|
||
|
def setup
|
||
|
teardown_db
|
||
|
setup_db
|
||
|
@root1 = TreeMixin.create!
|
||
| ... | ... | |
|
@root_child2 = TreeMixin.create! :parent_id => @root1.id
|
||
|
@root2 = TreeMixin.create!
|
||
|
@root3 = TreeMixin.create!
|
||
|
|
||
|
@rc1 = RecursivelyCascadedTreeMixin.create!
|
||
|
@rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
|
||
|
@rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
|
||
|
@rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
|
||
|
@rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
|
||
|
end
|
||
| ... | ... | |
|
def teardown
|
||
|
teardown_db
|
||
|
end
|
||
|
|
||
|
def test_eager_association_loading
|
||
|
roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
|
||
|
assert_equal [@root1, @root2, @root3], roots
|
||
|
assert_equal [@root1, @root2, @root3], roots
|
||
|
assert_no_queries do
|
||
|
assert_equal 2, roots[0].children.size
|
||
|
assert_equal 0, roots[1].children.size
|
||
|
assert_equal 0, roots[2].children.size
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
|
||
|
root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
|
||
|
assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
|
||
|
end
|
||
|
|
||
|
def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
|
||
|
root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
|
||
|
assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
|
||
|
end
|
||
|
|
||
|
def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
|
||
|
leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
|
||
|
assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
class TreeTestWithoutOrder < Test::Unit::TestCase
|
||
|
|
||
|
def setup
|
||
|
def setup
|
||
|
setup_db
|
||
|
@root1 = TreeMixinWithoutOrder.create!
|
||
|
@root2 = TreeMixinWithoutOrder.create!
|
||
| ... | ... | |
|
def test_root
|
||
|
assert [@root1, @root2].include?(TreeMixinWithoutOrder.root)
|
||
|
end
|
||
|
|
||
|
def test_roots
|
||
|
assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| lib/redmine/helpers/gantt.rb (working copy) | ||
|---|---|---|
|
def issues
|
||
|
@issues ||= @query.issues(
|
||
|
:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
|
||
|
:order => "#{Project.table_name}.lft ASC, #{Issue.table_name}.id ASC",
|
||
|
:order => Arel.sql("#{Project.table_name}.lft ASC, #{Issue.table_name}.id ASC"),
|
||
|
:limit => @max_rows
|
||
|
)
|
||
|
end
|
||
| lib/redmine/sort_criteria.rb (working copy) | ||
|---|---|---|
|
sql = self.collect do |k,o|
|
||
|
if s = sortable_columns[k]
|
||
|
s = [s] unless s.is_a?(Array)
|
||
|
s.collect {|c| append_order(c, o)}
|
||
|
s.collect {|c| Arel.sql(append_order(c, o))}
|
||
|
end
|
||
|
end.flatten.compact
|
||
|
sql.blank? ? nil : sql
|
||
| test/unit/query_test.rb (working copy) | ||
|---|---|---|
|
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
|
||
|
assert c
|
||
|
assert c.sortable
|
||
|
issues = q.issues(:order => "#{c.sortable} ASC")
|
||
|
issues = q.issues(:order => Arel.sql("#{c.sortable} ASC"))
|
||
|
values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
|
||
|
assert !values.empty?
|
||
|
assert_equal values.sort, values
|
||
| ... | ... | |
|
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
|
||
|
assert c
|
||
|
assert c.sortable
|
||
|
issues = q.issues(:order => "#{c.sortable} DESC")
|
||
|
issues = q.issues(:order => Arel.sql("#{c.sortable} DESC"))
|
||
|
values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
|
||
|
assert !values.empty?
|
||
|
assert_equal values.sort.reverse, values
|
||
| ... | ... | |
|
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' }
|
||
|
assert c
|
||
|
assert c.sortable
|
||
|
issues = q.issues(:order => "#{c.sortable} ASC")
|
||
|
issues = q.issues(:order => Arel.sql("#{c.sortable} ASC"))
|
||
|
values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact
|
||
|
assert !values.empty?
|
||
|
assert_equal values.sort, values
|
||
| ... | ... | |
|
def test_issue_ids
|
||
|
q = IssueQuery.new(:name => '_')
|
||
|
order = "issues.subject, issues.id"
|
||
|
order = Arel.sql("issues.subject, issues.id")
|
||
|
issues = q.issues(:order => order)
|
||
|
assert_equal issues.map(&:id), q.issue_ids(:order => order)
|
||
|
end
|
||