fixturator 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d13b00b65c1bbc1aeffdf694c39ea127e77ea6a0
4
+ data.tar.gz: 7969f24d2dcb767b5facf64c92520a77c0492b78
5
+ SHA512:
6
+ metadata.gz: 30a997bfc14ddf4b830109f9438b93d59bcd09f7b92b3a53081439e209f1c069161be669986e549e3171b7f08511c3fb3efec77ad352d4880bd627ffb31e9e43
7
+ data.tar.gz: e13e559f29ee1544fe27c9af0513d9832269b06a847d02399832a2a0f53bfa239b9dbb10b065375e4eb09a6820fec77f6c4d1da89f9354ce31e0bac6c56baf02
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Bernardo Farah
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Fixturator
2
+
3
+ Fixturator is a Rails plugin that generates fixtures from your ActiveRecord
4
+ models. It requires you to whitelist models in your `config/fixturator.yml`
5
+
6
+ **QUIRKS**
7
+
8
+ I've made `created_at` and `updated_at` always output the same time. The reason
9
+ this is part of the gem is that git diffs get really messy otherwise.
10
+
11
+ You can also exclude timestamps entirely by using the `exclude_timestamps`
12
+ setting
13
+
14
+ ## Usage
15
+
16
+ There are two ways to use Fixturator. There's a rake task that it comes with,
17
+ but you can also call it yourself with a model.
18
+
19
+ #### Rake task
20
+
21
+ This gem ships with a rake task that can read a file located at
22
+ `config/fixturator.yml`
23
+
24
+ ```sh
25
+ bin/rake db:fixtures:generate
26
+ ```
27
+
28
+ Here's an example configuration with all valid attributes:
29
+
30
+ ```yml
31
+ # Excludes created_at and updated_at on all models
32
+ exclude_timestamps: false
33
+
34
+ # The models for which fixtures are generated
35
+ models:
36
+ - name: Driver
37
+ - name: User
38
+ # excluded attributes for that model
39
+ exclude:
40
+ - secret_attribute
41
+ - ssn
42
+ ```
43
+
44
+
45
+ #### Ruby interface
46
+
47
+ ```rb
48
+ Fixturator.generate!
49
+ # Uses the config/fixturator.yml
50
+ # generates a bunch of fixtures in your configured fixture directory
51
+
52
+ Fixturator.call(User, exclude_attributes: ["created_at"])
53
+ # generates a User fixture at your configured fixture directory
54
+ # by default it's at test/fixtures/users.yml
55
+ ```
56
+
57
+ #### Locating all existing models
58
+
59
+ If you want to create fixtures for all of your models, you can check out [this
60
+ issue](https://github.com/coverhound/fixturator/issues/8) on more details of how
61
+ to get a complete list. You can use a locator in conjunction with
62
+ `Fixturator.call(model)` to do so.
63
+
64
+
65
+ ## Installation
66
+ Add this line to your application's Gemfile:
67
+
68
+ ```ruby
69
+ gem 'fixturator'
70
+ ```
71
+
72
+ And then execute:
73
+ ```bash
74
+ $ bundle
75
+ ```
76
+
77
+ Or install it yourself as:
78
+ ```bash
79
+ $ gem install fixturator
80
+ ```
81
+
82
+ ## Contributing
83
+ Please submit specific issue reports if you see this working in a way you
84
+ wouldn't expect. PRs and discussion is welcomed and appreciated!
85
+
86
+ ## License
87
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Fixturator'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,41 @@
1
+ require "yaml"
2
+ require "fixturator/model"
3
+
4
+ module Fixturator
5
+ class Configuration
6
+ class << self
7
+ def load
8
+ hash = YAML.safe_load(config_file)
9
+ hash ? new(**hash.symbolize_keys!) : new
10
+ end
11
+
12
+ private
13
+
14
+ def config_file
15
+ return "" unless File.exist?(config_path)
16
+ File.read(config_path)
17
+ end
18
+
19
+ def config_path
20
+ Rails.root.join("config", "fixturator.yml")
21
+ end
22
+ end
23
+
24
+ def initialize(models: [], exclude_timestamps: false)
25
+ @models = models.map(&Model.method(:new)).compact
26
+ exclude_timestamps_from(@models) if exclude_timestamps
27
+ end
28
+
29
+ attr_reader :models
30
+
31
+ private
32
+
33
+ def exclude_timestamps_from(models)
34
+ models.each { |model| model.excluded_attributes.concat(timestamps) }
35
+ end
36
+
37
+ def timestamps
38
+ %w(created_at updated_at)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,69 @@
1
+ require "yaml"
2
+
3
+ module Fixturator
4
+ class Generator
5
+ def initialize(model, excluded_attributes: [])
6
+ @model = model
7
+ @excluded_attributes = excluded_attributes
8
+ end
9
+
10
+ def call
11
+ with_fixture_file do |file|
12
+ model.find_each.with_index do |record, index|
13
+ file.puts(fixturize(record, index + 1))
14
+ end
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :model, :excluded_attributes
21
+
22
+ def with_fixture_file(&block)
23
+ File.open(fixture_path, "w", &block)
24
+ end
25
+
26
+ def fixturize(record, index)
27
+ to_yaml(
28
+ "#{model_lower_name}_#{index}" => attributes_for(record)
29
+ )
30
+ end
31
+
32
+ def to_yaml(hash)
33
+ remove_yaml_header(hash.to_yaml) + "\n"
34
+ end
35
+
36
+ def remove_yaml_header(yaml)
37
+ yaml.sub(/\A---\n/, "")
38
+ end
39
+
40
+ def attributes_for(record)
41
+ record.
42
+ attributes.
43
+ compact.
44
+ tap(&method(:replace_times)).
45
+ except(*excluded_attributes)
46
+ end
47
+
48
+ def replace_times(attributes)
49
+ attributes["created_at"] = static_time if attributes["created_at"]
50
+ attributes["updated_at"] = static_time if attributes["updated_at"]
51
+ end
52
+
53
+ def static_time
54
+ Time.parse("2016-10-10").utc
55
+ end
56
+
57
+ def fixture_path
58
+ if Rails::VERSION::MAJOR > 5
59
+ ActiveSupport::TestCase.fixture_path
60
+ else
61
+ Rails.root.join("test", "fixtures", model_lower_name.pluralize + ".yml")
62
+ end
63
+ end
64
+
65
+ def model_lower_name
66
+ model.name.underscore.gsub("/", "_")
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,32 @@
1
+ require "fixturator/generator"
2
+
3
+ module Fixturator
4
+ class Model
5
+ class << self
6
+ def new(hash)
7
+ name = to_model(hash["name"]) or return nil
8
+
9
+ hash["name"] = name
10
+ super(hash)
11
+ end
12
+
13
+ private
14
+
15
+ def to_model(name)
16
+ name = name.safe_constantize or return nil
17
+ name.ancestors.include?(ActiveRecord::Base) ? name : nil
18
+ end
19
+ end
20
+
21
+ def initialize(hash)
22
+ @name = hash.fetch("name")
23
+ @excluded_attributes = hash.fetch("exclude", [])
24
+ end
25
+
26
+ attr_reader :name, :excluded_attributes
27
+
28
+ def to_fixture
29
+ Generator.new(name, excluded_attributes: excluded_attributes).call
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ module Fixturator
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load "tasks/fixtures_generate.rake"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Fixturator
2
+ VERSION = '1.0.0'
3
+ end
data/lib/fixturator.rb ADDED
@@ -0,0 +1,25 @@
1
+ require "fixturator/railtie"
2
+ require "fixturator/configuration"
3
+ require "fixturator/generator"
4
+
5
+ module Fixturator
6
+ class << self
7
+ def generate!
8
+ config = Configuration.load
9
+ yield config if block_given?
10
+
11
+ config.models.each do |model|
12
+ puts "Generating fixtures for #{model.name}..."
13
+ model.to_fixture
14
+ end
15
+ end
16
+
17
+ def call(*args)
18
+ Generator.new(*args).call
19
+ end
20
+
21
+ def to_proc
22
+ method(:call)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'fixturator'
2
+
3
+ namespace :db do
4
+ namespace :fixtures do
5
+
6
+ desc "Generate fixtures based off of active_record models."
7
+ task generate: :environment do
8
+ Fixturator.generate!
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fixturator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bernardo Farah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 4.1.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: mocha
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '='
38
+ - !ruby/object:Gem::Version
39
+ version: '1.1'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: '1.1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: pry
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 0.10.3
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 0.10.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: sqlite3
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ description: Fixturator is a fixture generator for ActiveRecord models
76
+ email:
77
+ - bernardo@coverhound.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - MIT-LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - lib/fixturator.rb
86
+ - lib/fixturator/configuration.rb
87
+ - lib/fixturator/generator.rb
88
+ - lib/fixturator/model.rb
89
+ - lib/fixturator/railtie.rb
90
+ - lib/fixturator/version.rb
91
+ - lib/tasks/fixtures_generate.rake
92
+ homepage: https://github.com/coverhound/fixturator
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Fixturator is a fixture generator for ActiveRecord models
116
+ test_files: []