temporary_model 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +79 -0
- data/lib/temporary_model/test_helper.rb +30 -15
- data/lib/temporary_model/version.rb +1 -1
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 030a808617abb6bba6d515a77e9dee9a2714d07ab54997905deb1a0cec621ff3
|
4
|
+
data.tar.gz: b15daf9690ca648f00e13645f8d5661806fd429b6db4edf7ce908df21b009e59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21859643e40e3416aaac2032b5ac853f84809d33715ebd02f24ab99d728a2d1f44215dd6c5138f07a1d6cf459cae80ece4e757e8ac3430fc39c62ca847046b5d
|
7
|
+
data.tar.gz: 8f640495053f906153dfdcf87743cd89b4625daa7911a96eeb69a364664b7dc815b32c1268e68bca7c4843929355aeaa447935f5c0142039387e1b00650216b5
|
data/README.md
CHANGED
@@ -3,6 +3,8 @@ You can define a temporary class and use it in ActiveSupport::TestCase.
|
|
3
3
|
|
4
4
|
## Usage
|
5
5
|
|
6
|
+
### Minitest
|
7
|
+
|
6
8
|
```ruby
|
7
9
|
class YourTest < ActiveSupport::TestCase
|
8
10
|
include TemporaryModel::TestHelper # 1 of 2
|
@@ -39,6 +41,76 @@ end
|
|
39
41
|
|
40
42
|
Please check test/*test.rb
|
41
43
|
|
44
|
+
### RSpec
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
require 'rails_helper'
|
48
|
+
|
49
|
+
RSpec.describe 'TemporaryModelSpec' do
|
50
|
+
include TemporaryModel::TestHelper
|
51
|
+
|
52
|
+
temporary_model 'Tag' do
|
53
|
+
define_table do |t|
|
54
|
+
t.string :name, default: '', null: false
|
55
|
+
end
|
56
|
+
|
57
|
+
has_many :taggings, dependent: :destroy
|
58
|
+
has_many :posts, through: :taggings, source: :taggable, source_type: 'Post'
|
59
|
+
|
60
|
+
validates :name, presence: true
|
61
|
+
end
|
62
|
+
|
63
|
+
temporary_model 'Post' do
|
64
|
+
define_table do |t|
|
65
|
+
t.string :title, default: '', null: false
|
66
|
+
end
|
67
|
+
|
68
|
+
has_many :taggings, as: :taggable, dependent: :destroy
|
69
|
+
has_many :tags, through: :taggings, source: :tag
|
70
|
+
|
71
|
+
validates :title, presence: true
|
72
|
+
|
73
|
+
def tag_names
|
74
|
+
tags.pluck(:name)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
temporary_model 'Tagging' do
|
79
|
+
define_table do |t|
|
80
|
+
t.references :tag, foreign_key: true
|
81
|
+
t.references :taggable, polymorphic: true
|
82
|
+
end
|
83
|
+
|
84
|
+
belongs_to :tag
|
85
|
+
belongs_to :taggable, polymorphic: true
|
86
|
+
end
|
87
|
+
|
88
|
+
let(:ruby) { Post.create!(title: 'Ruby') }
|
89
|
+
let(:rust) { Post.create!(title: 'Rust') }
|
90
|
+
|
91
|
+
before do
|
92
|
+
ruby.taggings.create!(tag: Tag.create(name: 'Programming_Language'))
|
93
|
+
ruby.taggings.create!(tag: Tag.create(name: 'Dynamic_Typing'))
|
94
|
+
|
95
|
+
rust.taggings.create!(tag: Tag.create(name: 'Programming_Language'))
|
96
|
+
rust.taggings.create!(tag: Tag.create(name: 'Static_Typing'))
|
97
|
+
end
|
98
|
+
|
99
|
+
example 'You can define and use temporary classes' do
|
100
|
+
expect(ruby.title).to eq 'Ruby'
|
101
|
+
end
|
102
|
+
|
103
|
+
example 'Of course you can also use relation' do
|
104
|
+
expect(ruby.tag_names).to contain_exactly('Programming_Language', 'Dynamic_Typing')
|
105
|
+
expect(Tag.find_by(name: 'Static_Typing').posts).to match_array([rust])
|
106
|
+
end
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
|
111
|
+
Please check spec/*_spec.rb
|
112
|
+
|
113
|
+
|
42
114
|
## Installation
|
43
115
|
Add this line to your application's Gemfile:
|
44
116
|
|
@@ -59,5 +131,12 @@ $ gem install temporary_model
|
|
59
131
|
## Contributing
|
60
132
|
Contribution directions go here.
|
61
133
|
|
134
|
+
testing
|
135
|
+
|
136
|
+
```
|
137
|
+
bundle exec rake
|
138
|
+
bundle exec rspec
|
139
|
+
```
|
140
|
+
|
62
141
|
## License
|
63
142
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -2,37 +2,52 @@ module TemporaryModel::TestHelper
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
included do
|
5
|
-
|
6
|
-
|
5
|
+
class_attribute :_temporary_class_definitions, instance_accessor: false, instance_predicate: false
|
6
|
+
|
7
|
+
def self.temporary_class_definitions
|
8
|
+
parent_definitions = defined?(super) ? super : {}
|
9
|
+
self._temporary_class_definitions.nil? ? parent_definitions : parent_definitions.merge(self._temporary_class_definitions)
|
7
10
|
end
|
8
11
|
|
9
12
|
def self.temporary_model(model_name, &block)
|
10
13
|
raise(ArgumentError, "#{model_name} has already been defined") if Object.const_defined?(model_name)
|
11
|
-
|
14
|
+
self._temporary_class_definitions ||= {}
|
15
|
+
self._temporary_class_definitions[model_name] = block
|
12
16
|
end
|
13
17
|
end
|
14
18
|
|
15
|
-
|
16
|
-
|
17
|
-
# FIXME: テストごとに設定じゃなくてテストクラス全体でできないかな
|
18
|
-
def run
|
19
|
-
temporary_classes = self.class.class_definitions.map do |model_name, class_definition|
|
19
|
+
def before_setup
|
20
|
+
@temporary_classes = self.class.temporary_class_definitions.map do |model_name, class_definition|
|
20
21
|
Class.new(TemporaryModel::Record).tap do |temporary_class|
|
21
22
|
# 先に定数に設定しておかないと
|
22
23
|
# https://circleci.com/gh/takeyuwebinc/takeyuweb-rails/83
|
23
24
|
Object.const_set(model_name, temporary_class)
|
24
|
-
temporary_class.class_eval(&class_definition)
|
25
25
|
|
26
|
-
|
26
|
+
temporary_class.class_eval(&class_definition)
|
27
|
+
create_temporary_table(temporary_class.table_name, &temporary_class.define_table)
|
27
28
|
end
|
28
29
|
end
|
30
|
+
|
31
|
+
super
|
32
|
+
end
|
33
|
+
|
34
|
+
def after_teardown
|
29
35
|
super
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
36
|
+
if @temporary_classes.any?
|
37
|
+
TemporaryModel::Record.connection.disable_referential_integrity do
|
38
|
+
@temporary_classes.each do |temporary_class|
|
39
|
+
drop_temporary_table temporary_class.table_name
|
40
|
+
Object.send(:remove_const, temporary_class.name)
|
41
|
+
end
|
35
42
|
end
|
43
|
+
# テンポラリクラスでリレーションを使っている場合、
|
44
|
+
# ActiveSupport::Dependencies.clear をしないとリレーションのklassに再利用され、
|
45
|
+
#
|
46
|
+
# Post == Tag.find_by(name: 'Tag').posts.klass # => false
|
47
|
+
#
|
48
|
+
# になる
|
49
|
+
ActiveSupport::Dependencies.clear
|
50
|
+
@temporary_classes.clear
|
36
51
|
end
|
37
52
|
end
|
38
53
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: temporary_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuichi Takeuchi
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: You can define a temporary class and use it in ActiveSupport::TestCase.
|
42
56
|
email:
|
43
57
|
- yuichi.takeuchi@takeyuweb.co.jp
|