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
188
189
190
|
# File 'lib/subroutine/fields.rb', line 188
def all_default_params
get_param_group(:default)
end
|
#all_params ⇒ Object
Also known as:
params
183
184
185
|
# File 'lib/subroutine/fields.rb', line 183
def all_params
get_param_group(:all)
end
|
#all_params_with_defaults ⇒ Object
Also known as:
params_with_defaults
193
194
195
|
# File 'lib/subroutine/fields.rb', line 193
def all_params_with_defaults
all_default_params.merge(all_params)
end
|
#clear_field(name) ⇒ Object
231
232
233
234
235
|
# File 'lib/subroutine/fields.rb', line 231
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
211
212
213
|
# File 'lib/subroutine/fields.rb', line 211
def field_provided?(key)
!!@provided_fields[key]
end
|
#fields_in_group(group_name) ⇒ Object
237
238
239
|
# File 'lib/subroutine/fields.rb', line 237
def fields_in_group(group_name)
self.class.fields_in_group(group_name)
end
|
#get_field(name) ⇒ Object
215
216
217
|
# File 'lib/subroutine/fields.rb', line 215
def get_field(name)
field_provided?(name) ? all_params[name] : all_default_params[name]
end
|
#get_field_config(field_name) ⇒ Object
206
207
208
|
# File 'lib/subroutine/fields.rb', line 206
def get_field_config(field_name)
self.class.get_field_config(field_name)
end
|
#get_param_group(name) ⇒ Object
171
172
173
|
# File 'lib/subroutine/fields.rb', line 171
def get_param_group(name)
param_groups[name.to_sym]
end
|
#original_params ⇒ Object
175
176
177
|
# File 'lib/subroutine/fields.rb', line 175
def original_params
get_param_group(:original)
end
|
#param_groups ⇒ Object
167
168
169
|
# File 'lib/subroutine/fields.rb', line 167
def param_groups
@param_groups ||= Hash.new { |h, k| h[k] = {}.with_indifferent_access }
end
|
#set_field(name, value, track_provided: true) ⇒ Object
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/subroutine/fields.rb', line 219
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
158
159
160
161
162
163
164
165
|
# File 'lib/subroutine/fields.rb', line 158
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
198
199
200
|
# File 'lib/subroutine/fields.rb', line 198
def ungrouped_defaults
default_params.slice(*ungrouped_fields.keys)
end
|
#ungrouped_fields ⇒ Object
241
242
243
244
245
|
# File 'lib/subroutine/fields.rb', line 241
def ungrouped_fields
fields.select { |f| f.groups.empty? }.each_with_object({}) do |f, h|
h[f.name] = f
end
end
|
#ungrouped_params ⇒ Object
179
180
181
|
# File 'lib/subroutine/fields.rb', line 179
def ungrouped_params
get_param_group(:ungrouped)
end
|
#ungrouped_params_with_defaults ⇒ Object
202
203
204
|
# File 'lib/subroutine/fields.rb', line 202
def ungrouped_params_with_defaults
ungrouped_defaults.merge(ungrouped_params)
end
|