formwandler 0.6.0 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/formwandler/field.rb +6 -2
- data/lib/formwandler/field_definition.rb +36 -1
- data/lib/formwandler/form.rb +34 -21
- data/lib/formwandler/form_loader.rb +7 -11
- data/lib/formwandler/version.rb +1 -1
- metadata +12 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c58985c9314b2ee1dbeb4372f7c0554eb16322c27767daaf227ae6be46b8b61e
|
4
|
+
data.tar.gz: cc39146f7953edc67dc131ec87e48d54b6631882f2457b565744ebabb602ab92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55eeb2ca00dcf309f9eaa60a57326fb5feb3f684a5ddf2fcaf889e551b03048f60863570a97f4fa6b886b13bc9a27b01839d1ef751506158a010569d6f7b4cf4
|
7
|
+
data.tar.gz: 593380319ef6ac9e31a74ac204aa0ac9f88b402ff077a23caa0b67c3a4ae0d526814f6c18b15057953858c5db0fc4bcecd3434c4f0bec201359a6b09b67ac723
|
data/lib/formwandler/field.rb
CHANGED
@@ -14,7 +14,7 @@ module Formwandler
|
|
14
14
|
def value=(new_value)
|
15
15
|
new_value = transform_value(new_value, field_definition.in_transformation)
|
16
16
|
if model_instance
|
17
|
-
model_instance.public_send("#{
|
17
|
+
model_instance.public_send("#{source}=", new_value)
|
18
18
|
else
|
19
19
|
@value = new_value
|
20
20
|
end
|
@@ -25,7 +25,7 @@ module Formwandler
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def raw_value
|
28
|
-
model_instance ? model_instance.public_send(
|
28
|
+
model_instance ? model_instance.public_send(source) : @value
|
29
29
|
end
|
30
30
|
|
31
31
|
def hidden?
|
@@ -40,6 +40,10 @@ module Formwandler
|
|
40
40
|
_evaluate_field_definition_value(:disabled)
|
41
41
|
end
|
42
42
|
|
43
|
+
def readonly?
|
44
|
+
_evaluate_field_definition_value(:readonly)
|
45
|
+
end
|
46
|
+
|
43
47
|
def options
|
44
48
|
all_options - hidden_options
|
45
49
|
end
|
@@ -5,13 +5,41 @@ require_relative 'field_value_transformer'
|
|
5
5
|
module Formwandler
|
6
6
|
class FieldDefinition
|
7
7
|
attr_reader :name
|
8
|
-
attr_accessor :hidden, :disabled, :default, :delocalize, :model, :source, :options, :array
|
8
|
+
attr_accessor :hidden, :disabled, :readonly, :default, :delocalize, :model, :model_class, :source, :options, :array
|
9
9
|
|
10
10
|
def initialize(name)
|
11
11
|
@name = name
|
12
12
|
set_defaults
|
13
13
|
end
|
14
14
|
|
15
|
+
def model=(model)
|
16
|
+
set_model model, model
|
17
|
+
end
|
18
|
+
|
19
|
+
def model_class=(symbol_or_class)
|
20
|
+
@model_class =
|
21
|
+
if symbol_or_class.nil?
|
22
|
+
nil
|
23
|
+
elsif symbol_or_class.is_a?(Symbol)
|
24
|
+
symbol_or_class.to_s.classify.constantize rescue nil
|
25
|
+
elsif symbol_or_class < ActiveRecord::Base || symbol_or_class < ActiveModel::Model
|
26
|
+
symbol_or_class
|
27
|
+
else
|
28
|
+
fail 'The model class must be nil, a symbol or a descendant of ActiveModel::Model or ActiveRecord::Base.'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def configure(opts)
|
33
|
+
model = opts[:model]
|
34
|
+
model_class = opts[:model_class] || model
|
35
|
+
set_model model, model_class
|
36
|
+
opts.except(:model, :model_class).each do |key, value|
|
37
|
+
send("#{key}=", value)
|
38
|
+
rescue NoMethodError
|
39
|
+
raise ArgumentError, "Invalid option #{key}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
15
43
|
def hide_option(name, value = true)
|
16
44
|
hidden_options[name.to_s] = value
|
17
45
|
end
|
@@ -39,12 +67,19 @@ module Formwandler
|
|
39
67
|
def set_defaults
|
40
68
|
@hidden = false
|
41
69
|
@disabled = false
|
70
|
+
@readonly = false
|
42
71
|
@default = nil
|
43
72
|
@delocalize = nil
|
44
73
|
@model = nil
|
74
|
+
@model_class = nil
|
45
75
|
@source = name
|
46
76
|
@options = nil
|
47
77
|
@array = false
|
48
78
|
end
|
79
|
+
|
80
|
+
def set_model(model, model_class)
|
81
|
+
@model = model
|
82
|
+
self.model_class = model_class
|
83
|
+
end
|
49
84
|
end
|
50
85
|
end
|
data/lib/formwandler/form.rb
CHANGED
@@ -14,17 +14,23 @@ module Formwandler
|
|
14
14
|
ActiveModel::Name.new(self, nil, name.chomp('Form').underscore)
|
15
15
|
end
|
16
16
|
|
17
|
+
def human_attribute_name(attr, options = {})
|
18
|
+
field_definition = @field_definitions.fetch(attr) { nil }
|
19
|
+
if options[:default].nil? && field_definition && field_definition.model_class
|
20
|
+
not_found = '__formwandler_not_found__'
|
21
|
+
model_translation = field_definition.model_class.human_attribute_name(field_definition.source, default: not_found)
|
22
|
+
options[:default] = model_translation unless model_translation == not_found
|
23
|
+
end
|
24
|
+
super(attr, options)
|
25
|
+
end
|
26
|
+
|
17
27
|
def field_definitions
|
18
28
|
@field_definitions ||= {}
|
19
29
|
end
|
20
30
|
|
21
31
|
def field(name, opts = {}, &block)
|
22
32
|
field_definition = field_definitions[name] ||= FieldDefinition.new(name)
|
23
|
-
opts
|
24
|
-
field_definition.public_send("#{key}=", value)
|
25
|
-
rescue NoMethodError
|
26
|
-
raise ArgumentError, "Invalid option #{key}"
|
27
|
-
end
|
33
|
+
field_definition.configure(opts)
|
28
34
|
field_definition.instance_exec(&block) if block_given?
|
29
35
|
|
30
36
|
attribute_accessor(name)
|
@@ -83,22 +89,14 @@ module Formwandler
|
|
83
89
|
fields.select { |field| field.model == model }
|
84
90
|
end
|
85
91
|
|
86
|
-
def
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
next if model.valid?
|
91
|
-
fields_for_model(name).each do |field|
|
92
|
-
model.errors[field.source].each do |error_message|
|
93
|
-
errors.add(field.name, error_message)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
all_valid
|
92
|
+
def valid?
|
93
|
+
form_valid = super
|
94
|
+
models_valid = models_valid?
|
95
|
+
form_valid && models_valid
|
98
96
|
end
|
99
97
|
|
100
98
|
def submit
|
101
|
-
if valid?
|
99
|
+
if valid?
|
102
100
|
ActiveRecord::Base.transaction do
|
103
101
|
save_models!
|
104
102
|
end
|
@@ -125,6 +123,21 @@ module Formwandler
|
|
125
123
|
assign_attributes permitted_params
|
126
124
|
end
|
127
125
|
|
126
|
+
def models_valid?
|
127
|
+
all_valid = true
|
128
|
+
models.each do |name, model|
|
129
|
+
next if model.valid?
|
130
|
+
|
131
|
+
all_valid = false
|
132
|
+
fields_for_model(name).each do |field|
|
133
|
+
model.errors[field.source].each do |error_message|
|
134
|
+
errors.add(field.name, error_message) unless errors.messages[field.source].include? error_message
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
all_valid
|
139
|
+
end
|
140
|
+
|
128
141
|
def save_models!
|
129
142
|
models.sort_by do |(name, _model)|
|
130
143
|
self.class.model_save_order&.index(name) || self.class.model_save_order&.size
|
@@ -140,11 +153,11 @@ module Formwandler
|
|
140
153
|
end
|
141
154
|
|
142
155
|
def permitted_params
|
143
|
-
controller.params.require(model_name.param_key).permit(*
|
156
|
+
controller.params.require(model_name.param_key).permit(*permitted_fields).delocalize(delocalizations)
|
144
157
|
end
|
145
158
|
|
146
|
-
def
|
147
|
-
fields.reject(&:
|
159
|
+
def permitted_fields
|
160
|
+
fields.reject(&:disabled?).map do |field|
|
148
161
|
field.array? ? {field.name => []} : field.name
|
149
162
|
end
|
150
163
|
end
|
@@ -6,22 +6,18 @@ module Formwandler
|
|
6
6
|
|
7
7
|
class_methods do
|
8
8
|
def load_form(opts = {})
|
9
|
-
before_action
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
before_action(opts.except(:models)) do
|
10
|
+
if opts.fetch(:models, true)
|
11
|
+
models = {inferred_resource_name.to_sym => instance_variable_get("@#{inferred_resource_name}")}.compact
|
12
|
+
end
|
13
|
+
form_instance = form_class.new(models: models || {}, controller: self)
|
14
|
+
instance_variable_set("@#{form_instance_name}", form_instance)
|
15
|
+
end
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
17
19
|
private
|
18
20
|
|
19
|
-
def _formwandler_load_form
|
20
|
-
models = {inferred_resource_name.to_sym => instance_variable_get("@#{inferred_resource_name}")}.compact
|
21
|
-
form_instance = form_class.new(models: models, controller: self)
|
22
|
-
instance_variable_set("@#{form_instance_name}", form_instance)
|
23
|
-
end
|
24
|
-
|
25
21
|
def form_class_name
|
26
22
|
"#{namespace}::#{inferred_resource_name.camelize}Form"
|
27
23
|
end
|
data/lib/formwandler/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formwandler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Bilharz
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 5.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 5.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: delocalize
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,14 +86,14 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '1.
|
89
|
+
version: '1.4'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '1.
|
96
|
+
version: '1.4'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: byebug
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -127,7 +127,7 @@ homepage: https://github.com/sbilharz/formwandler
|
|
127
127
|
licenses:
|
128
128
|
- MIT
|
129
129
|
metadata: {}
|
130
|
-
post_install_message:
|
130
|
+
post_install_message:
|
131
131
|
rdoc_options: []
|
132
132
|
require_paths:
|
133
133
|
- lib
|
@@ -142,9 +142,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
142
|
- !ruby/object:Gem::Version
|
143
143
|
version: '0'
|
144
144
|
requirements: []
|
145
|
-
|
146
|
-
|
147
|
-
signing_key:
|
145
|
+
rubygems_version: 3.1.2
|
146
|
+
signing_key:
|
148
147
|
specification_version: 4
|
149
148
|
summary: Form objects for rails
|
150
149
|
test_files: []
|