readymade 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +37 -2
- data/lib/readymade/form.rb +63 -3
- data/lib/readymade/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d8b08e5bcf92035a49dbec37847653f0b99743c2f508c96e50b0aba312b3905
|
4
|
+
data.tar.gz: 2ec517133167b7ec909d9bf2e77c0fdb185774017f70f8871586ec0eb942925e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2388b93ffa8fd5bb5fe988546dab3383a70ec36296ca0efe6a34475c2e06514a43ff0b12c61986aaf50d6a03c529076c92d1b802647a310a12b44b966dc27d4
|
7
|
+
data.tar.gz: ec9b42d63e9ba52d2a27fa10ac2ccf572de6c287b14a1f0e9269c7554adb0f10deee2ad0a6fb62a05c5ac7db2854e96abb6ca554774b632bfaa4f037057822b0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Readymade
|
1
|
+
# Readymade 0.1.7
|
2
2
|
|
3
3
|
This gems contains basic components to follow [ABDI architecture](https://github.com/OrestF/OrestF/blob/master/abdi/ABDI_architecture.md)
|
4
4
|
|
@@ -35,6 +35,41 @@ Inherit your components from:
|
|
35
35
|
|
36
36
|
```TODO: add```
|
37
37
|
|
38
|
+
#### #form_options
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
# app/forms/my_form.rb
|
42
|
+
|
43
|
+
class MyForm < Readymade::Form
|
44
|
+
PERMITTED_ATTRIBUTES = %i[email name category country]
|
45
|
+
REQUIRED_ATTRIBUTES = %i[email]
|
46
|
+
|
47
|
+
def form_options
|
48
|
+
{
|
49
|
+
categories: args[:company].categories,
|
50
|
+
countries: Country.all
|
51
|
+
}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
# app/controllers/items_controller.rb
|
58
|
+
|
59
|
+
def new
|
60
|
+
@form_options = MyForm.form_options(company: current_company)
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
```slim
|
65
|
+
/ app/views/items/new.html.slim
|
66
|
+
|
67
|
+
= f.select :category, collection: @form_options[:categories]
|
68
|
+
= f.select :country, collection: @form_options[:countries]
|
69
|
+
= f.text_field :email, required: @form_options.required?(:email) # true
|
70
|
+
= f.text_field :name, required: @form_options.required?(:name) # false
|
71
|
+
```
|
72
|
+
|
38
73
|
### Readymade::InstantForm
|
39
74
|
|
40
75
|
Permit params and validates presence inline
|
@@ -60,7 +95,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
60
95
|
|
61
96
|
## Contributing
|
62
97
|
|
63
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
98
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/OrestF/readymade. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/readymade/blob/master/CODE_OF_CONDUCT.md).
|
64
99
|
|
65
100
|
|
66
101
|
## License
|
data/lib/readymade/form.rb
CHANGED
@@ -31,6 +31,7 @@ module Readymade
|
|
31
31
|
attr_accessor key
|
32
32
|
end
|
33
33
|
end
|
34
|
+
|
34
35
|
# automatically validates all REQUIRED_ATTRIBUTES
|
35
36
|
singleton_class.validates(*required_attributes, presence: true) if required_attributes.present?
|
36
37
|
|
@@ -52,8 +53,13 @@ module Readymade
|
|
52
53
|
next if params[attr].blank?
|
53
54
|
|
54
55
|
if form_class.is_a?(Array)
|
55
|
-
n_forms = params[attr].
|
56
|
-
|
56
|
+
n_forms = if params[attr].is_a?(Hash)
|
57
|
+
# { 0 => {id: 1, name: 'my name'}, 1 => { id: 2, name: 'other name' }}
|
58
|
+
params[attr].map { |_i, attrs| form_class[0].new(attrs) }
|
59
|
+
else
|
60
|
+
# [{id: 1, name: 'my name'}, { id: 2, name: 'other name' }]
|
61
|
+
params[attr].map { |attrs| form_class[0].new(attrs) }
|
62
|
+
end
|
57
63
|
@nested_forms.push(*n_forms)
|
58
64
|
define_singleton_method("#{attr}_forms") { n_forms }
|
59
65
|
else
|
@@ -71,6 +77,7 @@ module Readymade
|
|
71
77
|
nested_forms.compact.map(&:validate).all? || sync_nested_errors(nested_forms)
|
72
78
|
end
|
73
79
|
|
80
|
+
# copy errors from nested forms into parent form
|
74
81
|
def sync_nested_errors(nested_forms)
|
75
82
|
nested_forms.each do |n_form|
|
76
83
|
n_form.errors.each do |code, text|
|
@@ -81,11 +88,16 @@ module Readymade
|
|
81
88
|
false
|
82
89
|
end
|
83
90
|
|
91
|
+
# sync errors from form to record or vice-versa
|
84
92
|
def sync_errors(from: self, to: record)
|
85
93
|
return if [from, to].any?(&:blank?)
|
86
94
|
|
87
95
|
if Rails.version.to_f > 6.0
|
88
|
-
|
96
|
+
from.errors.messages.each do |key, values|
|
97
|
+
Array.wrap(values).uniq.each do |uv|
|
98
|
+
to.errors.add(key, uv)
|
99
|
+
end
|
100
|
+
end
|
89
101
|
else
|
90
102
|
errors = from.errors.instance_variable_get('@messages').to_h
|
91
103
|
errors.merge!(to.errors.instance_variable_get('@messages').to_h)
|
@@ -132,5 +144,53 @@ module Readymade
|
|
132
144
|
def nested_forms_mapping
|
133
145
|
{}
|
134
146
|
end
|
147
|
+
|
148
|
+
# EXAMPLE
|
149
|
+
# class Items::Forms::Create::Value < ::Readymade::Form
|
150
|
+
# PERMITTED_ATTRIBUTES = %i[attr1 attr2].freeze
|
151
|
+
# REQUIRED_ATTRIBUTES = %i[attr1].freeze
|
152
|
+
#
|
153
|
+
# class Value < ::Readymade::Form::Value
|
154
|
+
# def to_h
|
155
|
+
# {
|
156
|
+
# vat_percent: Item::VAT_OPTIONS,
|
157
|
+
# price_type: Item.price_types.keys,
|
158
|
+
# item_category: args[:company].item_categories
|
159
|
+
# }
|
160
|
+
# end
|
161
|
+
# end
|
162
|
+
|
163
|
+
# @form_options = Items::Forms::Create::Value.new(company: current_company)
|
164
|
+
# f.association :item_category, collection: @form_options[:item_category]
|
165
|
+
|
166
|
+
def self.form_options(**args)
|
167
|
+
Readymade::Form::FormOptions.new(**args.merge!(form_class: self))
|
168
|
+
end
|
169
|
+
class FormOptions
|
170
|
+
attr_reader :args
|
171
|
+
|
172
|
+
def initialize(**args)
|
173
|
+
@args = args
|
174
|
+
@f_class = args.delete(:form_class)
|
175
|
+
end
|
176
|
+
|
177
|
+
def [](key)
|
178
|
+
to_h[key]
|
179
|
+
end
|
180
|
+
|
181
|
+
def to_h
|
182
|
+
raise Readymade::Error.new('define form_options on your form') unless (f = @f_class.new({}, @args)).respond_to?(:form_options)
|
183
|
+
|
184
|
+
f.form_options
|
185
|
+
end
|
186
|
+
|
187
|
+
def as_json(options = {})
|
188
|
+
to_h.as_json(options)
|
189
|
+
end
|
190
|
+
|
191
|
+
def required?(attr)
|
192
|
+
@f_class::REQUIRED_ATTRIBUTES.include?(attr.to_sym)
|
193
|
+
end
|
194
|
+
end
|
135
195
|
end
|
136
196
|
end
|
data/lib/readymade/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: readymade
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OrestF
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: byebug
|