formwandler 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0db28341ef76b8524fd9d0bc11b243667f490f08
4
- data.tar.gz: c263b0a3736ed877b25d7648a175b15fd13053c5
3
+ metadata.gz: 2dd62e45f73ce8f70eb053af48e887d44f3fcc51
4
+ data.tar.gz: ddebd431d50447fe4306397484d272d5d5263059
5
5
  SHA512:
6
- metadata.gz: 3bd276f0a797234bdcd978948c6c0036b06b9fbf828671f7c95180935866933caddcc4b08e131e07a8fadc8a38393d5b65d2ac49316c6430c01d7ec717fa74b5
7
- data.tar.gz: 4846d5deb2a856e3e6ce63f0020e7dc7876f1fb6bb444c357bc73cc1e237a91b16ee99d27a24effd15247d93126714321394b37274e0a2513f2abc4873f3afb5
6
+ metadata.gz: cbb4804ab0e18db419b2dce0d889a8ce0517b065a0ccdd5d306e9375ab5d59b50dddeb901b211721de135fb6faf318bc4622a6420ec983809e33690452c405b4
7
+ data.tar.gz: fe9c5ad5b420b8e760b07c396165b6595b290daf34f3fd2a9029efbf3d9d790659b5eb6c89151c471ba484e285ab1f03895e2dec6832dcff6453beae592695dc
@@ -12,11 +12,7 @@ module Formwandler
12
12
  end
13
13
 
14
14
  def hidden?
15
- if field_definition.hidden.respond_to?(:call)
16
- form.instance_exec(&field_definition.hidden)
17
- else
18
- field_definition.hidden
19
- end
15
+ _evaluate_field_definition_value(:hidden)
20
16
  end
21
17
 
22
18
  def visible?
@@ -24,11 +20,7 @@ module Formwandler
24
20
  end
25
21
 
26
22
  def disabled?
27
- if field_definition.disabled.respond_to?(:call)
28
- form.instance_exec(&field_definition.disabled)
29
- else
30
- field_definition.disabled
31
- end
23
+ _evaluate_field_definition_value(:disabled)
32
24
  end
33
25
 
34
26
  def options
@@ -40,19 +32,11 @@ module Formwandler
40
32
  end
41
33
 
42
34
  def default
43
- if field_definition.default.respond_to?(:call)
44
- form.instance_exec(&field_definition.default)
45
- else
46
- field_definition.default
47
- end
35
+ _evaluate_field_definition_value(:default)
48
36
  end
49
37
 
50
38
  def delocalize
51
- if field_definition.delocalize.respond_to?(:call)
52
- form.instance_exec(&field_definition.delocalize)
53
- else
54
- field_definition.delocalize
55
- end
39
+ _evaluate_field_definition_value(:delocalize)
56
40
  end
57
41
 
58
42
  private
@@ -46,15 +46,16 @@ module Formwandler
46
46
  attr_reader :model_save_order
47
47
  end
48
48
 
49
- attr_reader :models, :current_user
49
+ attr_reader :models, :controller
50
50
 
51
- def initialize(models: {}, current_user: nil)
51
+ def initialize(models: {}, controller:)
52
52
  @models = models.symbolize_keys
53
- @current_user = current_user
53
+ @controller = controller
54
54
 
55
55
  initialize_fields
56
56
  initialize_models
57
57
  assign_defaults
58
+ assign_params
58
59
  end
59
60
 
60
61
  def persisted?
@@ -95,9 +96,7 @@ module Formwandler
95
96
  all_valid
96
97
  end
97
98
 
98
- def submit(params)
99
- assign_attributes permitted_params(params)
100
-
99
+ def submit
101
100
  if valid? && models_valid?
102
101
  save_models!
103
102
  load_results
@@ -106,7 +105,7 @@ module Formwandler
106
105
  end
107
106
  end
108
107
 
109
- protected
108
+ private
110
109
 
111
110
  def initialize_models
112
111
  end
@@ -118,6 +117,11 @@ module Formwandler
118
117
  end
119
118
  end
120
119
 
120
+ def assign_params
121
+ return unless form_params?
122
+ assign_attributes permitted_params
123
+ end
124
+
121
125
  def save_models!
122
126
  models.sort_by do |(name, _model)|
123
127
  self.class.model_save_order&.index(name) || self.class.model_save_order&.size
@@ -128,15 +132,16 @@ module Formwandler
128
132
  true
129
133
  end
130
134
 
131
- def permitted_params(params)
135
+ def form_params?
136
+ controller.params.fetch(model_name.param_key, {}).to_unsafe_h.any?
137
+ end
138
+
139
+ def permitted_params
132
140
  visible_fields = fields.reject(&:hidden?).map(&:name)
133
141
  delocalizations = fields.map(&:delocalize).compact
134
- params = ActionController::Parameters.new(params) if params.is_a?(Hash)
135
- params.require(model_name.param_key).permit(*visible_fields).delocalize(delocalizations)
142
+ controller.params.require(model_name.param_key).permit(*visible_fields).delocalize(delocalizations)
136
143
  end
137
144
 
138
- private
139
-
140
145
  def initialize_fields
141
146
  @fields = self.class.field_definitions.transform_values do |field_definition|
142
147
  Field.new(form: self, field_definition: field_definition)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formwandler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Bilharz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-07 00:00:00.000000000 Z
11
+ date: 2017-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -63,7 +63,7 @@ files:
63
63
  - lib/formwandler/field.rb
64
64
  - lib/formwandler/field_definition.rb
65
65
  - lib/formwandler/form.rb
66
- homepage:
66
+ homepage: https://github.com/sbilharz/formwandler
67
67
  licenses:
68
68
  - MIT
69
69
  metadata: {}