Skip to content

Commit

Permalink
Merge pull request #311 from n-rodriguez/wip/rework_numeric
Browse files Browse the repository at this point in the history
Fix #308
  • Loading branch information
seuros committed May 25, 2018
2 parents 3a9397a + a6a2d68 commit 6fcfe12
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ and in your model:

```ruby
class OrderedTag < ActiveRecord::Base
has_closure_tree order: 'sort_order'
has_closure_tree order: 'sort_order', numeric_order: true
end
```

Expand Down
1 change: 1 addition & 0 deletions lib/closure_tree/has_closure_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def has_closure_tree(options = {})
:hierarchy_table_name,
:name_column,
:order,
:numeric_order,
:touch,
:with_advisory_lock
)
Expand Down
3 changes: 2 additions & 1 deletion lib/closure_tree/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def initialize(model_class, options)
:parent_column_name => 'parent_id',
:dependent => :nullify, # or :destroy or :delete_all -- see the README
:name_column => 'name',
:with_advisory_lock => true
:with_advisory_lock => true,
:numeric_order => false
}.merge(options)
raise ArgumentError, "name_column can't be 'path'" if options[:name_column] == 'path'
if order_is_numeric?
Expand Down
8 changes: 1 addition & 7 deletions lib/closure_tree/support_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ 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]
c && c.type == :integer
options[:numeric_order]
end

def subclass?
Expand Down
1 change: 1 addition & 0 deletions spec/db/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class ContractType < ActiveRecord::Base
class Label < ActiveRecord::Base
# make sure order doesn't matter
acts_as_tree :order => :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order"
:numeric_order => true,
:parent_column_name => "mother_id",
:dependent => :destroy

Expand Down

0 comments on commit 6fcfe12

Please sign in to comment.