activerecord_to_poro 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Guardfile +3 -1
- data/README.md +68 -1
- data/lib/activerecord_to_poro.rb +1 -0
- data/lib/activerecord_to_poro/converter.rb +38 -23
- data/lib/activerecord_to_poro/default_poro_class_builder.rb +4 -4
- data/lib/activerecord_to_poro/mapper_extension.rb +27 -0
- data/lib/activerecord_to_poro/version.rb +1 -1
- data/spec/acceptance/map_ar_associations_spec.rb +46 -1
- data/spec/acceptance/map_ar_objects_spec.rb +22 -4
- data/spec/ar_support/db/migrate/20140127093447_create_my_ar_class.rb +15 -0
- data/spec/ar_support/models/address.rb +3 -0
- data/spec/ar_support/models/permission.rb +3 -0
- data/spec/ar_support/models/role.rb +2 -1
- data/spec/ar_support/models/salutation.rb +1 -1
- data/spec/ar_support/models/user.rb +6 -5
- data/spec/integration/lib/activerecord_to_poro/converter_spec.rb +67 -46
- data/spec/integration/lib/activerecord_to_poro/default_poro_class_builder_spec.rb +2 -2
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73555a31ecff01b8d794c304d390d6a742480dff
|
4
|
+
data.tar.gz: 7966c2cd299ba10bb133ee76e421b928990c826d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0980c84cee2f41eed81bf1e588b849be5cdd0e435f125a42ade0631f13d970ce6e187e15a690c01c35d7010c2c83e81b458c19ec1abf5db2e07c0572726e31f4
|
7
|
+
data.tar.gz: c1de463a85bec7018d6bdfa855acd24758ba9a46523a8abf96f45cbeb78811acea5190d87b8941b824c5e4031407b9f3f91164203032978b389188f04a80119d
|
data/Guardfile
CHANGED
@@ -11,7 +11,9 @@ guard :rspec, all_after_pass: true ,
|
|
11
11
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/unit/lib/#{m[1]}_spec.rb" }
|
12
12
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/integration/lib/#{m[1]}_spec.rb" }
|
13
13
|
|
14
|
-
watch('spec/spec_helper.rb')
|
14
|
+
watch('spec/spec_helper.rb') { "spec" }
|
15
|
+
watch(%r{spec/support/.+\.rb}) { "spec" }
|
16
|
+
watch(%r{spec/ar_support/.+\.rb}) { "spec" }
|
15
17
|
|
16
18
|
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
17
19
|
|
data/README.md
CHANGED
@@ -18,15 +18,82 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
For uptodate doc's take a look into the specs.
|
22
|
+
|
21
23
|
```ruby
|
22
24
|
|
25
|
+
# for instance you'r ActiveRecord models look like this
|
26
|
+
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
has_many :roles, autosave: true
|
29
|
+
has_many :permissions, through: :roles, autosave: true
|
30
|
+
|
31
|
+
belongs_to :salutation, autosave: true
|
32
|
+
has_one :address, autosave: true
|
33
|
+
end
|
34
|
+
|
35
|
+
class Role < ActiveRecord::Base
|
36
|
+
has_many :permissions, autosave: true
|
37
|
+
belongs_to :user, autosave: true
|
38
|
+
end
|
39
|
+
|
40
|
+
class Salutation < ActiveRecord::Base
|
41
|
+
has_many :users, autosave: true
|
42
|
+
end
|
43
|
+
|
44
|
+
class Permission < ActiveRecord::Base
|
45
|
+
belongs_to :role, autosave: true
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
# You can convert them to poro's like this (automatic genereation of a poro class out of you'r AR class)
|
50
|
+
|
23
51
|
roles_converter = ActiverecordToPoro::Converter.new(Role)
|
24
52
|
salutation_converter = ActiverecordToPoro::Converter.new(Salutation)
|
25
|
-
user_converter = ActiverecordToPoro::Converter.new(User, roles: roles_converter, salutation: salutation_converter)
|
53
|
+
user_converter = ActiverecordToPoro::Converter.new(User, convert_associations: {roles: roles_converter, salutation: salutation_converter})
|
26
54
|
|
27
55
|
|
28
56
|
poro = user_converter.load(User.first)
|
29
57
|
|
58
|
+
# Or with you'r custom class
|
59
|
+
|
60
|
+
roles_converter = ActiverecordToPoro::Converter.new(Role,
|
61
|
+
load_source: YourPoroClass
|
62
|
+
)
|
63
|
+
|
64
|
+
|
65
|
+
# default 1:1 mapping only or except
|
66
|
+
|
67
|
+
roles_converter = ActiverecordToPoro::Converter.new(Role,
|
68
|
+
except: [:lock_version]
|
69
|
+
)
|
70
|
+
|
71
|
+
roles_converter = ActiverecordToPoro::Converter.new(Role,
|
72
|
+
only: [:name]
|
73
|
+
)
|
74
|
+
|
75
|
+
# add you'r own mapping rules
|
76
|
+
|
77
|
+
roles_converter.extend_mapping do
|
78
|
+
rule to: :lock_version # for more look @ https://github.com/slowjack2k/yaoc
|
79
|
+
end
|
80
|
+
|
81
|
+
# new rule for faster association mapping
|
82
|
+
|
83
|
+
roles_converter.extend_mapping do
|
84
|
+
association_rule to: association_name,
|
85
|
+
lazy_loading: true,
|
86
|
+
converter: association_converter
|
87
|
+
# optional
|
88
|
+
# from: ...,
|
89
|
+
# reverse_to: ...,
|
90
|
+
# reverse_from: ...,
|
91
|
+
# converter: ..., # a ActiverecordToPoro::Converter
|
92
|
+
# is_collection: ..., # when for instance a scope is used or another method that delivers an ar object
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
|
30
97
|
|
31
98
|
|
32
99
|
```
|
data/lib/activerecord_to_poro.rb
CHANGED
@@ -3,6 +3,7 @@ require 'activerecord_to_poro/version'
|
|
3
3
|
require 'activerecord_to_poro/metadata_enabled'
|
4
4
|
require 'activerecord_to_poro/metadata'
|
5
5
|
require 'activerecord_to_poro/default_poro_class_builder'
|
6
|
+
require 'activerecord_to_poro/mapper_extension'
|
6
7
|
require 'activerecord_to_poro/converter'
|
7
8
|
|
8
9
|
module ActiverecordToPoro
|
@@ -5,13 +5,22 @@ module ActiverecordToPoro
|
|
5
5
|
attr_accessor :load_source_class,
|
6
6
|
:dump_source_class,
|
7
7
|
:association_converters,
|
8
|
-
:use_lazy_loading
|
9
|
-
|
10
|
-
|
8
|
+
:use_lazy_loading,
|
9
|
+
:except_attributes,
|
10
|
+
:only_attributes
|
11
|
+
|
12
|
+
def initialize(ar_class,
|
13
|
+
use_lazy_loading=true,
|
14
|
+
except: nil,
|
15
|
+
only: nil,
|
16
|
+
load_source: nil,
|
17
|
+
convert_associations: {})
|
11
18
|
self.load_source_class = ar_class
|
12
|
-
self.dump_source_class = DefaultPoroClassBuilder.new(ar_class).()
|
13
|
-
self.association_converters =
|
19
|
+
self.dump_source_class = load_source || DefaultPoroClassBuilder.new(ar_class).()
|
20
|
+
self.association_converters = convert_associations
|
14
21
|
self.use_lazy_loading = use_lazy_loading
|
22
|
+
self.except_attributes = Array(except)
|
23
|
+
self.only_attributes = only.nil? ? nil : Array(only) # an empty array can be wanted, so that there is no default mapping @ all
|
15
24
|
end
|
16
25
|
|
17
26
|
def load(to_convert)
|
@@ -22,20 +31,37 @@ module ActiverecordToPoro
|
|
22
31
|
mapper.dump(to_convert)
|
23
32
|
end
|
24
33
|
|
34
|
+
def extend_mapping(&block)
|
35
|
+
mapper.add_mapping &block
|
36
|
+
end
|
37
|
+
|
38
|
+
|
25
39
|
def mapper
|
26
40
|
@mapper||= Yaoc::ObjectMapper.new(self.dump_source_class, self.load_source_class).tap do |mapper|
|
27
|
-
|
41
|
+
mapper.extend ActiverecordToPoro::MapperExtension
|
42
|
+
mapper.fetcher(:public_send)
|
43
|
+
|
44
|
+
add_default_mapping_for_current_class(mapper)
|
28
45
|
add_mapping_for_associations(mapper)
|
29
46
|
end
|
30
47
|
end
|
31
48
|
|
49
|
+
def attributes_for_default_mapping
|
50
|
+
self.only_attributes || (
|
51
|
+
columns(self.load_source_class) -
|
52
|
+
primary_keys(self.load_source_class) -
|
53
|
+
association_specific_columns(self.load_source_class) -
|
54
|
+
associated_object_accessors(self.load_source_class) -
|
55
|
+
self.except_attributes
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
32
59
|
protected
|
33
60
|
|
34
|
-
def
|
35
|
-
tmp_quirk =
|
61
|
+
def add_default_mapping_for_current_class(mapper)
|
62
|
+
tmp_quirk = attributes_for_default_mapping
|
36
63
|
|
37
64
|
mapper.add_mapping do
|
38
|
-
fetcher :public_send
|
39
65
|
rule to: tmp_quirk
|
40
66
|
|
41
67
|
rule to: :_set_metadata,
|
@@ -55,17 +81,13 @@ module ActiverecordToPoro
|
|
55
81
|
|
56
82
|
def add_mapping_for_associations(mapper)
|
57
83
|
association_converters.each_pair do |association_name, association_converter|
|
58
|
-
map_collection = self.load_source_class.reflections[association_name].collection?
|
59
84
|
|
60
85
|
lazy_quirk = self.use_lazy_loading
|
61
86
|
|
62
87
|
mapper.add_mapping do
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
reverse_lazy_loading: false, #AR doesn't like ToProcDelegator
|
67
|
-
object_converter: association_converter.mapper,
|
68
|
-
is_collection: map_collection
|
88
|
+
association_rule to: association_name,
|
89
|
+
lazy_loading: lazy_quirk,
|
90
|
+
converter: association_converter
|
69
91
|
end
|
70
92
|
end
|
71
93
|
end
|
@@ -86,12 +108,5 @@ module ActiverecordToPoro
|
|
86
108
|
end
|
87
109
|
end
|
88
110
|
|
89
|
-
def plain_attributes
|
90
|
-
columns(self.load_source_class) -
|
91
|
-
primary_keys(self.load_source_class) -
|
92
|
-
association_specific_columns(self.load_source_class) -
|
93
|
-
associated_object_accessors(self.load_source_class)
|
94
|
-
end
|
95
|
-
|
96
111
|
end
|
97
112
|
end
|
@@ -2,19 +2,19 @@ module ActiverecordToPoro
|
|
2
2
|
module ColumnHelper
|
3
3
|
module_function
|
4
4
|
def columns(ar_class)
|
5
|
-
ar_class.column_names
|
5
|
+
ar_class.column_names.map &:to_sym
|
6
6
|
end
|
7
7
|
|
8
8
|
def primary_keys(ar_class)
|
9
|
-
[ar_class.primary_key]
|
9
|
+
[ar_class.primary_key].map &:to_sym
|
10
10
|
end
|
11
11
|
|
12
12
|
def association_specific_columns(ar_class)
|
13
|
-
ar_class.reflect_on_all_associations(:belongs_to).map(&:foreign_key)
|
13
|
+
ar_class.reflect_on_all_associations(:belongs_to).map(&:foreign_key).map &:to_sym
|
14
14
|
end
|
15
15
|
|
16
16
|
def associated_object_accessors(ar_class)
|
17
|
-
ar_class.reflections.keys
|
17
|
+
ar_class.reflections.keys.map &:to_sym
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ActiverecordToPoro
|
2
|
+
module MapperExtension
|
3
|
+
|
4
|
+
def association_rule(to: nil,
|
5
|
+
from: to,
|
6
|
+
reverse_to: from,
|
7
|
+
reverse_from: to,
|
8
|
+
converter: nil,
|
9
|
+
is_collection: false,
|
10
|
+
lazy_loading: nil
|
11
|
+
)
|
12
|
+
|
13
|
+
map_collection = (self.dump_result_source.reflections[from] &&
|
14
|
+
self.dump_result_source.reflections[from].collection?) || is_collection
|
15
|
+
|
16
|
+
rule to: to,
|
17
|
+
from: from,
|
18
|
+
reverse_to: reverse_to,
|
19
|
+
reverse_from: reverse_from,
|
20
|
+
reverse_lazy_loading: false, #AR doesn't like ToProcDelegator
|
21
|
+
object_converter: converter.mapper,
|
22
|
+
is_collection: map_collection,
|
23
|
+
lazy_loading: lazy_loading
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -7,13 +7,17 @@ feature 'Map active record associations', %q{
|
|
7
7
|
} do
|
8
8
|
|
9
9
|
given!(:mapper){
|
10
|
-
ActiverecordToPoro::Converter.new(a_active_record_class, roles: roles_converter, salutation: salutation_converter)
|
10
|
+
ActiverecordToPoro::Converter.new(a_active_record_class, convert_associations: {roles: roles_converter, salutation: salutation_converter} )
|
11
11
|
}
|
12
12
|
|
13
13
|
given!(:roles_converter){
|
14
14
|
ActiverecordToPoro::Converter.new(Role)
|
15
15
|
}
|
16
16
|
|
17
|
+
given!(:permissions_converter){
|
18
|
+
ActiverecordToPoro::Converter.new(Permission)
|
19
|
+
}
|
20
|
+
|
17
21
|
given!(:salutation_converter){
|
18
22
|
ActiverecordToPoro::Converter.new(Salutation)
|
19
23
|
}
|
@@ -26,12 +30,35 @@ feature 'Map active record associations', %q{
|
|
26
30
|
a_active_record_class.create!(name: "my name", email: "my_name@example.com").tap do |user|
|
27
31
|
user.roles.create!(name: "admin")
|
28
32
|
user.roles.create!(name: "guest")
|
33
|
+
|
34
|
+
user.roles.first.permissions.create!(name: "first_permission")
|
35
|
+
user.roles.first.permissions.create!(name: "second_permission")
|
36
|
+
|
37
|
+
user.roles.last.permissions.create!(name: "third_permission")
|
38
|
+
|
29
39
|
user.salutation = Salutation.create!(name: "Mister")
|
30
40
|
|
41
|
+
user.address = Address.create!(street: 'Westminster Abbey')
|
42
|
+
|
31
43
|
user.save!
|
44
|
+
|
45
|
+
user.reload
|
46
|
+
end
|
47
|
+
}
|
48
|
+
|
49
|
+
given(:a_custom_poro_class){
|
50
|
+
ActiverecordToPoro::DefaultPoroClassBuilder.new(a_active_record_class).().tap do |new_class|
|
51
|
+
new_class.send(:attr_accessor, :some_other_name)
|
32
52
|
end
|
33
53
|
}
|
34
54
|
|
55
|
+
given(:mapper_with_custom_source){
|
56
|
+
ActiverecordToPoro::Converter.new(a_active_record_class,
|
57
|
+
load_source: a_custom_poro_class,
|
58
|
+
except: [:lock_version]
|
59
|
+
)
|
60
|
+
}
|
61
|
+
|
35
62
|
|
36
63
|
scenario "creates a poro out of an ActiveRecord object with associations set" do
|
37
64
|
expect(mapper.load(a_active_record_object).roles.size).to eq 2
|
@@ -41,6 +68,7 @@ feature 'Map active record associations', %q{
|
|
41
68
|
scenario "creates an ActiveRecord object from a poro object with associations set" do
|
42
69
|
poro = mapper.load(a_active_record_object)
|
43
70
|
expect(mapper.dump(poro).roles.size).to eq 2
|
71
|
+
expect(mapper.dump(poro).permissions.size).to eq 3
|
44
72
|
end
|
45
73
|
|
46
74
|
scenario "lazy loads associated objects" do
|
@@ -50,4 +78,21 @@ feature 'Map active record associations', %q{
|
|
50
78
|
mapper.load(a_active_record_object)
|
51
79
|
end
|
52
80
|
|
81
|
+
scenario 'add custom association mappings' do
|
82
|
+
quirk_converter = permissions_converter
|
83
|
+
|
84
|
+
mapper_with_custom_source.extend_mapping do
|
85
|
+
|
86
|
+
association_rule to: :some_other_name,
|
87
|
+
from: :permissions,
|
88
|
+
converter: quirk_converter,
|
89
|
+
lazy_loading: true
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
user_poro = mapper_with_custom_source.load(a_active_record_object)
|
94
|
+
|
95
|
+
expect(user_poro.some_other_name.size).to eq 3
|
96
|
+
end
|
97
|
+
|
53
98
|
end
|
@@ -10,6 +10,13 @@ feature "Map active record objects", %q{
|
|
10
10
|
ActiverecordToPoro::Converter.new(a_active_record_class)
|
11
11
|
}
|
12
12
|
|
13
|
+
given(:mapper_with_custom_source){
|
14
|
+
ActiverecordToPoro::Converter.new(a_active_record_class,
|
15
|
+
load_source: custom_poro_class,
|
16
|
+
except: [:lock_version]
|
17
|
+
)
|
18
|
+
}
|
19
|
+
|
13
20
|
given(:a_active_record_class){
|
14
21
|
User
|
15
22
|
}
|
@@ -18,13 +25,12 @@ feature "Map active record objects", %q{
|
|
18
25
|
a_active_record_class.create!(name: "my name", email: "my_name@example.com")
|
19
26
|
}
|
20
27
|
|
21
|
-
given(:
|
22
|
-
|
28
|
+
given(:custom_poro_class){
|
29
|
+
ActiverecordToPoro::DefaultPoroClassBuilder.new(a_active_record_class).()
|
23
30
|
}
|
24
31
|
|
25
|
-
|
26
32
|
scenario "creates a poro out of an ActiveRecord object" do
|
27
|
-
expect(mapper.load(a_active_record_object)
|
33
|
+
expect(mapper.load(a_active_record_object)).not_to be_kind_of ActiveRecord::Base
|
28
34
|
end
|
29
35
|
|
30
36
|
scenario "creates an ActiveRecord object from a poro object" do
|
@@ -32,4 +38,16 @@ feature "Map active record objects", %q{
|
|
32
38
|
expect(mapper.dump(poro)).to eq a_active_record_object
|
33
39
|
end
|
34
40
|
|
41
|
+
scenario "use my own source class for converting ActiveRecord objects" do
|
42
|
+
expect(mapper_with_custom_source.load(a_active_record_object)).to be_kind_of custom_poro_class
|
43
|
+
end
|
44
|
+
|
45
|
+
scenario 'extend default mapping' do
|
46
|
+
mapper_with_custom_source.extend_mapping do
|
47
|
+
rule to: :lock_version
|
48
|
+
end
|
49
|
+
|
50
|
+
expect(mapper.load(a_active_record_object).lock_version).to eq a_active_record_object.lock_version
|
51
|
+
end
|
52
|
+
|
35
53
|
end
|
@@ -4,15 +4,30 @@ class CreateMyArClass < ActiveRecord::Migration
|
|
4
4
|
t.string :name
|
5
5
|
t.string :email
|
6
6
|
t.integer :salutation_id
|
7
|
+
t.integer :lock_version
|
7
8
|
end
|
8
9
|
|
9
10
|
create_table :roles do |t|
|
10
11
|
t.string :name
|
11
12
|
t.integer :user_id
|
13
|
+
t.integer :lock_version
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :permissions do |t|
|
17
|
+
t.string :name
|
18
|
+
t.integer :role_id
|
19
|
+
t.integer :lock_version
|
12
20
|
end
|
13
21
|
|
14
22
|
create_table :salutations do |t|
|
15
23
|
t.string :name
|
24
|
+
t.integer :lock_version
|
25
|
+
end
|
26
|
+
|
27
|
+
create_table :addresses do |t|
|
28
|
+
t.string :street
|
29
|
+
t.integer :user_id
|
30
|
+
t.integer :lock_version
|
16
31
|
end
|
17
32
|
end
|
18
33
|
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
class User < ActiveRecord::Base
|
2
|
-
has_many :roles
|
3
|
-
|
2
|
+
has_many :roles, autosave: true
|
3
|
+
has_many :permissions, through: :roles, autosave: true
|
4
|
+
|
5
|
+
belongs_to :salutation, autosave: true
|
6
|
+
has_one :address, autosave: true
|
7
|
+
|
4
8
|
|
5
|
-
def zz
|
6
|
-
ActiveRecord::Base.new
|
7
|
-
end
|
8
9
|
end
|
@@ -1,67 +1,88 @@
|
|
1
1
|
require 'integration_spec_helper'
|
2
2
|
|
3
3
|
describe ActiverecordToPoro::Converter do
|
4
|
-
subject!{
|
5
|
-
ActiverecordToPoro::Converter.new(User, roles: roles_converter, salutation: salutation_converter)
|
6
|
-
}
|
7
|
-
|
8
|
-
let(:roles_converter){
|
9
|
-
ActiverecordToPoro::Converter.new(Role)
|
10
|
-
}
|
11
|
-
|
12
|
-
let(:salutation_converter){
|
13
|
-
ActiverecordToPoro::Converter.new(Salutation)
|
14
|
-
}
|
15
|
-
|
16
|
-
let(:ar_object){
|
17
|
-
User.create!(name: 'my name', email: 'my_name@example.com').tap do |user|
|
18
|
-
user.roles.create!(name: 'admin')
|
19
|
-
user.roles.create!(name: 'guest')
|
20
|
-
end
|
21
|
-
}
|
22
4
|
|
23
|
-
|
24
|
-
subject
|
25
|
-
|
5
|
+
describe '.new' do
|
6
|
+
subject{
|
7
|
+
ActiverecordToPoro::Converter
|
8
|
+
}
|
26
9
|
|
27
|
-
|
28
|
-
|
29
|
-
expect(
|
10
|
+
it 'removes "except" attributes from default mapping' do
|
11
|
+
object = subject.new(User, except: :lock_version)
|
12
|
+
expect(object.attributes_for_default_mapping).not_to include :lock_version
|
30
13
|
end
|
31
14
|
|
32
|
-
it '
|
33
|
-
|
34
|
-
expect(
|
15
|
+
it 'uses "only" attributes for default mapping' do
|
16
|
+
object = subject.new(User, only: :id)
|
17
|
+
expect(object.attributes_for_default_mapping).to eq [:id]
|
35
18
|
end
|
36
19
|
|
37
|
-
|
38
|
-
|
39
|
-
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'instance methods' do
|
23
|
+
subject!{
|
24
|
+
ActiverecordToPoro::Converter.new(User, convert_associations: {roles: roles_converter, salutation: salutation_converter})
|
25
|
+
}
|
40
26
|
|
41
|
-
|
42
|
-
|
27
|
+
let(:roles_converter){
|
28
|
+
ActiverecordToPoro::Converter.new(Role)
|
29
|
+
}
|
30
|
+
|
31
|
+
let(:salutation_converter){
|
32
|
+
ActiverecordToPoro::Converter.new(Salutation)
|
33
|
+
}
|
34
|
+
|
35
|
+
let(:ar_object){
|
36
|
+
User.create!(name: 'my name', email: 'my_name@example.com').tap do |user|
|
37
|
+
user.roles.create!(name: 'admin')
|
38
|
+
user.roles.create!(name: 'guest')
|
39
|
+
end
|
40
|
+
}
|
41
|
+
|
42
|
+
let!(:loaded_poro_object){
|
43
43
|
subject.load(ar_object)
|
44
|
-
|
44
|
+
}
|
45
45
|
|
46
|
-
|
46
|
+
describe '#load' do
|
47
|
+
it 'creates a poro' do
|
48
|
+
expect(subject.load(ar_object)).not_to be_kind_of ActiveRecord::Base
|
49
|
+
end
|
47
50
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
51
|
+
it 'sets metadata for loaded objects' do
|
52
|
+
expect(loaded_poro_object._metadata).not_to be_nil
|
53
|
+
expect(loaded_poro_object._metadata.primary_key_value).to eq ar_object.id
|
54
|
+
end
|
52
55
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
+
it 'converts also associated objects' do
|
57
|
+
expect(subject.load(ar_object).roles.size).to eq 2
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'lazy loads associated objects' do
|
61
|
+
expect(ar_object).not_to receive :roles
|
62
|
+
subject.load(ar_object)
|
63
|
+
end
|
56
64
|
|
57
|
-
it 'sets new_record to false when it is an existing record' do
|
58
|
-
expect(subject.dump(loaded_poro_object).new_record?).to be_falsy
|
59
65
|
end
|
60
66
|
|
61
|
-
|
62
|
-
|
67
|
+
describe '#dump' do
|
68
|
+
it 'creates an ActiveRecordObject' do
|
69
|
+
expect(subject.dump(loaded_poro_object)).to be_kind_of ActiveRecord::Base
|
70
|
+
end
|
63
71
|
|
64
|
-
|
72
|
+
it 'sets the primary key when it existed before' do
|
73
|
+
expect(subject.dump(loaded_poro_object).id).to eq ar_object.id
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'sets new_record to false when it is an existing record' do
|
77
|
+
expect(subject.dump(loaded_poro_object).new_record?).to be_falsy
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'converts also associated objects' do
|
81
|
+
count_roles = ar_object.roles.size
|
82
|
+
|
83
|
+
expect(subject.dump(loaded_poro_object).roles.size).to eq count_roles
|
84
|
+
end
|
65
85
|
end
|
86
|
+
|
66
87
|
end
|
67
88
|
end
|
@@ -6,13 +6,13 @@ describe ActiverecordToPoro::DefaultPoroClassBuilder do
|
|
6
6
|
}
|
7
7
|
|
8
8
|
let(:expected_poro_class){
|
9
|
-
Yaoc::Helper::StructHE(:name, :email, :roles, :salutation)
|
9
|
+
Yaoc::Helper::StructHE(:name, :email, :roles, :salutation, :address, :permissions, :lock_version)
|
10
10
|
}
|
11
11
|
|
12
12
|
describe "#call" do
|
13
13
|
|
14
14
|
it "creates a poro class for an ActiveRecord class" do
|
15
|
-
expect(subject.call.members).to eq expected_poro_class.members
|
15
|
+
expect(subject.call.members.sort).to eq expected_poro_class.members.sort
|
16
16
|
end
|
17
17
|
|
18
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord_to_poro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dieter Späth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: abstract_type
|
@@ -280,6 +280,7 @@ files:
|
|
280
280
|
- lib/activerecord_to_poro.rb
|
281
281
|
- lib/activerecord_to_poro/converter.rb
|
282
282
|
- lib/activerecord_to_poro/default_poro_class_builder.rb
|
283
|
+
- lib/activerecord_to_poro/mapper_extension.rb
|
283
284
|
- lib/activerecord_to_poro/metadata.rb
|
284
285
|
- lib/activerecord_to_poro/metadata_enabled.rb
|
285
286
|
- lib/activerecord_to_poro/version.rb
|
@@ -288,6 +289,8 @@ files:
|
|
288
289
|
- spec/acceptance_spec_helper.rb
|
289
290
|
- spec/ar_spec_helper.rb
|
290
291
|
- spec/ar_support/db/migrate/20140127093447_create_my_ar_class.rb
|
292
|
+
- spec/ar_support/models/address.rb
|
293
|
+
- spec/ar_support/models/permission.rb
|
291
294
|
- spec/ar_support/models/role.rb
|
292
295
|
- spec/ar_support/models/salutation.rb
|
293
296
|
- spec/ar_support/models/user.rb
|
@@ -328,6 +331,8 @@ test_files:
|
|
328
331
|
- spec/acceptance_spec_helper.rb
|
329
332
|
- spec/ar_spec_helper.rb
|
330
333
|
- spec/ar_support/db/migrate/20140127093447_create_my_ar_class.rb
|
334
|
+
- spec/ar_support/models/address.rb
|
335
|
+
- spec/ar_support/models/permission.rb
|
331
336
|
- spec/ar_support/models/role.rb
|
332
337
|
- spec/ar_support/models/salutation.rb
|
333
338
|
- spec/ar_support/models/user.rb
|