taylors_enum 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: 22eddc0a8e5096da1be305ffcaf244c99e9cd53f502acf7d5d3827d64ceb5781
4
- data.tar.gz: 9dcbda215ceb54dc6aef75950ad45dfa8da07e20c88ac456f1aac22804f0c5e6
3
+ metadata.gz: b9bf0b48903cac0265a19a6a4481b7bdb7b83fb84d75c5e2aba516854ffb9641
4
+ data.tar.gz: 9484a8aaebc4dd57bb98bbaad251ce98acad9b3890e2d6a442e6df973b29531c
5
5
  SHA512:
6
- metadata.gz: 7c8cd3171a1e074a18550b8055ba93cb008f636ac9d350dddbe7387a9e4d5848fc00565aeff7fb6a9382fffa3afa555e7f1e27033de0ebc53cbb525677390a7f
7
- data.tar.gz: d672df8ce78450646179c625f27e596a74e9118f64140db5addf70b6c1d933049a3d368a143742222c9f5b15e8b7a5d076582b09a8c64de286e06de35701f4c5
6
+ metadata.gz: 3973e6ebeb788496ef8dcd6f66830cc92c40c8eda2e5c815da1b19d54d03c54ca83a68ec5bc13cf34dc0a4d8ae3f260d19cfd7e6e73a236f22accde757570de9
7
+ data.tar.gz: 4180f8d0860eb50afd1c3f04965d537789a42fd3c348a0a506806c8ebb6f39aec594d51faf89a37f8b0b75a396450d9cd01135a32bd630aaebc48f64f6f84aef
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at peter.wdj@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # TaylorsEnum
1
+ # taylors_enum
2
2
 
3
- TaylorsEnum is a gem that builds on top of ActiveRecord's built in [enums](https://api.rubyonrails.org/v5.2.4.4/classes/ActiveRecord/Enum.html#method-i-enum). Specifically, it will:
3
+ taylors_enum is a gem that builds on top of ActiveRecord's built in [enums](https://api.rubyonrails.org/v5.2.4.4/classes/ActiveRecord/Enum.html#method-i-enum). Specifically, it will:
4
4
 
5
5
  ###### - Define additional methods to make it clearer which values exist in Rails-land, and which in the database
6
6
  In Rails:
@@ -14,7 +14,7 @@ end
14
14
  => {"debut"=>0, "fearless"=>1, "speak_now"=>2, "red"=>3, "nineteen_eighty_nine"=>4, "reputation"=>5, "lover"=>6, "folklore"=>7, "evermore"=>8}
15
15
  ```
16
16
 
17
- With TaylorsEnum:
17
+ With taylors_enum:
18
18
  ```
19
19
  class AlbumBase < Album
20
20
  taylors_enum name: %w[debut fearless speak_now red nineteen_eighty_nine reputation lover folklore evermore]
