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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 94ccab5f507787082888f5a4d3b0e858157d4bd3cc32a8e705a2f77ea3175780
4
- data.tar.gz: 2b318d3c952f5e743b9313ec66367410cc858c61aea41514c3006242612390ad
3
+ metadata.gz: 3e392bcbdfc2f58c8d401f10447c1266a15c16fffee00244f9c8628e81aea379
4
+ data.tar.gz: 6f9aedbd9499eb8928c010bbdf881218a56a8931beb157104df38517b0bf3c6a
5
5
  SHA512:
6
- metadata.gz: 2e7d02aebe3a9cdd0680d65f2a47c04f9dfafd2731d0cfaad8284b16e0ecd2189cda5d738359f298036c62f16cccb18456194da1c3bdc4878f90670f43eb7e3f
7
- data.tar.gz: 8ab2c97394a88cb8de4cf6ef62c766d30d2b09fc6a021b65543689fccaf4c1173f1e6e07588bbe4873bf6f414e4e5104ebe90bb805a3d182ab0926e1197855a8
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
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
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 creating a positive environment
15
- include:
18
+ Examples of behavior that contributes to a positive environment for our
19
+ community include:
16
20
 
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
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 by participants include:
29
+ Examples of unacceptable behavior include:
24
30
 
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
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 electronic
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
- ## Our Responsibilities
40
+ ## Enforcement Responsibilities
35
41
 
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
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
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
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 both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
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 by contacting the project team at peter.boling@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
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
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
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], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
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]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
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://travis-ci.org/pboling/anonymous_active_record.svg?branch=master)](https://travis-ci.org/pboling/anonymous_active_record) |
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 ~♡ⓛⓞⓥⓔ♡~ | [🌍 🌎 🌏](https://about.me/peter.boling), [🍚](https://www.crowdrise.com/helprefugeeswithhopefortomorrowliberia/fundraiser/peterboling), [➕](https://plus.google.com/+PeterBoling/posts), [👼](https://angel.co/peter-boling), [🐛](https://www.topcoder.com/members/pboling/), [:shipit:](http://coderwall.com/pboling), [![Tweet Peter](https://img.shields.io/twitter/follow/galtzo.svg?style=social&label=Follow)](http://twitter.com/galtzo) |
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' ? true : false # only Spot has a balloon
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
- let(:ar_with_balloon) do
61
- Class.new(ActiveRecord::Base) do
62
- attr_accessor :name
63
- include HasBalloon
64
- def flowery_name
65
- "#{b_f}#{name}#{b_f}"
66
- end
67
- def b_f
68
- has_balloon? ? '🎈' : '🌸'
69
- end
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
- let(:ar_with_balloon) do
78
- AnonymousActiveRecord.generate(columns: ['name']) do
79
- include HasBalloon
80
- def flowery_name
81
- "#{b_f}#{name}#{b_f}"
82
- end
83
- def b_f
84
- has_balloon? ? '🎈' : '🌸'
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
- it 'can test the module' do
89
- expect(ar_with_balloon.new(name: 'Spot').flowery_name).to eq('🎈Spot🎈')
90
- expect(ar_with_balloon.new(name: 'Not Spot').flowery_name).to eq('🌸Not Spot🌸')
91
- end
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
- table_name: 'a_table_name',
99
- # if table_name is not set klass_basename will be used to derive a unique random table_name
100
- # default is a unique random table name
101
- klass_basename: 'anons', # is default
102
- columns: ['name'],
103
- # columns default is [],
104
- # meaning class will have ['id', 'created_at', 'updated_at'], as the AR defaults
105
- # Optionally provide an array of hashes and thereby designate column type:
106
- # [{name: 'name', type: 'string'}, {name: 'baked_at', type: 'time'}]
107
- timestamps: true, # is default
108
- indexes: [{columns: ['name'], unique: true}],
109
- # indexes default is [],
110
- # meaning class will have no indexes, as the AR defaults
111
- # Optionally provide an array of hashes of index options (similar to those used in Rails migrations):
112
- # [{columns: ['name'], unique: true}, {columns: ['baked_at']}]
113
- connection_params: { adapter: 'sqlite3', encoding: 'utf8', database: ':memory:' } # is default
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
- source_data: [{name: 'Phil'}, {name: 'Vickie'}],
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, **col)
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
- column_names = idx_options.delete(:columns)
63
- t.index column_names, **idx_options
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(source_data: source_data, table_name: table_name, klass_namespaces: klass_namespaces, klass_basename: klass_basename, columns: columns, indexes: indexes, timestamps: timestamps, connection_params: connection_params, &block)
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(source_data: source_data, table_name: table_name, klass_namespaces: klass_namespaces, klass_basename: klass_basename, columns: columns, indexes: indexes, timestamps: timestamps, connection_params: connection_params, &block)
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(table_name: table_name, klass_namespaces: klass_namespaces, klass_basename: klass_basename, columns: columns, timestamps: timestamps, indexes: indexes, connection_params: connection_params, &block)
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
  # records = AnonymousActiveRecord.factory(source_data: [{name: 'Bob McGurdy'}], columns: ['name']) do
@@ -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'.freeze
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, SecureRandom.uuid.gsub(/[^a-z0-9]+/i, '_')].join('_')
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} < ActiveRecord::Base; end"
36
+ eval "class ::#{klass_name} < #{parent_klass}; end"
33
37
  klass = Kernel.const_get(klass_name, true)
34
- klass.class_eval(&block) if block_given?
38
+ klass.class_eval(&block) if block
35
39
  klass.table_name = table_name
36
40
  klass
37
41
  end