edge 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aae17b676f3e8c7f0c5228f66a548816cd41bb17
4
- data.tar.gz: a45d3fb5955a229e42e319fbfa99e2da9342e533
3
+ metadata.gz: de6a7e89e268d6a04bcb12ea49f331c0492a12c0
4
+ data.tar.gz: d3223c0995b78bc3c9de539948b225ee2c0a148a
5
5
  SHA512:
6
- metadata.gz: 51317099a665b022b83d0d5bda61d8483a636fb9fe0ff01cd5e5560e87999d283878a71cba51dc3f62cca1c19cf26570932bdf884c3617fa1877e96b0e9c2aa2
7
- data.tar.gz: f526a15443b51f5cb52d7619f3cbdfd54cdf511cec4a33bf245b7e79c7a4a0bc0bfde6fbe5b00fae89f8d929e5a6ed4282db958b5f1d21291d29787b7872674c
6
+ metadata.gz: e906301864501743b6848b67a40b33d60e5447c7a34c39023327d577e03e43b0ad926834435c31dbaa5b924f16a1b4dcf3bf095565d18779d8cd180b9b4e3776
7
+ data.tar.gz: 85d2f8ea8e2e95dd163f17a2e436cec52b50bb80cc8bda6afc012cedba282548cd23991ea0bb4917b91847832ea9a7fb51734ff9a34bfc1b4a5df9011cd1f115
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.4.3 (October 23, 2015)
2
+
3
+ * Allow dependent option to acts_as_forest (WANG QUANG)
4
+
1
5
  # 0.4.2 (May 21, 2015)
2
6
 
3
7
  * Fixed premature SQL-ization that could result in PG protocol violation errors (Neil E. Pearson)
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ task :default => :spec
8
8
  namespace :db do
9
9
  desc 'bootstrap database'
10
10
  task :setup do
11
- sh "createdb edge_test"
11
+ sh "createdb edge_test || true"
12
12
  sh "psql edge_test < spec/database_structure.sql"
13
13
  end
14
14
  end
data/gemfiles/4.0.gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "activerecord", "4.0.0"
3
+ gem "activerecord", "4.0.13"
4
4
 
5
5
  gemspec :path=>"../"
data/gemfiles/4.1.gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "activerecord", "~> 4.1.8"
3
+ gem "activerecord", "~> 4.1.13"
4
4
 
5
5
  gemspec :path=>"../"
data/gemfiles/4.2.gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "activerecord", "~> 4.2.0"
3
+ gem "activerecord", "~> 4.2.4"
4
4
 
5
5
  gemspec :path=>"../"
data/lib/edge/forest.rb CHANGED
@@ -4,10 +4,11 @@ module Edge
4
4
  module ActsAsForest
5
5
  # options:
6
6
  #
7
+ # * dependent - passed to children has_many (default: none)
7
8
  # * foreign_key - column name to use for parent foreign_key (default: parent_id)
8
9
  # * order - how to order children (default: none)
9
10
  def acts_as_forest(options={})
10
- options.assert_valid_keys :foreign_key, :order
11
+ options.assert_valid_keys :foreign_key, :order, :dependent
11
12
 
12
13
  class_attribute :forest_foreign_key
13
14
  self.forest_foreign_key = options[:foreign_key] || "parent_id"
@@ -20,15 +21,16 @@ module Edge
20
21
  :foreign_key => forest_foreign_key
21
22
  }
22
23
 
24
+ dependent_options = options[:dependent] ? { dependent: options[:dependent] } : {}
25
+
23
26
  belongs_to :parent, common_options.merge(inverse_of: :children)
24
27
 
25
28
  if forest_order
26
- has_many :children, -> { order(forest_order) }, common_options.merge(inverse_of: :parent)
29
+ has_many :children, -> { order(forest_order) }, common_options.merge(inverse_of: :parent).merge(dependent_options)
27
30
  else
28
- has_many :children, common_options.merge(inverse_of: :parent)
31
+ has_many :children, common_options.merge(inverse_of: :parent).merge(dependent_options)
29
32
  end
30
33
 
31
-
32
34
  scope :root, -> { where(forest_foreign_key => nil) }
33
35
 
34
36
  include Edge::Forest::InstanceMethods
