Skip to content

Commit 3c9363c

Browse files
committed
StaticRecord のモデルをツリーにする例を追加
1 parent 609c76e commit 3c9363c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

examples/0240_static_record.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# StaticRecord を Treeable 対応
2+
require "bundler/setup"
3+
require "tree_support"
4+
require "static_record"
5+
6+
class Foo
7+
include StaticRecord
8+
static_record [
9+
{:key => :a, :name => "A", :parent => nil},
10+
{:key => :b, :name => "B", :parent => :a},
11+
{:key => :c, :name => "C", :parent => :b},
12+
]
13+
14+
# parent と children に反応できれば構造は何でもよい
15+
concerning :TreeMethods do
16+
included do
17+
include TreeSupport::Treeable
18+
include TreeSupport::Stringify
19+
end
20+
21+
def parent
22+
self.class[@attributes[:parent]]
23+
end
24+
25+
def children
26+
self.class.find_all {|e| e.parent == self }
27+
end
28+
end
29+
end
30+
31+
puts Foo.find_all(&:root?).collect(&:to_s_tree)
32+
# >> A
33+
# >> └─B
34+
# >> └─C

examples/Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
source "https://rubygems.org"
22

33
gem "tree_support", :path => ".."
4+
gem "static_record", :github => "akicho8/static_record"
45

56
gem "rails"
67
gem "activerecord"

0 commit comments

Comments
 (0)