relation 0.3.4 → 0.3.5
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/.rubocop.yml +51 -0
- data/.ruby-version +1 -1
- data/.travis.yml +3 -2
- data/MIT-LICENSE +1 -1
- data/README.md +5 -3
- data/app/models/dangling.rb +11 -14
- data/app/models/relation.rb +1 -3
- data/app/models/relation_ext.rb +4 -6
- data/lib/relation/engine.rb +17 -18
- data/lib/relation/version.rb +8 -5
- data/test/support/database.rb +19 -21
- data/test/test_helper.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93715ff3317b45a8eaca2ef3535a5e302c425d15da1ac6d0d3a5769f246c530c
|
4
|
+
data.tar.gz: 8d24db836b7135dfbfd7cf11a1853f30fef528fcce6123a0980c0cbf568d77fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4446a651fc0ba70ba460c81375f84cfe0e58045f5d30e5631a389ecd639197b9ffcc6aeb804eb133db6b7e677e4a38135ab234d2a88394e895314d5ca6dd3acb
|
7
|
+
data.tar.gz: 312cee0a84e13b28caa953226707dc805692c30da46805bd1b2e0d2bf4be6ef4fc03ad675376facff37b7840ade293e402b04d304a82f9e0839e85561c31061a
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# rubocop --auto-gen-config
|
2
|
+
# rubocop
|
3
|
+
# rubocop:disable all
|
4
|
+
|
5
|
+
#inherit_from: .rubocop_todo.yml
|
6
|
+
|
7
|
+
AllCops:
|
8
|
+
Include:
|
9
|
+
- '**/*.rb'
|
10
|
+
# - 'lib/**/*.rb'
|
11
|
+
# - '**/*.gemfile'
|
12
|
+
|
13
|
+
Exclude:
|
14
|
+
- 'gemfiles/*'
|
15
|
+
# - 'test/**/*_test.rb'
|
16
|
+
- '**/*_test.rb'
|
17
|
+
- lib/relation/version.rb
|
18
|
+
# - !ruby/regexp /old_and_unused\.rb$/
|
19
|
+
|
20
|
+
Bundler/OrderedGems:
|
21
|
+
Exclude:
|
22
|
+
- 'Gemfile'
|
23
|
+
|
24
|
+
Layout/AccessModifierIndentation:
|
25
|
+
Enabled: false
|
26
|
+
Layout/EmptyLinesAroundAccessModifier:
|
27
|
+
Enabled: false
|
28
|
+
Layout/LineLength:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Metrics/AbcSize:
|
32
|
+
Enabled: false
|
33
|
+
Metrics/MethodLength:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Style/HashEachMethods:
|
37
|
+
Enabled: false
|
38
|
+
Style/HashTransformKeys:
|
39
|
+
Enabled: false
|
40
|
+
Style/HashTransformValues:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Style/ClassAndModuleChildren:
|
44
|
+
Enabled: false
|
45
|
+
Style/Documentation:
|
46
|
+
Enabled: false
|
47
|
+
Style/FrozenStringLiteralComment:
|
48
|
+
Enabled: false
|
49
|
+
#Style/WhenThen:
|
50
|
+
# Exclude:
|
51
|
+
# - '.watchr'
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.6.
|
1
|
+
ruby-2.6.5
|
data/.travis.yml
CHANGED
@@ -13,8 +13,9 @@ bundler_args: --without production
|
|
13
13
|
#bundler_args: --without development
|
14
14
|
|
15
15
|
rvm:
|
16
|
-
- 2.6.
|
17
|
-
- 2.6.
|
16
|
+
- 2.6.5 # 2019-10-29
|
17
|
+
# - 2.6.4 # 2019-10-04
|
18
|
+
# - 2.6.3 # 2019-06-21
|
18
19
|
# - 2.6.1 # 2019-03-05
|
19
20
|
# - 2.5.1 # 2018-04-27
|
20
21
|
# - 2.5.0 # 2018-02-22
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -49,7 +49,7 @@ The migration is then done, as usual, by:
|
|
49
49
|
|
50
50
|
## Usage
|
51
51
|
|
52
|
-
In short (order* and user* are ActiveRecords):
|
52
|
+
In short (order* and user* are instances of ActiveRecords):
|
53
53
|
|
54
54
|
Relation.add order, user
|
55
55
|
Relation.add order, user2
|
@@ -86,9 +86,11 @@ They may be used for relationships which can not be based on the
|
|
86
86
|
class name of the ActiveRecords.
|
87
87
|
Keep in mind that dangling relations must be handled by yourself.
|
88
88
|
|
89
|
-
## Rails
|
89
|
+
## Rails 6
|
90
|
+
|
91
|
+
This gem is intended for Rails 6.
|
92
|
+
Rails 5 should work fine.
|
90
93
|
|
91
|
-
This gem is intended for Rails 5.
|
92
94
|
Older Rails versions may use "gem 'relation', '= 0.1.1'".
|
93
95
|
|
94
96
|
## License MIT
|
data/app/models/dangling.rb
CHANGED
@@ -1,31 +1,28 @@
|
|
1
1
|
# extended Relation: extracts relation from rows
|
2
|
-
class Relation < ActiveRecord::Base
|
3
2
|
|
3
|
+
class Relation < ActiveRecord::Base
|
4
4
|
def self.dangling
|
5
5
|
names = Relation.pluck(:name).uniq
|
6
6
|
models = []
|
7
|
-
names.each { |name|
|
8
|
-
models |= name.split(' ')
|
9
|
-
}
|
7
|
+
names.each { |name| models |= name.split(' ') }
|
10
8
|
hsh = {}
|
11
|
-
models.each
|
9
|
+
models.each do |class_name|
|
12
10
|
klass = class_name.constantize
|
13
11
|
ids = klass.pluck(:id)
|
14
12
|
idx = Relation.where('name like ?', "#{class_name} %").pluck(:from_id)
|
15
13
|
idy = Relation.where('name like ?', "% #{class_name}").pluck(:to_id)
|
16
14
|
arr = (idx | idy) - ids
|
17
|
-
hsh[class_name] = arr
|
18
|
-
|
15
|
+
hsh[class_name] = arr if arr.length.positive?
|
16
|
+
end
|
19
17
|
hsh
|
20
18
|
end
|
21
19
|
|
22
20
|
def self.remove_dangling(hsh)
|
23
|
-
hsh.each
|
24
|
-
arr.each
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
21
|
+
hsh.each do |name, arr|
|
22
|
+
arr.each do |idx|
|
23
|
+
Relation.where(from_id: idx).where('name like ?', "#{name} %").delete_all
|
24
|
+
Relation.where(to_id: idx).where('name like ?', "% #{name}").delete_all
|
25
|
+
end
|
26
|
+
end
|
29
27
|
end
|
30
|
-
|
31
28
|
end
|
data/app/models/relation.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
class Relation < ActiveRecord::Base
|
2
|
-
|
3
2
|
def self.add_raw(name, from_id, to_id)
|
4
3
|
hsh = { name: name, from_id: from_id, to_id: to_id }
|
5
|
-
Relation.create!(hsh)
|
4
|
+
Relation.create!(hsh) if Relation.where(hsh).first.nil?
|
6
5
|
end
|
7
6
|
|
8
7
|
def self.delete_raw(name, from_id, to_id)
|
@@ -17,5 +16,4 @@ class Relation < ActiveRecord::Base
|
|
17
16
|
def self.followers_raw(name, to_id)
|
18
17
|
Relation.where(name: name, to_id: to_id).pluck(:from_id)
|
19
18
|
end
|
20
|
-
|
21
19
|
end
|
data/app/models/relation_ext.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
# extended Relation: extracts relation from rows
|
2
2
|
class Relation < ActiveRecord::Base
|
3
|
-
|
4
3
|
def self.add(row_from, row_to)
|
5
4
|
hsh = normalize(row_from, row_to)
|
6
|
-
Relation.create!(hsh)
|
5
|
+
Relation.create!(hsh) if Relation.where(hsh).first.nil?
|
7
6
|
end
|
8
7
|
|
9
8
|
def self.delete(row_from, row_to)
|
@@ -25,9 +24,9 @@ class Relation < ActiveRecord::Base
|
|
25
24
|
klass.where(id: ids)
|
26
25
|
end
|
27
26
|
|
28
|
-
private
|
29
27
|
def self.name_id(resource)
|
30
|
-
raise 'missing resource'
|
28
|
+
raise 'missing resource' unless resource
|
29
|
+
|
31
30
|
[resource.class.name, resource.id]
|
32
31
|
end
|
33
32
|
|
@@ -40,9 +39,8 @@ class Relation < ActiveRecord::Base
|
|
40
39
|
|
41
40
|
def self.normalize2(kind, row)
|
42
41
|
klass = kind
|
43
|
-
klass = kind.constantize
|
42
|
+
klass = kind.constantize unless klass.is_a?(Class)
|
44
43
|
name, id = name_id(row)
|
45
44
|
[klass, name, id]
|
46
45
|
end
|
47
|
-
|
48
46
|
end
|
data/lib/relation/engine.rb
CHANGED
@@ -2,24 +2,23 @@ module ModRelation
|
|
2
2
|
class Engine < Rails::Engine
|
3
3
|
isolate_namespace ModRelation
|
4
4
|
|
5
|
-
# # https://github.com/rails/rails/issues/22261
|
6
|
-
# initializer :append_migrations do |app|
|
7
|
-
# config.paths['db/migrate'].expanded.each do |path|
|
8
|
-
# app.config.paths['db/migrate'] << path
|
9
|
-
# ActiveRecord::Migrator.migrations_paths << path
|
10
|
-
# end
|
11
|
-
# end
|
5
|
+
# # https://github.com/rails/rails/issues/22261
|
6
|
+
# initializer :append_migrations do |app|
|
7
|
+
# config.paths['db/migrate'].expanded.each do |path|
|
8
|
+
# app.config.paths['db/migrate'] << path
|
9
|
+
# ActiveRecord::Migrator.migrations_paths << path
|
10
|
+
# end
|
11
|
+
# end
|
12
12
|
|
13
|
+
# # https://github.com/rails/rails/issues/22261
|
14
|
+
# module ActiveRecord
|
15
|
+
# #module ActiveRecord::Current
|
16
|
+
# class Schema < Migration
|
17
|
+
# def migrations_paths
|
18
|
+
# (ActiveRecord::Migrator.migrations_paths +
|
19
|
+
# Rails.application.paths['db/migrate'].to_a).uniq
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
# end
|
13
23
|
end
|
14
24
|
end
|
15
|
-
|
16
|
-
|
17
|
-
# # https://github.com/rails/rails/issues/22261
|
18
|
-
# module ActiveRecord
|
19
|
-
# #module ActiveRecord::Current
|
20
|
-
# class Schema < Migration
|
21
|
-
# def migrations_paths
|
22
|
-
# (ActiveRecord::Migrator.migrations_paths + Rails.application.paths['db/migrate'].to_a).uniq
|
23
|
-
# end
|
24
|
-
# end
|
25
|
-
# end
|
data/lib/relation/version.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ModRelation
|
2
|
-
VERSION = '0.3.
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
4
|
+
VERSION = '0.3.5' # 2020-03-03
|
5
|
+
# VERSION = '0.3.4' # 2019-10-04
|
6
|
+
# VERSION = '0.3.3' # 2019-03-23
|
7
|
+
# VERSION = '0.3.2' # 2019-03-05
|
8
|
+
# VERSION = '0.3.1' # 2018-08-05
|
9
|
+
# VERSION = '0.3.0' #
|
7
10
|
end
|
data/test/support/database.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
class DB
|
2
|
-
ActiveRecord::Base.establish_connection adapter:
|
3
|
-
|
2
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3',
|
3
|
+
database: ':memory:'
|
4
4
|
|
5
5
|
def self.setup
|
6
6
|
capture_stdout do
|
7
7
|
ActiveRecord::Base.logger
|
8
8
|
ActiveRecord::Schema.define(version: 1) do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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 :from, null: false
|
18
|
+
t.references :to, null: false
|
19
|
+
end
|
20
20
|
end
|
21
21
|
|
22
22
|
Order.reset_column_information
|
@@ -26,17 +26,16 @@ class DB
|
|
26
26
|
def self.teardown
|
27
27
|
if ActiveRecord::Base.connection.respond_to?(:data_sources)
|
28
28
|
ActiveRecord::Base.connection.data_sources.each do |table|
|
29
|
-
|
29
|
+
ActiveRecord::Base.connection.drop_table(table)
|
30
30
|
end
|
31
31
|
else
|
32
32
|
ActiveRecord::Base.connection.tables.each do |table|
|
33
|
-
|
33
|
+
ActiveRecord::Base.connection.drop_table(table)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
|
-
def self.capture_stdout(&block)
|
38
|
+
def self.capture_stdout
|
40
39
|
real_stdout = $stdout
|
41
40
|
|
42
41
|
$stdout = StringIO.new
|
@@ -45,7 +44,6 @@ class DB
|
|
45
44
|
ensure
|
46
45
|
$stdout = real_stdout
|
47
46
|
end
|
48
|
-
|
49
47
|
end
|
50
48
|
|
51
49
|
class Order < ActiveRecord::Base
|
@@ -54,6 +52,6 @@ end
|
|
54
52
|
class User < ActiveRecord::Base
|
55
53
|
end
|
56
54
|
|
57
|
-
require_relative
|
58
|
-
require_relative
|
59
|
-
require_relative
|
55
|
+
require_relative '../../app/models/relation'
|
56
|
+
require_relative '../../app/models/relation_ext'
|
57
|
+
require_relative '../../app/models/dangling'
|
data/test/test_helper.rb
CHANGED
@@ -3,12 +3,12 @@ SimpleCov.start do
|
|
3
3
|
add_filter '/test/'
|
4
4
|
end
|
5
5
|
|
6
|
-
ENV[
|
6
|
+
ENV['RAILS_ENV'] = 'test'
|
7
7
|
require 'active_record'
|
8
8
|
require 'minitest/autorun'
|
9
9
|
|
10
10
|
# Load support files
|
11
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
11
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }
|
12
12
|
|
13
13
|
class Minitest::Test
|
14
14
|
require 'active_support/testing/assertions'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dittmar Krall
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -117,6 +117,7 @@ extensions: []
|
|
117
117
|
extra_rdoc_files: []
|
118
118
|
files:
|
119
119
|
- ".gitignore"
|
120
|
+
- ".rubocop.yml"
|
120
121
|
- ".ruby-gemset"
|
121
122
|
- ".ruby-version"
|
122
123
|
- ".travis.yml"
|
@@ -162,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
162
163
|
- !ruby/object:Gem::Version
|
163
164
|
version: '0'
|
164
165
|
requirements: []
|
165
|
-
rubygems_version: 3.0.
|
166
|
+
rubygems_version: 3.0.8
|
166
167
|
signing_key:
|
167
168
|
specification_version: 4
|
168
169
|
summary: Provides a simple directed relationship between active_record models.
|