@@ -116,7 +118,7 @@ module Edge
116
118
 
117
119
  as_statement = Arel::Nodes::As.new all_nodes, union
118
120
 
119
- manager = Arel::SelectManager.new(Arel::Table.engine)
121
+ Arel::SelectManager.new(Arel::Table.engine)
120
122
  .with(:recursive, as_statement)
121
123
  .from(all_nodes)
122
124
  .join(arel_table)
@@ -132,7 +134,7 @@ module Edge
132
134
 
133
135
  # Returns true is this node is a root or false otherwise
134
136
  def root?
135
- !parent_id
137
+ !self[forest_foreign_key]
136
138
  end
137
139
 
138
140
  # Returns all sibling nodes (nodes that have the same parent). If this
data/lib/edge/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Edge
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  DROP TABLE IF EXISTS locations;
2
+ DROP TABLE IF EXISTS body_parts;
2
3
 
3
4
  CREATE TABLE locations(
4
5
  id serial PRIMARY KEY,
@@ -6,3 +7,8 @@ CREATE TABLE locations(
6
7
  name varchar NOT NULL,
7
8
  attrs json DEFAULT NULL -- include a column that does not have an operator defined that can be used with union
8
9
  );
10
+
11
+ CREATE TABLE body_parts(
12
+ id serial PRIMARY KEY,
13
+ body_part_id integer REFERENCES body_parts -- something that uses a non-standard parent ID
14
+ );
data/spec/forest_spec.rb CHANGED
@@ -4,9 +4,14 @@ class Location < ActiveRecord::Base
4
4
  acts_as_forest :order => "name"
5
5
  end
6
6
 
7
+ class BodyPart < ActiveRecord::Base
8
+ acts_as_forest :foreign_key => "body_part_id"
9
+ end
10
+
7
11
  Location.delete_all
8
12
 
9
13
  describe "Edge::Forest" do
14
+ let(:skeleton) { BodyPart.create! }
10
15
  let!(:usa) { Location.create! :name => "USA" }
11
16
  let!(:illinois) { Location.create! :parent => usa, :name => "Illinois" }
12
17
  let!(:chicago) { Location.create! :parent => illinois, :name => "Chicago" }
@@ -21,6 +26,12 @@ describe "Edge::Forest" do
21
26
  end
22
27
  end
23
28
 
29
+ context "of model with custom foreign key" do
30
+ it "should be true" do
31
+ expect(skeleton.root?).to eq true
32
+ end
33
+ end
34
+
24
35
  context "of child node" do
25
36
  it "should be false" do
26
37
  expect(illinois.root?).to eq false
@@ -226,4 +237,24 @@ describe "Edge::Forest" do
226
237
  Location2.find_forest
227
238
  end
228
239
  end
240
+
241
+ describe "dependent destroy" do
242
+ it 'cascades destroys' do
243
+ class Location3 < ActiveRecord::Base
244
+ self.table_name = 'locations'
245
+ acts_as_forest dependent: :destroy
246
+ end
247
+
248
+ Location3.find(usa.id).destroy
249
+
250
+ expect(Location.exists?(usa.id)).to eq false
251
+ expect(Location.exists?(illinois.id)).to eq false
252
+ expect(Location.exists?(chicago.id)).to eq false
253
+ expect(Location.exists?(indiana.id)).to eq false
254
+
255
+ expect(Location.exists?(canada.id)).to eq true
256
+ expect(Location.exists?(british_columbia.id)).to eq true
257
+ end
258
+
259
+ end
229
260
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: edge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Christensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
11
+ date: 2015-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -159,9 +159,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  version: '0'
160
160
  requirements: []
161
161
  rubyforge_project:
162
- rubygems_version: 2.4.5
162
+ rubygems_version: 2.4.5.1
163
163
  signing_key:
164
164
  specification_version: 4
165
165
  summary: Graph functionality for ActiveRecord. Provides tree/forest modeling structure
166
166
  that can load entire trees in a single query.
167
- test_files: []
167
+ test_files:
168
+ - spec/database.yml
169
+ - spec/database.yml.travis
170
+ - spec/database_structure.sql
171
+ - spec/forest_spec.rb
172
+ - spec/spec_helper.rb