Class: Basepack::Forms::Base
- Inherits:
-
Object
- Object
- Basepack::Forms::Base
show all
- Includes:
- Renderable
- Defined in:
- lib/basepack/forms/base.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(factory, chain, options = {}) ⇒ Base
Returns a new instance of Base.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/basepack/forms/base.rb', line 15
def initialize(factory, chain, options = {})
@factory = factory
@fields_hash = {}
@groups = []
@nested_in = options[:nested_in]
chain = Array.wrap(chain)
@association_chain = chain[0...-1].freeze
if chain.last.is_a? Class
@resource_class = chain.last
else
@resource = chain.last
@resource_class = @resource.class
end
end
|
Instance Attribute Details
#association_chain ⇒ Object
Returns the value of attribute association_chain.
9
10
11
|
# File 'lib/basepack/forms/base.rb', line 9
def association_chain
@association_chain
end
|
#factory ⇒ Object
Returns the value of attribute factory.
7
8
9
|
# File 'lib/basepack/forms/base.rb', line 7
def factory
@factory
end
|
#groups ⇒ Object
Returns the value of attribute groups.
6
7
8
|
# File 'lib/basepack/forms/base.rb', line 6
def groups
@groups
end
|
#nested_in ⇒ Object
Returns the value of attribute nested_in.
13
14
15
|
# File 'lib/basepack/forms/base.rb', line 13
def nested_in
@nested_in
end
|
#partial ⇒ Object
Returns the value of attribute partial.
12
13
14
|
# File 'lib/basepack/forms/base.rb', line 12
def partial
@partial
end
|
#resource ⇒ Object
Returns the value of attribute resource.
10
11
12
|
# File 'lib/basepack/forms/base.rb', line 10
def resource
@resource
end
|
#resource_class ⇒ Object
Returns the value of attribute resource_class.
8
9
10
|
# File 'lib/basepack/forms/base.rb', line 8
def resource_class
@resource_class
end
|
#view ⇒ Object
Returns the value of attribute view.
11
12
13
|
# File 'lib/basepack/forms/base.rb', line 11
def view
@view
end
|
Instance Method Details
#build_from_factory ⇒ Object
32
33
34
|
# File 'lib/basepack/forms/base.rb', line 32
def build_from_factory
factory.build_form(self)
end
|
#chain ⇒ Object
58
59
60
|
# File 'lib/basepack/forms/base.rb', line 58
def chain
@association_chain + [resource]
end
|
#chain_with_class ⇒ Object
62
63
64
|
# File 'lib/basepack/forms/base.rb', line 62
def chain_with_class
@association_chain + [resource_class]
end
|
45
46
47
48
|
# File 'lib/basepack/forms/base.rb', line 45
def configure(&block)
yield(self)
self
end
|
#content_for_field(field_name, &block) ⇒ Object
164
165
166
167
168
|
# File 'lib/basepack/forms/base.rb', line 164
def content_for_field(field_name, &block)
field(field_name) do |f|
f.content(&block)
end
end
|
#default_group ⇒ Object
142
143
144
|
# File 'lib/basepack/forms/base.rb', line 142
def default_group
@groups.first
end
|
#default_partial ⇒ Object
154
155
|
# File 'lib/basepack/forms/base.rb', line 154
def default_partial
end
|
#field(name, delegate_or_attributes = nil) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/basepack/forms/base.rb', line 98
def field(name, delegate_or_attributes = nil)
if delegate_or_attributes or block_given?
field = @fields_hash[name]
if field
field.update_attributes(delegate_or_attributes) unless delegate_or_attributes.nil?
else
field = factory.new_field(name, self, delegate_or_attributes)
end
yield(field) if block_given?
@fields_hash[name] = field
else
@fields_hash[name]
end
end
|
#field_names ⇒ Object
78
79
80
|
# File 'lib/basepack/forms/base.rb', line 78
def field_names
@fields_hash.keys
end
|
#fields ⇒ Object
82
83
84
|
# File 'lib/basepack/forms/base.rb', line 82
def fields
@fields_hash.values
end
|
#fields_hash ⇒ Object
86
87
88
|
# File 'lib/basepack/forms/base.rb', line 86
def fields_hash
@fields_hash.dup
end
|
#group(attributes = nil) {|group| ... } ⇒ Object
135
136
137
138
139
140
|
# File 'lib/basepack/forms/base.rb', line 135
def group(attributes = nil)
group = factory.new_group(self, attributes)
yield(group) if block_given?
@groups << group
group
end
|
#has_field?(name) ⇒ Boolean
94
95
96
|
# File 'lib/basepack/forms/base.rb', line 94
def has_field?(name)
@fields_hash.has_key? name
end
|
#hide_field(field_name) ⇒ Object
118
119
120
|
# File 'lib/basepack/forms/base.rb', line 118
def hide_field(field_name)
@fields_hash[field_name].try :visible=, false
end
|
#hide_fields(*field_names) ⇒ Object
122
123
124
|
# File 'lib/basepack/forms/base.rb', line 122
def hide_fields(*field_names)
field_names.each {|f| hide_field(f)}
end
|
#inspect ⇒ Object
239
240
241
|
# File 'lib/basepack/forms/base.rb', line 239
def inspect
"#<#{self.class.name}[#{resource_class.name}] #{field_names}>"
end
|
#inverse_of_nested_in?(field) ⇒ Boolean
TODO remove and should be automatical (test in visible_fields or in visible?)
74
75
76
|
# File 'lib/basepack/forms/base.rb', line 74
def inverse_of_nested_in?(field) field.inverse_of_nested_in?
end
|
#label ⇒ Object
50
51
52
|
# File 'lib/basepack/forms/base.rb', line 50
def label
@label ||= resource_class.model_name.human
end
|
#label_plural ⇒ Object
54
55
56
|
# File 'lib/basepack/forms/base.rb', line 54
def label_plural
@label_plural ||= resource_class.model_name.human(count: 'other', default: label.pluralize)
end
|
36
37
38
39
40
41
42
43
|
# File 'lib/basepack/forms/base.rb', line 36
def new_form(*args)
form = factory.new_form(*args)
form.view = view
if configure_nested_form = form.nested_in.try(:configure_nested_form)
form.configure(&configure_nested_form)
end
form
end
|
#new_record? ⇒ Boolean
70
71
72
|
# File 'lib/basepack/forms/base.rb', line 70
def new_record?
!@resource or @resource.new_record?
end
|
#path ⇒ Object
170
171
172
|
# File 'lib/basepack/forms/base.rb', line 170
def path
view.url_for(chain)
end
|
#permit_params(params) ⇒ Object
174
175
176
|
# File 'lib/basepack/forms/base.rb', line 174
def permit_params(params)
sanitize_params(params).permit!
end
|
#render_field(field) ⇒ Object
157
158
159
|
# File 'lib/basepack/forms/base.rb', line 157
def render_field(field)
field.render
end
|
#render_field!(field) ⇒ Object
161
162
|
# File 'lib/basepack/forms/base.rb', line 161
def render_field!(field)
end
|
#sanitize_params(params, add_to_allwed = []) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/basepack/forms/base.rb', line 178
def sanitize_params(params, add_to_allwed = [])
new_params = params.dup
allowed = add_to_allwed.dup
visible_fields.each do |f|
next if f.read_only? or f.inverse_of_nested_in?
f.parse_input(new_params)
f.allowed_methods.each do |m|
if f.type == :serialized or (new_params[m].is_a?(Array) or new_params[m].is_a?(Hash)) ^ !f.multiple?
allowed << m
end
end
if nform = f.nform and nparams = new_params[f.method_name]
nested_allowed = []
field_nested_params = f.nested_form
if field_nested_params
nested_allowed << :id unless field_nested_params[:update_only]
nested_allowed << :_destroy if field_nested_params[:allow_destroy] or nform.new_record?
end
new_params[f.method_name] = case nparams
when Array
nparams.map {|p| p.is_a?(Hash) ? nform.sanitize_params(p, nested_allowed) : p.to_s}
when Hash
if f.multiple?
Hash[nparams.map do |k, p|
[k, p.is_a?(Hash) ? nform.sanitize_params(p, nested_allowed) : p.to_s]
end]
else
allowed << f.method_name
nform.sanitize_params(nparams, nested_allowed)
end
else
nparams
end
end
end
unpermitted = new_params.slice!(*allowed)
if unpermitted.present?
ActiveSupport::Notifications.instrument("unpermitted_parameters.action_controller", keys: unpermitted.keys)
end
new_params
end
|
#show_fields(*field_names) ⇒ Object
126
127
128
|
# File 'lib/basepack/forms/base.rb', line 126
def show_fields(*field_names)
field_names.each {|name| @fields_hash[name].try :visible=, true }
end
|
#show_only_fields(*field_names) ⇒ Object
130
131
132
133
|
# File 'lib/basepack/forms/base.rb', line 130
def show_only_fields(*field_names)
visible_fields.each { |field| hide_field(field.name) }
show_fields(*field_names)
end
|
#translate(resource, action, subaction = "menu") ⇒ Object
use more lookup paths for translations
244
245
246
|
# File 'lib/basepack/forms/base.rb', line 244
def translate(resource, action, subaction = "menu")
Utils.translate(resource, action, subaction)
end
|
#visible_field(name) ⇒ Object
113
114
115
116
|
# File 'lib/basepack/forms/base.rb', line 113
def visible_field(name)
field = @fields_hash[name]
field && field.visible? ? field : nil
end
|
#visible_fields ⇒ Object
90
91
92
|
# File 'lib/basepack/forms/base.rb', line 90
def visible_fields
@fields_hash.values.find_all {|f| f.visible? }
end
|
#visible_groups ⇒ Object
146
147
148
|
# File 'lib/basepack/forms/base.rb', line 146
def visible_groups
groups.find_all {|g| g.field_names.present? }
end
|
#with_resource(resource, *args, &block) ⇒ Object
230
231
232
233
234
235
236
237
|
# File 'lib/basepack/forms/base.rb', line 230
def with_resource(resource, *args, &block)
orig_resource, @resource = @resource, resource
begin
yield(resource, *args)
ensure
@resource = orig_resource
end
end
|