Module: Subroutine::Fields
- Extended by:
- ActiveSupport::Concern
- Included in:
- Op
- Defined in:
- lib/subroutine/fields.rb,
lib/subroutine/fields/configuration.rb,
lib/subroutine/fields/mass_assignment_error.rb
Defined Under Namespace
Modules: ClassMethods
Classes: Configuration, MassAssignmentError
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.action_controller_params_loaded? ⇒ Boolean
25
26
27
|
# File 'lib/subroutine/fields.rb', line 25
def self.action_controller_params_loaded?
defined?(::ActionController::Parameters)
end
|
17
18
19
20
21
22
23
|
# File 'lib/subroutine/fields.rb', line 17
def self.allowed_input_classes
@allowed_input_classes ||= begin
out = [Hash]
out << ActionController::Parameters if action_controller_params_loaded?
out
end
end
|
Instance Method Details
#all_default_params ⇒ Object
Also known as:
defaults
185
186
187
|
# File 'lib/subroutine/fields.rb', line 185
def all_default_params
get_param_group(:default)
end
|
#all_params ⇒ Object
Also known as:
params
180
181
182
|
# File 'lib/subroutine/fields.rb', line 180
def all_params
get_param_group(:all)
end
|
#all_params_with_defaults ⇒ Object
Also known as:
params_with_defaults
190
191
192
|
# File 'lib/subroutine/fields.rb', line 190
def all_params_with_defaults
all_default_params.merge(all_params)
end
|
#clear_field(name) ⇒ Object
228
229
230
231
232
|
# File 'lib/subroutine/fields.rb', line 228
def clear_field(name)
each_param_group_for_field(name) do |h|
h.delete(name)
end
end
|
#field_provided?(key) ⇒ Boolean
check if a specific field was provided
208
209
210
|
# File 'lib/subroutine/fields.rb', line 208
def field_provided?(key)
!!@provided_fields[key]
end
|
#fields_in_group(group_name) ⇒ Object
234
235
236
|
# File 'lib/subroutine/fields.rb', line 234
def fields_in_group(group_name)
self.class.fields_in_group(group_name)
end
|
#get_field(name) ⇒ Object
212
213
214
|
# File 'lib/subroutine/fields.rb', line 212
def get_field(name)
field_provided?(name) ? all_params[name] : all_default_params[name]
end
|
#get_field_config(field_name) ⇒ Object
203
204
205
|
# File 'lib/subroutine/fields.rb', line 203
def get_field_config(field_name)
self.class.get_field_config(field_name)
end
|
#get_param_group(name) ⇒ Object
168
169
170
|
# File 'lib/subroutine/fields.rb', line 168
def get_param_group(name)
param_groups[name.to_sym]
end
|
#original_params ⇒ Object
172
173
174
|
# File 'lib/subroutine/fields.rb', line 172
def original_params
get_param_group(:original)
end
|
#param_groups ⇒ Object
164
165
166
|
# File 'lib/subroutine/fields.rb', line 164
def param_groups
@param_groups ||= Hash.new { |h, k| h[k] = {}.with_indifferent_access }
end
|
#set_field(name, value, track_provided: true) ⇒ Object
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/subroutine/fields.rb', line 216
def set_field(name, value, track_provided: true)
config = get_field_config(name)
@provided_fields[name] = true if track_provided
value = attempt_cast(value, config) do |e|
"Error during assignment of field `#{name}`: #{e}"
end
each_param_group_for_field(name) do |h|
h[name] = value
end
value
end
|
#setup_fields(inputs = {}) ⇒ Object
155
156
157
158
159
160
161
162
|
# File 'lib/subroutine/fields.rb', line 155
def setup_fields(inputs = {})
if ::Subroutine::Fields.action_controller_params_loaded? && inputs.is_a?(::ActionController::Parameters)
inputs = inputs.to_unsafe_h if inputs.respond_to?(:to_unsafe_h)
end
@provided_fields = {}.with_indifferent_access
param_groups[:original] = inputs.with_indifferent_access
mass_assign_initial_params
end
|
#ungrouped_defaults ⇒ Object
195
196
197
|
# File 'lib/subroutine/fields.rb', line 195
def ungrouped_defaults
default_params.slice(*ungrouped_fields.keys)
end
|
#ungrouped_fields ⇒ Object
238
239
240
241
242
|
# File 'lib/subroutine/fields.rb', line 238
def ungrouped_fields
fields.select { |f| f.groups.empty? }.each_with_object({}) do |f, h|
h[f.name] = f
end
end
|
#ungrouped_params ⇒ Object
176
177
178
|
# File 'lib/subroutine/fields.rb', line 176
def ungrouped_params
get_param_group(:ungrouped)
end
|
#ungrouped_params_with_defaults ⇒ Object
199
200
201
|
# File 'lib/subroutine/fields.rb', line 199
def ungrouped_params_with_defaults
ungrouped_defaults.merge(ungrouped_params)
end
|