uuids 1.3.0 → 1.4.0
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/.rubocop.yml +4 -0
- data/README.rdoc +32 -35
- data/Rakefile +13 -27
- data/app/models/uuids/uuid.rb +1 -0
- data/bin/uuids +13 -0
- data/lib/uuids/base/has_uuids.rb +3 -3
- data/lib/uuids/generators/install/migration.rb +62 -0
- data/lib/uuids/generators/install.rb +84 -0
- data/lib/uuids/version.rb +1 -1
- data/lib/uuids.rb +7 -5
- data/spec/dummy/Rakefile +17 -4
- data/spec/dummy/config/database.yml +8 -24
- data/spec/dummy/config/environment.rb +28 -4
- data/spec/dummy/config/initializers/application.rb +37 -0
- data/spec/dummy/config/initializers/dummy.rb +13 -0
- data/spec/dummy/config/initializers/seed_loader.rb +44 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/lib/dummy.rb +16 -0
- data/spec/{dummy/spec/factories → factories}/records.rb +0 -0
- data/spec/{support/initializers → initializers}/database_cleaner.rb +0 -4
- data/spec/{support/initializers/factory_girl_rails.rb → initializers/factory_girl.rb} +1 -1
- data/spec/{support/initializers → initializers}/focus.rb +0 -0
- data/spec/{support/initializers → initializers}/garbage_collection.rb +0 -0
- data/spec/{support/initializers → initializers}/i18n.rb +1 -0
- data/spec/{support/initializers → initializers}/migrations.rb +0 -0
- data/spec/{support/initializers → initializers}/random_order.rb +0 -0
- data/spec/{support/initializers → initializers}/rspec.rb +0 -0
- data/spec/initializers/sandbox.rb +15 -0
- data/spec/spec_helper.rb +9 -9
- data/spec/support/capture.rb +16 -0
- data/spec/support/migrations.rb +16 -0
- data/spec/support/sandbox.rb +44 -0
- data/spec/uuids/cli/install_spec.rb +49 -0
- data/spec/{lib/uuids → uuids/lib}/base_spec.rb +0 -0
- data/spec/uuids/lib/generators/install/migration_spec.rb +41 -0
- data/spec/uuids/lib/generators/install_spec.rb +64 -0
- data/spec/{models/uuids → uuids/models}/uuid_spec.rb +0 -0
- metadata +96 -98
- data/lib/tasks/uuids/install.rake +0 -9
- data/lib/uuids/engine.rb +0 -12
- data/spec/dummy/README.rdoc +0 -28
- data/spec/dummy/app/assets/javascripts/application.js +0 -13
- data/spec/dummy/app/assets/stylesheets/application.css +0 -15
- data/spec/dummy/app/controllers/application_controller.rb +0 -5
- data/spec/dummy/app/helpers/application_helper.rb +0 -2
- data/spec/dummy/app/views/layouts/application.html.erb +0 -13
- data/spec/dummy/bin/bundle +0 -3
- data/spec/dummy/bin/rails +0 -4
- data/spec/dummy/bin/rake +0 -4
- data/spec/dummy/config/application.rb +0 -29
- data/spec/dummy/config/boot.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -37
- data/spec/dummy/config/environments/production.rb +0 -78
- data/spec/dummy/config/environments/test.rb +0 -39
- data/spec/dummy/config/initializers/assets.rb +0 -8
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/cookies_serializer.rb +0 -3
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/dummy/config/initializers/inflections.rb +0 -16
- data/spec/dummy/config/initializers/mime_types.rb +0 -4
- data/spec/dummy/config/initializers/session_store.rb +0 -3
- data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/spec/dummy/config/locales/en.yml +0 -23
- data/spec/dummy/config/routes.rb +0 -4
- data/spec/dummy/config/secrets.yml +0 -22
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/log/development.log +0 -0
- data/spec/dummy/log/test.log +0 -2962
- data/spec/dummy/public/404.html +0 -67
- data/spec/dummy/public/422.html +0 -67
- data/spec/dummy/public/500.html +0 -66
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/support/initializers/coveralls.rb +0 -4
- data/spec/support/initializers/rails.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d62f7dd03a435e8bba46acb72812918a4eba1b0
|
4
|
+
data.tar.gz: 3e94d4a3a0633b637b8c71a1bacec93f9d843f11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d637e584a7da88119ded6750a33ebf69fc3acaf29292a4ed550eef08a3907ee71a35d41229c058eb97f28b29259de6db41e6eaa2662c46f7aada4fe981d95fa
|
7
|
+
data.tar.gz: 723ad1cd61231f1984fb4ef327052b195a86ab892b20fc81e3bc3da07c5d3f0bfaef793d44dd3c66db558473c55258ac1244d02e59cb4e30d6d6e5485d5b3743
|
data/.rubocop.yml
CHANGED
data/README.rdoc
CHANGED
@@ -9,29 +9,27 @@
|
|
9
9
|
|
10
10
|
== About
|
11
11
|
|
12
|
-
The gem
|
12
|
+
The gem allows addressing ActiveRecord objects by {UUID}[http://en.wikipedia.org/wiki/Universally_Unique_Identifier]s following the {RFC4122}[http://www.ietf.org/rfc/rfc4122.txt]
|
13
13
|
standard.
|
14
14
|
|
15
|
-
It
|
16
|
-
|
17
|
-
<tt>Uuids::Uuid</tt>:: A model of UUIDs associated with various records.
|
18
|
-
<tt>Uuids::Base</tt>:: A module provides ActiveRecord class helpers +has_uuids+ and +has_many+ (with <tt>uuid: true</tt> option).
|
19
|
-
|
20
|
-
The gem is a {mountable Rails engine}[http://guides.rubyonrails.org/engines.html#generating-an-engine].
|
15
|
+
The gem requires Ruby 2.1+ and ActiveRecord 3.1+. It doesn't depend on full
|
16
|
+
stack Rails and can be used in domain apps based on ActiveRecord.
|
21
17
|
|
22
18
|
=== Pattern
|
23
19
|
|
24
|
-
|
25
|
-
It is expected any UUID is assigned to one record, but a record can be
|
20
|
+
The module allows adressing records by UUID(s) instead of ID. A record can be
|
26
21
|
identified by many UUIDs.
|
27
22
|
|
28
|
-
|
29
|
-
|
30
|
-
|
23
|
+
This makes it possible to merge records without touching external to the
|
24
|
+
records.
|
25
|
+
|
26
|
+
For merging records it's sufficient to:
|
31
27
|
|
32
|
-
|
33
|
-
|
34
|
-
|
28
|
+
* reassign all their uuids to the united record.
|
29
|
+
* destroy old records, which no uuid refers to.
|
30
|
+
|
31
|
+
Whatever external models are referred by uuid to the deleted records, that
|
32
|
+
references remains valid and will lead to the united record.
|
35
33
|
|
36
34
|
=== Example
|
37
35
|
|
@@ -48,11 +46,6 @@ will authomatically lead to merged record via old UUID.
|
|
48
46
|
|
49
47
|
Now the model of cities should **know nothing about outer models** that use it.
|
50
48
|
|
51
|
-
=== Translations
|
52
|
-
|
53
|
-
Error messages are translated to English and Russian (see <tt>config/locales</tt>).
|
54
|
-
Translations to other languages are welcome.
|
55
|
-
|
56
49
|
== Installation
|
57
50
|
|
58
51
|
Add this line to your application's Gemfile:
|
@@ -69,25 +62,27 @@ Or install it yourself as:
|
|
69
62
|
|
70
63
|
== Initialization
|
71
64
|
|
72
|
-
|
73
|
-
|
74
|
-
=== Rails application
|
65
|
+
After installation you should copy and run uuid's db migration into your module.
|
75
66
|
|
76
|
-
|
67
|
+
When you install the module to a final application, the migration should be
|
68
|
+
installed to `db/migrate` path directly.
|
77
69
|
|
78
|
-
$
|
70
|
+
$ uuids install
|
79
71
|
|
80
|
-
|
72
|
+
When you install the module to another gem as a part of its environment,
|
73
|
+
the migration isn't a part of your project. It only needed for testing
|
74
|
+
your gem in a proper environment.
|
81
75
|
|
82
|
-
|
76
|
+
In this case the migration should be installed to a dummy app in a
|
77
|
+
`spec/dummy/db/migrate` folder. Do it with the `-d` key:
|
83
78
|
|
84
|
-
$
|
79
|
+
$ uuids install -d
|
85
80
|
|
86
81
|
== Usage
|
87
82
|
|
88
83
|
=== Adding UUIDs to models
|
89
84
|
|
90
|
-
Add the assotiation to your AR model:
|
85
|
+
Add the assotiation to your AR model with a `has_uuids` helper:
|
91
86
|
|
92
87
|
class City < ActiveRecord::Base
|
93
88
|
include Uuids::Base
|
@@ -97,30 +92,31 @@ Add the assotiation to your AR model:
|
|
97
92
|
This will add methods:
|
98
93
|
|
99
94
|
+#uuids+:: List of <tt>Uuids::Uuid</tt> objects referred to the record.
|
100
|
-
+#uuid+:: main UUID for the record - a value of the first +uuids+ (ordered by value).
|
101
95
|
<tt>#uuid=(value)</tt>:: assigns the UUID (an alias for <tt>#uuids.new value: value</tt>).
|
96
|
+
+#uuid+:: main UUID for the record - a value of the first +uuids+ (ordered by value).
|
102
97
|
<tt>.by_uuid(*values)</tt>:: A scope for selecting unique records by UUID.
|
103
98
|
|
104
99
|
The first uuid is added by default. It can also be set manually:
|
105
100
|
|
106
|
-
# UUID
|
101
|
+
# UUID generated by default:
|
107
102
|
city = City.create!
|
108
103
|
city.uuid.nil?
|
109
104
|
# => false
|
110
105
|
|
111
|
-
# UUID
|
112
|
-
city = City.create!
|
106
|
+
# UUID(s) assigned manually:
|
107
|
+
city = City.create! uuids: "6d9456a9-8f54-4ff7-ba0d-9854f1954417"
|
113
108
|
city.uuid
|
114
109
|
# => "6d9456a9-8f54-4ff7-ba0d-9854f1954417"
|
115
110
|
|
116
|
-
|
117
|
-
all object's UUIDs to another
|
111
|
+
Destruction of object is forbidden while it has a +uuid+. You should reassign
|
112
|
+
all object's UUIDs to another record in advance.
|
118
113
|
|
119
114
|
=== Referring model by UUID
|
120
115
|
|
121
116
|
Instead of <tt>ActiveRecord::Associations</tt> +belongs_to+, +has_one+ and
|
122
117
|
+has_many+, you should define custom methods explicitly.
|
123
118
|
|
119
|
+
# db/migrate/*_create_streets.rb
|
124
120
|
class CreateStreetsTable < ActiveRecord::Migration
|
125
121
|
def change
|
126
122
|
create_table :streets do |t|
|
@@ -130,6 +126,7 @@ Instead of <tt>ActiveRecord::Associations</tt> +belongs_to+, +has_one+ and
|
|
130
126
|
end
|
131
127
|
end
|
132
128
|
|
129
|
+
# app/models/street.rb
|
133
130
|
class Street < ActiveRecord::Base
|
134
131
|
|
135
132
|
scope :by_city, ->(city) { City.by_uuid(city.uuid) }
|
data/Rakefile
CHANGED
@@ -1,44 +1,30 @@
|
|
1
1
|
begin
|
2
|
-
require "bundler/
|
2
|
+
require "bundler/gem_tasks"
|
3
3
|
rescue LoadError
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
begin
|
8
|
-
require "rdoc/task"
|
9
|
-
rescue LoadError
|
10
|
-
require "rdoc/rdoc"
|
11
|
-
require "rake/rdoctask"
|
12
|
-
RDoc::Task = Rake::RDocTask
|
13
|
-
end
|
14
|
-
|
15
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
-
rdoc.rdoc_dir = "rdoc"
|
17
|
-
rdoc.title = "Uuids"
|
18
|
-
rdoc.options << "--line-numbers"
|
19
|
-
rdoc.rdoc_files.include "README.rdoc"
|
20
|
-
rdoc.rdoc_files.include "lib/**/*.rb"
|
4
|
+
system "gem install bundler"
|
5
|
+
system "bundle install"
|
21
6
|
end
|
22
7
|
|
8
|
+
# Adds bundler tasks
|
23
9
|
Bundler::GemHelper.install_tasks
|
24
10
|
|
25
|
-
|
26
|
-
load "rails/tasks/engine.rake"
|
27
|
-
|
28
|
-
require "bundler/gem_tasks"
|
11
|
+
# Adds RSpec tasks.
|
29
12
|
require "rspec/core/rake_task"
|
13
|
+
RSpec::Core::RakeTask.new(:spec)
|
30
14
|
|
31
|
-
|
15
|
+
# Adds dummy:db tasks.
|
16
|
+
load "spec/dummy/Rakefile"
|
32
17
|
|
33
|
-
|
34
|
-
|
18
|
+
# Declares gem's own tasks.
|
19
|
+
desc "Runs test suite."
|
20
|
+
task default: %w(dummy:db:migrate dummy:db:reset) do
|
35
21
|
system "bundle exec rspec spec"
|
36
22
|
end
|
37
23
|
|
24
|
+
desc "Runs code metrics."
|
38
25
|
task :check do
|
39
26
|
system "coveralls report"
|
40
27
|
system "rubocop"
|
41
|
-
system "metric_fu"
|
42
28
|
system "inch --pedantic"
|
43
|
-
system "
|
29
|
+
system "metric_fu"
|
44
30
|
end
|
data/app/models/uuids/uuid.rb
CHANGED
data/bin/uuids
ADDED
data/lib/uuids/base/has_uuids.rb
CHANGED
@@ -24,7 +24,7 @@ module Uuids
|
|
24
24
|
# # => #<ActiveRecord::Relation ...>
|
25
25
|
#
|
26
26
|
# Params:
|
27
|
-
# <tt
|
27
|
+
# <tt>*values</tt>:: a list of uuids to select records by.
|
28
28
|
#
|
29
29
|
# Returns the <tt>ActiveRecord::Relation</tt> object.
|
30
30
|
#
|
@@ -76,8 +76,8 @@ module Uuids
|
|
76
76
|
# <tt>values</tt>:: an array of uuids string values to assign.
|
77
77
|
#
|
78
78
|
def uuids=(*values)
|
79
|
-
|
80
|
-
list
|
79
|
+
first = values.first
|
80
|
+
list = first.is_a?(Array) ? first : values
|
81
81
|
list.each { |value| self.uuid = value }
|
82
82
|
end
|
83
83
|
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require_relative "../install"
|
3
|
+
|
4
|
+
module Uuids
|
5
|
+
module Generators
|
6
|
+
class Install
|
7
|
+
|
8
|
+
# Description for migration to copy.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# migration = Migration.new(
|
12
|
+
# "/uuids/db/migrate/20141016112506_create_uuids_uuids.rb"
|
13
|
+
# )
|
14
|
+
# migration.source
|
15
|
+
# # => "/uuids/db/migrate/20141016112506_create_uuids_uuids.rb"
|
16
|
+
# migration.target
|
17
|
+
# # => "20150101000001_create_uuids_uuids.uuids.rb"
|
18
|
+
# migration.content
|
19
|
+
# # => returns uuids content.
|
20
|
+
#
|
21
|
+
class Migration
|
22
|
+
|
23
|
+
# A source file for migration.
|
24
|
+
attr_reader :source
|
25
|
+
|
26
|
+
# Initializes the migration from given source file.
|
27
|
+
#
|
28
|
+
# @example
|
29
|
+
# migration = Migration.new(
|
30
|
+
# "/uuids/db/migrate/20141016112506_create_uuids_uuids.rb"
|
31
|
+
# )
|
32
|
+
#
|
33
|
+
# Params:
|
34
|
+
# +source+:: A file name to initalize a migration from.
|
35
|
+
#
|
36
|
+
def initialize(source)
|
37
|
+
@source = source
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns a target file name to copy the migration to.
|
41
|
+
def target
|
42
|
+
@target ||= "#{ timestamp }#{ basename }.uuids.rb"
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns the migration content .
|
46
|
+
def content
|
47
|
+
@content ||= File.read source
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def timestamp
|
53
|
+
@timestamp ||= Time.now.utc.strftime "%Y%m%d%H%M%S"
|
54
|
+
end
|
55
|
+
|
56
|
+
def basename
|
57
|
+
@basename ||= File.basename(source, ".rb")[14..-1]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "thor"
|
3
|
+
|
4
|
+
module Uuids
|
5
|
+
|
6
|
+
# A namespace for +Uuids+ thor-based generators.
|
7
|
+
module Generators
|
8
|
+
|
9
|
+
# An installer for the module. Copies module migration to external gem.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# installer = Uuids::Generators::Install.start [], { dummy: true }
|
13
|
+
# # => creates `spec/dummy/db/migrage/{timestamp}_uuids_uuids.uuids.rb
|
14
|
+
#
|
15
|
+
# Options:
|
16
|
+
# <tt>dummy:</tt> (optional, boolean, default: false)
|
17
|
+
# ... flag to copy migrations to a dummy app. If not set, the migration
|
18
|
+
# will be added to the root `db/migrate` folder.
|
19
|
+
#
|
20
|
+
# Adds the "uuids" gem's migration to a module that uses the gem.
|
21
|
+
#
|
22
|
+
class Install < Thor::Group
|
23
|
+
include Thor::Actions
|
24
|
+
|
25
|
+
class_option(
|
26
|
+
:dummy,
|
27
|
+
aliases: "-d",
|
28
|
+
default: false,
|
29
|
+
desc: "Copy migrations to dummy (`spec/dummy/db/migrate`) folder.",
|
30
|
+
required: false,
|
31
|
+
type: :boolean
|
32
|
+
)
|
33
|
+
|
34
|
+
class_option(
|
35
|
+
:folder,
|
36
|
+
aliases: "-f",
|
37
|
+
desc: "A folder to copy migrations to (`given_folder/db/migrate`).",
|
38
|
+
required: false,
|
39
|
+
type: :string
|
40
|
+
)
|
41
|
+
|
42
|
+
# Creates a folder for migrations.
|
43
|
+
def prepare_folder
|
44
|
+
empty_directory dir, skip: true
|
45
|
+
end
|
46
|
+
|
47
|
+
# Copies migrations to proper folder
|
48
|
+
def copy_migrations
|
49
|
+
migrations.each do |migration|
|
50
|
+
FileUtils.copy_file(
|
51
|
+
migration.source,
|
52
|
+
"#{ destination_root }/#{ dir }#{ migration.target }",
|
53
|
+
migration.content
|
54
|
+
)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def dir
|
61
|
+
@dir ||= "#{ folder || dummy || "" }db/migrate/"
|
62
|
+
end
|
63
|
+
|
64
|
+
def folder
|
65
|
+
@folder ||= begin
|
66
|
+
folder = options["folder"]
|
67
|
+
folder ? "#{ folder }/" : nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def dummy
|
72
|
+
@dummy ||= "spec/dummy/" if options["dummy"]
|
73
|
+
end
|
74
|
+
|
75
|
+
def sources
|
76
|
+
@sources ||= Dir[File.expand_path "../../db/migrate/*.rb"]
|
77
|
+
end
|
78
|
+
|
79
|
+
def migrations
|
80
|
+
@migrations ||= sources.map { |source| Migration.new source }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/uuids/version.rb
CHANGED
data/lib/uuids.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
# Dependencies
|
2
|
+
require "active_record"
|
3
|
+
require "i18n"
|
2
4
|
require "securerandom"
|
3
|
-
require "uuids/engine"
|
4
|
-
require "uuids/base"
|
5
5
|
|
6
|
-
#
|
7
|
-
|
6
|
+
# Loads gem content.
|
7
|
+
root = File.expand_path "../..", __FILE__
|
8
|
+
%w(lib app).each do |dir|
|
9
|
+
Dir[File.join(root, dir, "/**/*.rb")].each { |f| require f }
|
8
10
|
end
|
data/spec/dummy/Rakefile
CHANGED
@@ -1,6 +1,19 @@
|
|
1
|
-
#
|
2
|
-
#
|
1
|
+
# ==============================================================================
|
2
|
+
# Loads ActiveRecord tasks for dummy db.
|
3
|
+
# ==============================================================================
|
3
4
|
|
4
|
-
require
|
5
|
+
require "active_record"
|
6
|
+
require "bundler/setup"
|
5
7
|
|
6
|
-
|
8
|
+
namespace :dummy do
|
9
|
+
|
10
|
+
task :environment do
|
11
|
+
require_relative "config/environment.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
namespace :db do
|
15
|
+
task load_config: :environment
|
16
|
+
end
|
17
|
+
|
18
|
+
load "active_record/railties/databases.rake"
|
19
|
+
end
|
@@ -1,25 +1,9 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
#
|
4
|
-
|
5
|
-
# gem 'sqlite3'
|
6
|
-
#
|
7
|
-
default: &default
|
8
|
-
adapter: sqlite3
|
9
|
-
pool: 5
|
10
|
-
timeout: 5000
|
11
|
-
|
12
|
-
development:
|
13
|
-
<<: *default
|
14
|
-
database: db/development.sqlite3
|
15
|
-
|
16
|
-
# Warning: The database defined as "test" will be erased and
|
17
|
-
# re-generated from your development database when you run "rake".
|
18
|
-
# Do not set this db to the same as development or production.
|
1
|
+
# ==============================================================================
|
2
|
+
# ActiveRecord connections settings for corresponding environments.
|
3
|
+
# ==============================================================================
|
4
|
+
---
|
19
5
|
test:
|
20
|
-
|
21
|
-
database: db/test.sqlite3
|
22
|
-
|
23
|
-
|
24
|
-
<<: *default
|
25
|
-
database: db/production.sqlite3
|
6
|
+
adapter: :sqlite3
|
7
|
+
database: spec/dummy/db/test.sqlite3
|
8
|
+
pool: 5
|
9
|
+
timeout: 5000
|
@@ -1,5 +1,29 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# ==============================================================================
|
2
|
+
# Configures database and establishes ActiveRecord connections.
|
3
|
+
# ==============================================================================
|
3
4
|
|
4
|
-
#
|
5
|
-
|
5
|
+
# Loads all initializers including Dummy::Application and Dummy::SeedLoader.
|
6
|
+
Dir[File.expand_path("../initializers/*.rb", __FILE__)].each { |f| require f }
|
7
|
+
|
8
|
+
# Paths to dummy and application roots.
|
9
|
+
root = Dummy.root
|
10
|
+
app_root = Dummy.app_root
|
11
|
+
|
12
|
+
# Configures databases and establishes ActiveRecord connections.
|
13
|
+
Dummy::Application.configure do |config|
|
14
|
+
|
15
|
+
# Paths to database and migrations
|
16
|
+
database_yml = File.read(File.join root, "config/database.yml")
|
17
|
+
migrations = [root, app_root].map { |path| File.join(path, "db/migrate") }
|
18
|
+
|
19
|
+
# Config setting
|
20
|
+
config.database_configuration = YAML.load database_yml
|
21
|
+
config.db_dir = File.join(root, "db")
|
22
|
+
config.env = ENV["ENV"] || :test
|
23
|
+
config.fixtures_path = File.join(app_root, "spec/fixtures")
|
24
|
+
config.migrations_paths = migrations
|
25
|
+
config.root = root
|
26
|
+
config.seed_loader = Dummy::SeedLoader.new
|
27
|
+
|
28
|
+
# ActiveRecord connections established authomatically after yielding the block
|
29
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
module Dummy
|
4
|
+
|
5
|
+
# Storage for application settings.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# Dummy::Application.configure do |config|
|
9
|
+
# config.env = :test
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
class Application
|
13
|
+
class << self
|
14
|
+
|
15
|
+
# Configuration settings wrapper for the
|
16
|
+
# <tt>ActiveRecord::Tasks::DatabaseTasks</tt>.
|
17
|
+
#
|
18
|
+
# Establishes AR connection after configuration.
|
19
|
+
#
|
20
|
+
def configure
|
21
|
+
yield tasks
|
22
|
+
base.configurations = tasks.database_configuration
|
23
|
+
base.establish_connection(tasks.env)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def base
|
29
|
+
@base ||= ActiveRecord::Base
|
30
|
+
end
|
31
|
+
|
32
|
+
def tasks
|
33
|
+
@tasks ||= ActiveRecord::Tasks::DatabaseTasks
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Namespace for dummy module.
|
2
|
+
module Dummy
|
3
|
+
|
4
|
+
# Path to dummy root.
|
5
|
+
def self.root
|
6
|
+
@root ||= File.expand_path "../../..", __FILE__
|
7
|
+
end
|
8
|
+
|
9
|
+
# Path to application root.
|
10
|
+
def self.app_root
|
11
|
+
@app_root ||= File.expand_path "../..", root
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require_relative "dummy"
|
2
|
+
|
3
|
+
module Dummy
|
4
|
+
|
5
|
+
# Loads dummy application and then loads seeds from both root and dummy
|
6
|
+
# applications.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# SeedLoader.new.load_seed
|
10
|
+
#
|
11
|
+
class SeedLoader
|
12
|
+
|
13
|
+
# List of files to be loaded.
|
14
|
+
attr_accessor :seed_files
|
15
|
+
|
16
|
+
# Initializes the loader with files to be loaded.
|
17
|
+
def initialize
|
18
|
+
|
19
|
+
# Adds dummy application.
|
20
|
+
self.seed_files = [File.join(root, "lib/dummy.rb")]
|
21
|
+
|
22
|
+
# Adds dummy seed if exists.
|
23
|
+
self.seed_files += Dir[File.join(root, "db/seed.rb")]
|
24
|
+
|
25
|
+
# Adds root application seed if exists.
|
26
|
+
self.seed_files += Dir[File.join(app_root, "db/seed.rb")]
|
27
|
+
end
|
28
|
+
|
29
|
+
# Loads seed files.
|
30
|
+
def load_seed
|
31
|
+
seed_files.each { |file| require file }
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def root
|
37
|
+
@root ||= Dummy.root
|
38
|
+
end
|
39
|
+
|
40
|
+
def app_root
|
41
|
+
@app_root ||= Dummy.app_root
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# ==============================================================================
|
2
|
+
# Configures ActiveRecord and loads dummy with gem application.
|
3
|
+
# ==============================================================================
|
4
|
+
|
5
|
+
# Configures environment.
|
6
|
+
# Among others loads the +Dummy+ module from `config/initializers/dummy.rb`
|
7
|
+
require_relative "../config/environment"
|
8
|
+
|
9
|
+
# Loads lib folder content.
|
10
|
+
Dir[File.join(Dummy.root, "lib/**/*.rb")].each { |f| require f }
|
11
|
+
|
12
|
+
# Loads app folder content.
|
13
|
+
Dir[File.join(Dummy.root, "app/**/*.rb")].each { |f| require f }
|
14
|
+
|
15
|
+
# Loads application.
|
16
|
+
Dir[File.join(Dummy.app_root, "lib/*.rb")].each { |f| require f }
|
File without changes
|
@@ -1,11 +1,7 @@
|
|
1
1
|
require "database_cleaner"
|
2
|
-
require "rspec/rails"
|
3
2
|
|
4
3
|
RSpec.configure do |config|
|
5
4
|
|
6
|
-
# stop wrapping each of test examples within a transaction
|
7
|
-
config.use_transactional_fixtures = false
|
8
|
-
|
9
5
|
config.before :suite do
|
10
6
|
DatabaseCleaner.clean_with :truncation
|
11
7
|
end
|
File without changes
|
File without changes
|