minimapper-extras 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/README.md +27 -1
- data/lib/minimapper/extras/version.rb +1 -1
- data/lib/minimapper/factory_girl.rb +51 -0
- data/minimapper-extras.gemspec +2 -0
- data/spec/minimapper/factory_girl_spec.rb +71 -0
- metadata +33 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0ad3696155f47065eb0f5378f2029eba9320aa5
|
4
|
+
data.tar.gz: b0cb60bb92ed7458ac6a8a19a67f731bd8de730c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bae580892e3dafc93ddaa787285345ba2ae3bac3fa4d402d9c91d101d7f934fbb9ce5c5c65751e1947f50338aa8477c4d304a35e608b1800aff6a5a6fe1e4cca
|
7
|
+
data.tar.gz: 6e8361ebff43f1a1fae9edd035096e163b93a3f4e847caba91ad83c7cda2513836726b2e1509f12dc157386f64640cf170fb9c6f4aa556d82f2029f19b457d20
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
# Minimapper::Extras
|
5
5
|
|
6
|
-
Extra tools for minimapper.
|
6
|
+
Extra tools for [minimapper](https://github.com/joakimk/minimapper).
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -23,6 +23,32 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
For now, see the specs. TODO: Write docs.
|
25
25
|
|
26
|
+
## Custom FactoryGirl strategy
|
27
|
+
|
28
|
+
Add this to your spec_helper after loading FactoryGirl:
|
29
|
+
|
30
|
+
require "minimapper/factory_girl"
|
31
|
+
|
32
|
+
It changes the create strategy used by FactoryGirl to make it compatible with minimapper.
|
33
|
+
|
34
|
+
So far it only supports saving single entities and belongs_to associations.
|
35
|
+
|
36
|
+
It assumes you can access your mappers though something like "Repository.employees". You can override that by
|
37
|
+
changing CreateThroughRepositoryStrategy::Create#mapper_with_name.
|
38
|
+
|
39
|
+
It also assumes the model has accessors for related objects, we use the "minimapper/entity/belongs_to" to do this.
|
40
|
+
|
41
|
+
Example factory definition:
|
42
|
+
|
43
|
+
require "customer"
|
44
|
+
|
45
|
+
FactoryGirl.define do
|
46
|
+
factory :order do
|
47
|
+
customer { FactoryGirl.build(:customer) }
|
48
|
+
description "ref. 123"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
26
52
|
## Contributing
|
27
53
|
|
28
54
|
1. Fork it
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "attr_extras"
|
2
|
+
require "backports/1.9.1/kernel/public_send"
|
3
|
+
|
4
|
+
class CreateThroughRepositoryStrategy
|
5
|
+
def result(evaluation)
|
6
|
+
Create.new(evaluation.object).create
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
class Create
|
12
|
+
pattr_initialize :entity
|
13
|
+
|
14
|
+
def create
|
15
|
+
entity_class = entity.class
|
16
|
+
mapper_name = entity_class.name.underscore.pluralize
|
17
|
+
|
18
|
+
# Recursively creates all dependencies of this entity before
|
19
|
+
# going on to the the entity itself. This is here so that we can do
|
20
|
+
# "customer { FactoryGirl.build(:customer) }" in factories.
|
21
|
+
create_dependencies
|
22
|
+
|
23
|
+
if mapper_with_name(mapper_name).create(entity)
|
24
|
+
entity
|
25
|
+
else
|
26
|
+
errors = entity.errors.full_messages.join(", ")
|
27
|
+
raise "Can't create invalid record: #{errors}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Override this if you want to access the mappers some other way.
|
32
|
+
def mapper_with_name(name)
|
33
|
+
Repository.public_send(name)
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_dependencies
|
37
|
+
associated_entity_names.each do |name|
|
38
|
+
dependency_entity = self.class.new(entity.public_send(name)).create
|
39
|
+
entity.public_send("#{name}=", dependency_entity)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def associated_entity_names
|
44
|
+
entity.class.column_names.find_all { |name| name[-3..-1] == '_id' }.map do |belongs_to_id|
|
45
|
+
belongs_to_id[0...-3]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
FactoryGirl.register_strategy(:create, CreateThroughRepositoryStrategy)
|
data/minimapper-extras.gemspec
CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
20
|
spec.add_dependency "minimapper"
|
21
|
+
spec.add_dependency "attr_extras"
|
22
|
+
spec.add_dependency "backports"
|
21
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
24
|
spec.add_development_dependency "rake"
|
23
25
|
spec.add_development_dependency "rspec"
|
@@ -0,0 +1,71 @@
|
|
1
|
+
FactoryGirl = Class.new do
|
2
|
+
def self.register_strategy(name, klass)
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
Repository = Class.new
|
7
|
+
|
8
|
+
require "minimapper/factory_girl"
|
9
|
+
require "minimapper/entity"
|
10
|
+
require "minimapper/entity/belongs_to"
|
11
|
+
|
12
|
+
class Payment
|
13
|
+
include Minimapper::Entity
|
14
|
+
|
15
|
+
attribute :amount
|
16
|
+
end
|
17
|
+
|
18
|
+
class Order
|
19
|
+
include Minimapper::Entity
|
20
|
+
|
21
|
+
attribute :name
|
22
|
+
belongs_to :payment
|
23
|
+
end
|
24
|
+
|
25
|
+
class Location
|
26
|
+
include Minimapper::Entity
|
27
|
+
|
28
|
+
attribute :name
|
29
|
+
end
|
30
|
+
|
31
|
+
describe CreateThroughRepositoryStrategy do
|
32
|
+
let(:entity) { Location.new }
|
33
|
+
let(:location_mapper) { mock }
|
34
|
+
let(:evaluation) { mock(:object => entity) }
|
35
|
+
|
36
|
+
before do
|
37
|
+
Repository.stub(:locations).and_return(location_mapper)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "can create a single entity though a factory class" do
|
41
|
+
location_mapper.should_receive(:create).with(entity).and_return(true)
|
42
|
+
CreateThroughRepositoryStrategy.new.result(evaluation)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "raises if the entity isn't valid after create" do
|
46
|
+
entity.errors.add :base, "some error"
|
47
|
+
location_mapper.stub(:create => false)
|
48
|
+
lambda {
|
49
|
+
CreateThroughRepositoryStrategy.new.result(evaluation)
|
50
|
+
}.should raise_error("Can't create invalid record: some error")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe CreateThroughRepositoryStrategy, "when there are belongs_to associations" do
|
55
|
+
let(:payment_mapper) { mock }
|
56
|
+
let(:order_mapper) { mock }
|
57
|
+
|
58
|
+
before do
|
59
|
+
Repository.stub(:orders => order_mapper)
|
60
|
+
Repository.stub(:payments => payment_mapper)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "creates dependencies first" do
|
64
|
+
payment = Payment.new
|
65
|
+
order = Order.new(:payment => payment)
|
66
|
+
payment_mapper.should_receive(:create).ordered.with(payment).and_return(true)
|
67
|
+
order_mapper.should_receive(:create).ordered.with(order).and_return(true)
|
68
|
+
evaluation = mock(:object => order)
|
69
|
+
CreateThroughRepositoryStrategy.new.result(evaluation)
|
70
|
+
end
|
71
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimapper-extras
|
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
|
- Joakim Kolsjö
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minimapper
|
@@ -25,6 +25,34 @@ dependencies:
|
|
25
25
|
- - '>='
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: attr_extras
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: backports
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
28
56
|
- !ruby/object:Gem::Dependency
|
29
57
|
name: bundler
|
30
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,11 +128,13 @@ files:
|
|
100
128
|
- lib/minimapper/entity/serialized_association.rb
|
101
129
|
- lib/minimapper/extras.rb
|
102
130
|
- lib/minimapper/extras/version.rb
|
131
|
+
- lib/minimapper/factory_girl.rb
|
103
132
|
- minimapper-extras.gemspec
|
104
133
|
- script/ci
|
105
134
|
- script/turbux_rspec
|
106
135
|
- spec/minimapper/entity/belongs_to_spec.rb
|
107
136
|
- spec/minimapper/entity/serialized_association_spec.rb
|
137
|
+
- spec/minimapper/factory_girl_spec.rb
|
108
138
|
- spec/spec_helper.rb
|
109
139
|
homepage: ''
|
110
140
|
licenses:
|
@@ -133,4 +163,5 @@ summary: Extras for Minimapper.
|
|
133
163
|
test_files:
|
134
164
|
- spec/minimapper/entity/belongs_to_spec.rb
|
135
165
|
- spec/minimapper/entity/serialized_association_spec.rb
|
166
|
+
- spec/minimapper/factory_girl_spec.rb
|
136
167
|
- spec/spec_helper.rb
|