Skip to content

Commit 9b13ab9

Browse files
committed
Fix #264
1 parent cb7363e commit 9b13ab9

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

lib/closure_tree/model.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Model
1313
touch: _ct.options[:touch],
1414
optional: true)
1515

16-
order_by_generations = Arel.sql("#{_ct.quoted_hierarchy_table_name}.generations ASC")
16+
order_by_generations = -> { Arel.sql("#{_ct.quoted_hierarchy_table_name}.generations ASC") }
1717

1818
has_many :children, *_ct.has_many_with_order_option(
1919
class_name: _ct.model_class.to_s,

lib/closure_tree/support.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,15 @@ def belongs_to_with_optional_option(opts)
8282

8383
# lambda-ize the order, but don't apply the default order_option
8484
def has_many_without_order_option(opts)
85-
[lambda { order(opts[:order]) }, opts.except(:order)]
85+
[lambda { order(opts[:order].call) }, opts.except(:order)]
8686
end
8787

8888
def has_many_with_order_option(opts)
8989
order_options = [opts[:order], order_by].compact
90-
[lambda { order(order_options) }, opts.except(:order)]
90+
[lambda {
91+
order_options = order_options.map { |o| o.is_a?(Proc) ? o.call : o }
92+
order(order_options)
93+
}, opts.except(:order)]
9194
end
9295

9396
def ids_from(scope)

lib/closure_tree/support_flags.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def order_option?
1717
end
1818

1919
def order_is_numeric?
20+
# skip if database is not connected.
21+
return false unless ::ActiveRecord::Base.connected?
22+
2023
# The table might not exist yet (in the case of ActiveRecord::Observer use, see issue 32)
2124
return false if !order_option? || !model_class.table_exists?
2225
c = model_class.columns_hash[order_column]

0 commit comments

Comments
 (0)