fixation 2.0.1 → 2.1.0

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
  SHA1:
3
- metadata.gz: 6b9a44ee63cacf43c742f6e4292f031aa718aa47
4
- data.tar.gz: 62924349c40e85c753bdefb14bfc3371a72739eb
3
+ metadata.gz: ebf779f055287d6af43f1dc2fc1625a06186d9cb
4
+ data.tar.gz: 8b77c04e6bfa3682a5662cc9833e5b34224b7c3e
5
5
  SHA512:
6
- metadata.gz: ebb9d086bd7296eda9f5e54de1475e875cb08f822d5a7d9776986a86f88dae9460c6dfb1fd70d4c231a33b29b282b8346e8b9e6e6b1bbc010807fa41ebe5a37b
7
- data.tar.gz: f4d907e2b7a1a7fddc5235534265dc562776cfb58d6ad9ffed13382f398e78f6c760de1b2fa88c6e8b0309eb51aab00aef1443dd34b9f3f832a88048a55d21dc
6
+ metadata.gz: 664900554e247b44d0029f426128df2909e5dd10cfcdbe254e088c92f88584480acb63976fec453f8e9bf70d1c3177feb8c6ac75d43290fe5377d0f93b281338
7
+ data.tar.gz: bc34ed6961ca5966c561b8d7faf70cc57fd93900acfd52acfdc301b002014f5bf7e90bdea8a32bf4a1612ab4fedb81f86c6d9613068928ad38a0124e96ba6bc9
data/README.md CHANGED
@@ -22,7 +22,7 @@ Or install it yourself as:
22
22
 
23
23
  $ gem install fixation
24
24
 
25
- Then, make an initializer:
25
+ Then, make an initializer file in config/initializers:
26
26
 
27
27
  ```ruby
28
28
  if Rails.env.test? && Fixation.running_under_spring?
@@ -32,9 +32,7 @@ if Rails.env.test? && Fixation.running_under_spring?
32
32
  end
33
33
  ```
34
34
 
35
- And run `spring stop` so this gets picked up.
36
-
37
- Finally, open up your spec_helper.rb, and find the current `global_fixtures` setting:
35
+ Open up your spec_helper.rb, and find the current `global_fixtures` setting:
38
36
 
39
37
  ```ruby
40
38
  config.global_fixtures = :all
@@ -47,6 +45,8 @@ config.global_fixtures = []
47
45
  config.include Fixation.fixture_methods
48
46
  ```
49
47
 
48
+ Finally run `spring stop` so these changes get picked up.
49
+
50
50
  ## Usage
51
51
 
52
52
  Simply run your tests under spring.
@@ -86,11 +86,27 @@ if Rails.env.test?
86
86
  end
