anonymous_active_record 1.0.1 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +105 -46
- data/README.md +47 -46
- data/lib/anonymous_active_record.rb +31 -11
- 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: 3e392bcbdfc2f58c8d401f10447c1266a15c16fffee00244f9c8628e81aea379
|
4
|
+
data.tar.gz: 6f9aedbd9499eb8928c010bbdf881218a56a8931beb157104df38517b0bf3c6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e76fd8bc9db439e5861855f5b027270e889face0f42fc24b4d0fd2f34d442a1b56d68035c310438ff3ffae15cab150d880e17106b5eae28978ef924b5964b6fe
|
7
|
+
data.tar.gz: 7f0aaf826a1ddd3d506536f6904ce6f09759a65cfd2b3001e4f69253693dcaeca9bd2a0a730ee845d6208d1c730406da939cef391154cc781e355b33cbeb60b2
|
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 | [![Downloads Today](https://img.shields.io/gem/rd/anonymous_active_record.svg)](https://github.com/pboling/anonymous_active_record) |
|
12
12
|
| version | [![Version](https://img.shields.io/gem/v/anonymous_active_record.svg)](https://rubygems.org/gems/anonymous_active_record) |
|
13
13
|
| dependencies | [![Depfu](https://badges.depfu.com/badges/272ce0df3bc6df5cbea9354e2c3b65af/count.svg)](https://depfu.com/github/pboling/anonymous_active_record?project_id=5614) |
|
14
|
-
| continuous integration | [![Build Status](https://
|
14
|
+
| continuous integration | [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fpboling%2Fanonymous_active_record%2Fbadge&style=flat)](https://actions-badge.atrox.dev/pboling/anonymous_active_record/goto) |
|
15
15
|
| test coverage | [![Test Coverage](https://api.codeclimate.com/v1/badges/fe504d4ab2fb77cecf7d/test_coverage)](https://codeclimate.com/github/pboling/anonymous_active_record/test_coverage) |
|
16
16
|
| maintainability | [![Maintainability](https://api.codeclimate.com/v1/badges/fe504d4ab2fb77cecf7d/maintainability)](https://codeclimate.com/github/pboling/anonymous_active_record/maintainability) |
|
17
17
|
| code triage | [![Open Source Helpers](https://www.codetriage.com/pboling/anonymous_active_record/badges/users.svg)](https://www.codetriage.com/pboling/anonymous_active_record) |
|
18
18
|
| FOSSA Licenses | [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fpboling%2Fanonymous_active_record.svg?type=shield)](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), [![Tweet Peter](https://img.shields.io/twitter/follow/galtzo.svg?style=social&label=Follow)](http://twitter.com/galtzo), [🌹](https://nationalprogressiveparty.org) |
|
22
22
|
|
23
23
|
## Installation
|
24
24
|
|
@@ -49,7 +49,7 @@ Let's say you want to write specs for a module, `HasBalloon`, which provides a m
|
|
49
49
|
```ruby
|
50
50
|
module HasBalloon
|
51
51
|
def has_balloon?
|
52
|
-
name == 'Spot'
|
52
|
+
name == 'Spot' # only Spot has a balloon
|
53
53
|
end
|
54
54
|
end
|
55
55
|
```
|
@@ -57,61 +57,62 @@ end
|
|
57
57
|
This won't work [(really!)](https://github.com/rails/rails/issues/8934):
|
58
58
|
|
59
59
|
```ruby
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
60
|
+
let(:ar_with_balloon) do
|
61
|
+
Class.new(ActiveRecord::Base) do
|
62
|
+
attr_accessor :name
|
63
|
+
|
64
|
+
include HasBalloon
|
65
|
+
def flowery_name
|
66
|
+
"#{b_f}#{name}#{b_f}"
|
67
|
+
end
|
68
|
+
def b_f
|
69
|
+
has_balloon? ? '🎈' : '🌸'
|
70
70
|
end
|
71
71
|
end
|
72
|
+
end
|
72
73
|
```
|
73
74
|
|
74
75
|
So do this instead:
|
75
76
|
|
76
77
|
```ruby
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
end
|
78
|
+
let(:ar_with_balloon) do
|
79
|
+
AnonymousActiveRecord.generate(columns: ['name']) do
|
80
|
+
include HasBalloon
|
81
|
+
def flowery_name
|
82
|
+
"#{b_f}#{name}#{b_f}"
|
83
|
+
end
|
84
|
+
def b_f
|
85
|
+
has_balloon? ? '🎈' : '🌸'
|
86
86
|
end
|
87
87
|
end
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
88
|
+
end
|
89
|
+
it 'can test the module' do
|
90
|
+
expect(ar_with_balloon.new(name: 'Spot').flowery_name).to eq('🎈Spot🎈')
|
91
|
+
expect(ar_with_balloon.new(name: 'Not Spot').flowery_name).to eq('🌸Not Spot🌸')
|
92
|
+
end
|
92
93
|
```
|
93
94
|
|
94
95
|
### Generate Options
|
95
96
|
|
96
97
|
```ruby
|
97
98
|
AnonymousActiveRecord.generate(
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
) do
|
99
|
+
table_name: 'a_table_name',
|
100
|
+
# if table_name is not set klass_basename will be used to derive a unique random table_name
|
101
|
+
# default is a unique random table name
|
102
|
+
klass_basename: 'anons', # is default
|
103
|
+
columns: ['name'],
|
104
|
+
# columns default is [],
|
105
|
+
# meaning class will have ['id', 'created_at', 'updated_at'], as the AR defaults
|
106
|
+
# Optionally provide an array of hashes and thereby designate column type:
|
107
|
+
# [{name: 'name', type: 'string'}, {name: 'baked_at', type: 'time'}]
|
108
|
+
timestamps: true, # is default
|
109
|
+
indexes: [{ columns: ['name'], unique: true }],
|
110
|
+
# indexes default is [],
|
111
|
+
# meaning class will have no indexes, as the AR defaults
|
112
|
+
# Optionally provide an array of hashes of index options (similar to those used in Rails migrations):
|
113
|
+
# [{columns: ['name'], unique: true}, {columns: ['baked_at']}]
|
114
|
+
connection_params: { adapter: 'sqlite3', encoding: 'utf8', database: ':memory:' } # is default
|
115
|
+
) do
|
115
116
|
# code which becomes part of the class definition
|
116
117
|
end
|
117
118
|
```
|
@@ -122,10 +123,10 @@ The block is optional.
|
|
122
123
|
|
123
124
|
```ruby
|
124
125
|
AnonymousActiveRecord.factory(
|
125
|
-
|
126
|
+
source_data: [{ name: 'Phil' }, { name: 'Vickie' }]
|
126
127
|
# Array of hashes, where each hash represents a record that will be created
|
127
128
|
# ... The rest of the options are the same as for generate, see above.
|
128
|
-
) do
|
129
|
+
) do
|
129
130
|
# same as for generate, see above.
|
130
131
|
end
|
131
132
|
```
|
@@ -170,7 +171,7 @@ spec.add_dependency 'anonymous_active_record', '~> 0.0'
|
|
170
171
|
|
171
172
|
* Copyright (c) 2018 [Peter H. Boling][peterboling] of [Rails Bling][railsbling]
|
172
173
|
|
173
|
-
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
|
174
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
|
174
175
|
|
175
176
|
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fpboling%2Fanonymous_active_record.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fpboling%2Fanonymous_active_record?ref=badge_large)
|
176
177
|
|
@@ -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,14 +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
|
-
t.column(name, type,
|
58
|
+
t.column(name, type, col)
|
59
|
+
elsif col.is_a?(Array)
|
60
|
+
t.column col[0], col[-1] || :string
|
57
61
|
else
|
58
62
|
t.column col, :string
|
59
63
|
end
|
60
64
|
end
|
61
65
|
indexes.each do |idx_options|
|
62
|
-
|
63
|
-
|
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
|
64
78
|
end
|
65
79
|
t.timestamps if timestamps
|
66
80
|
end
|
@@ -68,21 +82,27 @@ module AnonymousActiveRecord
|
|
68
82
|
end
|
69
83
|
|
70
84
|
# Initializes instances of a pseudo anonymous class in a particular namespace of your choosing.
|
71
|
-
def factory(source_data: [], table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
72
|
-
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
|
+
)
|
73
89
|
factory.run
|
74
90
|
end
|
75
91
|
|
76
92
|
# Initializes instances of a pseudo anonymous class in a particular namespace of your choosing.
|
77
|
-
def factory!(source_data: [], table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
78
|
-
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
|
+
)
|
79
97
|
factory.run!
|
80
98
|
end
|
81
99
|
|
82
100
|
private
|
83
101
|
|
84
|
-
def _factory(source_data: [], table_name: nil, klass_namespaces: [], klass_basename: nil, columns: [], indexes: [], timestamps: true, connection_params: DEFAULT_CONNECTION_PARAMS, &block)
|
85
|
-
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
|
+
)
|
86
106
|
AnonymousActiveRecord::Factory.new(source_data, klass)
|
87
107
|
end
|
88
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
|