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.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -0
  3. data/README.rdoc +32 -35
  4. data/Rakefile +13 -27
  5. data/app/models/uuids/uuid.rb +1 -0
  6. data/bin/uuids +13 -0
  7. data/lib/uuids/base/has_uuids.rb +3 -3
  8. data/lib/uuids/generators/install/migration.rb +62 -0
  9. data/lib/uuids/generators/install.rb +84 -0
  10. data/lib/uuids/version.rb +1 -1
  11. data/lib/uuids.rb +7 -5
  12. data/spec/dummy/Rakefile +17 -4
  13. data/spec/dummy/config/database.yml +8 -24
  14. data/spec/dummy/config/environment.rb +28 -4
  15. data/spec/dummy/config/initializers/application.rb +37 -0
  16. data/spec/dummy/config/initializers/dummy.rb +13 -0
  17. data/spec/dummy/config/initializers/seed_loader.rb +44 -0
  18. data/spec/dummy/db/test.sqlite3 +0 -0
  19. data/spec/dummy/lib/dummy.rb +16 -0
  20. data/spec/{dummy/spec/factories → factories}/records.rb +0 -0
  21. data/spec/{support/initializers → initializers}/database_cleaner.rb +0 -4
  22. data/spec/{support/initializers/factory_girl_rails.rb → initializers/factory_girl.rb} +1 -1
  23. data/spec/{support/initializers → initializers}/focus.rb +0 -0
  24. data/spec/{support/initializers → initializers}/garbage_collection.rb +0 -0
  25. data/spec/{support/initializers → initializers}/i18n.rb +1 -0
  26. data/spec/{support/initializers → initializers}/migrations.rb +0 -0
  27. data/spec/{support/initializers → initializers}/random_order.rb +0 -0
  28. data/spec/{support/initializers → initializers}/rspec.rb +0 -0
  29. data/spec/initializers/sandbox.rb +15 -0
  30. data/spec/spec_helper.rb +9 -9
  31. data/spec/support/capture.rb +16 -0
  32. data/spec/support/migrations.rb +16 -0
  33. data/spec/support/sandbox.rb +44 -0
  34. data/spec/uuids/cli/install_spec.rb +49 -0
  35. data/spec/{lib/uuids → uuids/lib}/base_spec.rb +0 -0
  36. data/spec/uuids/lib/generators/install/migration_spec.rb +41 -0
  37. data/spec/uuids/lib/generators/install_spec.rb +64 -0
  38. data/spec/{models/uuids → uuids/models}/uuid_spec.rb +0 -0
  39. metadata +96 -98
  40. data/lib/tasks/uuids/install.rake +0 -9
  41. data/lib/uuids/engine.rb +0 -12
  42. data/spec/dummy/README.rdoc +0 -28
  43. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  44. data/spec/dummy/app/assets/stylesheets/application.css +0 -15
  45. data/spec/dummy/app/controllers/application_controller.rb +0 -5
  46. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  47. data/spec/dummy/app/views/layouts/application.html.erb +0 -13
  48. data/spec/dummy/bin/bundle +0 -3
  49. data/spec/dummy/bin/rails +0 -4
  50. data/spec/dummy/bin/rake +0 -4
  51. data/spec/dummy/config/application.rb +0 -29
  52. data/spec/dummy/config/boot.rb +0 -5
  53. data/spec/dummy/config/environments/development.rb +0 -37
  54. data/spec/dummy/config/environments/production.rb +0 -78
  55. data/spec/dummy/config/environments/test.rb +0 -39
  56. data/spec/dummy/config/initializers/assets.rb +0 -8
  57. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  58. data/spec/dummy/config/initializers/cookies_serializer.rb +0 -3
  59. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  60. data/spec/dummy/config/initializers/inflections.rb +0 -16
  61. data/spec/dummy/config/initializers/mime_types.rb +0 -4
  62. data/spec/dummy/config/initializers/session_store.rb +0 -3
  63. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  64. data/spec/dummy/config/locales/en.yml +0 -23
  65. data/spec/dummy/config/routes.rb +0 -4
  66. data/spec/dummy/config/secrets.yml +0 -22
  67. data/spec/dummy/config.ru +0 -4
  68. data/spec/dummy/log/development.log +0 -0
  69. data/spec/dummy/log/test.log +0 -2962
  70. data/spec/dummy/public/404.html +0 -67
  71. data/spec/dummy/public/422.html +0 -67
  72. data/spec/dummy/public/500.html +0 -66
  73. data/spec/dummy/public/favicon.ico +0 -0
  74. data/spec/support/initializers/coveralls.rb +0 -4
  75. data/spec/support/initializers/rails.rb +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab2fee573db51b09e456f46051b38e4a4bb191a9
4
- data.tar.gz: 86a0819f977fb1866d60c286bffda28d090285c5
3
+ metadata.gz: 3d62f7dd03a435e8bba46acb72812918a4eba1b0
4
+ data.tar.gz: 3e94d4a3a0633b637b8c71a1bacec93f9d843f11
5
5
  SHA512:
6
- metadata.gz: 360f89779357fc89de36f625aac37ea2f8d16516e981f3428f92fe49574c43b11b2982769a02403630e9a18e1abf2f501d8c10e6d87330d819006a698757b2cc
7
- data.tar.gz: d88f464eeb0923ea24a0405f4aa4a45b4630beaea14ab14afdbff61817d29aaa53a80dd703418ee091c632e366ca7db10a52e43693dc77bc2d2e390ee7a06b1e
6
+ metadata.gz: 1d637e584a7da88119ded6750a33ebf69fc3acaf29292a4ed550eef08a3907ee71a35d41229c058eb97f28b29259de6db41e6eaa2662c46f7aada4fe981d95fa
7
+ data.tar.gz: 723ad1cd61231f1984fb4ef327052b195a86ab892b20fc81e3bc3da07c5d3f0bfaef793d44dd3c66db558473c55258ac1244d02e59cb4e30d6d6e5485d5b3743
data/.rubocop.yml CHANGED
@@ -3,6 +3,10 @@ AllCops:
3
3
  - 'spec/dummy/**/*'
