common_repository_model 0.0.2 → 0.0.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.
- data/lib/common_repository_model/area.rb +4 -0
- data/lib/common_repository_model/collection.rb +31 -3
- data/lib/common_repository_model/version.rb +1 -1
- data/spec/common_respository_model/area_spec.rb +8 -5
- data/spec/common_respository_model/collection_spec.rb +49 -30
- data/spec/common_respository_model/data_spec.rb +14 -11
- data/spec/common_respository_model/integration_spec.rb +3 -0
- data/spec/spec_helper.rb +8 -0
- metadata +1 -1
@@ -59,7 +59,10 @@ module CommonRepositoryModel
|
|
59
59
|
has_and_belongs_to_many(method_name, options)
|
60
60
|
end
|
61
61
|
|
62
|
-
|
62
|
+
before_save :register_parent_and_collections
|
63
|
+
def register_parent_and_collections
|
64
|
+
self.__area = CommonRepositoryModel::Area.find(area.pid) if area
|
65
|
+
|
63
66
|
collected_has_members = membership_registry.has_members.
|
64
67
|
collect do |association_name|
|
65
68
|
public_send(association_name)
|
@@ -73,11 +76,36 @@ module CommonRepositoryModel
|
|
73
76
|
|
74
77
|
self.parent_collections = collected_is_member_of
|
75
78
|
|
76
|
-
|
79
|
+
true
|
80
|
+
end
|
81
|
+
protected :register_parent_and_collections
|
82
|
+
|
83
|
+
def is_root?
|
84
|
+
parent_collections.size == 0
|
85
|
+
end
|
86
|
+
|
87
|
+
def parent_areas
|
88
|
+
parent_collections.collect(&:__area).uniq
|
89
|
+
end
|
90
|
+
|
91
|
+
def area=(an_area)
|
92
|
+
if is_root?
|
93
|
+
self.__area = an_area
|
94
|
+
else
|
95
|
+
self.__area = parent_areas.first
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def area
|
100
|
+
if is_root?
|
101
|
+
__area
|
102
|
+
else
|
103
|
+
parent_areas.first
|
104
|
+
end
|
77
105
|
end
|
78
106
|
|
79
107
|
belongs_to(
|
80
|
-
:
|
108
|
+
:__area,
|
81
109
|
class_name:'CommonRepositoryModel::Area',
|
82
110
|
property: :is_member_of_area
|
83
111
|
)
|
@@ -3,10 +3,8 @@ require 'common_repository_model/area'
|
|
3
3
|
require 'common_repository_model/collection'
|
4
4
|
|
5
5
|
describe CommonRepositoryModel::Area do
|
6
|
-
|
7
|
-
|
8
|
-
FactoryGirl.create(:area).persisted?.must_equal true
|
9
|
-
}
|
6
|
+
after(:each) do
|
7
|
+
subject.delete if subject.persisted?
|
10
8
|
end
|
11
9
|
|
12
10
|
describe 'without persisting' do
|
@@ -37,7 +35,12 @@ describe CommonRepositoryModel::Area do
|
|
37
35
|
|
38
36
|
describe 'integration (with persistence)' do
|
39
37
|
subject { FactoryGirl.create(:area) }
|
40
|
-
let(:collection) { FactoryGirl.build(:collection) }
|
38
|
+
let(:collection) { FactoryGirl.build(:collection, area: nil) }
|
39
|
+
it 'should find by name' do
|
40
|
+
CommonRepositoryModel::Area.find_by_name(subject.name).must_equal subject
|
41
|
+
CommonRepositoryModel::Area.
|
42
|
+
find_by_name("#{subject.name}-tmp").must_equal nil
|
43
|
+
end
|
41
44
|
it 'should save' do
|
42
45
|
# Before we can add a collection, the containing object
|
43
46
|
# must be saved
|
@@ -2,21 +2,15 @@ require_relative '../spec_helper'
|
|
2
2
|
require 'common_repository_model/collection'
|
3
3
|
|
4
4
|
describe CommonRepositoryModel::Collection do
|
5
|
-
|
6
5
|
subject { FactoryGirl.build(:collection) }
|
7
|
-
|
8
6
|
it 'should require an area' do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
subject.must_respond_to(:area)
|
17
|
-
subject.must_respond_to(:area=)
|
18
|
-
subject.must_respond_to(:area_id)
|
19
|
-
subject.must_respond_to(:area_id=)
|
7
|
+
with_persisted_area do |area|
|
8
|
+
subject.area = area
|
9
|
+
subject.area.must_be_kind_of(CommonRepositoryModel::Area)
|
10
|
+
subject.valid?.must_equal true
|
11
|
+
subject.area = nil
|
12
|
+
subject.valid?.must_equal false
|
13
|
+
end
|
20
14
|
end
|
21
15
|
|
22
16
|
describe '#find_or_build_data_for_given_slot_names' do
|
@@ -53,26 +47,51 @@ describe CommonRepositoryModel::Collection do
|
|
53
47
|
|
54
48
|
|
55
49
|
describe 'integration' do
|
56
|
-
let(:child_collection) { FactoryGirl.build(:collection) }
|
50
|
+
let(:child_collection) { FactoryGirl.build(:collection, area: nil) }
|
51
|
+
it 'should keep a child collection in the parent collection' do
|
52
|
+
with_persisted_area do |area_1|
|
53
|
+
with_persisted_area do |area_2|
|
54
|
+
area_1.wont_equal area_2
|
55
|
+
|
56
|
+
subject.area = area_1
|
57
|
+
subject.save!
|
58
|
+
|
59
|
+
child_collection.area = area_2
|
60
|
+
|
61
|
+
child_collection.parent_collections += [subject]
|
62
|
+
|
63
|
+
child_collection.area.must_equal subject.area
|
64
|
+
|
65
|
+
child_collection.save!
|
66
|
+
|
67
|
+
child_collection.area.must_equal subject.area
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
57
71
|
it 'should handle parent/child collection' do
|
58
72
|
# Before we can add a collection, the containing object
|
59
73
|
# must be saved
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
74
|
+
with_persisted_area do |area|
|
75
|
+
subject.area = area
|
76
|
+
child_collection.area = area
|
77
|
+
subject.save!
|
78
|
+
subject.child_collections << child_collection
|
79
|
+
subject.save!
|
80
|
+
|
81
|
+
@collection = subject.class.find(subject.pid)
|
82
|
+
@child_collection = child_collection.class.find(child_collection.pid)
|
83
|
+
|
84
|
+
# We shouldn't store any child relations
|
85
|
+
assert_rels_ext(@collection, :has_members, [])
|
86
|
+
assert_active_fedora_has_many(
|
87
|
+
@collection, :child_collections, [@child_collection]
|
88
|
+
)
|
89
|
+
|
90
|
+
assert_rels_ext(@child_collection, :is_member_of, [@collection])
|
91
|
+
assert_active_fedora_has_many(
|
92
|
+
@child_collection, :parent_collections, [@collection]
|
93
|
+
)
|
94
|
+
end
|
76
95
|
end
|
77
96
|
end
|
78
97
|
|
@@ -2,7 +2,6 @@ require_relative '../spec_helper'
|
|
2
2
|
require 'common_repository_model/data'
|
3
3
|
|
4
4
|
describe CommonRepositoryModel::Data do
|
5
|
-
|
6
5
|
subject { FactoryGirl.build(:data) }
|
7
6
|
|
8
7
|
describe 'integration' do
|
@@ -15,12 +14,12 @@ describe CommonRepositoryModel::Data do
|
|
15
14
|
it 'should have #slot_name' do
|
16
15
|
subject.must_respond_to :slot_name
|
17
16
|
subject.must_respond_to :slot_name=
|
18
|
-
|
17
|
+
end
|
19
18
|
|
20
19
|
it 'should have #md5_checksum' do
|
21
20
|
subject.must_respond_to :md5_checksum
|
22
21
|
subject.must_respond_to :md5_checksum=
|
23
|
-
|
22
|
+
end
|
24
23
|
|
25
24
|
it 'should have content versions' do
|
26
25
|
subject.content = file_1
|
@@ -33,16 +32,20 @@ describe CommonRepositoryModel::Data do
|
|
33
32
|
end
|
34
33
|
|
35
34
|
it 'should save' do
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
with_persisted_area do |area|
|
36
|
+
collection.area = area
|
37
|
+
# Before we can add a collection, the containing object
|
38
|
+
# must be saved
|
39
|
+
collection.save!
|
40
|
+
subject.collection = collection
|
41
|
+
subject.save!
|
42
|
+
|
43
|
+
@subject = subject.class.find(subject.pid)
|
41
44
|
|
42
|
-
|
45
|
+
assert_rels_ext(@subject, :is_part_of, [collection])
|
46
|
+
assert_active_fedora_belongs_to(@subject, :collection, collection)
|
43
47
|
|
44
|
-
|
45
|
-
assert_active_fedora_belongs_to(@subject, :collection, collection)
|
48
|
+
end
|
46
49
|
end
|
47
50
|
end
|
48
51
|
end
|
@@ -53,6 +53,9 @@ class Person < CommonRepositoryModel::Collection
|
|
53
53
|
end
|
54
54
|
|
55
55
|
describe CommonRepositoryModel::Collection do
|
56
|
+
after(:each) do
|
57
|
+
area.delete if area.persisted?
|
58
|
+
end
|
56
59
|
let(:area) { FactoryGirl.create(:area) }
|
57
60
|
let(:family) { Family.new(area: area) }
|
58
61
|
let(:lawyer) { Job.new(area: area) }
|
data/spec/spec_helper.rb
CHANGED
@@ -11,6 +11,14 @@ ActiveFedora.init(
|
|
11
11
|
|
12
12
|
|
13
13
|
class MiniTest::Unit::TestCase
|
14
|
+
|
15
|
+
def with_persisted_area
|
16
|
+
area = FactoryGirl.create(:area)
|
17
|
+
yield(area)
|
18
|
+
ensure
|
19
|
+
area.delete
|
20
|
+
end
|
21
|
+
|
14
22
|
def assert_rels_ext(subject, predicate, objects = [])
|
15
23
|
assert_equal objects.count, subject.relationships(predicate).count
|
16
24
|
objects.each do |object|
|