model_form_facade 0.1.0
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 +7 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/lib/model_form_facade/version.rb +5 -0
- data/lib/model_form_facade.rb +164 -0
- data/sig/model_form_facade.rbs +4 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df7bfe8f520d385db578e47e05b9a08397eaed01ac108828b9d92a59b8aeca29
|
4
|
+
data.tar.gz: 12975038ffe2a8dc51253383fa75a9e11e31c0e5c9911bf30ef605549ee8ea31
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca388061f33ff239feb1cb9579181ecc469459db8ab364c4842fc8c6025bdb0140208cf97c7a82716e305a4ce098493d5696cb48af438fa9782d37cc9858f538
|
7
|
+
data.tar.gz: 7121b04f4921390f4ca4285abff657cf4885f84a679af0bc3672d9dc37e7637d8f5040d7e5413075c46c18a88acc40a4565d4b6d18d7c306f36ada6d76574ef0
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Joshua Paine
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# ModelFormFacade
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/model_form_facade`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
```bash
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
15
|
+
```
|
16
|
+
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/model_form_facade.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
require_relative "model_form_facade/version"
|
2
|
+
require "activesupport"
|
3
|
+
|
4
|
+
module ModelFormFacade
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
class_attribute :fields, instance_accessor: false, instance_predicate: false, default: {}
|
11
|
+
class_attribute :params_root, default: true # infer from model
|
12
|
+
attr_reader :object
|
13
|
+
private attr_writer :object
|
14
|
+
private attr_accessor :component_props
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(model_object = nil, root: nil, **component_props)
|
18
|
+
self.params_root = root unless root.nil?
|
19
|
+
self.object = model_object
|
20
|
+
self.component_props = component_props
|
21
|
+
end
|
22
|
+
|
23
|
+
def options = {} # Override me
|
24
|
+
|
25
|
+
def as_json(root: params_root)
|
26
|
+
hash = self.class.fields.values.filter_map do |field|
|
27
|
+
next nil unless field.read
|
28
|
+
|
29
|
+
raw = send(field.name)
|
30
|
+
value = case field.type
|
31
|
+
in :scalar then raw
|
32
|
+
in :object then field.form.new(raw).as_json(root: false)
|
33
|
+
in :array then (raw || []).map { field.form.new(_1).as_json(root: false) }
|
34
|
+
end
|
35
|
+
[field.name, field.serialize(value)]
|
36
|
+
end.to_h.compact
|
37
|
+
return hash unless (root = _params_root(root:))
|
38
|
+
{root => hash}
|
39
|
+
end
|
40
|
+
|
41
|
+
def errors(root: params_root)
|
42
|
+
errs = object_error_messages.reject { |k, v| k.to_s.include? "." } # Remove nested, we'll recurse
|
43
|
+
hash = self.class.fields.values.filter_map do |field|
|
44
|
+
err = case field.type
|
45
|
+
in :scalar then errs[field.attribute]&.map(&:capitalize)&.join(";").presence
|
46
|
+
in :object then field.form.new(send(field.name)).errors(root: false)
|
47
|
+
in :array then (send(field.name) || []).map { field.form.new(_1).errors(root: false) }
|
48
|
+
end
|
49
|
+
[field.name, err] if err
|
50
|
+
end.to_h.compact
|
51
|
+
return hash unless (root = _params_root(root:))
|
52
|
+
{root => hash}
|
53
|
+
end
|
54
|
+
|
55
|
+
def form_props(root: params_root)
|
56
|
+
{
|
57
|
+
**component_props,
|
58
|
+
data: as_json(root:),
|
59
|
+
options: options,
|
60
|
+
errors: errors(root:)
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def set_fields(params, root: nil)
|
65
|
+
params = params.expect(expectation(root:)) if params.respond_to?(:expect) && !params.permitted?
|
66
|
+
self.attributes = params
|
67
|
+
end
|
68
|
+
alias_method :fields=, :set_fields
|
69
|
+
|
70
|
+
def attributes=(hash)
|
71
|
+
hash.each do |key, value|
|
72
|
+
send("#{key}=", value)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def attributes = as_json
|
77
|
+
|
78
|
+
def expectation(root: nil)
|
79
|
+
self.class.expectation(root: _params_root(root:))
|
80
|
+
end
|
81
|
+
|
82
|
+
def save! = object.save!
|
83
|
+
|
84
|
+
def save = object.save
|
85
|
+
|
86
|
+
private def _params_root(root: nil)
|
87
|
+
case root.nil? ? params_root : root
|
88
|
+
in true then object.class.name&.split("::")&.last&.downcase&.to_sym
|
89
|
+
in false then nil
|
90
|
+
in Symbol => sym then sym
|
91
|
+
in String => str then str.to_sym
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
private def object_error_messages = object&.errors&.messages || {}
|
96
|
+
|
97
|
+
Field = Data.define(:name, :read, :write, :attribute, :type, :form) do
|
98
|
+
def serialize(v)
|
99
|
+
return v unless type == :scalar
|
100
|
+
v&.to_s
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
module FieldMethods; end
|
105
|
+
|
106
|
+
class_methods do
|
107
|
+
def field(name, read: nil, write: nil, attribute: nil)
|
108
|
+
_field(name, read:, write:, attribute:, type: :scalar)
|
109
|
+
end
|
110
|
+
|
111
|
+
def one(*, **, &block) = _relation(*, **, type: :object, &block)
|
112
|
+
|
113
|
+
def nested(*, **, &block) = _relation(*, **, type: :array, &block)
|
114
|
+
|
115
|
+
def expectation(root:)
|
116
|
+
expected = fields.values.select(&:write).map do |field|
|
117
|
+
case field.type
|
118
|
+
in :scalar then field.name
|
119
|
+
in :object then {field.name => field.form.expectation(root: false)}
|
120
|
+
in :array then {field.name => [field.form.expectation(root: false)]}
|
121
|
+
end
|
122
|
+
end
|
123
|
+
return expected unless root &&= root.to_sym
|
124
|
+
{root => expected}
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
def _relation(name, allow_destroy: false, form: nil, read: nil, write: nil, attribute: nil, type: nil, &block)
|
130
|
+
form ||= create_form_class_for(name)
|
131
|
+
form.field :_destroy, read: false if allow_destroy
|
132
|
+
form.instance_exec(&block) if block
|
133
|
+
if write.nil? && attribute.nil? && !name.to_s.ends_with?("_attributes")
|
134
|
+
write = :"#{name}_attributes="
|
135
|
+
end
|
136
|
+
_field(name, read:, write:, attribute:, type:, form:)
|
137
|
+
end
|
138
|
+
|
139
|
+
def _field(name, read: nil, write: nil, attribute: nil, type: :scalar, form: nil)
|
140
|
+
name = name.to_sym
|
141
|
+
attribute = (attribute || name).to_sym
|
142
|
+
read = attribute if read.nil?
|
143
|
+
write = :"#{attribute}=" if write.nil?
|
144
|
+
field = Field.new(name:, read:, write:, attribute:, type:, form:)
|
145
|
+
self.fields = {**fields, name.to_sym => field}
|
146
|
+
field_methods_module.define_method(name) { object&.send(read) } unless read == false
|
147
|
+
field_methods_module.define_method(:"#{name}=") { |value| object.send(write, value) } unless write == false
|
148
|
+
end
|
149
|
+
|
150
|
+
def create_form_class_for(field_name)
|
151
|
+
class_name = "anonymous_#{field_name}_form".camelize
|
152
|
+
const_set(class_name, Class.new.tap { _1.include ModelForm })
|
153
|
+
end
|
154
|
+
|
155
|
+
def field_methods_module
|
156
|
+
return @field_methods_module if @field_methods_module
|
157
|
+
@field_methods_module = Module.new
|
158
|
+
module_name = name.presence && name.gsub(/\W+/, "__") + "Fields"
|
159
|
+
FieldMethods.const_set(module_name, @field_methods_module) if module_name
|
160
|
+
include @field_methods_module
|
161
|
+
@field_methods_module
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: model_form_facade
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua Paine
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: activesupport
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
email:
|
27
|
+
- joshua@letterblock.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- ".standard.yml"
|
33
|
+
- CHANGELOG.md
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/model_form_facade.rb
|
38
|
+
- lib/model_form_facade/version.rb
|
39
|
+
- sig/model_form_facade.rbs
|
40
|
+
homepage: https://github.com/midnightmonster/model_form_facade
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
allowed_push_host: https://rubygems.org
|
45
|
+
homepage_uri: https://github.com/midnightmonster/model_form_facade
|
46
|
+
source_code_uri: https://github.com/midnightmonster/model_form_facade
|
47
|
+
changelog_uri: https://github.com/midnightmonster/model_form_facade/blob/main/CHANGELOG.md
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 3.3.0
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.6.9
|
63
|
+
specification_version: 4
|
64
|
+
summary: Form objects as read/write facades for your models, with a convenient DSL
|
65
|
+
test_files: []
|