4
4
  - 'Rakefile'
5
5
 
6
+ Lint/Eval:
7
+ Exclude:
8
+ - 'spec/**/*'
9
+
6
10
  Lint/HandleExceptions:
7
11
  Exclude:
8
12
  - 'spec/**/*'
data/README.rdoc CHANGED
@@ -9,29 +9,27 @@
9
9
 
10
10
  == About
11
11
 
12
- The gem adds addressing ActiveRecord objects by {UUID}[http://en.wikipedia.org/wiki/Universally_Unique_Identifier]s following the {RFC4122}[http://www.ietf.org/rfc/rfc4122.txt]
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 provides:
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
- This model allows adressing records by some UUID (not the ID).
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
- If necessary any referred record can be merged with another one.
29
- All you need is to reassign all UUIDs to merged record and outer associations
30
- remains valid.
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
- A **UUID** should be neither deleted, no changed. It can only be reassigned to
33
- another resource. On ther other hand, the outer record should be merged to
34
- another one (reassign UUIDs) to prevent existance of UUIDs assigned to nothing.
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
- To initialize the module you need to copy and run uuid's db migrations.
73
-
74
- === Rails application
65
+ After installation you should copy and run uuid's db migration into your module.
75
66
 
76
- Run from a command line in application root:
67
+ When you install the module to a final application, the migration should be
68
+ installed to `db/migrate` path directly.
77
69
 
78
- $ rake uuids:install
70
+ $ uuids install
79
71
 
80
- === Rails mountable engine
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
- Run from a command line in application root:
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
- $ rake app:uuids:install
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 is generated by default
101
+ # UUID generated by default:
107
102
  city = City.create!
108
103
  city.uuid.nil?
109
104
  # => false
110
105
 
111
- # UUID is assigned manually
112
- city = City.create! uuid: "6d9456a9-8f54-4ff7-ba0d-9854f1954417"
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
- The destruction of object is forbidden if it has a +uuid+. You should reassign
117
- all object's UUIDs to another object in advance.
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/setup"
2
+ require "bundler/gem_tasks"
3
3
  rescue LoadError
4
- puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
- end
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
- APP_RAKEFILE = File.expand_path "../spec/dummy/Rakefile", __FILE__
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
- RSpec::Core::RakeTask.new :spec
15
+ # Adds dummy:db tasks.
16
+ load "spec/dummy/Rakefile"
32
17
 
33
- task :default do
34
- system "rake db:migrate RAILS_ENV=test"
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 "rails_best_practices"
29
+ system "metric_fu"
44
30
  end
@@ -20,6 +20,7 @@ module Uuids
20
20
  # uuid.update_attributes! record: another_record
21
21
  #
22
22
  class Uuid < ActiveRecord::Base
23
+ self.table_name = :uuids_uuids
23
24
 
24
25
  # A format for UUID following RFC4122
25
26
  UUID_FORMAT = /[a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12}/
data/bin/uuids ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require "uuids"
3
+
4
+ # Runs the generator.
5
+ #
6
+ # @example
7
+ # $ uuids install
8
+ #
9
+ # To see additional options call
10
+ #
11
+ # $ uuids install -h
12
+ #
13
+ Uuids::Generators::Install.start
@@ -24,7 +24,7 @@ module Uuids
24
24
  # # => #<ActiveRecord::Relation ...>
25
25
  #
26
26
  # Params:
27
- # <tt>values</tt>:: a list of string uuids values to select records by.
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
- first_value = values.first
80
- list = first_value.is_a?(Array) ? first_value : values
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
@@ -2,5 +2,5 @@
2
2
  module Uuids
3
3
 
4
4
  # Current release.
5
- VERSION = "1.3.0"
5
+ VERSION = "1.4.0"
6
6
  end
data/lib/uuids.rb CHANGED
@@ -1,8 +1,10 @@
1
- require "rails"
1
+ # Dependencies
2
+ require "active_record"
3
+ require "i18n"
2
4
  require "securerandom"
3
- require "uuids/engine"
4
- require "uuids/base"
5
5
 
6
- # Namespace for a module
7
- module Uuids
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
- # Add your own tasks in files placed in lib/tasks ending in .rake,
2
- # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
1
+ # ==============================================================================
2
+ # Loads ActiveRecord tasks for dummy db.
3
+ # ==============================================================================
3
4
 
4
- require File.expand_path('../config/application', __FILE__)
5
+ require "active_record"
6
+ require "bundler/setup"
5
7
 
6
- Rails.application.load_tasks
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
- # SQLite version 3.x
2
- # gem install sqlite3
3
- #
4
- # Ensure the SQLite 3 gem is defined in your Gemfile
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
- <<: *default
21
- database: db/test.sqlite3
22
-
23
- production:
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
- # Load the Rails application.
2
- require File.expand_path('../application', __FILE__)
1
+ # ==============================================================================
2
+ # Configures database and establishes ActiveRecord connections.
3
+ # ==============================================================================
3
4
 
4
- # Initialize the Rails application.
5
- Rails.application.initialize!
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
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
@@ -1,4 +1,4 @@
1
- require "factory_girl_rails"
1
+ require "factory_girl"
2
2
 
3
3
  RSpec.configure do |config|
4
4
  config.include FactoryGirl::Syntax::Methods