@@ -94,7 +94,7 @@ end
94
94
  ```
95
95
 
96
96
  ###### - Enables support for all the enum goodness when enumerating classes for Polymorphic Associations
97
- Note: this excludes the `<attribute>!` method to update a value, as, as I write this, I can't conceive of a scenario in which you'd want to update just the type, and not also the ID, of an associated object. If you want to do this, you can still do this manually via the `ActiveRecord#update` method.
97
+ Note: this excludes the `<attribute>!` method to update a value, as, as I write this, I can't conceive of a scenario in which you'd want to update just the type, and not also the ID, of an associated object. That doesn't mean you won't ever want to! And if you do, you can still do so manually via the `ActiveRecord#update` [method](https://guides.rubyonrails.org/active_record_basics.html#update).
98
98
 
99
99
  ```
100
100
  class Award < ActiveRecord::Base
@@ -128,21 +128,21 @@ end
128
128
 
129
129
  ## Installation
130
130
 
131
- Install the gem and add to the application's Gemfile by executing:
131
+ Install the gem and add to the application's Gemfile by running:
132
132
 
133
133
  $ bundle add taylors_enum
134
134
 
135
- If bundler is not being used to manage dependencies, install the gem by executing:
135
+ If bundler is not being used to manage dependencies, install the gem by running:
136
136
 
137
137
  $ gem install taylors_enum
138
138
 
139
139
  ## Usage
140
140
 
141
- Using taylors_enum is simple! Once the gem is installed, just add `taylors_enum <my_column>: ['array', 'of', 'values']` to any model that ultimately inherits from `ActiveRecord::Base`.
141
+ Once the gem is installed, add `taylors_enum <my_column>: ['array', 'of', 'values'], **options` to any model that ultimately inherits from `ActiveRecord::Base`.
142
142
 
143
- The values that you pass will be the values that are stored in the database. taylors_enum will then generate a companion for each value that will be used to define constants, `?` methods to check if an object has the given value in the specified column, `!` methods to update the column to that value, and scopes to query for records with that value. See the top of this README for what this looks like in practice.
143
+ The values that you pass will be the values that are stored in the database - except when passing `integer: true` (see below) in the options; in this case, pass an array of the values you expect to see in the Rails application. taylors_enum will then generate a companion for each value that will be used to define constants, `?` methods to check if an object has the given value in the specified column, `!` methods to update the column to that value, and scopes to query for records with that value. See the top of this README for what this looks like in practice.
144
144
 
145
- taylors_enum also takes a series of options, provided as a hash:
145
+ taylors_enum also takes a series of options, provided as a hash following the specified values:
146
146
  - `prefix`: defaults to `nil`. If `true` is passed, the name of the column will be prepended to the start of the helper methods, scopes, and constants. If a string is passed, the given string will be prepended to the start of the helper methods, scopes, and constants.
147
147
 
148
148
  - `suffix`: defaults to `nil`. If `true` is passed, the name of the column will be appended to the end of the helper methods, scopes, and constants. If a string is passed, the given string will be appended to the end of the helper methods, scopes, and constants.
@@ -151,11 +151,23 @@ taylors_enum also takes a series of options, provided as a hash:
151
151
 
152
152
  - `validations`: defaults to `true`. If `false` is passed, validations, `nil` will be allowed as a value in the specified column.
153
153
 
154
- - `single_table_inheritance`: defaults to `false`. When using taylors_enum to help with columns on [Single Table Inheritance](https://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html) models, pass this value as `true`, otherwise things won't work, and then you'll be sad.
154
+ - `single_table_inheritance`: defaults to `false`. When using taylors_enum to help with columns on [Single Table Inheritance](https://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html) models, pass this value as `true`, otherwise things won't work, and then you'll be sad. Note: this cannot be passed as `true` if `integer` or `polymorphic` are both specified as `true`; this will raise an OptionsConflictError when loading the application.
155
155
 
156
- - `polymorphic`: defaults to `false`. When using taylors_enum to help with type columns for [polymorphic associations](https://guides.rubyonrails.org/association_basics.html#polymorphic-associations), pass this value as `true`; this will ensure validations are run correctly, against database values rather than Rails values, and will also not create a `!` method to update the value of the column.
156
+ - `polymorphic`: defaults to `false`. When using taylors_enum to help with _type column for [polymorphic associations](https://guides.rubyonrails.org/association_basics.html#polymorphic-associations), pass this value as `true`; this will ensure validations are run correctly, against database values rather than Rails values, and will also not create a `!` method to update the value of the column. Note: this cannot be passed as `true` if `single_table_inheritance` or `integer` are both specified as `true`; this will raise an OptionsConflictError when loading the application.
157
157
 
158
- If you want to see the base Rails value that will be used to generate the `value?` and `value!` methods, that `VALUE` constants, and the `value` scopes, you can load
158
+ - `integer`: defaults to `false`. When using taylors_enum with an integer column rather than a string column, pass this as `true` to ensure constants, scopes, and helper methods are defined appropriately. Should this be something that can be inferred, rather than needing to be passed explicitly? Absolutely. However, at the time of writing, I can't figure out how to get access to this information at the point that this code is called (in the `ActiveSupport::LazyLoadHooks.on_load` [callback](https://api.rubyonrails.org/classes/ActiveSupport/LazyLoadHooks.html)), so this fudge allows integer columns to function as expected for now. Note: this cannot be passed as `true` if `single_table_inheritance` or `polymorphic` are both specified as `true`; this will raise an OptionsConflictError when loading the application.
159
+
160
+ If you want to see the base Rails value that will be used to generate the `value?` and `value!` methods, that `VALUE` constants, and the `value` scopes, you can load up a Rails console with `rails c`, and run `MyModel.check_rails_value_for(database_value)`. For example:
161
+ ```
162
+ class AlbumBase < Album
163
+ taylors_enum name: %w[debut fearless speak_now red nineteen_eighty_nine reputation lover folklore evermore]
164
+ end
165
+
166
+ [2] pry(#<TestDefault>)> AlbumBase.check_rails_value_for('folklore')
167
+ => 'folklore'
168
+ [3] pry(#<TestDefault>)> AlbumBase.check_rails_value_for('folklore', column: :name, prefix: true)
169
+ => 'name_folklore'
170
+ ```
159
171
 
160
172
  ## Development
161
173
 
@@ -2,11 +2,14 @@ module TaylorsEnum
2
2
  module ActiveRecord
3
3
  module TaylorsEnum
4
4
  module ClassMethods
5
+ class OptionsConflictError < StandardError; end
6
+
5
7
  DEFAULT_OPTIONS = {
6
8
  prefix: nil, # can be true, false, or a string; if true, will use the column_name as the default value
7
9
  suffix: nil, # can be true, false, or a string; if true, will use the column_name as the default value
8
10
  constants: true, # can be true or false
9
11
  validations: true, # can be true or false; TODO: allow a hash of validations args to be passed
12
+ integer: false, # can be true or false; pass true if the column taylors_enum is called on is an integer value rather than a string
10
13
  single_table_inheritance: false,
11
14
  polymorphic: false,
12
15
  column: nil # this is set by the first argument passed to taylors_enum
@@ -21,11 +24,13 @@ module TaylorsEnum
21
24
  .merge(column: enum_column)
22
25
  .with_indifferent_access
23
26
 
27
+ maybe_raise_options_conflict_errors(options)
28
+
24
29
  values = format_values(raw_values, options: options)
25
30
 
26
31
  define_list_methods!(values, column: enum_column)
27
32
 
28
- define_constants!(values) if options[:constants].present?
33
+ define_constants!(values, integer: options[:integer]) if options[:constants].present?
29
34
 
30
35
  # NOTE: when relying on the default rails enum, we validate against the rails-side values, as enum does its own custom validation; however, taylors_enum validates against the database-side values, as it relies on active_record validations
31
36
  values_to_validate = options[:single_table_inheritance] || options[:polymorphic] ? values.values : values.keys
@@ -36,7 +41,7 @@ module TaylorsEnum
36
41
  elsif options[:polymorphic]
37
42
  manually_define_polymorphic_enum_methods(values, column: enum_column)
38
43
  else
39
- enum(enum_column => values) unless options[:single_table_inheritance]
44
+ enum(enum_column => values)
40
45
  end
41
46
  end
42
47
 
@@ -53,6 +58,20 @@ module TaylorsEnum
53
58
 
54
59
  private
55
60
 
61
+ def maybe_raise_options_conflict_errors(options)
62
+ raise OptionsConflictError.new(
63
+ 'taylors_enum cannot work for both polymorphic associations and single table inheritance on the same column'
64
+ ) if options[:single_table_inheritance] && options[:polymorphic]
65
+
66
+ raise OptionsConflictError.new(
67
+ 'taylors_enum cannot work for integer columns with single table inheritance'
68
+ ) if options[:integer] && options[:single_table_inheritance]
69
+
70
+ raise OptionsConflictError.new(
71
+ 'taylors_enum cannot work for integer columns with polymorphic associations'
72
+ ) if options[:integer] && options[:polymorphic]
73
+ end
74
+
56
75
  def manually_define_single_table_inheritance_enum_methods(values, column:)
57
76
  values.each do |rails_value, database_value|
58
77
  define_boolean_method(rails_value, database_value, column)
@@ -109,9 +128,9 @@ module TaylorsEnum
109
128
  end
110
129
  end
111
130
 
112
- def define_constants!(values)
131
+ def define_constants!(values, integer:)
113
132
  values.each do |rails_value, database_value|
114
- const_set(rails_value.upcase, database_value)
133
+ const_set(rails_value.upcase, integer ? rails_value : database_value)
115
134
  end
116
135
  end
117
136
 
@@ -123,8 +142,9 @@ module TaylorsEnum
123
142
  prefix = generate_prefix_name(options[:prefix], column: options[:column])
124
143
  suffix = generate_suffix_name(options[:suffix], column: options[:column])
125
144
 
126
- values.map do |value|
145
+ values.map.with_index do |value, index|
127
146
  formatted_value = value.to_s.demodulize.underscore
147
+ value = index if options[:integer]
128
148
  [
129
149
  "#{prefix}#{formatted_value}#{suffix}",
130
150
  value
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TaylorsEnum
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/taylors_enum.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  require_relative 'taylors_enum/version'
4
4
 
5
5
  module TaylorsEnum
6
- class Error < StandardError; end
7
6
  require 'active_support'
8
7
  require 'active_support/core_ext/hash'
9
8
  require 'taylors_enum/active_record/active_record'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taylors_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cassie Johnstone
@@ -90,6 +90,7 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - ".rubocop.yml"
92
92
  - CHANGELOG.md
93
+ - CODE_OF_CONDUCT.md
93
94
  - Gemfile
94
95
  - Gemfile.lock
95
96
  - LICENSE.txt