with_model 2.1.6 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.bundle/config +2 -0
- data/.github/dependabot.yml +8 -0
- data/.github/workflows/ci.yml +21 -27
- data/.gitignore +1 -0
- data/.rspec +1 -1
- data/CHANGELOG.md +17 -0
- data/Gemfile +21 -14
- data/Rakefile +8 -24
- data/lib/with_model/constant_stubber.rb +1 -1
- data/lib/with_model/descendants_tracker.rb +87 -0
- data/lib/with_model/model.rb +12 -16
- data/lib/with_model/table.rb +6 -6
- data/lib/with_model/version.rb +1 -1
- data/lib/with_model.rb +27 -10
- data/spec/active_record_behaviors_spec.rb +28 -28
- data/spec/constant_stubber_spec.rb +7 -8
- data/spec/descendants_tracking_spec.rb +48 -0
- data/spec/readme_spec.rb +27 -27
- data/spec/spec_helper.rb +7 -19
- data/spec/with_model_spec.rb +119 -125
- data/test/test_helper.rb +7 -18
- data/test/with_model_test.rb +9 -9
- data/with_model.gemspec +15 -34
- metadata +11 -189
- data/.rubocop.yml +0 -30
- data/.rubocop_todo.yml +0 -78
- data/spec/.rubocop.yml +0 -5
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe "Descendants tracking" do
|
6
|
+
with_model :BlogPost do
|
7
|
+
model do
|
8
|
+
def self.inspect
|
9
|
+
"BlogPost class #{object_id}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def blog_post_classes
|
15
|
+
ActiveRecord::Base.descendants.select do |c|
|
16
|
+
c.table_name == BlogPost.table_name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
shared_examples "clearing descendants between test runs" do
|
21
|
+
it "includes the correct model class in descendants on the first test run" do
|
22
|
+
expect(blog_post_classes).to eq [BlogPost]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "includes the correct model class in descendants on the second test run" do
|
26
|
+
expect(blog_post_classes).to eq [BlogPost]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "with ActiveSupport::DescendantsTracker (cache_classes: true)" do
|
31
|
+
before do
|
32
|
+
expect(ActiveSupport::DescendantsTracker.clear_disabled).to be_falsey
|
33
|
+
expect { ActiveSupport::DescendantsTracker.clear([]) }.not_to raise_exception
|
34
|
+
end
|
35
|
+
|
36
|
+
include_examples "clearing descendants between test runs"
|
37
|
+
end
|
38
|
+
|
39
|
+
context "without ActiveSupport::DescendantsTracker (cache_classes: false)" do
|
40
|
+
before do
|
41
|
+
ActiveSupport::DescendantsTracker.disable_clear!
|
42
|
+
expect(ActiveSupport::DescendantsTracker.clear_disabled).to be_truthy
|
43
|
+
expect { ActiveSupport::DescendantsTracker.clear([]) }.to raise_exception(RuntimeError)
|
44
|
+
end
|
45
|
+
|
46
|
+
include_examples "clearing descendants between test runs"
|
47
|
+
end
|
48
|
+
end
|
data/spec/readme_spec.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe "A blog post" do
|
6
6
|
before do
|
7
|
-
stub_const(
|
7
|
+
stub_const("MyModule", Module.new)
|
8
8
|
end
|
9
9
|
|
10
10
|
with_model :BlogPost do
|
@@ -21,11 +21,11 @@ describe 'A blog post' do
|
|
21
21
|
validates_presence_of :title
|
22
22
|
|
23
23
|
def self.some_class_method
|
24
|
-
|
24
|
+
"chunky"
|
25
25
|
end
|
26
26
|
|
27
27
|
def some_instance_method
|
28
|
-
|
28
|
+
"bacon"
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -43,46 +43,46 @@ describe 'A blog post' do
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
it
|
46
|
+
it "can be accessed as a constant" do
|
47
47
|
expect(BlogPost).to be
|
48
48
|
end
|
49
49
|
|
50
|
-
it
|
51
|
-
expect(BlogPost.include?(MyModule)).to
|
50
|
+
it "has the module" do
|
51
|
+
expect(BlogPost.include?(MyModule)).to be true
|
52
52
|
end
|
53
53
|
|
54
|
-
it
|
55
|
-
expect(BlogPost.some_class_method).to eq
|
54
|
+
it "has the class method" do
|
55
|
+
expect(BlogPost.some_class_method).to eq "chunky"
|
56
56
|
end
|
57
57
|
|
58
|
-
it
|
59
|
-
expect(BlogPost.new.some_instance_method).to eq
|
58
|
+
it "has the instance method" do
|
59
|
+
expect(BlogPost.new.some_instance_method).to eq "bacon"
|
60
60
|
end
|
61
61
|
|
62
|
-
it
|
62
|
+
it "can do all the things a regular model can" do
|
63
63
|
record = BlogPost.new
|
64
64
|
expect(record).not_to be_valid
|
65
|
-
record.title =
|
65
|
+
record.title = "foo"
|
66
66
|
expect(record).to be_valid
|
67
|
-
expect(record.save).to
|
67
|
+
expect(record.save).to be true
|
68
68
|
expect(record.reload).to eq record
|
69
|
-
record.comments.create!(text:
|
69
|
+
record.comments.create!(text: "Lorem ipsum")
|
70
70
|
expect(record.comments.count).to eq 1
|
71
71
|
end
|
72
72
|
|
73
73
|
# with_model classes can have inheritance.
|
74
|
-
class Car < ActiveRecord::Base
|
74
|
+
class Car < ActiveRecord::Base # standard:disable Lint/ConstantDefinitionInBlock
|
75
75
|
self.abstract_class = true
|
76
76
|
end
|
77
77
|
|
78
78
|
with_model :Ford, superclass: Car
|
79
79
|
|
80
|
-
it
|
81
|
-
expect(Ford < Car).to
|
80
|
+
it "has a specified superclass" do
|
81
|
+
expect(Ford < Car).to be true
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
-
describe
|
85
|
+
describe "with_model can be run within RSpec :all hook" do
|
86
86
|
with_model :BlogPost, scope: :all do
|
87
87
|
table do |t|
|
88
88
|
t.string :title
|
@@ -93,26 +93,26 @@ describe 'with_model can be run within RSpec :all hook' do
|
|
93
93
|
BlogPost.create # without scope: :all these will fail
|
94
94
|
end
|
95
95
|
|
96
|
-
it
|
96
|
+
it "has been initialized within before(:all)" do
|
97
97
|
expect(BlogPost.count).to eq 1
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
describe
|
102
|
-
it
|
101
|
+
describe "another example group" do
|
102
|
+
it "does not have the constant anymore" do
|
103
103
|
expect(defined?(BlogPost)).to be_falsy
|
104
104
|
end
|
105
105
|
end
|
106
106
|
|
107
|
-
describe
|
107
|
+
describe "with table options" do
|
108
108
|
with_model :WithOptions do
|
109
109
|
table id: false do |t|
|
110
|
-
t.string
|
110
|
+
t.string "foo"
|
111
111
|
t.timestamps null: false
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
-
it
|
116
|
-
expect(WithOptions.columns.map(&:name)).not_to include(
|
115
|
+
it "respects the additional options" do
|
116
|
+
expect(WithOptions.columns.map(&:name)).not_to include("id")
|
117
117
|
end
|
118
118
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,16 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
require 'i18n/backend'
|
6
|
-
require 'i18n/backend/simple'
|
7
|
-
end
|
8
|
-
|
9
|
-
require 'simplecov'
|
10
|
-
SimpleCov.start
|
11
|
-
|
12
|
-
require 'bundler/setup'
|
13
|
-
require 'with_model'
|
3
|
+
require "bundler/setup"
|
4
|
+
require "with_model"
|
14
5
|
|
15
6
|
RSpec.configure do |config|
|
16
7
|
config.extend WithModel
|
@@ -26,24 +17,21 @@ RSpec.configure do |config|
|
|
26
17
|
end
|
27
18
|
end
|
28
19
|
|
29
|
-
is_jruby = RUBY_PLATFORM == 'java'
|
30
|
-
adapter = is_jruby ? 'jdbcsqlite3' : 'sqlite3'
|
31
|
-
|
32
20
|
# WithModel requires ActiveRecord::Base.connection to be established.
|
33
21
|
# If ActiveRecord already has a connection, as in a Rails app, this is unnecessary.
|
34
|
-
require
|
35
|
-
ActiveRecord::Base.establish_connection(adapter:
|
22
|
+
require "active_record"
|
23
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
36
24
|
|
37
25
|
I18n.enforce_available_locales = true if defined?(I18n) && I18n.respond_to?(:enforce_available_locales=)
|
38
26
|
|
39
|
-
if ENV[
|
40
|
-
require
|
27
|
+
if ENV["LOGGER"]
|
28
|
+
require "logger"
|
41
29
|
ActiveRecord::Base.logger = Logger.new($stdout)
|
42
30
|
end
|
43
31
|
|
44
32
|
module SpecHelper
|
45
33
|
module RailsTestCompatibility
|
46
|
-
require
|
34
|
+
require "minitest"
|
47
35
|
include Minitest::Assertions
|
48
36
|
|
49
37
|
def assertions
|