87
87
  ```
88
88
 
89
+ ## Ruby fixtures
90
+
91
+ As well as traditional .yml fixture files, Fixation allows you to drop .rb files into your fixture directory. You can then call the `add_fixture` method, passing the name of the table, the name of the fixture, and the attributes:
92
+
93
+ ```ruby
94
+ 10.times do |n|
95
+ Fixation.add_fixture(:customers, "active_customer_#{n}", active: true, name: "Sue #{n}")
96
+ end
97
+ ```
98
+
99
+ (You should avoid accessing your actual model classes here, since that will cause them to be auto-loaded.)
89
100
 
90
101
  ## Contributing
91
102
 
92
103
  Bug reports and pull requests are welcome on GitHub at https://github.com/willbryant/fixation.
93
104
 
105
+ ## Thanks
106
+
107
+ * Andy Newport (@newportandy)
108
+ * Andrew Clemons (@aclemons)
109
+
94
110
  ## License
95
111
 
96
112
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -25,6 +25,7 @@ module Fixation
25
25
 
26
26
  def self.build_fixtures
27
27
  @fixtures = Fixtures.new
28
+ @fixtures.compile_fixture_files
28
29
  end
29
30
 
30
31
  def self.apply_fixtures
@@ -37,6 +38,10 @@ module Fixation
37
38
  @fixtures.fixture_methods
38
39
  end
39
40
 
41
+ def self.add_fixture(fixture_for, name, attributes)
42
+ @fixtures.add_fixture(fixture_for, name, attributes)
43
+ end
44
+
40
45
  # Returns a consistent, platform-independent identifier for +label+.
41
46
  # Integer identifiers are values less than 2^30. UUIDs are RFC 4122 version 5 SHA-1 hashes.
42
47
  #
@@ -1,11 +1,13 @@
1
1
  module Fixation
2
2
  class FixtureTable
3
- attr_reader :filename, :class_name, :table_name, :connection, :now
3
+ attr_reader :filename, :fixture_name, :class_name, :table_name, :connection, :loaded_at
4
4
 
5
- def initialize(filename, basename, connection, now)
5
+ def initialize(filename, basename, connection, loaded_at)
6
6
  @filename = filename
7
7
  @connection = connection
8
- @now = now
8
+ @loaded_at = loaded_at
9
+
10
+ @fixture_name = basename.gsub('/', '_')
9
11
 
10
12
  @class_name = basename.classify
11
13
  begin
@@ -52,11 +54,11 @@ module Fixation
52
54
 
53
55
  def embellished_rows
54
56
  @embellished_rows ||= parsed_rows.each do |name, attributes|
55
- embellish_fixture(name, attributes, columns_hash)
57
+ embellish_fixture(name, attributes)
56
58
  end
57
59
  end
58
60
 
59
- def embellish_fixture(name, attributes, columns_hash)
61
+ def embellish_fixture(name, attributes)
60
62
  # populate the primary key column, if not already set
61
63
  if @primary_key && columns_hash[@primary_key] && !attributes.has_key?(@primary_key)
62
64
  attributes[@primary_key] = Fixation.identify(name, columns_hash[@primary_key].type)
@@ -70,10 +72,10 @@ module Fixation
70
72
  # populate any timestamp columns, if not already set
71
73
  if @record_timestamps
72
74
  %w(created_at updated_at).each do |column_name|
73
- attributes[column_name] = now if columns_hash[column_name] && !attributes.has_key?(column_name)
75
+ attributes[column_name] = loaded_at if columns_hash[column_name] && !attributes.has_key?(column_name)
74
76
  end
75
77
  %w(created_at updated_at).each do |column_name|
76
- attributes[column_name] = now.to_date if columns_hash[column_name] && !attributes.has_key?(column_name)
78
+ attributes[column_name] = loaded_at.to_date if columns_hash[column_name] && !attributes.has_key?(column_name)
77
79
  end
78
80
  end
79
81
 
@@ -116,6 +118,11 @@ module Fixation
116
118
  end
117
119
  end
118
120
 
121
+ def add_row(name, attributes)
122
+ embellish_fixture(name, attributes)
123
+ embellished_rows[name] = attributes
124
+ end
125
+
119
126
  def fixture_ids
120
127
  embellished_rows.each_with_object({}) do |(name, attributes), ids|
121
128
  ids[name] = attributes['id'] || attributes['uuid']
@@ -1,33 +1,62 @@
1
1
  module Fixation
2
+ class FixtureContent
3
+ end
4
+
2
5
  class Fixtures
3
6
  def initialize
4
- @class_names = {}
5
- @fixture_ids = {}
6
- @statements = {}
7
-
8
- compile_fixture_files
7
+ @fixture_tables = {}
9
8
  end
10
9
 
11
10
  def compile_fixture_files(connection = ActiveRecord::Base.connection)
12
11
  puts "#{Time.now} building fixtures" if Fixation.trace
13
12
 
14
- now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
13
+ @class_names = {}
14
+
15
+ @loaded_at = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
16
+
15
17
  Fixation.paths.each do |path|
16
18
  Dir["#{path}/{**,*}/*.yml"].each do |pathname|
17
19
  basename = pathname[path.size + 1..-5]
18
- compile_fixture_file(pathname, basename, connection, now) if ::File.file?(pathname)
20
+ load_fixture_file(pathname, basename, connection) if ::File.file?(pathname)
21
+ end
22
+ end
23
+
24
+ Fixation.paths.each do |path|
25
+ Dir["#{path}/{**,*}/*.rb"].each do |pathname|
26
+ FixtureContent.instance_eval(File.read(pathname)) if ::File.file?(pathname)
19
27
  end
20
28
  end
21
29
 
30
+ bake_fixtures
31
+
22
32
  puts "#{Time.now} built fixtures for #{@fixture_ids.size} tables" if Fixation.trace
23
33
  end
24
34
 
25
- def compile_fixture_file(filename, basename, connection, now)
26
- fixture_table = FixtureTable.new(filename, basename, connection, now)
27
- fixture_name = basename.gsub('/', '_')
28
- @fixture_ids[fixture_name] = fixture_table.fixture_ids
29
- @class_names[fixture_name] = fixture_table.class_name
30
- @statements[fixture_table.table_name] = fixture_table.statements
35
+ def load_fixture_file(filename, basename, connection)
36
+ fixture_table = FixtureTable.new(filename, basename, connection, @loaded_at)
37
+ @fixture_tables[fixture_table.fixture_name] = fixture_table
38
+ @class_names[fixture_table.fixture_name] = fixture_table.class_name
39
+ end
40
+
41
+ def add_fixture(fixture_for, name, attributes)
42
+ raise "Fixtures have already been compiled! You can only call add_fixture from a file in one of the fixture directories, which is loaded on boot." if baked_fixtures?
43
+ fixture_table = @fixture_tables[fixture_for.to_s] or raise(ArgumentError, "No fixture file for #{fixture_for}") # TODO: consider allowing this
44
+ fixture_table.add_row(name.to_s, attributes.stringify_keys)
45
+ name
46
+ end
47
+
48
+ def bake_fixtures
49
+ @fixture_ids = {}
50
+ @statements = {}
51
+
52
+ @fixture_tables.each do |fixture_name, fixture_table|
53
+ @fixture_ids[fixture_table.fixture_name] = fixture_table.fixture_ids
54
+ @statements[fixture_table.table_name] = fixture_table.statements
55
+ end
56
+ end
57
+
58
+ def baked_fixtures?
59
+ !@fixture_ids.nil? || !@statements.nil?
31
60
  end
32
61
 
33
62
  def apply_fixtures(connection = ActiveRecord::Base.connection)
@@ -48,7 +77,8 @@ module Fixation
48
77
  end
49
78
 
50
79
  def clear_other_tables(connection)
51
- (connection.tables - Fixation.tables_not_to_clear - @statements.keys).each do |table_name|
80
+ data_sources = connection.respond_to?(:data_sources) ? connection.data_sources : connection.tables
81
+ (data_sources - Fixation.tables_not_to_clear - @statements.keys).each do |table_name|
52
82
  connection.execute("DELETE FROM #{connection.quote_table_name table_name}")
53
83
  end
54
84
  end
@@ -1,3 +1,3 @@
1
1
  module Fixation
2
- VERSION = "2.0.1"
2
+ VERSION = "2.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixation
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Bryant
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-09 00:00:00.000000000 Z
11
+ date: 2017-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,8 +80,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  version: '0'
81
81
  requirements: []
82
82
  rubyforge_project:
83
- rubygems_version: 2.5.1
83
+ rubygems_version: 2.5.2
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: 10x faster fixture startup under spring.
87
87
  test_files: []
88
+ has_rdoc: