-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module LtreeRails | ||
module Model | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
belongs_to :parent, | ||
foreign_key: _ltree_parent_column_name, | ||
class_name: self, | ||
inverse_of: :children | ||
|
||
has_many :children, | ||
foreign_key: _ltree_parent_column_name, | ||
dependent: ltree_config.children_dependent, | ||
class_name: self, | ||
inverse_of: :parent | ||
end | ||
|
||
def root? | ||
persisted? ? _ltree_parent_column_value.nil? : parent.nil? | ||
end | ||
|
||
def child? | ||
!root? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module LtreeRails | ||
module Support | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def _ltree_parent_column_value | ||
read_attribute(self.class._ltree_parent_column_name) | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def _ltree_parent_column_name | ||
ltree_config.parent_column_name.to_s | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
describe LtreeRails::Model do | ||
let(:root_category) { Category.create! } | ||
|
||
it 'adds parent relation' do | ||
c = Category.new(parent_id: root_category.id) | ||
c.save! | ||
expect(c.parent).to eq root_category | ||
end | ||
|
||
it 'adds category relation' do | ||
c1 = Category.new(parent_id: root_category.id) | ||
c1.save! | ||
c2 = Category.new(parent_id: root_category.id) | ||
c2.save! | ||
expect(root_category.children).to eq [c1, c2] | ||
end | ||
|
||
context 'root?' do | ||
context 'when persisted' do | ||
it 'returns true when parent column value is nil' do | ||
expect(root_category.root?).to be true | ||
expect(root_category.child?).to be false | ||
end | ||
|
||
it 'returns false when parent column value is not nil' do | ||
c = Category.new(parent_id: root_category.id) | ||
c.save! | ||
expect(c.root?).to be false | ||
expect(c.child?).to be true | ||
end | ||
end | ||
|
||
context 'when new record' do | ||
it 'returns true if parent is nil' do | ||
c = Category.new | ||
expect(c.root?).to be true | ||
expect(c.child?).to be false | ||
end | ||
|
||
it 'returns false if parent is not nil' do | ||
c = Category.new | ||
c.parent = root_category | ||
expect(c.root?).to be false | ||
expect(c.child?).to be true | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) | ||
require 'ltree_rails' | ||
require 'coveralls' | ||
require 'database_cleaner' | ||
require 'active_record' | ||
Coveralls.wear! | ||
|
||
Dir['./spec/support/**/*.rb'].sort.each { |f| require f } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
RSpec.configure do |config| | ||
DatabaseCleaner.strategy = :truncation | ||
|
||
config.before(:each) do | ||
DatabaseCleaner.start | ||
end | ||
|
||
config.after(:each) do | ||
DatabaseCleaner.clean | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
describe Category do | ||
it_behaves_like 'has_ltree' | ||
end | ||
end |