application_form 0.5.0 → 0.5.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 +4 -4
- data/README.md +12 -5
- data/lib/application_form.rb +3 -2
- data/lib/application_form/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb0692a0002ec9525e01b40a0a4ffddf2c8da9fd625259642965e06a2b135c0a
|
4
|
+
data.tar.gz: e110d5eaa1c9a5b06c6cc49042858d3706a8fcde8652921f2f60da8594dd4ab8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69d6e90cfe3c4bc58ccc4eab4ab5d435f0ecdb33ae0fe429d2942d3bc27d76e3a8f4110ddb3ff3f0c3cd732a97fa79db628d105feaa7ff8ec0193b10fd8ccda9
|
7
|
+
data.tar.gz: f090c870dfa53da190725d84cf75b2e9ec0299808ebb4f437a11de19f5a736d949fc352880ba70bd92b7f1e108a6e0bef35560c5bd861784761f6ebd8bea7228
|
data/README.md
CHANGED
@@ -66,7 +66,14 @@ class UserSignUpForm < User
|
|
66
66
|
end
|
67
67
|
```
|
68
68
|
|
69
|
-
|
69
|
+
```
|
70
|
+
form = UserSignUpForm.new(user_params)
|
71
|
+
form.valid?
|
72
|
+
```
|
73
|
+
|
74
|
+
### Usage with `becomes`
|
75
|
+
|
76
|
+
In some cases it is necessary to use ActiveRecord object directly without form. For such cases conveniently to use method `becomes()` (built-in ActiveRecord):
|
70
77
|
|
71
78
|
```ruby
|
72
79
|
user = User.find(params[:id])
|
@@ -93,19 +100,19 @@ form = ReservationCreateForm.new(prepared_params)
|
|
93
100
|
if form.checks_passed?
|
94
101
|
# ...
|
95
102
|
else
|
96
|
-
render_error!(form.first_failed_check) # form.first_failed_check returns "max_number_of_reservations_reached"
|
103
|
+
render_error!(form.first_failed_check) # form.first_failed_check returns "reservation.error.max_number_of_reservations_reached"
|
97
104
|
end
|
98
105
|
```
|
99
106
|
|
100
|
-
You can also
|
107
|
+
You can also assign check to a specific field:
|
101
108
|
|
102
|
-
```
|
109
|
+
```ruby
|
103
110
|
check :end_at_must_be_greater_then_start_at, ->(form) { form.end_at > form.start_at }, :end_at
|
104
111
|
```
|
105
112
|
|
106
113
|
In this case it will work as a regular validation.
|
107
114
|
|
108
|
-
### `
|
115
|
+
### `assign_attrs`
|
109
116
|
|
110
117
|
It works as regular `assign_attributes` but it also returns the object, so that you can chain it:
|
111
118
|
|
data/lib/application_form.rb
CHANGED
@@ -35,10 +35,11 @@ module ApplicationForm
|
|
35
35
|
@_permitted_args || (superclass.respond_to?(:_permitted_args) && superclass._permitted_args) || []
|
36
36
|
end
|
37
37
|
|
38
|
-
def check(
|
38
|
+
def check(key, block, field = :base)
|
39
39
|
validate do |form|
|
40
40
|
if !block.call(form)
|
41
|
-
|
41
|
+
entity_name = self.class.superclass.to_s.tableize.split('/').last.singularize
|
42
|
+
key = "#{entity_name}.errors.#{key}"
|
42
43
|
form.add_error_key(field, key)
|
43
44
|
end
|
44
45
|
end
|