hierarchical_db 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +38 -4
- data/hierarchical_db-0.0.1.gem +0 -0
- data/hierarchical_db.gemspec +2 -2
- data/lib/generators/hierarchical_db_generator.rb +29 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64883431f195b17073aa612d3557ceb12c8fd81e
|
4
|
+
data.tar.gz: 6db08ffc8ac1a1a2b3d130814dee8af8f93b1b72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f71f74d22c63b69addb577443565ef5872732354668234a3c2570fd32e8096e8ad32b4d7c0706774b9dca796674013d330207cb34a9c98dd43709aa7ed6c444
|
7
|
+
data.tar.gz: e9587fd1e7683d319186b67aa4e04bd601b920b742be8cb202b05d6ba3fff165c8e6764b3a3db33dc3dcb6e8938389b89f8f56d3bacd757b4bb743bf54ff02bd
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# HierarchicalDb
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
This gem has the implementation of **[Hierarchical databases](http://www.sitepoint.com/hierarchical-data-database/)**
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -20,9 +18,45 @@ Or install it yourself as:
|
|
20
18
|
|
21
19
|
$ gem install hierarchical_db
|
22
20
|
|
21
|
+
Next you have to add a migration into your model with attributes *lft* and *rgt*. For example, if you have a model called *Territory* (to manage countries, cities and all the other hierarchies) with the following relationship:
|
22
|
+
```ruby
|
23
|
+
class Territory < ActiveRecord::Base
|
24
|
+
belongs_to :parent_territory, class_name: 'Territory'
|
25
|
+
end
|
26
|
+
```
|
27
|
+
You have to add a migration like this:
|
28
|
+
```ruby
|
29
|
+
class AddSortedTreeFields < ActiveRecord::Migration
|
30
|
+
def change
|
31
|
+
add_column :territories, :lft, :integer
|
32
|
+
add_column :territories, :rgt, :integer
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
Into the model you have to include Hierarchies adding the code below:
|
37
|
+
```ruby
|
38
|
+
class Territory < ActiveRecord::Base
|
39
|
+
include HierarchicalDb #we added this
|
40
|
+
belongs_to :parent_territory, class_name: 'Territory'
|
41
|
+
end
|
42
|
+
```
|
43
|
+
Finally we add two alias methods that are useful and necessary to deal with hierarchies:
|
44
|
+
```ruby
|
45
|
+
class Territory < ActiveRecord::Base
|
46
|
+
include HierarchicalDb
|
47
|
+
belongs_to :parent_territory, class_name: 'Territory'
|
48
|
+
# alias methods
|
49
|
+
alias_method :children, :territories #we added this
|
50
|
+
alias_method :parent, :parent_territory #we added this
|
51
|
+
end
|
52
|
+
```
|
23
53
|
## Usage
|
24
54
|
|
25
|
-
|
55
|
+
Continuing the example, if you have data inside your Territory model, then you have to execute :
|
56
|
+
```ruby
|
57
|
+
Territory.sort_tree
|
58
|
+
```
|
59
|
+
This command initializes your tree and fills *lft* and *rgt* attributes with corresponding information.
|
26
60
|
|
27
61
|
## Development
|
28
62
|
|
Binary file
|
data/hierarchical_db.gemspec
CHANGED
@@ -5,12 +5,12 @@ require 'hierarchical_db/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "hierarchical_db"
|
8
|
-
spec.version = "0.0.
|
8
|
+
spec.version = "0.0.2"
|
9
9
|
spec.authors = ["Gustavo"]
|
10
10
|
spec.email = ["ginzunza@rebuss.cl"]
|
11
11
|
|
12
12
|
spec.summary = ""
|
13
|
-
spec.description = "
|
13
|
+
spec.description = "helps to store and deal with data inside hierarchical databases"
|
14
14
|
spec.homepage = "https://github.com/ginzunza/hierarchical_db"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
require 'rails/generators/active_record'
|
3
|
+
|
4
|
+
module GemName
|
5
|
+
module Generators
|
6
|
+
class GemNameGenerator < ActiveRecord::Generators::Base
|
7
|
+
|
8
|
+
include Rails::Generators::ResourceHelpers
|
9
|
+
|
10
|
+
namespace "gem_name"
|
11
|
+
|
12
|
+
desc "Creates GemName Migrations"
|
13
|
+
|
14
|
+
source_root File.expand_path("../templates", __FILE__)
|
15
|
+
|
16
|
+
def copy_migration
|
17
|
+
migration_template "migration_existing.rb", "db/migrate/add_gem_name_to_#{plural_name.downcase}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def migration_data
|
21
|
+
<<RUBY
|
22
|
+
## Add active column to table
|
23
|
+
t.integer :lft
|
24
|
+
t.integer :rgt
|
25
|
+
RUBY
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hierarchical_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gustavo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
-
description:
|
41
|
+
description: helps to store and deal with data inside hierarchical databases
|
42
42
|
email:
|
43
43
|
- ginzunza@rebuss.cl
|
44
44
|
executables: []
|
@@ -55,7 +55,9 @@ files:
|
|
55
55
|
- bin/console
|
56
56
|
- bin/setup
|
57
57
|
- hierarchical_db-0.0.0.gem
|
58
|
+
- hierarchical_db-0.0.1.gem
|
58
59
|
- hierarchical_db.gemspec
|
60
|
+
- lib/generators/hierarchical_db_generator.rb
|
59
61
|
- lib/hierarchical_db.rb
|
60
62
|
- lib/hierarchical_db/version.rb
|
61
63
|
homepage: https://github.com/ginzunza/hierarchical_db
|