Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix #264 #306

Merged
merged 1 commit into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/closure_tree/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Model
touch: _ct.options[:touch],
optional: true)

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

has_many :children, *_ct.has_many_with_order_option(
class_name: _ct.model_class.to_s,
Expand Down
7 changes: 5 additions & 2 deletions lib/closure_tree/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ def belongs_to_with_optional_option(opts)

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

def has_many_with_order_option(opts)
order_options = [opts[:order], order_by].compact
[lambda { order(order_options) }, opts.except(:order)]
[lambda {
order_options = order_options.map { |o| o.is_a?(Proc) ? o.call : o }
order(order_options)
}, opts.except(:order)]
end

def ids_from(scope)
Expand Down
3 changes: 3 additions & 0 deletions lib/closure_tree/support_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def order_option?
end

def order_is_numeric?
# skip if database is not connected.
return false unless ::ActiveRecord::Base.connected?

# The table might not exist yet (in the case of ActiveRecord::Observer use, see issue 32)
return false if !order_option? || !model_class.table_exists?
c = model_class.columns_hash[order_column]
Expand Down