common_repository_model 0.0.6 → 0.0.7
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/common_repository_model.gemspec +3 -1
- data/config/README.md +5 -0
- data/config/solrconfig.xml +1858 -0
- data/lib/common_repository_model/area.rb +2 -1
- data/lib/common_repository_model/area_serializer.rb +4 -0
- data/lib/common_repository_model/collection.rb +10 -0
- data/lib/common_repository_model/collection_serializer.rb +4 -0
- data/lib/common_repository_model/data.rb +2 -2
- data/lib/common_repository_model/data_serializer.rb +5 -0
- data/lib/common_repository_model/persistence_base.rb +16 -0
- data/lib/common_repository_model/persistence_base_serializer.rb +6 -0
- data/lib/common_repository_model/railtie.rb +6 -1
- data/lib/common_repository_model/test_support.rb +19 -0
- data/lib/common_repository_model/version.rb +1 -1
- data/lib/generators/common_repository_model/collection/USAGE +7 -4
- data/lib/generators/common_repository_model/collection/collection_generator.rb +24 -1
- data/lib/generators/common_repository_model/collection/templates/collection_factory.rb.erb +13 -0
- data/lib/generators/common_repository_model/collection/templates/collection_serializer.rb.erb +9 -0
- data/lib/generators/common_repository_model/collection/templates/collection_serializer_spec.rb.erb +12 -0
- data/lib/generators/common_repository_model/collection/templates/collection_spec.rb.erb +6 -1
- data/spec/common_respository_model/area_serializer_spec.rb +13 -0
- data/spec/common_respository_model/area_spec.rb +2 -2
- data/spec/common_respository_model/collection_serializer_spec.rb +15 -0
- data/spec/common_respository_model/collection_spec.rb +17 -2
- data/spec/common_respository_model/data_serializer_spec.rb +15 -0
- data/spec/common_respository_model/data_spec.rb +7 -7
- data/spec/common_respository_model/integration_spec.rb +1 -1
- data/spec/common_respository_model/persistence_base_serializer_spec.rb +12 -0
- data/spec/common_respository_model/test_support_spec.rb +12 -0
- data/spec/factories/common_repository_model/area_factory.rb +8 -0
- data/spec/factories/common_repository_model/collection_factory.rb +7 -0
- data/spec/factories/common_repository_model/data_factory.rb +10 -0
- data/spec/spec_helper.rb +2 -8
- metadata +63 -7
- data/spec/factories/common_repository_model_factories.rb +0 -17
@@ -1,5 +1,6 @@
|
|
1
1
|
require_relative './persistence_base'
|
2
2
|
require_relative './collection'
|
3
|
+
require_relative './area_serializer'
|
3
4
|
module CommonRepositoryModel
|
4
5
|
class Area < PersistenceBase
|
5
6
|
|
@@ -15,7 +16,7 @@ module CommonRepositoryModel
|
|
15
16
|
|
16
17
|
validates :name, presence: true
|
17
18
|
|
18
|
-
|
19
|
+
delegate :name, to: 'properties', unique: true
|
19
20
|
|
20
21
|
def self.find_by_name(name)
|
21
22
|
find({'name_s' => name}).first
|
@@ -2,6 +2,7 @@ require_relative './persistence_base'
|
|
2
2
|
require_relative './area'
|
3
3
|
require_relative './data'
|
4
4
|
require 'set'
|
5
|
+
require 'morphine'
|
5
6
|
|
6
7
|
class PersistedValidator < ActiveModel::EachValidator
|
7
8
|
def validate_each(record, attribute, value)
|
@@ -104,6 +105,15 @@ module CommonRepositoryModel
|
|
104
105
|
end
|
105
106
|
end
|
106
107
|
|
108
|
+
def area_name
|
109
|
+
area.name
|
110
|
+
end
|
111
|
+
|
112
|
+
include Morphine
|
113
|
+
register :named_area_finder do
|
114
|
+
CommonRepositoryModel::Area.method(:find_by_name!)
|
115
|
+
end
|
116
|
+
|
107
117
|
belongs_to(
|
108
118
|
:__area,
|
109
119
|
class_name:'CommonRepositoryModel::Area',
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require_relative './persistence_base'
|
2
2
|
require_relative './collection'
|
3
3
|
require_relative './file_datastream'
|
4
|
+
require_relative './data_serializer'
|
4
5
|
|
5
6
|
class CommonRepositoryModel::Data < CommonRepositoryModel::PersistenceBase
|
6
7
|
belongs_to(
|
@@ -18,8 +19,7 @@ class CommonRepositoryModel::Data < CommonRepositoryModel::PersistenceBase
|
|
18
19
|
m.field :slot_name, :string
|
19
20
|
m.field :md5_checksum, :string
|
20
21
|
end
|
21
|
-
|
22
|
-
delegate :md5_checksum, unique: true, to: :properties
|
22
|
+
delegate_to :properties, [:slot_name, :md5_checksum], unique: true
|
23
23
|
validates :slot_name, presence: true
|
24
24
|
|
25
25
|
def content=(file)
|
@@ -1,8 +1,24 @@
|
|
1
1
|
require 'active_fedora'
|
2
|
+
require 'active_model_serializers'
|
3
|
+
require_relative './persistence_base_serializer'
|
2
4
|
module CommonRepositoryModel
|
3
5
|
class ObjectNotFoundError < ActiveFedora::ObjectNotFoundError
|
4
6
|
end
|
5
7
|
|
6
8
|
class PersistenceBase < ActiveFedora::Base
|
9
|
+
include ActiveModel::SerializerSupport
|
10
|
+
def active_model_serializer
|
11
|
+
"#{self.class}Serializer".constantize
|
12
|
+
end
|
13
|
+
|
14
|
+
class_attribute :attributes_for_json
|
15
|
+
self.attributes_for_json = []
|
16
|
+
|
17
|
+
def self.register_attribute(attribute_name, options = {})
|
18
|
+
delegate(attribute_name, options)
|
19
|
+
self.attributes_for_json ||= []
|
20
|
+
self.attributes_for_json += [attribute_name]
|
21
|
+
end
|
22
|
+
|
7
23
|
end
|
8
24
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CommonRepositoryModel
|
2
|
+
module TestSupport
|
3
|
+
def with_persisted_area(name = nil)
|
4
|
+
options = {}
|
5
|
+
options[:name] = name if name
|
6
|
+
area = FactoryGirl.create(:common_repository_model_area, options)
|
7
|
+
yield(area)
|
8
|
+
ensure
|
9
|
+
area.delete if area
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
require 'factory_girl'
|
14
|
+
['collection','area','data'].each do |name|
|
15
|
+
begin
|
16
|
+
require_relative "../../spec/factories/common_repository_model/#{name}_factory"
|
17
|
+
rescue FactoryGirl::DuplicateDefinitionError
|
18
|
+
end
|
19
|
+
end
|
@@ -1,9 +1,12 @@
|
|
1
1
|
Description:
|
2
|
-
This generator provides a means of creating a
|
2
|
+
This generator provides a means of creating a collection
|
3
3
|
|
4
4
|
Example:
|
5
|
-
rails generate
|
5
|
+
rails generate common_repository_model:collection ChickenPie
|
6
6
|
|
7
7
|
This will create:
|
8
|
-
app/
|
9
|
-
|
8
|
+
app/repository_models/chicken_pie.rb
|
9
|
+
app/repository_serializers/chicken_pie_serializer.rb
|
10
|
+
spec/repository_models/chicken_pie_spec.rb
|
11
|
+
spec/repository_serializers/chicken_pie_serializer.rb
|
12
|
+
spec/factories/chicken_pie_factory.rb
|
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'rails/generators'
|
2
|
-
|
2
|
+
|
3
|
+
class CommonRepositoryModel::CollectionGenerator < Rails::Generators::NamedBase
|
3
4
|
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
6
|
+
class_option :test_dir, :type => :string, :default => "spec/factories", :desc => "The directory where the factories should go"
|
4
7
|
|
5
8
|
def create_collection
|
6
9
|
template(
|
@@ -8,10 +11,30 @@ class CommonRepositoryModel::Collection < Rails::Generators::NamedBase
|
|
8
11
|
File.join('app/repository_models/', "#{file_name}.rb")
|
9
12
|
)
|
10
13
|
end
|
14
|
+
|
11
15
|
def create_service_spec
|
12
16
|
template(
|
13
17
|
'collection_spec.rb.erb',
|
14
18
|
File.join('spec/repository_models/', "#{file_name}_spec.rb")
|
15
19
|
)
|
16
20
|
end
|
21
|
+
|
22
|
+
def create_fixture_file
|
23
|
+
template 'collection_factory.rb.erb', File.join(options[:test_dir], "#{file_name}_factory.rb")
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_serializer
|
27
|
+
template(
|
28
|
+
'collection_serializer.rb.erb',
|
29
|
+
File.join('app/repository_serializers/', "#{file_name}_serializer.rb")
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_serializer_spec
|
34
|
+
template(
|
35
|
+
'collection_serializer_spec.rb.erb',
|
36
|
+
File.join('spec/repository_serializers/', "#{file_name}_serializer_spec.rb")
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
17
40
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Generated via
|
2
|
+
# `rails generate common_repository_model::collection <%= class_name %>`
|
3
|
+
#
|
4
|
+
# Read about factories at https://github.com/thoughtbot/factory_girl
|
5
|
+
|
6
|
+
require 'common_repository_model/test_support'
|
7
|
+
FactoryGirl.define do
|
8
|
+
factory :<%= singular_table_name %>, class: <%= class_name %> do
|
9
|
+
<% for attribute in attributes -%>
|
10
|
+
<%= attribute.name %> <%= attribute.default.inspect %>
|
11
|
+
<% end -%>
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# Generated via
|
2
|
+
# `rails generate common_repository_model::collection <%= class_name %>`
|
3
|
+
|
4
|
+
require 'common_repository_model/collection_serializer'
|
5
|
+
class <%= class_name %>Serializer < CommonRepositoryModel::CollectionSerializer
|
6
|
+
# You already will have the area (PID); See corresponding spec
|
7
|
+
#
|
8
|
+
# https://github.com/rails-api/active_model_serializers
|
9
|
+
end
|
data/lib/generators/common_repository_model/collection/templates/collection_serializer_spec.rb.erb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe <%= class_name %>Serializer do
|
4
|
+
subject { <%= class_name %>Serializer.new(<%= singular_table_name %>) }
|
5
|
+
let(:<%= singular_table_name %>) { FactoryGirl.build(:<%= singular_table_name %>) }
|
6
|
+
let(:json) { JSON.parse(subject.to_json) }
|
7
|
+
let(:root) { json.fetch('<%= singular_table_name %>') }
|
8
|
+
it 'should be JSON' do
|
9
|
+
root.fetch('pid')
|
10
|
+
root.fetch('area').should == <%= singular_table_name %>.area.pid
|
11
|
+
end
|
12
|
+
end
|
@@ -1,4 +1,9 @@
|
|
1
|
-
|
1
|
+
# Generated via
|
2
|
+
# `rails generate common_repository_model::collection <%= class_name %>`
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'common_repository_model/test_support'
|
2
5
|
|
3
6
|
describe <%= class_name %> do
|
7
|
+
include CommonRepositoryModel::TestSupport
|
8
|
+
subject { FactoryGirl.build(:<% singular_table_name %>) }
|
4
9
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'common_repository_model/area_serializer'
|
3
|
+
|
4
|
+
describe CommonRepositoryModel::AreaSerializer do
|
5
|
+
subject { CommonRepositoryModel::AreaSerializer.new(area) }
|
6
|
+
let(:area) { CommonRepositoryModel::Area.new(name: 'hello') }
|
7
|
+
let(:json) { JSON.parse(subject.to_json) }
|
8
|
+
let(:root) { json.fetch('area') }
|
9
|
+
it 'should be JSON' do
|
10
|
+
root.fetch('pid')
|
11
|
+
root.fetch('name').must_equal area.name
|
12
|
+
end
|
13
|
+
end
|
@@ -4,7 +4,7 @@ require 'common_repository_model/collection'
|
|
4
4
|
|
5
5
|
describe CommonRepositoryModel::Area do
|
6
6
|
describe 'without persisting' do
|
7
|
-
subject { FactoryGirl.build(:
|
7
|
+
subject { FactoryGirl.build(:common_repository_model_area, name: name) }
|
8
8
|
let(:name) { 'My Area Name'}
|
9
9
|
|
10
10
|
it 'should find or create by #name' do
|
@@ -30,7 +30,7 @@ describe CommonRepositoryModel::Area do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
describe 'integration (with persistence)' do
|
33
|
-
let(:collection) { FactoryGirl.build(:
|
33
|
+
let(:collection) { FactoryGirl.build(:common_repository_model_collection, area: nil) }
|
34
34
|
it 'should .find_by_name and .find_by_name!' do
|
35
35
|
with_persisted_area do |area|
|
36
36
|
CommonRepositoryModel::Area.find_by_name(area.name).must_equal area
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'common_repository_model/collection_serializer'
|
3
|
+
|
4
|
+
describe CommonRepositoryModel::CollectionSerializer do
|
5
|
+
subject { CommonRepositoryModel::CollectionSerializer.new(collection) }
|
6
|
+
let(:collection) {
|
7
|
+
FactoryGirl.build(:common_repository_model_collection)
|
8
|
+
}
|
9
|
+
let(:json) { JSON.parse(subject.to_json) }
|
10
|
+
let(:root) { json.fetch('collection') }
|
11
|
+
it 'should be JSON' do
|
12
|
+
root.fetch('pid')
|
13
|
+
root.fetch('area').must_equal collection.area.pid
|
14
|
+
end
|
15
|
+
end
|
@@ -2,9 +2,22 @@ require_relative '../spec_helper'
|
|
2
2
|
require 'common_repository_model/collection'
|
3
3
|
|
4
4
|
describe CommonRepositoryModel::Collection do
|
5
|
-
subject { FactoryGirl.build(:
|
5
|
+
subject { FactoryGirl.build(:common_repository_model_collection) }
|
6
|
+
|
7
|
+
it 'should have an #area_name' do
|
8
|
+
subject.respond_to?(:area_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should have #named_area_finder' do
|
12
|
+
lambda {
|
13
|
+
subject.named_area_finder.call('NEVER-GONNA_EXIST')
|
14
|
+
}.must_raise CommonRepositoryModel::ObjectNotFoundError
|
15
|
+
end
|
16
|
+
|
6
17
|
it 'should require an area' do
|
7
18
|
with_persisted_area do |area|
|
19
|
+
subject.named_area_finder.call(area.name)
|
20
|
+
|
8
21
|
subject.area = area
|
9
22
|
subject.area.must_be_kind_of(CommonRepositoryModel::Area)
|
10
23
|
subject.valid?.must_equal true
|
@@ -47,7 +60,9 @@ describe CommonRepositoryModel::Collection do
|
|
47
60
|
|
48
61
|
|
49
62
|
describe 'integration' do
|
50
|
-
let(:child_collection) {
|
63
|
+
let(:child_collection) {
|
64
|
+
FactoryGirl.build(:common_repository_model_collection, area: nil)
|
65
|
+
}
|
51
66
|
it 'should keep a child collection in the parent collection' do
|
52
67
|
with_persisted_area do |area_1|
|
53
68
|
with_persisted_area do |area_2|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'common_repository_model/data_serializer'
|
3
|
+
|
4
|
+
describe CommonRepositoryModel::DataSerializer do
|
5
|
+
subject { CommonRepositoryModel::DataSerializer.new(data) }
|
6
|
+
let(:data) { FactoryGirl.build(:common_repository_model_data) }
|
7
|
+
let(:json) { JSON.parse(subject.to_json) }
|
8
|
+
let(:root) { json.fetch('data') }
|
9
|
+
it 'should be JSON' do
|
10
|
+
root.fetch('pid')
|
11
|
+
root.fetch('slot_name').must_equal data.slot_name
|
12
|
+
root.fetch('md5_checksum').must_equal data.md5_checksum
|
13
|
+
root.fetch('collection').must_equal data.collection.pid
|
14
|
+
end
|
15
|
+
end
|
@@ -2,10 +2,10 @@ require_relative '../spec_helper'
|
|
2
2
|
require 'common_repository_model/data'
|
3
3
|
|
4
4
|
describe CommonRepositoryModel::Data do
|
5
|
-
subject { FactoryGirl.build(:
|
5
|
+
subject { FactoryGirl.build(:common_repository_model_data) }
|
6
6
|
|
7
7
|
describe 'integration' do
|
8
|
-
let(:collection) { FactoryGirl.build(:
|
8
|
+
let(:collection) { FactoryGirl.build(:common_repository_model_collection) }
|
9
9
|
let(:file_1) { File.new(__FILE__) }
|
10
10
|
let(:file_2) {
|
11
11
|
File.new(File.join(File.dirname(__FILE__), '../spec_helper.rb'))
|
@@ -13,13 +13,13 @@ describe CommonRepositoryModel::Data do
|
|
13
13
|
|
14
14
|
it 'should have #slot_name' do
|
15
15
|
subject.must_respond_to :slot_name
|
16
|
-
subject.must_respond_to
|
17
|
-
|
16
|
+
subject.must_respond_to(:slot_name=)
|
17
|
+
end
|
18
18
|
|
19
19
|
it 'should have #md5_checksum' do
|
20
|
-
subject.must_respond_to :md5_checksum
|
21
|
-
subject.must_respond_to
|
22
|
-
|
20
|
+
subject.must_respond_to( :md5_checksum)
|
21
|
+
subject.must_respond_to(:md5_checksum=)
|
22
|
+
end
|
23
23
|
|
24
24
|
it 'should have content versions' do
|
25
25
|
subject.content = file_1
|
@@ -56,7 +56,7 @@ describe CommonRepositoryModel::Collection do
|
|
56
56
|
after(:each) do
|
57
57
|
area.delete if area.persisted?
|
58
58
|
end
|
59
|
-
let(:area) { FactoryGirl.create(:
|
59
|
+
let(:area) { FactoryGirl.create(:common_repository_model_area) }
|
60
60
|
let(:family) { Family.new(area: area) }
|
61
61
|
let(:lawyer) { Job.new(area: area) }
|
62
62
|
let(:doctor) { Job.new(area: area) }
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require 'common_repository_model/persistence_base_serializer'
|
3
|
+
|
4
|
+
describe CommonRepositoryModel::PersistenceBaseSerializer do
|
5
|
+
subject { CommonRepositoryModel::PersistenceBaseSerializer.new(base) }
|
6
|
+
let(:base) { CommonRepositoryModel::PersistenceBase.new }
|
7
|
+
let(:json) { JSON.parse(subject.to_json) }
|
8
|
+
let(:root) { json.fetch('persistence_base') }
|
9
|
+
it 'should be JSON with a PID' do
|
10
|
+
root.fetch('pid')
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# This is a spec that should not include the spec_helper. It is to be used by
|
2
|
+
# other applications.
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require_relative '../../lib/common_repository_model/test_support'
|
5
|
+
|
6
|
+
describe CommonRepositoryModel::TestSupport do
|
7
|
+
describe 'area' do
|
8
|
+
subject { FactoryGirl.build(:common_repository_model_area) }
|
9
|
+
it { subject.must_be_instance_of CommonRepositoryModel::Area }
|
10
|
+
it { subject.valid?.must_equal true }
|
11
|
+
end
|
12
|
+
end
|