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)


25
26
27
# File 'lib/subroutine/fields.rb', line 25

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

.allowed_input_classesObject



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



188
189
190
# File 'lib/subroutine/fields.rb', line 188

def all_default_params
  get_param_group(:default)
end

#all_paramsObject 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_defaultsObject 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

Returns:

  • (Boolean)


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_paramsObject



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

def original_params
  get_param_group(:original)
end

#param_groupsObject



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_defaultsObject



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

def ungrouped_defaults
  default_params.slice(*ungrouped_fields.keys)
end

#ungrouped_fieldsObject



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_paramsObject



179
180
181
# File 'lib/subroutine/fields.rb', line 179

def ungrouped_params
  get_param_group(:ungrouped)
end

#ungrouped_params_with_defaultsObject



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

def ungrouped_params_with_defaults
  ungrouped_defaults.merge(ungrouped_params)
end