formwandler 0.7.0 → 0.9.0
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 +7 -3
- data/lib/formwandler/field_definition.rb +40 -3
- data/lib/formwandler/form.rb +14 -7
- 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: 9ffd1df3fd9bd73b09841ad5f2ea2179f4a9366fd11f65723948e0e43b217ff4
|
4
|
+
data.tar.gz: 5981014ba28b9dee2ecd47650d701ea8bc5d8de47c0094d96ed367e5d9eb332f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15d5c19992dda87e2e26c7cde5c38ce6040b4f9074f78639860f2c896ff04734f25dc0bf97d325ee49c3a52ac3de3b23f68a3d87dbd56b070e8066df5d23960e
|
7
|
+
data.tar.gz: dc25518420cd974025447a7bb824f09d99d583e006e5d8795e4ea860843243985577012afd2d8676b4b98a1010bda7b1794da44a8575043d4369198d9c468d6e
|
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
|
@@ -63,7 +67,7 @@ module Formwandler
|
|
63
67
|
private
|
64
68
|
|
65
69
|
def transform_value(value, transformation)
|
66
|
-
return value if value.nil? || value == ''
|
70
|
+
return value if !field_definition.transform_blank && (value.nil? || value == '')
|
67
71
|
if transformation.respond_to?(:call)
|
68
72
|
form.instance_exec(value, &transformation)
|
69
73
|
else
|
@@ -4,14 +4,43 @@ require_relative 'field_value_transformer'
|
|
4
4
|
|
5
5
|
module Formwandler
|
6
6
|
class FieldDefinition
|
7
|
-
attr_reader :name
|
8
|
-
attr_accessor :hidden, :disabled, :default, :delocalize, :model, :source, :options, :array
|
7
|
+
attr_reader :name, :transform_blank
|
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
|
+
@transform_blank = false
|
12
13
|
set_defaults
|
13
14
|
end
|
14
15
|
|
16
|
+
def model=(model)
|
17
|
+
set_model model, model
|
18
|
+
end
|
19
|
+
|
20
|
+
def model_class=(symbol_or_class)
|
21
|
+
@model_class =
|
22
|
+
if symbol_or_class.nil?
|
23
|
+
nil
|
24
|
+
elsif symbol_or_class.is_a?(Symbol)
|
25
|
+
symbol_or_class.to_s.classify.constantize rescue nil
|
26
|
+
elsif symbol_or_class < ActiveRecord::Base || symbol_or_class < ActiveModel::Model
|
27
|
+
symbol_or_class
|
28
|
+
else
|
29
|
+
fail 'The model class must be nil, a symbol or a descendant of ActiveModel::Model or ActiveRecord::Base.'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def configure(opts)
|
34
|
+
model = opts[:model]
|
35
|
+
model_class = opts[:model_class] || model
|
36
|
+
set_model model, model_class
|
37
|
+
opts.except(:model, :model_class).each do |key, value|
|
38
|
+
send("#{key}=", value)
|
39
|
+
rescue NoMethodError
|
40
|
+
raise ArgumentError, "Invalid option #{key}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
15
44
|
def hide_option(name, value = true)
|
16
45
|
hidden_options[name.to_s] = value
|
17
46
|
end
|
@@ -20,7 +49,8 @@ module Formwandler
|
|
20
49
|
@hidden_options ||= {}
|
21
50
|
end
|
22
51
|
|
23
|
-
def transform(&block)
|
52
|
+
def transform(allow_blank: false, &block)
|
53
|
+
@transform_blank = allow_blank
|
24
54
|
@value_transformer = FieldValueTransformer.new(&block)
|
25
55
|
end
|
26
56
|
|
@@ -39,12 +69,19 @@ module Formwandler
|
|
39
69
|
def set_defaults
|
40
70
|
@hidden = false
|
41
71
|
@disabled = false
|
72
|
+
@readonly = false
|
42
73
|
@default = nil
|
43
74
|
@delocalize = nil
|
44
75
|
@model = nil
|
76
|
+
@model_class = nil
|
45
77
|
@source = name
|
46
78
|
@options = nil
|
47
79
|
@array = false
|
48
80
|
end
|
81
|
+
|
82
|
+
def set_model(model, model_class)
|
83
|
+
@model = model
|
84
|
+
self.model_class = model_class
|
85
|
+
end
|
49
86
|
end
|
50
87
|
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)
|
@@ -120,11 +126,12 @@ module Formwandler
|
|
120
126
|
def models_valid?
|
121
127
|
all_valid = true
|
122
128
|
models.each do |name, model|
|
123
|
-
all_valid = false if model.invalid?
|
124
129
|
next if model.valid?
|
130
|
+
|
131
|
+
all_valid = false
|
125
132
|
fields_for_model(name).each do |field|
|
126
133
|
model.errors[field.source].each do |error_message|
|
127
|
-
errors.add(field.name, error_message)
|
134
|
+
errors.add(field.name, error_message) unless errors.messages[field.source].include? error_message
|
128
135
|
end
|
129
136
|
end
|
130
137
|
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.9.0
|
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: 2021-06-09 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.4
|
146
|
+
signing_key:
|
148
147
|
specification_version: 4
|
149
148
|
summary: Form objects for rails
|
150
149
|
test_files: []
|