relation 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/.watchr +54 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +128 -0
- data/MIT-LICENSE +20 -0
- data/README.md +73 -0
- data/Rakefile +9 -0
- data/app/models/relation.rb +78 -0
- data/db/migrate/20150810152808_relation.rb +14 -0
- data/lib/relation.rb +2 -0
- data/lib/relation/version.rb +1 -0
- data/relation.gemspec +25 -0
- data/test/dangling_test.rb +53 -0
- data/test/relation_test.rb +73 -0
- data/test/support/database.rb +57 -0
- data/test/test_helper.rb +17 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 08e41d0cbb15099b476990c5439330536f2ea1e6
|
4
|
+
data.tar.gz: d76e0cd15e30ba8de2acea617d7a938ece0fcbb2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f7a6736fa2a59aa8141e4ab2d9f36b0e027fc01f5412a92ed5a185848445e45c2dc3d479c9d599f1ddeed39cf5e90168670707939792c9dad00c6e8ca95a6df2
|
7
|
+
data.tar.gz: 882cbbd31bb2f884f3d3c19e182fd29cce03b50444c0012e6c762fcc72d34e73aea4db2a59de9dccc45477fa5ffbf35d02e86e07abad0aba27fe6c4c90f7c6e1
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rails-5.0
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.1
|
data/.travis.yml
ADDED
data/.watchr
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
HH = '#' * 22 unless defined?(HH)
|
2
|
+
H = '#' * 5 unless defined?(H)
|
3
|
+
|
4
|
+
def usage
|
5
|
+
puts <<-EOS
|
6
|
+
Ctrl-\\ or ctrl-4 Running all tests
|
7
|
+
Ctrl-C Exit
|
8
|
+
EOS
|
9
|
+
end
|
10
|
+
|
11
|
+
def run(cmd)
|
12
|
+
puts "#{HH} #{Time.now} #{HH}"
|
13
|
+
puts "#{H} #{cmd}"
|
14
|
+
system "/usr/bin/time --format '#{HH} Elapsed time %E' #{cmd}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def run_it(type, file)
|
18
|
+
case type
|
19
|
+
when 'test'; run %Q{ruby -I"lib:test" -rubygems #{file}}
|
20
|
+
# when 'spec'; run %Q{spring rspec -X #{file}}
|
21
|
+
else; puts "#{H} unknown type: #{type}, file: #{file}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def run_all_tests
|
26
|
+
puts "\n#{HH} Running all tests #{HH}\n"
|
27
|
+
# %w{test spec}.each { |dir| run "spring rake #{dir} RAILS_ENV=test" if File.exists?(dir) }
|
28
|
+
# %w{test}.each { |dir| run "spring rake #{dir} RAILS_ENV=test" if File.exists?(dir) }
|
29
|
+
%w{test}.each { |dir| run "rake #{dir} RAILS_ENV=test" if File.exists?(dir) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def run_matching_files(base)
|
33
|
+
base = base.split('_').first
|
34
|
+
%w{test spec}.each { |type|
|
35
|
+
files = Dir["#{type}/**/*.rb"].select { |file| file =~ /#{base}_.*\.rb/ }
|
36
|
+
run_it type, files.join(' ') unless files.empty?
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
%w{test spec}.each { |type|
|
41
|
+
watch("#{type}/#{type}_helper\.rb") { run_all_tests }
|
42
|
+
watch("#{type}/.*/*_#{type}\.rb") { |match| run_it type, match[0] }
|
43
|
+
}
|
44
|
+
%w{rb erb haml slim}.each { |type|
|
45
|
+
watch("app/.*/.*\.#{type}") { |m|
|
46
|
+
run_matching_files("#{m[0].split('/').at(2).split('.').first}")
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
# Ctrl-\ or ctrl-4
|
51
|
+
Signal.trap('QUIT') { run_all_tests }
|
52
|
+
# Ctrl-C
|
53
|
+
Signal.trap('INT') { abort("Interrupted\n") }
|
54
|
+
usage
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
relation (0.1.0)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actioncable (5.0.0.1)
|
11
|
+
actionpack (= 5.0.0.1)
|
12
|
+
nio4r (~> 1.2)
|
13
|
+
websocket-driver (~> 0.6.1)
|
14
|
+
actionmailer (5.0.0.1)
|
15
|
+
actionpack (= 5.0.0.1)
|
16
|
+
actionview (= 5.0.0.1)
|
17
|
+
activejob (= 5.0.0.1)
|
18
|
+
mail (~> 2.5, >= 2.5.4)
|
19
|
+
rails-dom-testing (~> 2.0)
|
20
|
+
actionpack (5.0.0.1)
|
21
|
+
actionview (= 5.0.0.1)
|
22
|
+
activesupport (= 5.0.0.1)
|
23
|
+
rack (~> 2.0)
|
24
|
+
rack-test (~> 0.6.3)
|
25
|
+
rails-dom-testing (~> 2.0)
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
+
actionview (5.0.0.1)
|
28
|
+
activesupport (= 5.0.0.1)
|
29
|
+
builder (~> 3.1)
|
30
|
+
erubis (~> 2.7.0)
|
31
|
+
rails-dom-testing (~> 2.0)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
33
|
+
activejob (5.0.0.1)
|
34
|
+
activesupport (= 5.0.0.1)
|
35
|
+
globalid (>= 0.3.6)
|
36
|
+
activemodel (5.0.0.1)
|
37
|
+
activesupport (= 5.0.0.1)
|
38
|
+
activerecord (5.0.0.1)
|
39
|
+
activemodel (= 5.0.0.1)
|
40
|
+
activesupport (= 5.0.0.1)
|
41
|
+
arel (~> 7.0)
|
42
|
+
activesupport (5.0.0.1)
|
43
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
44
|
+
i18n (~> 0.7)
|
45
|
+
minitest (~> 5.1)
|
46
|
+
tzinfo (~> 1.1)
|
47
|
+
arel (7.1.4)
|
48
|
+
builder (3.2.2)
|
49
|
+
concurrent-ruby (1.0.2)
|
50
|
+
docile (1.1.5)
|
51
|
+
erubis (2.7.0)
|
52
|
+
globalid (0.3.7)
|
53
|
+
activesupport (>= 4.1.0)
|
54
|
+
i18n (0.7.0)
|
55
|
+
json (2.0.2)
|
56
|
+
loofah (2.0.3)
|
57
|
+
nokogiri (>= 1.5.9)
|
58
|
+
mail (2.6.4)
|
59
|
+
mime-types (>= 1.16, < 4)
|
60
|
+
method_source (0.8.2)
|
61
|
+
mime-types (3.1)
|
62
|
+
mime-types-data (~> 3.2015)
|
63
|
+
mime-types-data (3.2016.0521)
|
64
|
+
mini_portile2 (2.1.0)
|
65
|
+
minitest (5.10.1)
|
66
|
+
nio4r (1.2.1)
|
67
|
+
nokogiri (1.6.8.1)
|
68
|
+
mini_portile2 (~> 2.1.0)
|
69
|
+
rack (2.0.1)
|
70
|
+
rack-test (0.6.3)
|
71
|
+
rack (>= 1.0)
|
72
|
+
rails (5.0.0.1)
|
73
|
+
actioncable (= 5.0.0.1)
|
74
|
+
actionmailer (= 5.0.0.1)
|
75
|
+
actionpack (= 5.0.0.1)
|
76
|
+
actionview (= 5.0.0.1)
|
77
|
+
activejob (= 5.0.0.1)
|
78
|
+
activemodel (= 5.0.0.1)
|
79
|
+
activerecord (= 5.0.0.1)
|
80
|
+
activesupport (= 5.0.0.1)
|
81
|
+
bundler (>= 1.3.0, < 2.0)
|
82
|
+
railties (= 5.0.0.1)
|
83
|
+
sprockets-rails (>= 2.0.0)
|
84
|
+
rails-dom-testing (2.0.1)
|
85
|
+
activesupport (>= 4.2.0, < 6.0)
|
86
|
+
nokogiri (~> 1.6.0)
|
87
|
+
rails-html-sanitizer (1.0.3)
|
88
|
+
loofah (~> 2.0)
|
89
|
+
railties (5.0.0.1)
|
90
|
+
actionpack (= 5.0.0.1)
|
91
|
+
activesupport (= 5.0.0.1)
|
92
|
+
method_source
|
93
|
+
rake (>= 0.8.7)
|
94
|
+
thor (>= 0.18.1, < 2.0)
|
95
|
+
rake (11.3.0)
|
96
|
+
simplecov (0.12.0)
|
97
|
+
docile (~> 1.1.0)
|
98
|
+
json (>= 1.8, < 3)
|
99
|
+
simplecov-html (~> 0.10.0)
|
100
|
+
simplecov-html (0.10.0)
|
101
|
+
sprockets (3.7.0)
|
102
|
+
concurrent-ruby (~> 1.0)
|
103
|
+
rack (> 1, < 3)
|
104
|
+
sprockets-rails (3.2.0)
|
105
|
+
actionpack (>= 4.0)
|
106
|
+
activesupport (>= 4.0)
|
107
|
+
sprockets (>= 3.0.0)
|
108
|
+
sqlite3 (1.3.12)
|
109
|
+
thor (0.19.4)
|
110
|
+
thread_safe (0.3.5)
|
111
|
+
tzinfo (1.2.2)
|
112
|
+
thread_safe (~> 0.1)
|
113
|
+
websocket-driver (0.6.4)
|
114
|
+
websocket-extensions (>= 0.1.0)
|
115
|
+
websocket-extensions (0.1.2)
|
116
|
+
|
117
|
+
PLATFORMS
|
118
|
+
ruby
|
119
|
+
|
120
|
+
DEPENDENCIES
|
121
|
+
minitest
|
122
|
+
rails (>= 5.0)
|
123
|
+
relation!
|
124
|
+
simplecov
|
125
|
+
sqlite3
|
126
|
+
|
127
|
+
BUNDLED WITH
|
128
|
+
1.11.2
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Dittmar Krall - http://matique.de
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Relation
|
2
|
+
[![Build Status](http://img.shields.io/travis/matique/relation.svg)](https://travis-ci.org/matique/relation)
|
3
|
+
[![Dependency Status](http://img.shields.io/gemnasium/matique/relation.svg)](https://gemnasium.com/matique/relation)
|
4
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/matique/relation.svg)](https://codeclimate.com/github/matique/relation)
|
5
|
+
[![Gem Version](http://img.shields.io/gem/v/relation.svg)](https://rubygems.org/gems/relation)
|
6
|
+
|
7
|
+
Relation is a Rails gem that adds relationships to
|
8
|
+
ActiveRecord items stored in tables.
|
9
|
+
The relationship is stored in an additional table;
|
10
|
+
no additional column/field is required in the particular tables.
|
11
|
+
|
12
|
+
A habtm (has and belong to many) association of Rails requires an
|
13
|
+
additional table containing the id's of the associated records.
|
14
|
+
The name of this table indicates which tables are being associated.
|
15
|
+
Relation just move the name of the association table into an additional
|
16
|
+
column enabling relationship between any ActiveRecords in any,
|
17
|
+
including themselves, tables.
|
18
|
+
|
19
|
+
Rails furthermore adds some "magic" to the habtm like additional methods
|
20
|
+
and administration of the association table.
|
21
|
+
These additions are not supported by Relation,
|
22
|
+
i.e. you are responsible for them.
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
As usual:
|
27
|
+
|
28
|
+
$ [sudo] gem install relation
|
29
|
+
|
30
|
+
or:
|
31
|
+
|
32
|
+
# Gemfile file
|
33
|
+
gem 'relation'
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Furthermore the association table (an n-ways-join table) must be
|
38
|
+
installed and migrated.
|
39
|
+
You may copy the migration "db/migrate/20150810152808_relation.rb"
|
40
|
+
from the gem.
|
41
|
+
The migration is then done, as usual, by:
|
42
|
+
|
43
|
+
$ rake db:migrate
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
In short (order* and user* are ActiveRecords):
|
48
|
+
|
49
|
+
Relation.add order, user
|
50
|
+
Relation.add order, user2
|
51
|
+
Relation.add order2, user2
|
52
|
+
|
53
|
+
Relation.references order, User # -> [user, user2]
|
54
|
+
Relation.references order2, User # -> [user2]
|
55
|
+
Relation.followers Order, user # -> [order]
|
56
|
+
Relation.followers Order, user2 # -> [order, order2]
|
57
|
+
|
58
|
+
Relation.delete order2, user2
|
59
|
+
Relation.followers Order, user2 # -> [order]
|
60
|
+
|
61
|
+
See also the tests.
|
62
|
+
|
63
|
+
Dangling, i.e. inaccessible records, references are detected by:
|
64
|
+
|
65
|
+
hsh = Relation.dangling
|
66
|
+
|
67
|
+
and cleaned by:
|
68
|
+
|
69
|
+
Relation.remove_dangling hsh
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
Copyright (c) 2015-2016 [Dittmar Krall], released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
class Relation < ActiveRecord::Base
|
2
|
+
|
3
|
+
def self.add(row_from, row_to)
|
4
|
+
name_from, id_from = name_id(row_from)
|
5
|
+
name_to, id_to = name_id(row_to)
|
6
|
+
name = "#{name_from} #{name_to}"
|
7
|
+
hsh = { name: name, x_id: id_from, y_id: id_to }
|
8
|
+
|
9
|
+
Relation.create!(hsh) if Relation.where(hsh).first == nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.delete(row_from, row_to)
|
13
|
+
name_from, id_from = name_id(row_from)
|
14
|
+
name_to, id_to = name_id(row_to)
|
15
|
+
name = "#{name_from} #{name_to}"
|
16
|
+
hsh = { name: name, x_id: id_from, y_id: id_to }
|
17
|
+
Relation.where(hsh).delete_all
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.references(row, kind)
|
21
|
+
klass = kind
|
22
|
+
klass = kind.constantize unless klass.kind_of?(Class)
|
23
|
+
ids = self.references_ids(row, klass)
|
24
|
+
klass.where(id: ids)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.followers(kind, row)
|
28
|
+
klass = kind
|
29
|
+
klass = kind.constantize unless klass.kind_of?(Class)
|
30
|
+
ids = self.followers_ids(klass, row)
|
31
|
+
klass.where(id: ids)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.dangling
|
35
|
+
names = Relation.pluck(:name).uniq
|
36
|
+
models = []
|
37
|
+
names.each { |name|
|
38
|
+
models |= name.split(' ')
|
39
|
+
}
|
40
|
+
hsh = {}
|
41
|
+
models.each { |class_name|
|
42
|
+
klass = class_name.constantize
|
43
|
+
ids = klass.pluck(:id)
|
44
|
+
idx = Relation.where('name like ?', "#{class_name} %").pluck(:x_id)
|
45
|
+
idy = Relation.where('name like ?', "% #{class_name}").pluck(:y_id)
|
46
|
+
arr = (idx | idy) - ids
|
47
|
+
hsh[class_name] = arr if arr.length > 0
|
48
|
+
}
|
49
|
+
hsh
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.remove_dangling(hsh)
|
53
|
+
hsh.each { |name, arr|
|
54
|
+
arr.each { |idx|
|
55
|
+
Relation.where(x_id: idx).where('name like ?', "#{name} %").delete_all
|
56
|
+
Relation.where(y_id: idx).where('name like ?', "% #{name}").delete_all
|
57
|
+
}
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def self.name_id(resource)
|
63
|
+
raise 'missing resource' unless resource
|
64
|
+
[resource.class.name, resource.id]
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.references_ids(row, klass)
|
68
|
+
name_from, id_from = name_id(row)
|
69
|
+
name = "#{name_from} #{klass.name}"
|
70
|
+
Relation.where(name: name, x_id: id_from).pluck(:y_id)
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.followers_ids(klass, row)
|
74
|
+
name_to, id_to = name_id(row)
|
75
|
+
name = "#{klass.name} #{name_to}"
|
76
|
+
Relation.where(name: name, y_id: id_to).pluck(:x_id)
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateRelations < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :relations, id: false do |t|
|
4
|
+
t.string :name
|
5
|
+
t.references :x, null: false
|
6
|
+
t.references :y, null: false
|
7
|
+
t.timestamps
|
8
|
+
|
9
|
+
t.index :name
|
10
|
+
t.index :x
|
11
|
+
t.index :y
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/relation.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
VERSION = '0.1.1'
|
data/relation.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'relation/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'relation'
|
6
|
+
s.version = VERSION
|
7
|
+
s.authors = ['Dittmar Krall']
|
8
|
+
s.email = ['dittmar.krall@matique.de']
|
9
|
+
s.homepage = 'https://github.com/matique/relation'
|
10
|
+
s.summary = %q{Provides a simple directed relationship between active_record models.}
|
11
|
+
s.description = %q{A gem that adds simple support for organizing ActiveRecord models.}
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ['lib']
|
17
|
+
|
18
|
+
s.add_dependency 'activerecord'
|
19
|
+
|
20
|
+
# Dependencies (installed via 'bundle install')...
|
21
|
+
s.add_development_dependency 'sqlite3'
|
22
|
+
s.add_development_dependency 'minitest'
|
23
|
+
s.add_development_dependency 'simplecov'
|
24
|
+
# s.add_development_dependency 'coveralls'
|
25
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Relation do
|
4
|
+
let(:user) { User.create! name: 'user' }
|
5
|
+
let(:user2) { User.create! name: 'user2' }
|
6
|
+
let(:order) { Order.create! name: 'order' }
|
7
|
+
let(:order2) { Order.create! name: 'order2' }
|
8
|
+
|
9
|
+
def setup
|
10
|
+
DB.setup
|
11
|
+
|
12
|
+
Relation.add user, order
|
13
|
+
Relation.add user2, order
|
14
|
+
Relation.add user2, order2
|
15
|
+
end
|
16
|
+
|
17
|
+
def teardown
|
18
|
+
DB.teardown
|
19
|
+
end
|
20
|
+
|
21
|
+
it "test remove dangling #1" do
|
22
|
+
hsh = { 'User' => [user.id] }
|
23
|
+
assert_difference('Relation.count', -1) do
|
24
|
+
Relation.remove_dangling(hsh)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "test remove dangling #2" do
|
29
|
+
hsh = { 'User' => [user2.id] }
|
30
|
+
assert_difference('Relation.count', -2) do
|
31
|
+
Relation.remove_dangling(hsh)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "test remove dangling #3" do
|
36
|
+
hsh = { 'Order' => [order.id] }
|
37
|
+
assert_difference('Relation.count', -2) do
|
38
|
+
Relation.remove_dangling(hsh)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "test dangling" do
|
43
|
+
assert_equal({}, Relation.dangling)
|
44
|
+
user.destroy
|
45
|
+
assert_equal({"User"=>[user.id]}, Relation.dangling)
|
46
|
+
user2.destroy
|
47
|
+
assert_equal({"User"=>[user.id, user2.id]}, Relation.dangling)
|
48
|
+
order.destroy
|
49
|
+
assert_equal({"User"=>[user.id, user2.id].sort, "Order"=>[order.id]},
|
50
|
+
Relation.dangling)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Relation do
|
4
|
+
let(:user) { User.create! name: 'user' }
|
5
|
+
let(:user2) { User.create! name: 'user2' }
|
6
|
+
let(:order) { Order.create! name: 'order' }
|
7
|
+
let(:order2) { Order.create! name: 'order2' }
|
8
|
+
|
9
|
+
def setup
|
10
|
+
DB.setup
|
11
|
+
|
12
|
+
Relation.add user, order
|
13
|
+
Relation.add user2, order
|
14
|
+
Relation.add user2, order2
|
15
|
+
end
|
16
|
+
|
17
|
+
def teardown
|
18
|
+
DB.teardown
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should add a connection" do
|
22
|
+
assert_difference('Relation.count') do
|
23
|
+
Relation.add user2, user
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should delete a connection" do
|
28
|
+
Relation.add user2, user
|
29
|
+
assert_difference('Relation.count', -1) do
|
30
|
+
Relation.delete user2, user
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return references (using class name)" do
|
35
|
+
arr = Relation.references(user, 'Order')
|
36
|
+
assert_equal [order].sort, arr.sort
|
37
|
+
|
38
|
+
arr = Relation.references(user2, 'Order')
|
39
|
+
assert_equal [order, order2].sort, arr.sort
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return references (using class)" do
|
43
|
+
arr = Relation.references(user, Order)
|
44
|
+
assert_equal [order].sort, arr.sort
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return followers (using class name)" do
|
48
|
+
arr = Relation.followers('User', order)
|
49
|
+
assert_equal [user, user2].sort, arr.sort
|
50
|
+
|
51
|
+
arr = Relation.followers('User', order2)
|
52
|
+
assert_equal [user2].sort, arr.sort
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should return followers (using class)" do
|
56
|
+
arr = Relation.followers(User, order)
|
57
|
+
assert_equal [user, user2].sort, arr.sort
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should not add twice the same connection" do
|
61
|
+
assert_difference('Relation.count') do
|
62
|
+
Relation.add user2, user
|
63
|
+
Relation.add user2, user
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should handle unexistent connection" do
|
68
|
+
assert_difference('Relation.count', 0) do
|
69
|
+
Relation.delete user2, user
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
class DB
|
2
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3",
|
3
|
+
database: ":memory:"
|
4
|
+
|
5
|
+
def self.setup
|
6
|
+
capture_stdout do
|
7
|
+
ActiveRecord::Base.logger
|
8
|
+
ActiveRecord::Schema.define(version: 1) do
|
9
|
+
create_table :orders do |t|
|
10
|
+
t.column :name, :string
|
11
|
+
end
|
12
|
+
create_table :users do |t|
|
13
|
+
t.column :name, :string
|
14
|
+
end
|
15
|
+
create_table :relations, id: false do |t|
|
16
|
+
t.string :name
|
17
|
+
t.references :x, null: false
|
18
|
+
t.references :y, null: false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Order.reset_column_information
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.teardown
|
27
|
+
if ActiveRecord::Base.connection.respond_to?(:data_sources)
|
28
|
+
ActiveRecord::Base.connection.data_sources.each do |table|
|
29
|
+
ActiveRecord::Base.connection.drop_table(table)
|
30
|
+
end
|
31
|
+
else
|
32
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
33
|
+
ActiveRecord::Base.connection.drop_table(table)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def self.capture_stdout(&block)
|
40
|
+
real_stdout = $stdout
|
41
|
+
|
42
|
+
$stdout = StringIO.new
|
43
|
+
yield
|
44
|
+
$stdout.string
|
45
|
+
ensure
|
46
|
+
$stdout = real_stdout
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
class Order < ActiveRecord::Base
|
52
|
+
end
|
53
|
+
|
54
|
+
class User < ActiveRecord::Base
|
55
|
+
end
|
56
|
+
|
57
|
+
require_relative ('../../app/models/relation')
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start do
|
3
|
+
# add_filter '/spec/'
|
4
|
+
add_filter '/test/'
|
5
|
+
end
|
6
|
+
|
7
|
+
ENV["RAILS_ENV"] = "test"
|
8
|
+
require 'active_record'
|
9
|
+
require 'minitest/autorun'
|
10
|
+
|
11
|
+
# Load support files
|
12
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
13
|
+
|
14
|
+
class Minitest::Test
|
15
|
+
require 'active_support/testing/assertions'
|
16
|
+
include ActiveSupport::Testing::Assertions
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: relation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dittmar Krall
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A gem that adds simple support for organizing ActiveRecord models.
|
70
|
+
email:
|
71
|
+
- dittmar.krall@matique.de
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".ruby-gemset"
|
77
|
+
- ".ruby-version"
|
78
|
+
- ".travis.yml"
|
79
|
+
- ".watchr"
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- MIT-LICENSE
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- app/models/relation.rb
|
86
|
+
- db/migrate/20150810152808_relation.rb
|
87
|
+
- lib/relation.rb
|
88
|
+
- lib/relation/version.rb
|
89
|
+
- relation.gemspec
|
90
|
+
- test/dangling_test.rb
|
91
|
+
- test/relation_test.rb
|
92
|
+
- test/support/database.rb
|
93
|
+
- test/test_helper.rb
|
94
|
+
homepage: https://github.com/matique/relation
|
95
|
+
licenses: []
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.5.1
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Provides a simple directed relationship between active_record models.
|
117
|
+
test_files: []
|
118
|
+
has_rdoc:
|