declare_schema 0.12.0 → 0.12.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 741e1030227b2cd6e472d1f3cc9a8f3838c927b3702087428f01b2af09a7c758
4
- data.tar.gz: 439d95f21fd5e8f677aea5c1ef4bb52fc37c8321b0066876f0970028e75a654a
3
+ metadata.gz: 2bace1c4f410e4766d4b9aa1414a5394414e8294db88ccd3d168d6db5d9802f6
4
+ data.tar.gz: da2b3467c656b0bad95b85627c341fb4d447872b144a452e3569a85a7369c452
5
5
  SHA512:
6
- metadata.gz: b332745b815b6ac442158727c4d6ded6f320144f40b895ff4e303bd0e67a25d305fbed845ab3eedb9d49754078fff0f2b25b58f417fd42e00552b58b4bd67d1a
7
- data.tar.gz: 600fb3635f8a65defcb0643501553e73befc7548da6a6821840b3a487c80e66a0933f51cbde7a83679a2ac2dc69f7f54e71aab2d878dd03b9742308cb0d845f5
6
+ metadata.gz: cf6eb2e6b67bd3071b9261a5d07eaa56dc019c148b02bbc11e70dd929934855e0b48a0c4c178d348286ec7bba4ed25f90c8041d855150c6b23c1aa81f26af25f
7
+ data.tar.gz: 3517a656b7b7edda5ed4164495747b0d879329d96bb44294f5b347acacc96a983e3cb6ab99fad8bf6e7b9fe998634ceb7cbd4ad79383164ab05ea6b273974c9f
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4
4
 
5
5
  Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.12.1] - 2021-05-10
8
+ ### Fixed
9
+ - When an `enum` type field is declared, there is now enforcement that its `limit:` must be an array of 1 or more Symbols,
10
+ and its `default:`--if given--must be a Symbol or `nil`.
11
+
7
12
  ## [0.12.0] - 2021-04-28
8
13
  ### Added
9
14
  - `belongs_to` now always infers the `limit:` of the foreign key to match that of the primary key it points to.
@@ -174,6 +179,7 @@ using the appropriate Rails configuration attributes.
174
179
  ### Added
175
180
  - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
176
181
 
182
+ [0.12.1]: https://github.com/Invoca/declare_schema/compare/v0.12.0...v0.12.1
177
183
  [0.12.0]: https://github.com/Invoca/declare_schema/compare/v0.11.1...v0.12.0
178
184
  [0.11.1]: https://github.com/Invoca/declare_schema/compare/v0.11.0...v0.11.1
179
185
  [0.11.0]: https://github.com/Invoca/declare_schema/compare/v0.10.1...v0.11.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (0.12.0)
4
+ declare_schema (0.12.1)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
@@ -76,12 +76,19 @@ module DeclareSchema
76
76
  when :bigint
77
77
  @type = :integer
78
78
  @options[:limit] = 8
79
+ when :enum
80
+ @options[:default].nil? || @options[:default].is_a?(Symbol) or
81
+ raise ArgumentError, "enum default: must be nil or a Symbol; got #{@options[:default].inspect}"
82
+ @options[:limit].is_a?(Array) && @options[:limit].size >= 1 && @options[:limit].all? { |value| value.is_a?(Symbol) } or
83
+ raise ArgumentError, "enum limit: must be an array of 1 or more Symbols; got #{@options[:limit].inspect}"
79
84
  end
80
85
 
81
86
  Column.native_type?(@type) or raise UnknownTypeError, "#{@type.inspect} not found in #{Column.native_types.inspect} for adapter #{ActiveRecord::Base.connection.class.name}"
82
87
 
83
- if @type.in?([:string, :text, :binary, :varbinary, :integer, :enum])
88
+ if @type.in?([:string, :text, :binary, :varbinary, :integer])
84
89
  @options[:limit] ||= Column.native_types.dig(@type, :limit)
90
+ elsif @type.in?([:enum])
91
+ # nothing to do
85
92
  else
86
93
  @type != :decimal && @options.has_key?(:limit) and warn("unsupported limit: for SQL type #{@type} in field #{model}##{@name}")
87
94
  @options.delete(:limit)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "0.12.0"
4
+ VERSION = "0.12.1"
5
5
  end
@@ -115,6 +115,48 @@ RSpec.describe DeclareSchema::Model::FieldSpec do
115
115
  end
116
116
  end
117
117
 
118
+ describe 'enum' do
119
+ before do
120
+ allow(::DeclareSchema::Model::Column).to receive(:native_types).and_wrap_original do |m, *_args|
121
+ result = m.call
122
+ if result.has_key?(:enum)
123
+ result
124
+ else
125
+ result.merge(enum: { name: "enum" })
126
+ end
127
+ end
128
+ end
129
+
130
+ describe 'default' do
131
+ it 'allows default of nil or a Symbol' do
132
+ [nil, :first].each do |default|
133
+ expect do
134
+ subject = described_class.new(model, :status, :enum, limit: [:first, :second, :third], default: default, null: false, position: 2)
135
+ expect(subject.default).to eq(default)
136
+ end.to_not raise_exception
137
+ end
138
+ end
139
+
140
+ it 'raises ArgumentError if default is not nil or a Symbol' do
141
+ ["first", 1].each do |default|
142
+ expect do
143
+ described_class.new(model, :status, :enum, limit: [:first, :second, :third], default: default, null: false, position: 2)
144
+ end.to raise_exception(ArgumentError, /enum default: must be nil or a Symbol; got #{Regexp.escape(default.inspect)}/)
145
+ end
146
+ end
147
+ end
148
+
149
+ describe 'limit' do
150
+ it 'raises ArgumentError if any of the limit values are not Symbols' do
151
+ [['first', 'second', 'third'], [1, 2, 3], nil, []].each do |limit|
152
+ expect do
153
+ described_class.new(model, :status, :enum, limit: limit, null: false, position: 2)
154
+ end.to raise_exception(ArgumentError, /enum limit: must be an array of 1 or more Symbols; got #{Regexp.escape(limit.inspect)}/)
155
+ end
156
+ end
157
+ end
158
+ end
159
+
118
160
  if defined?(Mysql2)
119
161
  describe 'varbinary' do # TODO: :varbinary is an Invoca addition to Rails; make it a configurable option
120
162
  it 'is supported' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: declare_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development adapted from hobo_fields by Tom Locke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-29 00:00:00.000000000 Z
11
+ date: 2021-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails