rom-sql 0.4.1 → 0.4.3
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/CHANGELOG.md +28 -0
- data/Gemfile +1 -1
- data/README.md +12 -3
- data/lib/rom/sql.rb +5 -0
- data/lib/rom/sql/commands/transaction.rb +19 -3
- data/lib/rom/sql/plugin/pagination.rb +1 -1
- data/lib/rom/sql/relation.rb +2 -0
- data/lib/rom/sql/relation/associations.rb +20 -8
- data/lib/rom/sql/relation/class_methods.rb +5 -6
- data/lib/rom/sql/version.rb +1 -1
- data/rom-sql.gemspec +1 -1
- data/spec/integration/combine_spec.rb +37 -0
- data/spec/integration/commands/create_spec.rb +56 -7
- data/spec/integration/read_spec.rb +28 -25
- data/spec/shared/database_setup.rb +1 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/unit/association_errors_spec.rb +20 -0
- data/spec/unit/one_to_many_spec.rb +36 -6
- data/spec/unit/plugin/pagination_spec.rb +13 -1
- metadata +14 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fae231a134bab168af4e2cc51bc9f525839165aa
|
|
4
|
+
data.tar.gz: dcaa467ae85f00b2298d7a11ba24612bd1db17b6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4279201898c5ff53884bd2dd1d8743baea691965bd2e26c573a9453001deff5fe1f6e17b37ec05b1bf6c67907ea7f2e40ecc75301746921c310a72c6a29b4a9d
|
|
7
|
+
data.tar.gz: 15878ffaebb4557cb9bd1b95fa3c1326d21a91017175f532cad871bb3cddeae039bf80f72e4139735ad932af589b3d6a05cf97c0269c2600c165e3d3c79f9914
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## v0.4.3 2015-05-17
|
|
2
|
+
|
|
3
|
+
### Fixed
|
|
4
|
+
|
|
5
|
+
* `transaction` doesn't swallow errors now other than CommandError (solnic)
|
|
6
|
+
|
|
7
|
+
[Compare v0.4.2...v0.4.3](https://github.com/rom-rb/rom-sql/compare/v0.4.2...v0.4.3)
|
|
8
|
+
|
|
9
|
+
## v0.4.2 2015-05-17
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
* Support for setting custom association name (solnic)
|
|
14
|
+
* Support for `:conditions` option in association definition (solnic)
|
|
15
|
+
* Better error message when accessing undefined associations (pdswan)
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
* Correct `ROM::SQL::Plugin::Pagination::Pager#total_pages` when total is not
|
|
20
|
+
evenly divisible by page size (larribas)
|
|
21
|
+
* `association_join` behaves correctly when dataset is different than register_as (nepalez)
|
|
22
|
+
|
|
23
|
+
### Changed
|
|
24
|
+
|
|
25
|
+
* `transaction` returns command failure objects when there was a rollback (solnic)
|
|
26
|
+
|
|
27
|
+
[Compare v0.4.1...v0.4.2](https://github.com/rom-rb/rom-sql/compare/v0.4.1...v0.4.2)
|
|
28
|
+
|
|
1
29
|
## v0.4.1 2015-04-04
|
|
2
30
|
|
|
3
31
|
### Added
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
[][codeclimate]
|
|
14
14
|
[][inchpages]
|
|
15
15
|
|
|
16
|
-
RDBMS
|
|
16
|
+
RDBMS support for [Ruby Object Mapper](https://github.com/rom-rb/rom).
|
|
17
17
|
|
|
18
18
|
## Issues
|
|
19
19
|
|
|
@@ -44,6 +44,9 @@ in relation objects. For schema migrations you can use its
|
|
|
44
44
|
which is available via repositories.
|
|
45
45
|
|
|
46
46
|
``` ruby
|
|
47
|
+
require "rom-sql"
|
|
48
|
+
require "sqlite3"
|
|
49
|
+
|
|
47
50
|
setup = ROM.setup(:sql, "sqlite::memory")
|
|
48
51
|
|
|
49
52
|
setup.default.connection.create_table(:users) do
|
|
@@ -69,6 +72,9 @@ class Users < ROM::Relation[:sql]
|
|
|
69
72
|
end
|
|
70
73
|
end
|
|
71
74
|
|
|
75
|
+
class Tasks < ROM::Relation[:sql]
|
|
76
|
+
end
|
|
77
|
+
|
|
72
78
|
rom = ROM.finalize.env
|
|
73
79
|
|
|
74
80
|
users = rom.relations.users
|
|
@@ -77,8 +83,8 @@ tasks = rom.relations.tasks
|
|
|
77
83
|
users.insert(id: 1, name: "Piotr")
|
|
78
84
|
tasks.insert(user_id: 1, title: "Be happy")
|
|
79
85
|
|
|
80
|
-
puts rom.relation(:users).by_name("Piotr").
|
|
81
|
-
# => [{:id=>1, :name=>"Piotr", :user_id=>1
|
|
86
|
+
puts rom.relation(:users).by_name("Piotr").to_a.inspect
|
|
87
|
+
# => [{:id=>1, :name=>"Piotr", :user_id=>1}]
|
|
82
88
|
```
|
|
83
89
|
|
|
84
90
|
## Mapping joins to aggregates
|
|
@@ -127,6 +133,9 @@ tasks = rom.relations.tasks
|
|
|
127
133
|
users.insert(id: 1, name: "Piotr")
|
|
128
134
|
tasks.insert(user_id: 1, title: "Be happy")
|
|
129
135
|
|
|
136
|
+
rom.relation(:users).with_tasks.by_name("Piotr").to_a
|
|
137
|
+
# => [{:id=>1, :name=>"Piotr", :admin=>nil, :title=>"Be happy"}]
|
|
138
|
+
|
|
130
139
|
rom.relation(:users).as(:model).with_tasks.by_name("Piotr").to_a
|
|
131
140
|
# => [#<User:0x007fb31542a098 @id=1, @name="Piotr", @tasks=[{:title=>"Be happy"}]>]
|
|
132
141
|
```
|
data/lib/rom/sql.rb
CHANGED
|
@@ -3,6 +3,7 @@ require "rom"
|
|
|
3
3
|
|
|
4
4
|
module ROM
|
|
5
5
|
module SQL
|
|
6
|
+
NoAssociationError = Class.new(StandardError)
|
|
6
7
|
ConstraintError = Class.new(ROM::CommandError)
|
|
7
8
|
|
|
8
9
|
class DatabaseError < ROM::CommandError
|
|
@@ -27,3 +28,7 @@ if defined?(Rails)
|
|
|
27
28
|
end
|
|
28
29
|
|
|
29
30
|
ROM.register_adapter(:sql, ROM::SQL)
|
|
31
|
+
|
|
32
|
+
ROM.plugins do
|
|
33
|
+
register :pagination, ROM::SQL::Plugin::Pagination, type: :relation
|
|
34
|
+
end
|
|
@@ -3,13 +3,29 @@ require 'rom/commands/result'
|
|
|
3
3
|
module ROM
|
|
4
4
|
module SQL
|
|
5
5
|
module Commands
|
|
6
|
+
# Adds transaction interface to commands
|
|
7
|
+
#
|
|
8
|
+
# @api private
|
|
6
9
|
module Transaction
|
|
7
10
|
ROM::SQL::Rollback = Class.new(Sequel::Rollback)
|
|
8
11
|
|
|
12
|
+
# Start a transaction
|
|
13
|
+
#
|
|
14
|
+
# @param [Hash] options The options hash supported by Sequel
|
|
15
|
+
#
|
|
16
|
+
# @return [ROM::Commands::Result::Success,ROM::Commands::Result::Failure]
|
|
17
|
+
#
|
|
18
|
+
# @api public
|
|
9
19
|
def transaction(options = {}, &block)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
20
|
+
result = relation.dataset.db.transaction(options, &block)
|
|
21
|
+
|
|
22
|
+
if result
|
|
23
|
+
ROM::Commands::Result::Success.new(result)
|
|
24
|
+
else
|
|
25
|
+
ROM::Commands::Result::Failure.new(result)
|
|
26
|
+
end
|
|
27
|
+
rescue ROM::CommandError => e
|
|
28
|
+
ROM::Commands::Result::Failure.new(e)
|
|
13
29
|
end
|
|
14
30
|
end
|
|
15
31
|
end
|
data/lib/rom/sql/relation.rb
CHANGED
|
@@ -45,18 +45,30 @@ module ROM
|
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
# @api private
|
|
48
|
-
def graph_join(
|
|
49
|
-
assoc = model.association_reflection(
|
|
48
|
+
def graph_join(assoc_name, join_type, options = {})
|
|
49
|
+
assoc = model.association_reflection(assoc_name)
|
|
50
|
+
|
|
51
|
+
if assoc.nil?
|
|
52
|
+
raise NoAssociationError,
|
|
53
|
+
"Association #{assoc_name.inspect} has not been " \
|
|
54
|
+
"defined for relation #{name.inspect}"
|
|
55
|
+
end
|
|
50
56
|
|
|
51
57
|
key = assoc[:key]
|
|
52
58
|
type = assoc[:type]
|
|
59
|
+
table_name = assoc[:class].table_name
|
|
53
60
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
61
|
+
graph_rel =
|
|
62
|
+
if type == :many_to_many
|
|
63
|
+
select = options[:select] || {}
|
|
64
|
+
graph_join_many_to_many(table_name, assoc, select)
|
|
65
|
+
else
|
|
66
|
+
graph_join_other(table_name, key, type, join_type, options)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
graph_rel = graph_rel.where(assoc[:conditions]) if assoc[:conditions]
|
|
70
|
+
|
|
71
|
+
graph_rel
|
|
60
72
|
end
|
|
61
73
|
|
|
62
74
|
# @api private
|
|
@@ -14,11 +14,11 @@ module ROM
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def one_to_many(name, options)
|
|
17
|
-
associations << [__method__, name,
|
|
17
|
+
associations << [__method__, name, { relation: name }.merge(options)]
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def many_to_many(name, options = {})
|
|
21
|
-
associations << [__method__, name,
|
|
21
|
+
associations << [__method__, name, { relation: name }.merge(options)]
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def many_to_one(name, options = {})
|
|
@@ -31,10 +31,9 @@ module ROM
|
|
|
31
31
|
model.set_dataset(relation.dataset)
|
|
32
32
|
model.dataset.naked!
|
|
33
33
|
|
|
34
|
-
associations.each do |*args,
|
|
35
|
-
|
|
36
|
-
other = relations[options.
|
|
37
|
-
|
|
34
|
+
associations.each do |*args, assoc_opts|
|
|
35
|
+
options = Hash[assoc_opts]
|
|
36
|
+
other = relations[options.delete(:relation) || args[1]].model
|
|
38
37
|
model.public_send(*args, options.merge(class: other))
|
|
39
38
|
end
|
|
40
39
|
|
data/lib/rom/sql/version.rb
CHANGED
data/rom-sql.gemspec
CHANGED
|
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
|
20
20
|
|
|
21
21
|
spec.add_runtime_dependency "sequel", "~> 4.18"
|
|
22
22
|
spec.add_runtime_dependency "equalizer", "~> 0.0", ">= 0.0.9"
|
|
23
|
-
spec.add_runtime_dependency "rom", "~> 0.
|
|
23
|
+
spec.add_runtime_dependency "rom", "~> 0.7", ">= 0.7.0"
|
|
24
24
|
|
|
25
25
|
spec.add_development_dependency "bundler"
|
|
26
26
|
spec.add_development_dependency "rake", "~> 10.0"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Eager loading' do
|
|
4
|
+
include_context 'users and tasks'
|
|
5
|
+
|
|
6
|
+
before do
|
|
7
|
+
setup.relation(:users) do
|
|
8
|
+
def by_name(name)
|
|
9
|
+
where(name: name)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
setup.relation(:tasks) do
|
|
14
|
+
def for_users(users)
|
|
15
|
+
where(user_id: users.map { |tuple| tuple[:id] })
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
setup.relation(:tags) do
|
|
20
|
+
def for_tasks(tasks)
|
|
21
|
+
inner_join(:task_tags, task_id: :id)
|
|
22
|
+
.where(task_id: tasks.map { |tuple| tuple[:id] })
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'issues 3 queries for 3 combined relations' do
|
|
28
|
+
users = rom.relation(:users).by_name('Piotr')
|
|
29
|
+
tasks = rom.relation(:tasks)
|
|
30
|
+
tags = rom.relation(:tags)
|
|
31
|
+
|
|
32
|
+
relation = users.combine(tasks.for_users.combine(tags.for_tasks))
|
|
33
|
+
|
|
34
|
+
# TODO: figure out a way to assert correct number of issued queries
|
|
35
|
+
expect(relation.call).to be_instance_of(ROM::Relation::Loaded)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -20,6 +20,11 @@ describe 'Commands / Create' do
|
|
|
20
20
|
setup.commands(:users) do
|
|
21
21
|
define(:create) do
|
|
22
22
|
input Params
|
|
23
|
+
|
|
24
|
+
validator -> tuple {
|
|
25
|
+
raise ROM::CommandError.new('name cannot be empty') if tuple[:name] == ''
|
|
26
|
+
}
|
|
27
|
+
|
|
23
28
|
result :one
|
|
24
29
|
end
|
|
25
30
|
|
|
@@ -32,7 +37,7 @@ describe 'Commands / Create' do
|
|
|
32
37
|
end
|
|
33
38
|
|
|
34
39
|
context '#transaction' do
|
|
35
|
-
it '
|
|
40
|
+
it 'creates record if nothing was raised' do
|
|
36
41
|
result = users.create.transaction {
|
|
37
42
|
users.create.call(name: 'Jane')
|
|
38
43
|
}
|
|
@@ -50,15 +55,59 @@ describe 'Commands / Create' do
|
|
|
50
55
|
expect(result.value).to eq(id: 1, name: 'Jane')
|
|
51
56
|
end
|
|
52
57
|
|
|
53
|
-
it '
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
it 'creates nothing if command error was raised' do
|
|
59
|
+
expect {
|
|
60
|
+
passed = false
|
|
61
|
+
|
|
62
|
+
result = users.create.transaction {
|
|
63
|
+
users.create.call(name: 'Jane')
|
|
64
|
+
users.create.call(name: '')
|
|
65
|
+
} >-> value {
|
|
66
|
+
passed = true
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
expect(result.value).to be(nil)
|
|
70
|
+
expect(result.error.message).to eql('name cannot be empty')
|
|
71
|
+
expect(passed).to be(false)
|
|
72
|
+
}.to_not change { rom.relations.users.count }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it 'creates nothing if rollback was raised' do
|
|
76
|
+
expect {
|
|
77
|
+
passed = false
|
|
57
78
|
|
|
58
|
-
|
|
79
|
+
result = users.create.transaction {
|
|
80
|
+
users.create.call(name: 'Jane')
|
|
81
|
+
users.create.call(name: 'John')
|
|
82
|
+
raise ROM::SQL::Rollback
|
|
83
|
+
} >-> value {
|
|
84
|
+
passed = true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
expect(result.value).to be(nil)
|
|
88
|
+
expect(result.error).to be(nil)
|
|
89
|
+
expect(passed).to be(false)
|
|
90
|
+
}.to_not change { rom.relations.users.count }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it 'creates nothing if constraint error was raised' do
|
|
94
|
+
expect {
|
|
95
|
+
passed = false
|
|
96
|
+
|
|
97
|
+
result = users.create.transaction {
|
|
98
|
+
users.create.call(name: 'Jane')
|
|
99
|
+
users.create.call(name: 'Jane')
|
|
100
|
+
} >-> value {
|
|
101
|
+
passed = true
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
expect(result.value).to be(nil)
|
|
105
|
+
expect(result.error).to_not be(nil)
|
|
106
|
+
expect(passed).to be(false)
|
|
107
|
+
}.to_not change { rom.relations.users.count }
|
|
59
108
|
end
|
|
60
109
|
|
|
61
|
-
it '
|
|
110
|
+
it 'creates nothing if anything was raised in any nested transaction' do
|
|
62
111
|
expect {
|
|
63
112
|
expect {
|
|
64
113
|
users.create.transaction {
|
|
@@ -5,7 +5,7 @@ describe 'Reading relations' do
|
|
|
5
5
|
include_context 'users and tasks'
|
|
6
6
|
|
|
7
7
|
before :each do
|
|
8
|
-
class
|
|
8
|
+
class Goal
|
|
9
9
|
include Virtus.value_object(coerce: true)
|
|
10
10
|
|
|
11
11
|
values do
|
|
@@ -20,31 +20,34 @@ describe 'Reading relations' do
|
|
|
20
20
|
values do
|
|
21
21
|
attribute :id, Integer
|
|
22
22
|
attribute :name, String
|
|
23
|
-
attribute :
|
|
23
|
+
attribute :goals, Array[Goal]
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
class
|
|
27
|
+
class UserGoalCount
|
|
28
28
|
include Virtus.value_object(coerce: true)
|
|
29
29
|
|
|
30
30
|
values do
|
|
31
31
|
attribute :id, Integer
|
|
32
32
|
attribute :name, String
|
|
33
|
-
attribute :
|
|
33
|
+
attribute :goal_count, Integer
|
|
34
34
|
end
|
|
35
35
|
end
|
|
36
36
|
|
|
37
|
-
setup.relation(:
|
|
37
|
+
setup.relation(:goals) do
|
|
38
|
+
register_as :goals
|
|
39
|
+
dataset :tasks
|
|
40
|
+
end
|
|
38
41
|
|
|
39
42
|
setup.relation(:users) do
|
|
40
|
-
one_to_many :
|
|
43
|
+
one_to_many :goals, key: :user_id
|
|
41
44
|
|
|
42
45
|
def by_name(name)
|
|
43
46
|
where(name: name)
|
|
44
47
|
end
|
|
45
48
|
|
|
46
|
-
def
|
|
47
|
-
association_left_join(:
|
|
49
|
+
def with_goals
|
|
50
|
+
association_left_join(:goals, select: [:id, :title])
|
|
48
51
|
end
|
|
49
52
|
|
|
50
53
|
def all
|
|
@@ -52,19 +55,19 @@ describe 'Reading relations' do
|
|
|
52
55
|
end
|
|
53
56
|
end
|
|
54
57
|
|
|
55
|
-
setup.relation(:
|
|
58
|
+
setup.relation(:user_goal_counts) do
|
|
56
59
|
dataset :users
|
|
57
|
-
register_as :
|
|
58
|
-
one_to_many :
|
|
60
|
+
register_as :user_goal_counts
|
|
61
|
+
one_to_many :goals, key: :user_id
|
|
59
62
|
|
|
60
63
|
def all
|
|
61
|
-
|
|
62
|
-
count(:tasks).as(:
|
|
64
|
+
with_goals.select_group(:users__id, :users__name).select_append {
|
|
65
|
+
count(:tasks).as(:goal_count)
|
|
63
66
|
}
|
|
64
67
|
end
|
|
65
68
|
|
|
66
|
-
def
|
|
67
|
-
association_left_join(:
|
|
69
|
+
def with_goals
|
|
70
|
+
association_left_join(:goals, select: [:id, :title])
|
|
68
71
|
end
|
|
69
72
|
end
|
|
70
73
|
|
|
@@ -72,36 +75,36 @@ describe 'Reading relations' do
|
|
|
72
75
|
define(:users) do
|
|
73
76
|
model User
|
|
74
77
|
|
|
75
|
-
group :
|
|
76
|
-
model
|
|
78
|
+
group :goals do
|
|
79
|
+
model Goal
|
|
77
80
|
|
|
78
81
|
attribute :id, from: :tasks_id
|
|
79
82
|
attribute :title
|
|
80
83
|
end
|
|
81
84
|
end
|
|
82
85
|
|
|
83
|
-
define(:
|
|
84
|
-
model
|
|
86
|
+
define(:user_goal_counts) do
|
|
87
|
+
model UserGoalCount
|
|
85
88
|
end
|
|
86
89
|
end
|
|
87
90
|
end
|
|
88
91
|
|
|
89
92
|
it 'loads domain objects' do
|
|
90
|
-
user = rom.relation(:users).as(:users).
|
|
93
|
+
user = rom.relation(:users).as(:users).with_goals.by_name('Piotr').to_a.first
|
|
91
94
|
|
|
92
95
|
expect(user).to eql(
|
|
93
96
|
User.new(
|
|
94
|
-
id: 1, name: 'Piotr',
|
|
97
|
+
id: 1, name: 'Piotr', goals: [Goal.new(id: 1, title: 'Finish ROM')]
|
|
95
98
|
))
|
|
96
99
|
end
|
|
97
100
|
|
|
98
101
|
it 'works with grouping and aggregates' do
|
|
99
|
-
rom.relations[:
|
|
102
|
+
rom.relations[:goals].insert(id: 2, user_id: 1, title: 'Get Milk')
|
|
100
103
|
|
|
101
|
-
|
|
104
|
+
users_with_goal_count = rom.relation(:user_goal_counts).as(:user_goal_counts).all
|
|
102
105
|
|
|
103
|
-
expect(
|
|
104
|
-
|
|
106
|
+
expect(users_with_goal_count.to_a).to eq([
|
|
107
|
+
UserGoalCount.new(id: 1, name: "Piotr", goal_count: 2)
|
|
105
108
|
])
|
|
106
109
|
end
|
|
107
110
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Association errors' do
|
|
4
|
+
include_context 'users and tasks'
|
|
5
|
+
|
|
6
|
+
describe 'accessing an undefined association' do
|
|
7
|
+
specify do
|
|
8
|
+
setup.relation(:users) do
|
|
9
|
+
def with_undefined
|
|
10
|
+
association_join(:undefined)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
expect {
|
|
15
|
+
rom.relations.users.with_undefined
|
|
16
|
+
}.to raise_error ROM::SQL::NoAssociationError, 'Association :undefined has not been defined for relation :users'
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
@@ -3,6 +3,16 @@ require 'spec_helper'
|
|
|
3
3
|
describe 'Defining one-to-many association' do
|
|
4
4
|
include_context 'users and tasks'
|
|
5
5
|
|
|
6
|
+
before do
|
|
7
|
+
setup.mappers do
|
|
8
|
+
define(:users)
|
|
9
|
+
|
|
10
|
+
define(:with_tasks, parent: :users) do
|
|
11
|
+
group tasks: [:tasks_id, :title]
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
6
16
|
it 'extends relation with association methods' do
|
|
7
17
|
setup.relation(:users) do
|
|
8
18
|
one_to_many :tasks, key: :user_id
|
|
@@ -20,22 +30,42 @@ describe 'Defining one-to-many association' do
|
|
|
20
30
|
end
|
|
21
31
|
end
|
|
22
32
|
|
|
23
|
-
|
|
24
|
-
define(:users)
|
|
33
|
+
users = rom.relations.users
|
|
25
34
|
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
expect(users.with_tasks.by_name("Piotr").to_a).to eql(
|
|
36
|
+
[{ id: 1, name: 'Piotr', tasks_id: 1, title: 'Finish ROM' }]
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
result = rom.relation(:users).map_with(:with_tasks)
|
|
40
|
+
.all.with_tasks.by_name("Piotr").to_a
|
|
41
|
+
|
|
42
|
+
expect(result).to eql(
|
|
43
|
+
[{ id: 1, name: 'Piotr', tasks: [{ tasks_id: 1, title: 'Finish ROM' }] }]
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it 'allows setting :conditions' do
|
|
48
|
+
setup.relation(:users) do
|
|
49
|
+
one_to_many :piotrs_tasks, relation: :tasks, key: :user_id,
|
|
50
|
+
conditions: { name: 'Piotr' }
|
|
51
|
+
|
|
52
|
+
def with_piotrs_tasks
|
|
53
|
+
association_left_join(:piotrs_tasks, select: [:id, :title])
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def all
|
|
57
|
+
select(:id, :name)
|
|
28
58
|
end
|
|
29
59
|
end
|
|
30
60
|
|
|
31
61
|
users = rom.relations.users
|
|
32
62
|
|
|
33
|
-
expect(users.
|
|
63
|
+
expect(users.with_piotrs_tasks.to_a).to eql(
|
|
34
64
|
[{ id: 1, name: 'Piotr', tasks_id: 1, title: 'Finish ROM' }]
|
|
35
65
|
)
|
|
36
66
|
|
|
37
67
|
result = rom.relation(:users).map_with(:with_tasks)
|
|
38
|
-
.all.
|
|
68
|
+
.all.with_piotrs_tasks.to_a
|
|
39
69
|
|
|
40
70
|
expect(result).to eql(
|
|
41
71
|
[{ id: 1, name: 'Piotr', tasks: [{ tasks_id: 1, title: 'Finish ROM' }] }]
|
|
@@ -9,7 +9,7 @@ describe 'Plugin / Pagination' do
|
|
|
9
9
|
9.times { |i| conn[:users].insert(name: "User #{i}") }
|
|
10
10
|
|
|
11
11
|
setup.relation(:users) do
|
|
12
|
-
|
|
12
|
+
use :pagination
|
|
13
13
|
|
|
14
14
|
per_page 4
|
|
15
15
|
end
|
|
@@ -53,6 +53,18 @@ describe 'Plugin / Pagination' do
|
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
+
describe '#total_pages' do
|
|
57
|
+
it 'returns a single page when elements are a perfect fit' do
|
|
58
|
+
users = rom.relation(:users).page(1).per_page(3)
|
|
59
|
+
expect(users.pager.total_pages).to eql(3)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it 'returns the exact number of pages to accommodate all elements' do
|
|
63
|
+
users = rom.relation(:users).per_page(9)
|
|
64
|
+
expect(users.pager.total_pages).to eql(1)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
56
68
|
describe '#pager' do
|
|
57
69
|
it 'returns a pager with pagination meta-info' do
|
|
58
70
|
users = rom.relation(:users).page(1)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rom-sql
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Piotr Solnica
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-
|
|
11
|
+
date: 2015-05-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: sequel
|
|
@@ -50,14 +50,20 @@ dependencies:
|
|
|
50
50
|
requirements:
|
|
51
51
|
- - "~>"
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: 0.
|
|
53
|
+
version: '0.7'
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: 0.7.0
|
|
54
57
|
type: :runtime
|
|
55
58
|
prerelease: false
|
|
56
59
|
version_requirements: !ruby/object:Gem::Requirement
|
|
57
60
|
requirements:
|
|
58
61
|
- - "~>"
|
|
59
62
|
- !ruby/object:Gem::Version
|
|
60
|
-
version: 0.
|
|
63
|
+
version: '0.7'
|
|
64
|
+
- - ">="
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: 0.7.0
|
|
61
67
|
- !ruby/object:Gem::Dependency
|
|
62
68
|
name: bundler
|
|
63
69
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -132,6 +138,7 @@ files:
|
|
|
132
138
|
- log/.gitkeep
|
|
133
139
|
- rom-sql.gemspec
|
|
134
140
|
- spec/fixtures/migrations/20150403090603_create_carrots.rb
|
|
141
|
+
- spec/integration/combine_spec.rb
|
|
135
142
|
- spec/integration/commands/create_spec.rb
|
|
136
143
|
- spec/integration/commands/delete_spec.rb
|
|
137
144
|
- spec/integration/commands/update_spec.rb
|
|
@@ -142,6 +149,7 @@ files:
|
|
|
142
149
|
- spec/spec_helper.rb
|
|
143
150
|
- spec/support/active_support_notifications_spec.rb
|
|
144
151
|
- spec/support/rails_log_subscriber_spec.rb
|
|
152
|
+
- spec/unit/association_errors_spec.rb
|
|
145
153
|
- spec/unit/combined_associations_spec.rb
|
|
146
154
|
- spec/unit/logger_spec.rb
|
|
147
155
|
- spec/unit/many_to_many_spec.rb
|
|
@@ -179,6 +187,7 @@ specification_version: 4
|
|
|
179
187
|
summary: SQL databases support for ROM
|
|
180
188
|
test_files:
|
|
181
189
|
- spec/fixtures/migrations/20150403090603_create_carrots.rb
|
|
190
|
+
- spec/integration/combine_spec.rb
|
|
182
191
|
- spec/integration/commands/create_spec.rb
|
|
183
192
|
- spec/integration/commands/delete_spec.rb
|
|
184
193
|
- spec/integration/commands/update_spec.rb
|
|
@@ -189,6 +198,7 @@ test_files:
|
|
|
189
198
|
- spec/spec_helper.rb
|
|
190
199
|
- spec/support/active_support_notifications_spec.rb
|
|
191
200
|
- spec/support/rails_log_subscriber_spec.rb
|
|
201
|
+
- spec/unit/association_errors_spec.rb
|
|
192
202
|
- spec/unit/combined_associations_spec.rb
|
|
193
203
|
- spec/unit/logger_spec.rb
|
|
194
204
|
- spec/unit/many_to_many_spec.rb
|
|
@@ -200,4 +210,3 @@ test_files:
|
|
|
200
210
|
- spec/unit/relation_spec.rb
|
|
201
211
|
- spec/unit/repository_spec.rb
|
|
202
212
|
- spec/unit/schema_spec.rb
|
|
203
|
-
has_rdoc:
|