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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd5e8e30e23e453616fbc089d73a7b28486e4db679ed908a364dcd6d71e492e6
4
- data.tar.gz: b89c1526fe89934da2ddcface1dd3f77be17ea6ff929d3943f2ac6dd595ee030
3
+ metadata.gz: 030a808617abb6bba6d515a77e9dee9a2714d07ab54997905deb1a0cec621ff3
4
+ data.tar.gz: b15daf9690ca648f00e13645f8d5661806fd429b6db4edf7ce908df21b009e59
5
5
  SHA512:
6
- metadata.gz: 395cc8a44d0a92c42398605c91bc6af03af03361250cf84315bd0d5d04149361b991757640ede015d110d6a5b2566731db0fc9062e13404f9b193d2219ec642a
7
- data.tar.gz: d0022a76803e7d469b377fd7e4de8c23142866dd6bed51fc8fdb354fc12e06176571a60e6e5c4b3e3f97b0d15a131a315bacbb5556c8812996e7a60709d299dd
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
- def self.class_definitions
6
- @class_definitions ||= {}
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
- class_definitions[model_name] = block
14
+ self._temporary_class_definitions ||= {}
15
+ self._temporary_class_definitions[model_name] = block
12
16
  end
13
17
  end
14
18
 
15
- # Override ActiveSupport::TestCase#run
16
- # FIXME: こんなにループさせんでもできるんじゃないか?
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
- create_temporary_table temporary_class.table_name, &temporary_class.define_table
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
- ensure
31
- TemporaryModel::Record.connection.disable_referential_integrity do
32
- temporary_classes.each do |temporary_class|
33
- drop_temporary_table temporary_class.table_name
34
- Object.send(:remove_const, temporary_class.name)
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
 
@@ -1,3 +1,3 @@
1
1
  module TemporaryModel
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
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.1
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