anonymous_active_record 1.0.2 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +105 -46
- data/README.md +52 -46
- data/lib/anonymous_active_record.rb +31 -15
- data/lib/anonymous_active_record/factory.rb +2 -0
- data/lib/anonymous_active_record/generator.rb +10 -6
- data/lib/anonymous_active_record/version.rb +3 -1
- data/spec/anonymous_active_record_spec.rb +1025 -0
- data/spec/db/database.yml +4 -0
- data/spec/spec_helper.rb +22 -0
- metadata +115 -32
- data/.gitignore +0 -13
- data/.rspec +0 -3
- data/.travis.yml +0 -32
- data/Gemfile +0 -13
- data/Rakefile +0 -6
- data/anonymous_active_record.gemspec +0 -33
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 07dc07f9d324b2e9a0eb1bbe5df6950c1bb13e6b2ce482e3a4c7fb120dd2ed52
|
|
4
|
+
data.tar.gz: 284a9119d3f2435a35eb7264caff017d04840d4839698636ecbef5df6b2311b2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aa09c958e0f3bfd13a3da9e66df741c738a23b3766f1991410b7ad929d5bd5a4571e974a9a84e7043a40850c89bc80280238fb95e9d4da9ee8ed7859618b2a8a
|
|
7
|
+
data.tar.gz: db09ff69d00af99b04f192c43e52491317f44bbafa77c76dd3d34243a03cd7124bfadea8153e8964f5fd475091f273433c87029475479fb4a98d488ca737f3be
|
data/CODE_OF_CONDUCT.md
CHANGED
|
@@ -1,74 +1,133 @@
|
|
|
1
|
+
|
|
1
2
|
# Contributor Covenant Code of Conduct
|
|
2
3
|
|
|
3
4
|
## Our Pledge
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
nationality, personal appearance, race, religion, or sexual identity
|
|
10
|
-
orientation.
|
|
6
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
7
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
9
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
10
|
+
nationality, personal appearance, race, religion, or sexual identity
|
|
11
|
+
and orientation.
|
|
12
|
+
|
|
13
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
14
|
+
diverse, inclusive, and healthy community.
|
|
11
15
|
|
|
12
16
|
## Our Standards
|
|
13
17
|
|
|
14
|
-
Examples of behavior that contributes to
|
|
15
|
-
include:
|
|
18
|
+
Examples of behavior that contributes to a positive environment for our
|
|
19
|
+
community include:
|
|
16
20
|
|
|
17
|
-
*
|
|
18
|
-
* Being respectful of differing viewpoints and experiences
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
|
|
21
|
+
* Demonstrating empathy and kindness toward other people
|
|
22
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
23
|
+
* Giving and gracefully accepting constructive feedback
|
|
24
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
25
|
+
and learning from the experience
|
|
26
|
+
* Focusing on what is best not just for us as individuals, but for the
|
|
27
|
+
overall community
|
|
22
28
|
|
|
23
|
-
Examples of unacceptable behavior
|
|
29
|
+
Examples of unacceptable behavior include:
|
|
24
30
|
|
|
25
|
-
* The use of sexualized language or imagery and
|
|
26
|
-
advances
|
|
27
|
-
* Trolling, insulting
|
|
31
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
32
|
+
advances of any kind
|
|
33
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
28
34
|
* Public or private harassment
|
|
29
|
-
* Publishing others' private information, such as a physical or
|
|
30
|
-
address, without explicit permission
|
|
35
|
+
* Publishing others' private information, such as a physical or email
|
|
36
|
+
address, without their explicit permission
|
|
31
37
|
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
38
|
professional setting
|
|
33
39
|
|
|
34
|
-
##
|
|
40
|
+
## Enforcement Responsibilities
|
|
35
41
|
|
|
36
|
-
|
|
37
|
-
behavior and
|
|
38
|
-
response to any
|
|
42
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
43
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
44
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
45
|
+
or harmful.
|
|
39
46
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
threatening, offensive, or harmful.
|
|
47
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
48
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
49
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
50
|
+
decisions when appropriate.
|
|
45
51
|
|
|
46
52
|
## Scope
|
|
47
53
|
|
|
48
|
-
This Code of Conduct applies
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
representative at an online or offline event.
|
|
53
|
-
further defined and clarified by project maintainers.
|
|
54
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
55
|
+
an individual is officially representing the community in public spaces.
|
|
56
|
+
Examples of representing our community include using an official e-mail address,
|
|
57
|
+
posting via an official social media account, or acting as an appointed
|
|
58
|
+
representative at an online or offline event.
|
|
54
59
|
|
|
55
60
|
## Enforcement
|
|
56
61
|
|
|
57
62
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
-
reported
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
reported to the community leaders responsible for enforcement at
|
|
64
|
+
[INSERT CONTACT METHOD].
|
|
65
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
66
|
+
|
|
67
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
68
|
+
reporter of any incident.
|
|
69
|
+
|
|
70
|
+
## Enforcement Guidelines
|
|
71
|
+
|
|
72
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
73
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
74
|
+
|
|
75
|
+
### 1. Correction
|
|
76
|
+
|
|
77
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
78
|
+
unprofessional or unwelcome in the community.
|
|
79
|
+
|
|
80
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
81
|
+
clarity around the nature of the violation and an explanation of why the
|
|
82
|
+
behavior was inappropriate. A public apology may be requested.
|
|
83
|
+
|
|
84
|
+
### 2. Warning
|
|
63
85
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
86
|
+
**Community Impact**: A violation through a single incident or series
|
|
87
|
+
of actions.
|
|
88
|
+
|
|
89
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
90
|
+
interaction with the people involved, including unsolicited interaction with
|
|
91
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
92
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
93
|
+
like social media. Violating these terms may lead to a temporary or
|
|
94
|
+
permanent ban.
|
|
95
|
+
|
|
96
|
+
### 3. Temporary Ban
|
|
97
|
+
|
|
98
|
+
**Community Impact**: A serious violation of community standards, including
|
|
99
|
+
sustained inappropriate behavior.
|
|
100
|
+
|
|
101
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
102
|
+
communication with the community for a specified period of time. No public or
|
|
103
|
+
private interaction with the people involved, including unsolicited interaction
|
|
104
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
105
|
+
Violating these terms may lead to a permanent ban.
|
|
106
|
+
|
|
107
|
+
### 4. Permanent Ban
|
|
108
|
+
|
|
109
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
110
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
111
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
112
|
+
|
|
113
|
+
**Consequence**: A permanent ban from any sort of public interaction within
|
|
114
|
+
the community.
|
|
67
115
|
|
|
68
116
|
## Attribution
|
|
69
117
|
|
|
70
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
71
|
-
available at
|
|
118
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
119
|
+
version 2.0, available at
|
|
120
|
+
[https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0].
|
|
121
|
+
|
|
122
|
+
Community Impact Guidelines were inspired by
|
|
123
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
124
|
+
|
|
125
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
126
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available
|
|
127
|
+
at [https://www.contributor-covenant.org/translations][translations].
|
|
72
128
|
|
|
73
|
-
[homepage]:
|
|
74
|
-
[
|
|
129
|
+
[homepage]: https://www.contributor-covenant.org
|
|
130
|
+
[v2.0]: https://www.contributor-covenant.org/version/2/0/code_of_conduct.html
|
|
131
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
132
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
133
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/README.md
CHANGED
|
@@ -11,14 +11,14 @@ Warning: Use of this gem is a **security risk**, due to the use of Ruby's `eval`
|
|
|
11
11
|
| download rank | [](https://github.com/pboling/anonymous_active_record) |
|
|
12
12
|
| version | [](https://rubygems.org/gems/anonymous_active_record) |
|
|
13
13
|
| dependencies | [](https://depfu.com/github/pboling/anonymous_active_record?project_id=5614) |
|
|
14
|
-
| continuous integration | [](https://actions-badge.atrox.dev/pboling/anonymous_active_record/goto) |
|
|
15
15
|
| test coverage | [](https://codeclimate.com/github/pboling/anonymous_active_record/test_coverage) |
|
|
16
16
|
| maintainability | [](https://codeclimate.com/github/pboling/anonymous_active_record/maintainability) |
|
|
17
17
|
| code triage | [](https://www.codetriage.com/pboling/anonymous_active_record) |
|
|
18
18
|
| FOSSA Licenses | [](https://app.fossa.io/projects/git%2Bgithub.com%2Fpboling%2Fanonymous_active_record?ref=badge_shield) |
|
|
19
19
|
| homepage | [on Github.com][homepage], [on Railsbling.com][blogpage] |
|
|
20
20
|
| documentation | [on RDoc.info][documentation] |
|
|
21
|
-
| Spread ~♡ⓛⓞⓥⓔ♡~ | [
|
|
21
|
+
| Spread ~♡ⓛⓞⓥⓔ♡~ | [🌏](https://about.me/peter.boling), [👼](https://angel.co/peter-boling), [:shipit:](http://coderwall.com/pboling), [](http://twitter.com/galtzo), [🌹](https://nationalprogressiveparty.org) |
|
|
22
22
|
|
|
23
23
|
## Installation
|
|
24
24
|
|
|
@@ -36,6 +36,11 @@ Or install it yourself as:
|
|
|
36
36
|
|
|
37
37
|
$ gem install anonymous_active_record
|
|
38
38
|
|
|
39
|
+
## Compatibility
|
|
40
|
+
|
|
41
|
+
This gem is compatible with, as of Feb 2021:
|
|
42
|
+
• Ruby 2.4, 2.5, 2.6, 2.7, 3.0, ruby-head
|
|
43
|
+
|
|
39
44
|
## Usage
|
|
40
45
|
|
|
41
46
|
Require the library in your `spec_helper` or other test suite boot file.
|
|
@@ -49,7 +54,7 @@ Let's say you want to write specs for a module, `HasBalloon`, which provides a m
|
|
|
49
54
|
```ruby
|
|
50
55
|
module HasBalloon
|
|
51
56
|
def has_balloon?
|
|
52
|
-
name == 'Spot'
|
|
57
|
+
name == 'Spot' # only Spot has a balloon
|
|
53
58
|
end
|
|
54
59
|
end
|
|
55
60
|
```
|
|
@@ -57,61 +62,62 @@ end
|
|
|
57
62
|
This won't work [(really!)](https://github.com/rails/rails/issues/8934):
|
|
58
63
|
|
|
59
64
|
```ruby
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
let(:ar_with_balloon) do
|
|
66
|
+
Class.new(ActiveRecord::Base) do
|
|
67
|
+
attr_accessor :name
|
|
68
|
+
|
|
69
|
+
include HasBalloon
|
|
70
|
+
def flowery_name
|
|
71
|
+
"#{b_f}#{name}#{b_f}"
|
|
72
|
+
end
|
|
73
|
+
def b_f
|
|
74
|
+
has_balloon? ? '🎈' : '🌸'
|
|
70
75
|
end
|
|
71
76
|
end
|
|
77
|
+
end
|
|
72
78
|
```
|
|
73
79
|
|
|
74
80
|
So do this instead:
|
|
75
81
|
|
|
76
82
|
```ruby
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
end
|
|
83
|
+
let(:ar_with_balloon) do
|
|
84
|
+
AnonymousActiveRecord.generate(columns: ['name']) do
|
|
85
|
+
include HasBalloon
|
|
86
|
+
def flowery_name
|
|
87
|
+
"#{b_f}#{name}#{b_f}"
|
|
88
|
+
end
|
|
89
|
+
def b_f
|
|
90
|
+
has_balloon? ? '🎈' : '🌸'
|
|
86
91
|
end
|
|
87
92
|
end
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
93
|
+
end
|
|
94
|
+
it 'can test the module' do
|
|
95
|
+
expect(ar_with_balloon.new(name: 'Spot').flowery_name).to eq('🎈Spot🎈')
|
|
96
|
+
expect(ar_with_balloon.new(name: 'Not Spot').flowery_name).to eq('🌸Not Spot🌸')
|
|
97
|
+
end
|
|
92
98
|
```
|
|
93
99
|
|
|
94
100
|
### Generate Options
|
|
95
101
|
|
|
96
102
|
```ruby
|
|
97
103
|
AnonymousActiveRecord.generate(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
) do
|
|
104
|
+
table_name: 'a_table_name',
|
|
105
|
+
# if table_name is not set klass_basename will be used to derive a unique random table_name
|
|
106
|
+
# default is a unique random table name
|
|
107
|
+
klass_basename: 'anons', # is default
|
|
108
|
+
columns: ['name'],
|
|
109
|
+
# columns default is [],
|
|
110
|
+
# meaning class will have ['id', 'created_at', 'updated_at'], as the AR defaults
|
|
111
|
+
# Optionally provide an array of hashes and thereby designate column type:
|
|
112
|
+
# [{name: 'name', type: 'string'}, {name: 'baked_at', type: 'time'}]
|
|
113
|
+
timestamps: true, # is default
|
|
114
|
+
indexes: [{ columns: ['name'], unique: true }],
|
|
115
|
+
# indexes default is [],
|
|
116
|
+
# meaning class will have no indexes, as the AR defaults
|
|
117
|
+
# Optionally provide an array of hashes of index options (similar to those used in Rails migrations):
|
|
118
|
+
# [{columns: ['name'], unique: true}, {columns: ['baked_at']}]
|
|
119
|
+
connection_params: { adapter: 'sqlite3', encoding: 'utf8', database: ':memory:' } # is default
|
|
120
|
+
) do
|
|
115
121
|
# code which becomes part of the class definition
|
|
116
122
|
end
|
|
117
123
|
```
|
|
@@ -122,10 +128,10 @@ The block is optional.
|
|
|
122
128
|
|
|
123
129
|
```ruby
|
|
124
130
|
AnonymousActiveRecord.factory(
|
|
125
|
-
|
|
131
|
+
source_data: [{ name: 'Phil' }, { name: 'Vickie' }]
|
|
126
132
|
# Array of hashes, where each hash represents a record that will be created
|
|
127
133
|
# ... The rest of the options are the same as for generate, see above.
|
|
128
|
-
) do
|
|
134
|
+
) do
|
|
129
135
|
# same as for generate, see above.
|
|
130
136
|
end
|
|
131
137
|
```
|
|
@@ -170,7 +176,7 @@ spec.add_dependency 'anonymous_active_record', '~> 0.0'
|
|
|
170
176
|
|
|
171
177
|
* Copyright (c) 2018 [Peter H. Boling][peterboling] of [Rails Bling][railsbling]
|
|
172
178
|
|
|
173
|
-
[](https://opensource.org/licenses/MIT)
|
|
179
|
+
[](https://opensource.org/licenses/MIT)
|
|
174
180
|
|
|
175
181
|
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fpboling%2Fanonymous_active_record?ref=badge_large)
|
|
176
182
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'active_record'
|
|
2
4
|
|
|
3
5
|
require 'anonymous_active_record/version'
|
|
@@ -42,8 +44,8 @@ module AnonymousActiveRecord
|
|
|
42
44
|
}.freeze
|
|
43
45
|
|
|
44
46
|
# Defines a pseudo anonymous class in a particular namespace of your choosing.
|
|
45
|
-
def generate(table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
|
46
|
-
gen = AnonymousActiveRecord::Generator.new(table_name, klass_namespaces, klass_basename)
|
|
47
|
+
def generate(table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, parent_klass: 'ActiveRecord::Base', connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
|
48
|
+
gen = AnonymousActiveRecord::Generator.new(table_name, klass_namespaces, klass_basename, parent_klass)
|
|
47
49
|
klass = gen.generate(&block)
|
|
48
50
|
connection_params = YAML.load_file(connection_params) if connection_params.is_a?(String)
|
|
49
51
|
klass.establish_connection(connection_params.dup)
|
|
@@ -53,18 +55,26 @@ module AnonymousActiveRecord
|
|
|
53
55
|
# :name and :type are required at minimum
|
|
54
56
|
name = col.delete(:name)
|
|
55
57
|
type = col.delete(:type)
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
t.column(name, type, **col)
|
|
60
|
-
end
|
|
58
|
+
t.column(name, type, **col)
|
|
59
|
+
elsif col.is_a?(Array)
|
|
60
|
+
t.column col[0], col[-1] || :string
|
|
61
61
|
else
|
|
62
62
|
t.column col, :string
|
|
63
63
|
end
|
|
64
64
|
end
|
|
65
65
|
indexes.each do |idx_options|
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
if idx_options.is_a?(Hash)
|
|
67
|
+
column_names = idx_options.delete(:columns)
|
|
68
|
+
t.index column_names, **idx_options
|
|
69
|
+
elsif idx_options.is_a?(Array)
|
|
70
|
+
if idx_options.length == 1
|
|
71
|
+
t.index idx_options[0]
|
|
72
|
+
else
|
|
73
|
+
t.index idx_options[0], **idx_options[-1]
|
|
74
|
+
end
|
|
75
|
+
else
|
|
76
|
+
t.index idx_options
|
|
77
|
+
end
|
|
68
78
|
end
|
|
69
79
|
t.timestamps if timestamps
|
|
70
80
|
end
|
|
@@ -72,21 +82,27 @@ module AnonymousActiveRecord
|
|
|
72
82
|
end
|
|
73
83
|
|
|
74
84
|
# Initializes instances of a pseudo anonymous class in a particular namespace of your choosing.
|
|
75
|
-
def factory(source_data: [], table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
|
76
|
-
factory = _factory(
|
|
85
|
+
def factory(source_data: [], table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, parent_klass: 'ActiveRecord::Base', connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
|
86
|
+
factory = _factory(
|
|
87
|
+
source_data: source_data, table_name: table_name, klass_namespaces: klass_namespaces, klass_basename: klass_basename, columns: columns, indexes: indexes, timestamps: timestamps, parent_klass: parent_klass, connection_params: connection_params, &block
|
|
88
|
+
)
|
|
77
89
|
factory.run
|
|
78
90
|
end
|
|
79
91
|
|
|
80
92
|
# Initializes instances of a pseudo anonymous class in a particular namespace of your choosing.
|
|
81
|
-
def factory!(source_data: [], table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
|
82
|
-
factory = _factory(
|
|
93
|
+
def factory!(source_data: [], table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, parent_klass: 'ActiveRecord::Base', connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
|
94
|
+
factory = _factory(
|
|
95
|
+
source_data: source_data, table_name: table_name, klass_namespaces: klass_namespaces, klass_basename: klass_basename, columns: columns, indexes: indexes, timestamps: timestamps, parent_klass: parent_klass, connection_params: connection_params, &block
|
|
96
|
+
)
|
|
83
97
|
factory.run!
|
|
84
98
|
end
|
|
85
99
|
|
|
86
100
|
private
|
|
87
101
|
|
|
88
|
-
def _factory(source_data: [], table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
|
89
|
-
klass = generate(
|
|
102
|
+
def _factory(source_data: [], table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, parent_klass: 'ActiveRecord::Base', connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
|
103
|
+
klass = generate(
|
|
104
|
+
table_name: table_name, klass_namespaces: klass_namespaces, klass_basename: klass_basename, columns: columns, timestamps: timestamps, parent_klass: parent_klass, indexes: indexes, connection_params: connection_params, &block
|
|
105
|
+
)
|
|
90
106
|
AnonymousActiveRecord::Factory.new(source_data, klass)
|
|
91
107
|
end
|
|
92
108
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Usage:
|
|
2
4
|
#
|
|
3
5
|
# klass = AnonymousActiveRecord.generate(columns: ['name']) do
|
|
@@ -12,16 +14,18 @@
|
|
|
12
14
|
module AnonymousActiveRecord
|
|
13
15
|
# Generator defines a pseudo anonymous class in a particular namespace of your choosing.
|
|
14
16
|
class Generator
|
|
15
|
-
DEFAULT_NAME = 'anons'
|
|
17
|
+
DEFAULT_NAME = 'anons'
|
|
16
18
|
|
|
17
|
-
attr_reader :klass_namespaces, :table_name, :klass_name
|
|
19
|
+
attr_reader :klass_namespaces, :table_name, :klass_name, :parent_klass
|
|
18
20
|
|
|
19
|
-
def initialize(table_name, klass_namespaces = [], klass_basename = nil)
|
|
21
|
+
def initialize(table_name, klass_namespaces = [], klass_basename = nil, parent_klass = 'ActiveRecord::Base')
|
|
20
22
|
@klass_namespaces = Array(klass_namespaces).map(&:to_s).map { |x| x.gsub(/[^a-z0-9]+/i, '_') }
|
|
21
|
-
klass_context = [(klass_basename || DEFAULT_NAME).gsub(/[^a-z0-9]+/i, '_').capitalize,
|
|
23
|
+
klass_context = [(klass_basename || DEFAULT_NAME).gsub(/[^a-z0-9]+/i, '_').capitalize,
|
|
24
|
+
SecureRandom.uuid.gsub(/[^a-z0-9]+/i, '_')].join('_')
|
|
22
25
|
@table_name = (table_name || klass_context).downcase.to_s
|
|
23
26
|
# String#capitalize is Ruby >= 2.4, https://stackoverflow.com/a/3725154/213191
|
|
24
27
|
@klass_name = (klass_namespaces << klass_context).join('::')
|
|
28
|
+
@parent_klass = parent_klass
|
|
25
29
|
end
|
|
26
30
|
|
|
27
31
|
def generate(&block)
|
|
@@ -29,9 +33,9 @@ module AnonymousActiveRecord
|
|
|
29
33
|
# Class.new(ActiveRecord::Base)
|
|
30
34
|
# which was useful for specs, and other use cases. But it is now explicitly not supported, see
|
|
31
35
|
# https://github.com/rails/rails/issues/8934
|
|
32
|
-
eval "class ::#{klass_name} <
|
|
36
|
+
eval "class ::#{klass_name} < #{parent_klass}; end"
|
|
33
37
|
klass = Kernel.const_get(klass_name, true)
|
|
34
|
-
klass.class_eval(&block) if
|
|
38
|
+
klass.class_eval(&block) if block
|
|
35
39
|
klass.table_name = table_name
|
|
36
40
|
klass
|
|
37
41
|
end
|