Skip to content

Commit

Permalink
Merge pull request #306 from n-rodriguez/master
Browse files Browse the repository at this point in the history
Fix #264
  • Loading branch information
seuros authored Apr 26, 2018
2 parents cb7363e + 9b13ab9 commit 3b945f5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
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

0 comments on commit 3b945f5

Please sign in to comment.