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

Returns:

  • (Boolean)


20
21
22
# File 'lib/subroutine/fields.rb', line 20

def self.action_controller_params_loaded?
  defined?(::ActionController::Parameters)
end

.allowed_input_classesObject



12
13
14
15
16
17
18
# File 'lib/subroutine/fields.rb', line 12

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_paramsObject Also known as: defaults, default_params



208
209
210
# File 'lib/subroutine/fields.rb', line 208

def all_default_params
  get_param_group(:default)
end

#all_paramsObject Also known as: params



214
215
216
217
218
# File 'lib/subroutine/fields.rb', line 214

def all_params
  return all_params_with_default_params if include_defaults_in_params?

  all_provided_params
end

#all_params_with_default_paramsObject Also known as: all_params_with_defaults



221
222
223
# File 'lib/subroutine/fields.rb', line 221

def all_params_with_default_params
  param_cache[:provided_and_default] ||= all_default_params.merge(all_provided_params)
end

#all_provided_paramsObject Also known as: provided_params



203
204
205
# File 'lib/subroutine/fields.rb', line 203

def all_provided_params
  get_param_group(:provided)
end

#clear_field(name) ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'lib/subroutine/fields.rb', line 257

def clear_field(name)
  param_cache.clear
  param_groups.each_pair do |key, group|
    next if key == :original
    next if key == :default

    group.delete(name)
  end
end

#field_provided?(key) ⇒ Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/subroutine/fields.rb', line 232

def field_provided?(key)
  all_provided_params.key?(key)
end

#get_field(name) ⇒ Object



236
237
238
# File 'lib/subroutine/fields.rb', line 236

def get_field(name)
  all_params_with_default_params[name]
end

#get_field_config(field_name) ⇒ Object



228
229
230
# File 'lib/subroutine/fields.rb', line 228

def get_field_config(field_name)
  self.class.get_field_config(field_name)
end

#get_param_group(name) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/subroutine/fields.rb', line 180

def get_param_group(name)
  name = name.to_sym
  return param_groups[name] if param_groups.key?(name)

  param_groups[name] = yield if block_given?
  param_groups[name]
end

#include_defaults_in_params?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/subroutine/fields.rb', line 168

def include_defaults_in_params?
  self.class.include_defaults_in_params?
end

#original_paramsObject



199
200
201
# File 'lib/subroutine/fields.rb', line 199

def original_params
  get_param_group(:original)
end

#param_cacheObject



172
173
174
# File 'lib/subroutine/fields.rb', line 172

def param_cache
  @param_cache ||= {}
end

#param_groupsObject



176
177
178
# File 'lib/subroutine/fields.rb', line 176

def param_groups
  @param_groups ||= Hash.new { |h, k| h[k] = {}.with_indifferent_access }
end

#set_field(name, value, group_type: :provided) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/subroutine/fields.rb', line 240

def set_field(name, value, group_type: :provided)
  config = get_field_config(name)
  value = attempt_cast(value, config) do |e|
    "Error during assignment of field `#{name}`: #{e}"
  end

  set_param_group_value(group_type, config.field_name, value)

  config.groups.each do |group_name|
    set_param_group_value(:"#{group_name}_#{group_type}", config.field_name, value)
  end

  param_cache.clear

  value
end

#set_param_group_value(name, key, value) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/subroutine/fields.rb', line 188

def set_param_group_value(name, key, value)
  config = get_field_config(key)
  group = get_param_group(name)

  if config&.bypass_indifferent_assignment?
    group.regular_writer(group.send(:convert_key, key), value)
  else
    group[key.to_sym] = value
  end
end

#setup_fields(inputs = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
# 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
  inputs.each_pair do |k, v|
    set_param_group_value(:original, k, v)
  end
  mass_assign_initial_params
end