with_model 2.1.0 → 2.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -13
- data/.jrubyrc +1 -0
- data/.rubocop.yml +20 -4
- data/.rubocop_todo.yml +93 -0
- data/.travis.yml +32 -31
- data/.yardopts +1 -0
- data/CHANGELOG.md +31 -0
- data/Gemfile +11 -4
- data/LICENSE +1 -1
- data/README.md +19 -9
- data/lib/with_model.rb +9 -1
- data/lib/with_model/constant_stubber.rb +5 -3
- data/lib/with_model/methods.rb +3 -0
- data/lib/with_model/model.rb +13 -4
- data/lib/with_model/model/dsl.rb +2 -1
- data/lib/with_model/table.rb +10 -2
- data/lib/with_model/version.rb +1 -1
- data/spec/.rubocop.yml +5 -0
- data/spec/constant_stubber_spec.rb +19 -0
- data/spec/readme_spec.rb +3 -4
- data/spec/spec_helper.rb +5 -5
- data/spec/with_model_spec.rb +38 -20
- data/with_model.gemspec +9 -4
- metadata +89 -21
- data/bin/rake +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a685dd2fcd809ce446e7dfde25b4fea90014e6bf5456d99e939f79b2ff3b18af
|
4
|
+
data.tar.gz: 3966fbcbeae1bd24fbeacf1f4c53b67abd78b771e03de3f6263a989ad8e180ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 659fff704f44481540e91d6ebe00322dd07fec0beda3142bc2581ed02bc962b7384dacf871a43c5231cc93757a428ef857f2ea093e8b82fb0ee0b9603932793f
|
7
|
+
data.tar.gz: 588cb797971b7dc861aa207a09cbddebf69c356878993d0edf98458afb83affb0cc2d5d26856a079e8bef4ae5d420698e9f65abcb636ee8990ab6178479467d2
|
data/.gitignore
CHANGED
data/.jrubyrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
debug.fullTrace=true
|
data/.rubocop.yml
CHANGED
@@ -1,6 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
require:
|
4
|
+
- rubocop-rake
|
5
|
+
- rubocop-rspec
|
4
6
|
|
5
7
|
AllCops:
|
6
|
-
|
8
|
+
NewCops: enable
|
9
|
+
|
10
|
+
Metrics/BlockLength:
|
11
|
+
Exclude:
|
12
|
+
- 'spec/**/*'
|
13
|
+
- 'with_model.gemspec'
|
14
|
+
|
15
|
+
Bundler/OrderedGems:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Bundler/DuplicatedGem:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Style/Documentation:
|
22
|
+
Enabled: false
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2020-11-22 01:30:26 UTC using RuboCop version 1.3.1.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 4
|
10
|
+
RSpec/Be:
|
11
|
+
Exclude:
|
12
|
+
- 'spec/readme_spec.rb'
|
13
|
+
- 'spec/with_model_spec.rb'
|
14
|
+
|
15
|
+
# Offense count: 3
|
16
|
+
RSpec/BeforeAfterAll:
|
17
|
+
Exclude:
|
18
|
+
- 'spec/spec_helper.rb'
|
19
|
+
- 'spec/rails_helper.rb'
|
20
|
+
- 'spec/support/**/*.rb'
|
21
|
+
- 'spec/readme_spec.rb'
|
22
|
+
- 'spec/with_model_spec.rb'
|
23
|
+
|
24
|
+
# Offense count: 6
|
25
|
+
# Configuration parameters: Prefixes.
|
26
|
+
# Prefixes: when, with, without
|
27
|
+
RSpec/ContextWording:
|
28
|
+
Exclude:
|
29
|
+
- 'spec/active_record_behaviors_spec.rb'
|
30
|
+
- 'spec/with_model_spec.rb'
|
31
|
+
|
32
|
+
# Offense count: 6
|
33
|
+
# Configuration parameters: IgnoredMetadata.
|
34
|
+
RSpec/DescribeClass:
|
35
|
+
Exclude:
|
36
|
+
- 'spec/active_record_behaviors_spec.rb'
|
37
|
+
- 'spec/readme_spec.rb'
|
38
|
+
- 'spec/with_model_spec.rb'
|
39
|
+
|
40
|
+
# Offense count: 5
|
41
|
+
# Configuration parameters: Max.
|
42
|
+
RSpec/ExampleLength:
|
43
|
+
Exclude:
|
44
|
+
- 'spec/active_record_behaviors_spec.rb'
|
45
|
+
- 'spec/readme_spec.rb'
|
46
|
+
- 'spec/with_model_spec.rb'
|
47
|
+
|
48
|
+
# Offense count: 7
|
49
|
+
# Configuration parameters: AssignmentOnly.
|
50
|
+
RSpec/InstanceVariable:
|
51
|
+
Exclude:
|
52
|
+
- 'spec/active_record_behaviors_spec.rb'
|
53
|
+
|
54
|
+
# Offense count: 2
|
55
|
+
RSpec/LeakyConstantDeclaration:
|
56
|
+
Exclude:
|
57
|
+
- 'spec/readme_spec.rb'
|
58
|
+
- 'spec/with_model_spec.rb'
|
59
|
+
|
60
|
+
# Offense count: 2
|
61
|
+
# Configuration parameters: .
|
62
|
+
# SupportedStyles: have_received, receive
|
63
|
+
RSpec/MessageSpies:
|
64
|
+
EnforcedStyle: receive
|
65
|
+
|
66
|
+
# Offense count: 1
|
67
|
+
RSpec/MultipleDescribes:
|
68
|
+
Exclude:
|
69
|
+
- 'spec/readme_spec.rb'
|
70
|
+
|
71
|
+
# Offense count: 13
|
72
|
+
RSpec/MultipleExpectations:
|
73
|
+
Max: 5
|
74
|
+
|
75
|
+
# Offense count: 4
|
76
|
+
# Configuration parameters: IgnoreSharedExamples.
|
77
|
+
RSpec/NamedSubject:
|
78
|
+
Exclude:
|
79
|
+
- 'spec/with_model_spec.rb'
|
80
|
+
|
81
|
+
# Offense count: 2
|
82
|
+
RSpec/NestedGroups:
|
83
|
+
Max: 4
|
84
|
+
|
85
|
+
# Offense count: 4
|
86
|
+
RSpec/RepeatedExample:
|
87
|
+
Exclude:
|
88
|
+
- 'spec/with_model_spec.rb'
|
89
|
+
|
90
|
+
# Offense count: 2
|
91
|
+
RSpec/SubjectStub:
|
92
|
+
Exclude:
|
93
|
+
- 'spec/with_model_spec.rb'
|
data/.travis.yml
CHANGED
@@ -1,43 +1,44 @@
|
|
1
1
|
language: ruby
|
2
|
-
|
2
|
+
cache: bundler
|
3
3
|
|
4
4
|
rvm:
|
5
|
-
-
|
6
|
-
-
|
7
|
-
-
|
8
|
-
-
|
9
|
-
|
10
|
-
install:
|
11
|
-
- bundle install --retry=3
|
5
|
+
- 2.7.1
|
6
|
+
- 2.6.6
|
7
|
+
- 2.5.8
|
8
|
+
- 2.4.10
|
9
|
+
- jruby-9.2.13.0
|
12
10
|
|
13
11
|
env:
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
- ACTIVE_RECORD_VERSION="~> 4.2.0"
|
12
|
+
global:
|
13
|
+
- CC_TEST_REPORTER_ID=fe5c8a8b1b951a54707c08b6fb2a9a5edf9e0522d28bccc648454f774c9ccab1
|
14
|
+
matrix:
|
15
|
+
- ACTIVE_RECORD_BRANCH="master"
|
16
|
+
- ACTIVE_RECORD_VERSION="~> 6.1.0.rc1"
|
17
|
+
- ACTIVE_RECORD_VERSION="~> 6.0.0"
|
18
|
+
- ACTIVE_RECORD_VERSION="~> 5.2.0"
|
22
19
|
|
23
20
|
matrix:
|
21
|
+
fast_finish: true
|
24
22
|
allow_failures:
|
25
23
|
- env: ACTIVE_RECORD_BRANCH="master"
|
26
|
-
- env:
|
27
|
-
- env: ACTIVE_RECORD_BRANCH="5-1-stable"
|
28
|
-
- env: ACTIVE_RECORD_BRANCH="5-0-stable"
|
24
|
+
- env: ACTIVE_RECORD_VERSION="~> 6.1.0.rc1"
|
29
25
|
exclude:
|
30
|
-
- rvm: 2.
|
31
|
-
env: ACTIVE_RECORD_BRANCH="master"
|
32
|
-
- rvm: jruby-9.2.0.0
|
26
|
+
- rvm: 2.4.10
|
33
27
|
env: ACTIVE_RECORD_BRANCH="master"
|
34
|
-
- rvm:
|
35
|
-
env:
|
36
|
-
- rvm:
|
37
|
-
env: ACTIVE_RECORD_VERSION="~>
|
38
|
-
- rvm: jruby-9.2.
|
39
|
-
env: ACTIVE_RECORD_VERSION="~>
|
28
|
+
- rvm: 2.4.10
|
29
|
+
env: ACTIVE_RECORD_VERSION="~> 6.0.0"
|
30
|
+
- rvm: 2.4.10
|
31
|
+
env: ACTIVE_RECORD_VERSION="~> 6.1.0.rc1"
|
32
|
+
- rvm: jruby-9.2.13.0
|
33
|
+
env: ACTIVE_RECORD_VERSION="~> 6.0.0"
|
34
|
+
|
35
|
+
before_install:
|
36
|
+
- gem install bundler
|
37
|
+
|
38
|
+
before_script:
|
39
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
40
|
+
- chmod +x ./cc-test-reporter
|
41
|
+
- ./cc-test-reporter before-build
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
repo_token: fe5c8a8b1b951a54707c08b6fb2a9a5edf9e0522d28bccc648454f774c9ccab1
|
43
|
+
after_script:
|
44
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,34 @@
|
|
1
|
+
### 2.1.5
|
2
|
+
|
3
|
+
- Add support for Active Record 6.1
|
4
|
+
|
5
|
+
### 2.1.4
|
6
|
+
|
7
|
+
- Remove deprecation warnings for Ruby 2.7
|
8
|
+
- Require Ruby 2.4 or later
|
9
|
+
- Require Active Record 5.2 or later
|
10
|
+
|
11
|
+
### 2.1.3
|
12
|
+
|
13
|
+
- Support ActiveRecord 6.0, working around new dependency tracking API
|
14
|
+
|
15
|
+
### 2.1.2
|
16
|
+
|
17
|
+
- Don't clobber existing rake executable
|
18
|
+
|
19
|
+
### 2.1.1
|
20
|
+
|
21
|
+
- Don't fail erroneously when running the after hook when the before hook failed
|
22
|
+
- Improve documentation
|
23
|
+
|
24
|
+
### 2.1.0
|
25
|
+
|
26
|
+
- Support creating constant in an existing namespace (#26)
|
27
|
+
- Prefer keyword arguments over manually-validated hash argument
|
28
|
+
- Improve documentation
|
29
|
+
- Remove support for obsolete Ruby
|
30
|
+
- Internal cleanup
|
31
|
+
|
1
32
|
### 2.0.0
|
2
33
|
|
3
34
|
- Require Ruby 2.1 or later
|
data/Gemfile
CHANGED
@@ -4,14 +4,21 @@ source 'https://rubygems.org'
|
|
4
4
|
|
5
5
|
gemspec
|
6
6
|
|
7
|
-
gem 'lint-config-ruby', git: 'https://github.com/Casecommons/lint-config-ruby.git', tag: 'v1.0.0'
|
8
|
-
|
9
7
|
ar_branch = ENV['ACTIVE_RECORD_BRANCH']
|
10
8
|
ar_version = ENV['ACTIVE_RECORD_VERSION']
|
9
|
+
is_jruby = RUBY_PLATFORM == 'java'
|
11
10
|
|
12
11
|
if ar_branch
|
13
12
|
gem 'activerecord', git: 'https://github.com/rails/rails.git', branch: ar_branch
|
14
|
-
|
13
|
+
if ar_branch == 'master'
|
14
|
+
gem 'arel', git: 'https://github.com/rails/arel.git'
|
15
|
+
gem 'activerecord-jdbcsqlite3-adapter', git: 'https://github.com/jruby/activerecord-jdbc-adapter.git' if is_jruby
|
16
|
+
end
|
15
17
|
elsif ar_version
|
16
|
-
gem 'activerecord', ar_version
|
18
|
+
gem 'activerecord', ar_version
|
19
|
+
if is_jruby && !Gem::Requirement.new(ar_version).satisfied_by?(Gem::Version.new('5.2.0'))
|
20
|
+
gem 'activerecord-jdbcsqlite3-adapter', git: 'https://github.com/jruby/activerecord-jdbc-adapter.git'
|
21
|
+
end
|
17
22
|
end
|
23
|
+
|
24
|
+
gem 'sqlite3', '~> 1.4.1' unless is_jruby
|
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2010-
|
1
|
+
Copyright (c) 2010-2019 Casebook, PBC <http://www.casebook.net>
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
4
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://img.shields.io/gem/v/with_model.svg?style=flat)](https://rubygems.org/gems/with_model)
|
4
4
|
[![Build Status](https://secure.travis-ci.org/Casecommons/with_model.svg?branch=master)](https://travis-ci.org/Casecommons/with_model)
|
5
|
+
[![API Documentation](https://img.shields.io/badge/yard-api%20docs-lightgrey.svg)](https://www.rubydoc.info/gems/with_model)
|
5
6
|
|
6
|
-
`with_model` dynamically builds an
|
7
|
+
`with_model` dynamically builds an Active Record model (with table) before each test in a group and destroys it afterwards.
|
7
8
|
|
8
9
|
## Development status
|
9
10
|
|
@@ -48,13 +49,13 @@ describe "A blog post" do
|
|
48
49
|
module MyModule; end
|
49
50
|
|
50
51
|
with_model :BlogPost do
|
51
|
-
# The table block (and an options hash) is passed to
|
52
|
+
# The table block (and an options hash) is passed to Active Record migration’s `create_table`.
|
52
53
|
table do |t|
|
53
54
|
t.string :title
|
54
55
|
t.timestamps null: false
|
55
56
|
end
|
56
57
|
|
57
|
-
# The model block is the
|
58
|
+
# The model block is the Active Record model’s class body.
|
58
59
|
model do
|
59
60
|
include MyModule
|
60
61
|
has_many :comments
|
@@ -101,7 +102,7 @@ describe "A blog post" do
|
|
101
102
|
|
102
103
|
it "can do all the things a regular model can" do
|
103
104
|
record = BlogPost.new
|
104
|
-
expect(record).
|
105
|
+
expect(record).not_to be_valid
|
105
106
|
record.title = "foo"
|
106
107
|
expect(record).to be_valid
|
107
108
|
expect(record.save).to eq true
|
@@ -154,16 +155,25 @@ describe "with table options" do
|
|
154
155
|
end
|
155
156
|
|
156
157
|
it "respects the additional options" do
|
157
|
-
expect(WithOptions.columns.map(&:name)).
|
158
|
+
expect(WithOptions.columns.map(&:name)).not_to include("id")
|
158
159
|
end
|
159
160
|
end
|
160
161
|
```
|
161
162
|
|
162
163
|
## Requirements
|
163
164
|
|
164
|
-
|
165
|
-
|
166
|
-
-
|
165
|
+
See the [gemspec metadata](https://rubygems.org/gems/with_model) for dependency requirements. RSpec and minitest are indirect dependencies, and `with_model` should support any maintained version of both.
|
166
|
+
|
167
|
+
## Thread-safety
|
168
|
+
|
169
|
+
- A unique table name is used for tables generated via `with_model`/`WithModel::Model.new`. This allows `with_model` (when limited to this API) to run concurrently (in processes or threads) with a single database schema. While there is a possibility of collision, it is very small.
|
170
|
+
- A user-supplied table name is used for tables generated via `with_table`/`WithModel::Table.new`. This may cause collisions at runtime if tests are run concurrently against a single database schema, unless the caller takes care to ensure the table names passed as arguments are unique across threads/processes.
|
171
|
+
- Generated models are created in stubbed constants, which are global; no guarantee is made to the uniqueness of a constant, and this may be unsafe.
|
172
|
+
- Generated classes are Active Record subclasses:
|
173
|
+
- This library makes no guarantee as to the thread-safety of creating Active Record subclasses concurrently.
|
174
|
+
- This library makes no guarantee as to the thread-safety of cleaning up Active Record/Active Support’s internals which are polluted upon class creation.
|
175
|
+
|
176
|
+
In general, `with_model` is not guaranteed to be thread-safe, but is, in certain usages, safe to use concurrently across multiple processes with a single database schema.
|
167
177
|
|
168
178
|
## Versioning
|
169
179
|
|
@@ -171,5 +181,5 @@ end
|
|
171
181
|
|
172
182
|
## License
|
173
183
|
|
174
|
-
Copyright © 2010–
|
184
|
+
Copyright © 2010–2020 [Casebook PBC](https://www.casebook.net).
|
175
185
|
Licensed under the MIT license, see [LICENSE](/LICENSE) file.
|
data/lib/with_model.rb
CHANGED
@@ -6,8 +6,12 @@ require 'with_model/table'
|
|
6
6
|
require 'with_model/version'
|
7
7
|
|
8
8
|
module WithModel
|
9
|
+
# @param [Symbol] name The constant name to assign the model class to.
|
10
|
+
# @param scope Passed to `before`/`after` in the test context.
|
11
|
+
# @param options Passed to {WithModel::Model#initialize}.
|
12
|
+
# @param block Yielded an instance of {WithModel::Model::DSL}.
|
9
13
|
def with_model(name, scope: nil, **options, &block)
|
10
|
-
model = Model.new name, options
|
14
|
+
model = Model.new name, **options
|
11
15
|
dsl = Model::DSL.new model
|
12
16
|
dsl.instance_exec(&block) if block
|
13
17
|
|
@@ -20,6 +24,10 @@ module WithModel
|
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
27
|
+
# @param [Symbol] name The table name to create.
|
28
|
+
# @param scope Passed to `before`/`after` in the test context.
|
29
|
+
# @param options Passed to {WithModel::Table#initialize}.
|
30
|
+
# @param block Passed to {WithModel::Table#initialize} (like {WithModel::Model::DSL#table}).
|
23
31
|
def with_table(name, scope: nil, **options, &block)
|
24
32
|
table = Table.new name, options, &block
|
25
33
|
|
@@ -19,9 +19,11 @@ module WithModel
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def unstub_const
|
22
|
-
@namespace
|
23
|
-
|
24
|
-
|
22
|
+
if @namespace
|
23
|
+
@namespace.__send__ :remove_const, basename
|
24
|
+
@namespace.const_set basename, @original_value if @original_value
|
25
|
+
@namespace = nil
|
26
|
+
end
|
25
27
|
@original_value = nil
|
26
28
|
end
|
27
29
|
|
data/lib/with_model/methods.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module WithModel
|
4
|
+
# Extended into all ActiveRecord models created by with_model.
|
4
5
|
module Methods
|
6
|
+
# Since model classes not created by with_model won't have this
|
7
|
+
# method, one should instead test `respond_to?(:with_model?)`.
|
5
8
|
def with_model?
|
6
9
|
true
|
7
10
|
end
|
data/lib/with_model/model.rb
CHANGED
@@ -8,9 +8,14 @@ require 'with_model/methods'
|
|
8
8
|
require 'with_model/table'
|
9
9
|
|
10
10
|
module WithModel
|
11
|
+
# In general, direct use of this class should be avoided. Instead use
|
12
|
+
# either the {WithModel high-level API} or {WithModel::Model::DSL low-level API}.
|
11
13
|
class Model
|
12
14
|
attr_writer :model_block, :table_block, :table_options
|
13
15
|
|
16
|
+
# @param [Symbol] name The constant name to assign the model class to.
|
17
|
+
# @param [Class] superclass The superclass for the created class. Should
|
18
|
+
# have `ActiveRecord::Base` as an ancestor.
|
14
19
|
def initialize(name, superclass: ActiveRecord::Base)
|
15
20
|
@name = name.to_sym
|
16
21
|
@model_block = nil
|
@@ -30,7 +35,7 @@ module WithModel
|
|
30
35
|
|
31
36
|
def destroy
|
32
37
|
stubber.unstub_const
|
33
|
-
|
38
|
+
cleanup_descendants_tracking
|
34
39
|
reset_dependencies_cache
|
35
40
|
table.destroy
|
36
41
|
@model = nil
|
@@ -48,13 +53,17 @@ module WithModel
|
|
48
53
|
@model.reset_column_information
|
49
54
|
end
|
50
55
|
|
51
|
-
def
|
52
|
-
|
53
|
-
|
56
|
+
def cleanup_descendants_tracking
|
57
|
+
if defined?(ActiveSupport::DescendantsTracker)
|
58
|
+
ActiveSupport::DescendantsTracker.class_variable_get(:@@direct_descendants).delete(ActiveRecord::Base)
|
59
|
+
elsif @model.superclass.respond_to?(:direct_descendants)
|
60
|
+
@model.superclass.direct_descendants.delete(@model)
|
61
|
+
end
|
54
62
|
end
|
55
63
|
|
56
64
|
def reset_dependencies_cache
|
57
65
|
return unless defined?(ActiveSupport::Dependencies::Reference)
|
66
|
+
|
58
67
|
ActiveSupport::Dependencies::Reference.clear!
|
59
68
|
end
|
60
69
|
|
data/lib/with_model/model/dsl.rb
CHANGED
@@ -3,11 +3,12 @@
|
|
3
3
|
module WithModel
|
4
4
|
class Model
|
5
5
|
class DSL
|
6
|
+
# @param model [WithModel::Model] The Model to mutate via this DSL.
|
6
7
|
def initialize(model)
|
7
8
|
@model = model
|
8
9
|
end
|
9
10
|
|
10
|
-
# Provide a schema definition for the table, passed to ActiveRecord
|
11
|
+
# Provide a schema definition for the table, passed to ActiveRecord's `create_table`.
|
11
12
|
# The table name will be auto-generated.
|
12
13
|
#
|
13
14
|
# @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table
|
data/lib/with_model/table.rb
CHANGED
@@ -3,20 +3,28 @@
|
|
3
3
|
require 'active_record'
|
4
4
|
|
5
5
|
module WithModel
|
6
|
+
# In general, direct use of this class should be avoided. Instead use
|
7
|
+
# either the {WithModel high-level API} or {WithModel::Model::DSL low-level API}.
|
6
8
|
class Table
|
9
|
+
# @param [Symbol] name The name of the table to create.
|
10
|
+
# @param options Passed to ActiveRecord `create_table`.
|
11
|
+
# @param block Passed to ActiveRecord `create_table`.
|
12
|
+
# @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table
|
7
13
|
def initialize(name, options = {}, &block)
|
8
14
|
@name = name.freeze
|
9
15
|
@options = options.freeze
|
10
16
|
@block = block
|
11
17
|
end
|
12
18
|
|
19
|
+
# Creates the table with the initialized options. Drops the table if
|
20
|
+
# it already exists.
|
13
21
|
def create
|
14
22
|
connection.drop_table(@name) if exists?
|
15
|
-
connection.create_table(@name,
|
23
|
+
connection.create_table(@name, **@options, &@block)
|
16
24
|
end
|
17
25
|
|
18
26
|
def destroy
|
19
|
-
|
27
|
+
connection.drop_table(@name)
|
20
28
|
end
|
21
29
|
|
22
30
|
private
|
data/lib/with_model/version.rb
CHANGED
data/spec/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module WithModel
|
6
|
+
describe ConstantStubber do
|
7
|
+
it 'allows calling unstub_const multiple times' do
|
8
|
+
stubber = described_class.new('Foo')
|
9
|
+
stubber.stub_const(1)
|
10
|
+
stubber.unstub_const
|
11
|
+
stubber.unstub_const
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'allows calling unstub_const without stub_const' do
|
15
|
+
stubber = described_class.new('Foo')
|
16
|
+
stubber.unstub_const
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/readme_spec.rb
CHANGED
@@ -61,7 +61,7 @@ describe 'A blog post' do
|
|
61
61
|
|
62
62
|
it 'can do all the things a regular model can' do
|
63
63
|
record = BlogPost.new
|
64
|
-
expect(record).
|
64
|
+
expect(record).not_to be_valid
|
65
65
|
record.title = 'foo'
|
66
66
|
expect(record).to be_valid
|
67
67
|
expect(record.save).to eq true
|
@@ -75,8 +75,7 @@ describe 'A blog post' do
|
|
75
75
|
self.abstract_class = true
|
76
76
|
end
|
77
77
|
|
78
|
-
with_model :Ford, superclass: Car
|
79
|
-
end
|
78
|
+
with_model :Ford, superclass: Car
|
80
79
|
|
81
80
|
it 'has a specified superclass' do
|
82
81
|
expect(Ford < Car).to eq true
|
@@ -114,6 +113,6 @@ describe 'with table options' do
|
|
114
113
|
end
|
115
114
|
|
116
115
|
it 'respects the additional options' do
|
117
|
-
expect(WithOptions.columns.map(&:name)).
|
116
|
+
expect(WithOptions.columns.map(&:name)).not_to include('id')
|
118
117
|
end
|
119
118
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start
|
5
5
|
|
6
|
+
require 'bundler/setup'
|
6
7
|
require 'with_model'
|
8
|
+
|
7
9
|
RSpec.configure do |config|
|
8
10
|
config.extend WithModel
|
9
11
|
|
@@ -26,9 +28,7 @@ adapter = is_jruby ? 'jdbcsqlite3' : 'sqlite3'
|
|
26
28
|
require 'active_record'
|
27
29
|
ActiveRecord::Base.establish_connection(adapter: adapter, database: ':memory:')
|
28
30
|
|
29
|
-
if defined?(I18n) && I18n.respond_to?(:enforce_available_locales=)
|
30
|
-
I18n.enforce_available_locales = true
|
31
|
-
end
|
31
|
+
I18n.enforce_available_locales = true if defined?(I18n) && I18n.respond_to?(:enforce_available_locales=)
|
32
32
|
|
33
33
|
if ENV['LOGGER']
|
34
34
|
require 'logger'
|
data/spec/with_model_spec.rb
CHANGED
@@ -59,7 +59,8 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
59
59
|
|
60
60
|
describe 'the class' do
|
61
61
|
subject { BlogPost.new }
|
62
|
-
|
62
|
+
|
63
|
+
it_behaves_like 'ActiveModel'
|
63
64
|
end
|
64
65
|
|
65
66
|
it 'has the methods defined in its model block' do
|
@@ -90,6 +91,7 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
90
91
|
|
91
92
|
describe 'constant restoration' do
|
92
93
|
before { stub_const('MyConst', 1) }
|
94
|
+
|
93
95
|
shadowing_example_ran = false
|
94
96
|
|
95
97
|
context 'with the with_model block' do
|
@@ -145,6 +147,7 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
145
147
|
|
146
148
|
describe 'with a name which is namespaced' do
|
147
149
|
before { stub_const('Stuff', Module.new) }
|
150
|
+
|
148
151
|
with_model :'Stuff::BlogPost'
|
149
152
|
|
150
153
|
it 'creates the model in the namespace' do
|
@@ -160,13 +163,16 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
160
163
|
end
|
161
164
|
end
|
162
165
|
|
163
|
-
it 'is available' do
|
166
|
+
it 'is available' do
|
167
|
+
expect(BlogPost).to be
|
168
|
+
end
|
164
169
|
end
|
165
170
|
|
166
171
|
context 'with a mixin' do
|
167
172
|
let(:mixin) do
|
168
173
|
Module.new { def foo; end }
|
169
174
|
end
|
175
|
+
|
170
176
|
before { stub_const('AMixin', mixin) }
|
171
177
|
|
172
178
|
with_model :WithAMixin do
|
@@ -176,12 +182,14 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
176
182
|
end
|
177
183
|
|
178
184
|
it 'has the mixin' do
|
179
|
-
expect(-> { ::WithAMixin.new.foo }).
|
185
|
+
expect(-> { ::WithAMixin.new.foo }).not_to raise_error
|
180
186
|
expect(::WithAMixin.include?(AMixin)).to eq true
|
181
187
|
end
|
182
188
|
end
|
183
189
|
|
184
190
|
context 'with a mixin that has a class_eval' do
|
191
|
+
subject { WithAClassEval.new }
|
192
|
+
|
185
193
|
let(:mixin) do
|
186
194
|
Module.new do
|
187
195
|
def self.included(klass)
|
@@ -191,9 +199,8 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
191
199
|
end
|
192
200
|
end
|
193
201
|
end
|
194
|
-
before { stub_const('AMixin', mixin) }
|
195
202
|
|
196
|
-
|
203
|
+
before { stub_const('AMixin', mixin) }
|
197
204
|
|
198
205
|
with_model :WithAClassEval do
|
199
206
|
model do
|
@@ -222,7 +229,7 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
222
229
|
end
|
223
230
|
|
224
231
|
it 'respects the additional options' do
|
225
|
-
expect(WithOptions.columns.map(&:name)).
|
232
|
+
expect(WithOptions.columns.map(&:name)).not_to include('id')
|
226
233
|
end
|
227
234
|
end
|
228
235
|
|
@@ -238,12 +245,13 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
238
245
|
|
239
246
|
describe 'the class' do
|
240
247
|
subject { BlogPost.new }
|
241
|
-
|
248
|
+
|
249
|
+
it_behaves_like 'ActiveModel'
|
242
250
|
end
|
243
251
|
end
|
244
252
|
|
245
253
|
context 'with an empty block' do
|
246
|
-
with_model(:BlogPost) {}
|
254
|
+
with_model(:BlogPost) {} # rubocop:disable Lint/EmptyBlock
|
247
255
|
|
248
256
|
it 'acts like a normal ActiveRecord model' do
|
249
257
|
record = BlogPost.create!
|
@@ -254,7 +262,8 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
254
262
|
|
255
263
|
describe 'the class' do
|
256
264
|
subject { BlogPost.new }
|
257
|
-
|
265
|
+
|
266
|
+
it_behaves_like 'ActiveModel'
|
258
267
|
end
|
259
268
|
end
|
260
269
|
|
@@ -283,7 +292,8 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
283
292
|
|
284
293
|
describe 'the class' do
|
285
294
|
subject { BlogPost.new }
|
286
|
-
|
295
|
+
|
296
|
+
it_behaves_like 'ActiveModel'
|
287
297
|
end
|
288
298
|
end
|
289
299
|
|
@@ -300,25 +310,32 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
300
310
|
|
301
311
|
describe 'the class' do
|
302
312
|
subject { BlogPost.new }
|
303
|
-
|
313
|
+
|
314
|
+
it_behaves_like 'ActiveModel'
|
304
315
|
end
|
305
316
|
end
|
306
317
|
|
307
318
|
context 'with ActiveSupport::DescendantsTracker' do
|
308
|
-
with_model :BlogPost
|
319
|
+
with_model :BlogPost do
|
320
|
+
model do
|
321
|
+
def self.inspect
|
322
|
+
"BlogPost class #{object_id}"
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
309
326
|
|
310
|
-
|
311
|
-
|
327
|
+
def blog_post_classes
|
328
|
+
ActiveRecord::Base.descendants.select do |c|
|
312
329
|
c.table_name == BlogPost.table_name
|
313
330
|
end
|
314
|
-
|
331
|
+
end
|
332
|
+
|
333
|
+
it 'includes the correct model class in descendants on the first test run' do
|
334
|
+
expect(blog_post_classes).to eq [BlogPost]
|
315
335
|
end
|
316
336
|
|
317
337
|
it 'includes the correct model class in descendants on the second test run' do
|
318
|
-
|
319
|
-
c.table_name == BlogPost.table_name
|
320
|
-
end
|
321
|
-
expect(descendant).to eq BlogPost
|
338
|
+
expect(blog_post_classes).to eq [BlogPost]
|
322
339
|
end
|
323
340
|
end
|
324
341
|
|
@@ -355,7 +372,8 @@ describe 'a temporary ActiveRecord model created with with_model' do
|
|
355
372
|
|
356
373
|
describe 'the class' do
|
357
374
|
subject { BlogPost.new }
|
358
|
-
|
375
|
+
|
376
|
+
it_behaves_like 'ActiveModel'
|
359
377
|
end
|
360
378
|
|
361
379
|
it 'is a subclass of the supplied superclass' do
|
data/with_model.gemspec
CHANGED
@@ -14,18 +14,23 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
|
-
spec.executables = spec.files.grep(%r
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.required_ruby_version = '>= 2.
|
21
|
+
spec.required_ruby_version = '>= 2.4'
|
22
22
|
|
23
|
-
spec.add_dependency 'activerecord', '>=
|
23
|
+
spec.add_dependency 'activerecord', '>= 5.2'
|
24
24
|
|
25
|
-
spec.add_development_dependency 'bundler', '~>
|
25
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
26
26
|
spec.add_development_dependency 'minitest'
|
27
27
|
spec.add_development_dependency 'rake'
|
28
28
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
spec.add_development_dependency 'rubocop'
|
30
|
+
spec.add_development_dependency 'rubocop-rake'
|
31
|
+
spec.add_development_dependency 'rubocop-rspec'
|
32
|
+
spec.add_development_dependency 'simplecov'
|
33
|
+
spec.add_development_dependency 'yard'
|
29
34
|
|
30
35
|
if RUBY_PLATFORM == 'java'
|
31
36
|
spec.add_development_dependency 'activerecord-jdbcsqlite3-adapter'
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: with_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Case Commons, LLC
|
8
8
|
- Grant Hutchins
|
9
9
|
- Andrew Marshall
|
10
|
-
autorequire:
|
10
|
+
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2020-11-22 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -18,34 +18,28 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
22
|
-
- - "<"
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version: '6.0'
|
21
|
+
version: '5.2'
|
25
22
|
type: :runtime
|
26
23
|
prerelease: false
|
27
24
|
version_requirements: !ruby/object:Gem::Requirement
|
28
25
|
requirements:
|
29
26
|
- - ">="
|
30
27
|
- !ruby/object:Gem::Version
|
31
|
-
version: '
|
32
|
-
- - "<"
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: '6.0'
|
28
|
+
version: '5.2'
|
35
29
|
- !ruby/object:Gem::Dependency
|
36
30
|
name: bundler
|
37
31
|
requirement: !ruby/object:Gem::Requirement
|
38
32
|
requirements:
|
39
33
|
- - "~>"
|
40
34
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
35
|
+
version: '2.0'
|
42
36
|
type: :development
|
43
37
|
prerelease: false
|
44
38
|
version_requirements: !ruby/object:Gem::Requirement
|
45
39
|
requirements:
|
46
40
|
- - "~>"
|
47
41
|
- !ruby/object:Gem::Version
|
48
|
-
version: '
|
42
|
+
version: '2.0'
|
49
43
|
- !ruby/object:Gem::Dependency
|
50
44
|
name: minitest
|
51
45
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,6 +82,76 @@ dependencies:
|
|
88
82
|
- - "~>"
|
89
83
|
- !ruby/object:Gem::Version
|
90
84
|
version: '3.0'
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: rubocop
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
type: :development
|
93
|
+
prerelease: false
|
94
|
+
version_requirements: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: rubocop-rake
|
101
|
+
requirement: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
type: :development
|
107
|
+
prerelease: false
|
108
|
+
version_requirements: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: rubocop-rspec
|
115
|
+
requirement: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
type: :development
|
121
|
+
prerelease: false
|
122
|
+
version_requirements: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
name: simplecov
|
129
|
+
requirement: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
- !ruby/object:Gem::Dependency
|
142
|
+
name: yard
|
143
|
+
requirement: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
type: :development
|
149
|
+
prerelease: false
|
150
|
+
version_requirements: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
91
155
|
- !ruby/object:Gem::Dependency
|
92
156
|
name: sqlite3
|
93
157
|
requirement: !ruby/object:Gem::Requirement
|
@@ -107,21 +171,22 @@ email:
|
|
107
171
|
- casecommons-dev@googlegroups.com
|
108
172
|
- gems@nertzy.com
|
109
173
|
- andrew@johnandrewmarshall.com
|
110
|
-
executables:
|
111
|
-
- rake
|
174
|
+
executables: []
|
112
175
|
extensions: []
|
113
176
|
extra_rdoc_files: []
|
114
177
|
files:
|
115
178
|
- ".gitignore"
|
179
|
+
- ".jrubyrc"
|
116
180
|
- ".rspec"
|
117
181
|
- ".rubocop.yml"
|
182
|
+
- ".rubocop_todo.yml"
|
118
183
|
- ".travis.yml"
|
184
|
+
- ".yardopts"
|
119
185
|
- CHANGELOG.md
|
120
186
|
- Gemfile
|
121
187
|
- LICENSE
|
122
188
|
- README.md
|
123
189
|
- Rakefile
|
124
|
-
- bin/rake
|
125
190
|
- lib/with_model.rb
|
126
191
|
- lib/with_model/constant_stubber.rb
|
127
192
|
- lib/with_model/methods.rb
|
@@ -129,7 +194,9 @@ files:
|
|
129
194
|
- lib/with_model/model/dsl.rb
|
130
195
|
- lib/with_model/table.rb
|
131
196
|
- lib/with_model/version.rb
|
197
|
+
- spec/.rubocop.yml
|
132
198
|
- spec/active_record_behaviors_spec.rb
|
199
|
+
- spec/constant_stubber_spec.rb
|
133
200
|
- spec/readme_spec.rb
|
134
201
|
- spec/spec_helper.rb
|
135
202
|
- spec/with_model_spec.rb
|
@@ -138,7 +205,7 @@ homepage: https://github.com/Casecommons/with_model
|
|
138
205
|
licenses:
|
139
206
|
- MIT
|
140
207
|
metadata: {}
|
141
|
-
post_install_message:
|
208
|
+
post_install_message:
|
142
209
|
rdoc_options: []
|
143
210
|
require_paths:
|
144
211
|
- lib
|
@@ -146,20 +213,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
146
213
|
requirements:
|
147
214
|
- - ">="
|
148
215
|
- !ruby/object:Gem::Version
|
149
|
-
version: '2.
|
216
|
+
version: '2.4'
|
150
217
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
218
|
requirements:
|
152
219
|
- - ">="
|
153
220
|
- !ruby/object:Gem::Version
|
154
221
|
version: '0'
|
155
222
|
requirements: []
|
156
|
-
|
157
|
-
|
158
|
-
signing_key:
|
223
|
+
rubygems_version: 3.1.4
|
224
|
+
signing_key:
|
159
225
|
specification_version: 4
|
160
226
|
summary: Dynamically build a model within an RSpec context
|
161
227
|
test_files:
|
228
|
+
- spec/.rubocop.yml
|
162
229
|
- spec/active_record_behaviors_spec.rb
|
230
|
+
- spec/constant_stubber_spec.rb
|
163
231
|
- spec/readme_spec.rb
|
164
232
|
- spec/spec_helper.rb
|
165
233
|
- spec/with_model_spec.rb
|
data/bin/rake
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
#
|
5
|
-
# This file was generated by Bundler.
|
6
|
-
#
|
7
|
-
# The application 'rake' is installed as part of a gem, and
|
8
|
-
# this file is here to facilitate running it.
|
9
|
-
#
|
10
|
-
|
11
|
-
require 'pathname'
|
12
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
13
|
-
Pathname.new(__FILE__).realpath)
|
14
|
-
|
15
|
-
require 'rubygems'
|
16
|
-
require 'bundler/setup'
|
17
|
-
|
18
|
-
load Gem.bin_path('rake', 'rake')
|