declare_schema 4.0.2 → 4.0.3
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/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +69 -0
- data/lib/declare_schema/version.rb +1 -1
- metadata +3 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 67a8ff59b5f29818efd240f2d5b07d3631f54c21bf7c0b983c045fa2b6f2d4ac
|
|
4
|
+
data.tar.gz: cf0ddc6128d5f0e2e3f0d867c3a384942ce35c561f38a35ca58090369c0ab25f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: edb10d4c1b0018a198007a8a9862455991e62df80e786b141a870ac998ed7f2dc7653cbdff14bab4615649fbe45d9caa3be1f3c8ae420f863f28c6ba09b2a06b
|
|
7
|
+
data.tar.gz: 2b916bf151cbf69d55c52f071fc4a6006e1906e918231ae4f973982034fcae35af3da1b05f19174a741f9461e47998d1c0cd8cdd30e9b88ef0a30f6e6c20109a
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,10 @@ 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
|
+
## [4.0.3] - 2026-07-01
|
|
8
|
+
### Changed
|
|
9
|
+
- Documented the `field` DSL macro and the underlying `declare_field` class method.
|
|
10
|
+
|
|
7
11
|
## [4.0.2] - 2026-05-29
|
|
8
12
|
### Fixed
|
|
9
13
|
- Fixed Rails 7.2 compatibility for the `serialize:` field option. Rails 7.2 changed
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -89,6 +89,75 @@ create_table :companies, id: :bigint do |t|
|
|
|
89
89
|
...
|
|
90
90
|
end
|
|
91
91
|
```
|
|
92
|
+
### The `field` Macro
|
|
93
|
+
Every `t.<type> :<column_name>, <options>` declaration shown above is syntactic sugar for calling the `field` macro directly:
|
|
94
|
+
```ruby
|
|
95
|
+
create_table :companies, id: :bigint do |t|
|
|
96
|
+
t.field :company_name, :string, null: false, limit: 100
|
|
97
|
+
...
|
|
98
|
+
end
|
|
99
|
+
```
|
|
100
|
+
`field(name, type, *flags, **options)` is the primitive method underlying every typed shorthand (`t.string`, `t.integer`, etc.), as well as the `timestamps` and `optimistic_lock` helpers. A call like `t.string :company_name, limit: 100` is dispatched (via `method_missing`) to `t.field :company_name, :string, limit: 100`.
|
|
101
|
+
|
|
102
|
+
`field` only exists inside the `declare_schema do ... end` block, but within it, it can be called either with an explicit `t.` receiver (`t.field ...`) or bare (`field ...`), since the block is `instance_eval`'d against the DSL object -- both forms are equivalent. `field` cannot be called outside of the `declare_schema do ... end` block.
|
|
103
|
+
|
|
104
|
+
Calling `field` directly is useful when you need to dynamically declare field(s) based on meta-data that is available at runtime:
|
|
105
|
+
```ruby
|
|
106
|
+
declare_schema do
|
|
107
|
+
[[:company_name, :string, { limit: 100 }], [:employee_count, :integer, {}]].each do |name, type, options|
|
|
108
|
+
field name, type, **options
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### `:required` and `:unique` Flags
|
|
114
|
+
In addition to keyword options, `field` (and by extension, `t.<type>`) accepts optional positional flags placed immediately after the column name:
|
|
115
|
+
- `:required` - adds `validates_presence_of :<column_name>` to the model.
|
|
116
|
+
- `:unique` - adds `validates_uniqueness_of :<column_name>, allow_nil: <true unless :required is also given>` to the model.
|
|
117
|
+
|
|
118
|
+
For example:
|
|
119
|
+
```ruby
|
|
120
|
+
declare_schema do
|
|
121
|
+
string :title, :required, limit: 255
|
|
122
|
+
string :slug, :unique, limit: 255
|
|
123
|
+
end
|
|
124
|
+
```
|
|
125
|
+
is equivalent to:
|
|
126
|
+
```ruby
|
|
127
|
+
declare_schema do
|
|
128
|
+
field :title, :string, :required, limit: 255
|
|
129
|
+
field :slug, :string, :unique, limit: 255
|
|
130
|
+
end
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### Calling `declare_field` outside the block
|
|
134
|
+
`field` is DSL sugar that simply calls the model's `declare_field` class method: `field(name, type, *flags, **options)` calls `declare_field(name, type, *flags, **options)`. Unlike `field`, `declare_field` is a regular class method, so it can be called directly on the model outside of a `declare_schema do ... end` block, e.g. from a shared concern that adds a field to any model that includes it:
|
|
135
|
+
```ruby
|
|
136
|
+
module SoftDeletable
|
|
137
|
+
extend ActiveSupport::Concern
|
|
138
|
+
|
|
139
|
+
included do
|
|
140
|
+
declare_schema { }
|
|
141
|
+
declare_field :title, :string, :required, limit: 255
|
|
142
|
+
declare_field :slug, :string, :unique, limit: 255
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
```
|
|
146
|
+
which is equivalent to:
|
|
147
|
+
```ruby
|
|
148
|
+
module SoftDeletable
|
|
149
|
+
extend ActiveSupport::Concern
|
|
150
|
+
|
|
151
|
+
included do
|
|
152
|
+
declare_schema do
|
|
153
|
+
field :title, :string, :required, limit: 255
|
|
154
|
+
field :slug, :string, :unique, limit: 255
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
```
|
|
159
|
+
**Note:** `declare_field` is only defined once `declare_schema` has been called at least once on the model (even with no block, e.g. bare `declare_schema`), since that's what mixes in the `DeclareSchema` class methods.
|
|
160
|
+
|
|
92
161
|
### Field (Column) Types
|
|
93
162
|
All of the ActiveRecord field types are supported, as returned by the database driver in use at the time.
|
|
94
163
|
These typically include:
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: declare_schema
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.0.
|
|
4
|
+
version: 4.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Invoca Development adapted from hobo_fields by Tom Locke
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: rails
|
|
@@ -144,7 +143,6 @@ homepage: https://github.com/Invoca/declare_schema
|
|
|
144
143
|
licenses: []
|
|
145
144
|
metadata:
|
|
146
145
|
allowed_push_host: https://rubygems.org
|
|
147
|
-
post_install_message:
|
|
148
146
|
rdoc_options: []
|
|
149
147
|
require_paths:
|
|
150
148
|
- lib
|
|
@@ -159,8 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
159
157
|
- !ruby/object:Gem::Version
|
|
160
158
|
version: 1.3.6
|
|
161
159
|
requirements: []
|
|
162
|
-
rubygems_version: 3.
|
|
163
|
-
signing_key:
|
|
160
|
+
rubygems_version: 3.6.8
|
|
164
161
|
specification_version: 4
|
|
165
162
|
summary: Database schema declaration and migration generator for Rails
|
|
166
163
|
test_files: []
|