Skip to content

Commit efb2660

Browse files
authored
Merge pull request #204 from jayfredlund/master
adds ActiveRecord >= 5.0.0.beta1 support for tree roots; fixes #195
2 parents 7dbcefb + 9197ef9 commit efb2660

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/closure_tree/model.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ module Model
55
extend ActiveSupport::Concern
66

77
included do
8-
belongs_to :parent,
9-
class_name: _ct.model_class.to_s,
10-
foreign_key: _ct.parent_column_name,
11-
inverse_of: :children,
12-
touch: _ct.options[:touch]
8+
9+
belongs_to :parent, nil, *_ct.belongs_to_with_optional_option(
10+
class_name: _ct.model_class.to_s,
11+
foreign_key: _ct.parent_column_name,
12+
inverse_of: :children,
13+
touch: _ct.options[:touch],
14+
optional: true)
1315

1416
order_by_generations = "#{_ct.quoted_hierarchy_table_name}.generations asc"
1517

lib/closure_tree/support.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def scope_with_order(scope, additional_order_by = nil)
7676
end
7777
end
7878

79+
def belongs_to_with_optional_option(opts)
80+
[ActiveRecord::VERSION::MAJOR < 5 ? opts.except(:optional) : opts]
81+
end
82+
7983
# lambda-ize the order, but don't apply the default order_option
8084
def has_many_without_order_option(opts)
8185
[lambda { order(opts[:order]) }, opts.except(:order)]

0 commit comments

Comments
 (0)