Skip to content

Commit

Permalink
Merge 3c3cede into 2e06c88
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-tikhonov committed Mar 2, 2015
2 parents 2e06c88 + 3c3cede commit afe388b
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/ltree_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module LtreeRails
autoload :HasLtree
autoload :Configuration
autoload :Configurable
autoload :Model
autoload :Support
end

ActiveSupport.on_load(:active_record) do
Expand Down
7 changes: 4 additions & 3 deletions lib/ltree_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ class Configuration
include ActiveSupport::Configurable

CONFIGURABLE_OPTIONS = {
parent_id_column: 'parent_id',
path_column: 'path',
label_column: 'parent_id'
parent_column_name: 'parent_id',
path_column_name: 'path',
label_column_name: 'parent_id',
children_dependent: :nullify
}.freeze

CONFIGURABLE_OPTIONS.each do |option, default|
Expand Down
2 changes: 2 additions & 0 deletions lib/ltree_rails/has_ltree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def has_ltree(opt = {})
private

def include_dependencies
include ::LtreeRails::Support
include ::LtreeRails::Configurable
include ::LtreeRails::Model
end

def check_options!(opt)
Expand Down
26 changes: 26 additions & 0 deletions lib/ltree_rails/model.rb
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
17 changes: 17 additions & 0 deletions lib/ltree_rails/support.rb
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
1 change: 1 addition & 0 deletions ltree_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rspec', '~> 3.2'
spec.add_development_dependency 'coveralls'
spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'database_cleaner'
end
4 changes: 2 additions & 2 deletions spec/has_ltree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

it 'set option value when pass valid option' do
c = Class.new(ActiveRecord::Base) do
has_ltree(parent_id_column: :some_custom_column)
has_ltree(parent_column_name: :some_custom_column)
end
expect(c.ltree_config.parent_id_column).to eq(:some_custom_column)
expect(c.ltree_config.parent_column_name).to eq(:some_custom_column)
end
end
48 changes: 48 additions & 0 deletions spec/model_spec.rb
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
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
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 }
11 changes: 11 additions & 0 deletions spec/support/database_cleaner.rb
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
8 changes: 8 additions & 0 deletions spec/support/shared_examples_for_has_ltree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@
it 'allows user to pass config params' do
expect(described_class).to respond_to(:ltree_config)
end

it 'adds parent association' do
expect(described_class.new).to respond_to(:parent)
end

it 'adds children association' do
expect(described_class.new).to respond_to(:children)
end
end
2 changes: 1 addition & 1 deletion spec/models_spec.rb → spec/support/test_models_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
describe Category do
it_behaves_like 'has_ltree'
end
end

0 comments on commit afe388b

Please sign in to comment.