class Rectify::Form

Attributes

context[R]

Public Class Methods

ensure_hash(object) click to toggle source
# File lib/rectify/form.rb, line 57
def self.ensure_hash(object)
  if object.is_a?(Hash)
    object
  else
    {}
  end
end
from_json(json) click to toggle source
# File lib/rectify/form.rb, line 29
def self.from_json(json)
  from_params(JSON.parse(json))
end
from_model(model) click to toggle source
# File lib/rectify/form.rb, line 25
def self.from_model(model)
  Rectify::BuildFormFromModel.new(self, model).build
end
from_params(params, additional_params = {}) click to toggle source
# File lib/rectify/form.rb, line 10
def self.from_params(params, additional_params = {})
  params_hash = hash_from(params)
  mimicked_params = ensure_hash(params_hash[mimicked_model_name])

  attributes_hash = params_hash
    .merge(mimicked_params)
    .merge(additional_params)

  formatted_attributes = FormatAttributesHash
    .new(attribute_set)
    .format(attributes_hash)

  new(formatted_attributes)
end
hash_from(params) click to toggle source
# File lib/rectify/form.rb, line 52
def self.hash_from(params)
  params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
  params.with_indifferent_access
end
infer_model_name() click to toggle source
# File lib/rectify/form.rb, line 41
def self.infer_model_name
  class_name = name.split("::").last
  return :form if class_name == "Form"

  class_name.chomp("Form").underscore.to_sym
end
mimic(model_name) click to toggle source
# File lib/rectify/form.rb, line 33
def self.mimic(model_name)
  @model_name = model_name.to_s.underscore.to_sym
end
mimicked_model_name() click to toggle source
# File lib/rectify/form.rb, line 37
def self.mimicked_model_name
  @model_name || infer_model_name
end
model_name() click to toggle source
# File lib/rectify/form.rb, line 48
def self.model_name
  ActiveModel::Name.new(self, nil, mimicked_model_name.to_s.camelize)
end

Public Instance Methods

attributes() click to toggle source
Calls superclass method
# File lib/rectify/form.rb, line 98
def attributes
  super.except(:id)
end
attributes_with_values() click to toggle source
# File lib/rectify/form.rb, line 102
def attributes_with_values
  attributes.reject { |attribute| public_send(attribute).nil? }
end
before_validation() click to toggle source
# File lib/rectify/form.rb, line 112
def before_validation
  # Implement this in your form object if you would like to perform some
  # some processing before validation happens (optional).
end
invalid?(options = {}) click to toggle source
# File lib/rectify/form.rb, line 82
def invalid?(options = {})
  !valid?(options)
end
map_model(model) click to toggle source
# File lib/rectify/form.rb, line 106
def map_model(model)
  # Implement this in your form object for custom mapping from model to form
  # object as part of the `.from_model` call after matching attributes are
  # populated (optional).
end
persisted?() click to toggle source
# File lib/rectify/form.rb, line 65
def persisted?
  id.present? && id.to_i > 0
end
to_key() click to toggle source
# File lib/rectify/form.rb, line 86
def to_key
  [id]
end
to_model() click to toggle source
# File lib/rectify/form.rb, line 90
def to_model
  self
end
to_param() click to toggle source
# File lib/rectify/form.rb, line 94
def to_param
  id.to_s
end
valid?(options = {}) click to toggle source
Calls superclass method
# File lib/rectify/form.rb, line 69
def valid?(options = {})
  before_validation

  options     = {} if options.blank?
  context     = options[:context]
  validations = [super(context)]

  validations << form_attributes_valid? unless options[:exclude_nested]
  validations << array_attributes_valid? unless options[:exclude_arrays]

  validations.all?
end
with_context(new_context) click to toggle source
# File lib/rectify/form.rb, line 117
def with_context(new_context)
  @context = if new_context.is_a?(Hash)
               OpenStruct.new(new_context)
             else
               new_context
             end

  attributes_that_respond_to(:with_context)
    .each { |f| f.with_context(context) }

  array_attributes_that_respond_to(:with_context)
    .each { |f| f.with_context(context) }

  self
end

Private Instance Methods

array_attributes_that_respond_to(message) click to toggle source
# File lib/rectify/form.rb, line 153
def array_attributes_that_respond_to(message)
  attributes
    .each_value
    .select { |a| a.is_a?(Array) }
    .flatten
    .select { |f| f.respond_to?(message) }
end
array_attributes_valid?() click to toggle source
# File lib/rectify/form.rb, line 141
def array_attributes_valid?
  array_attributes_that_respond_to(:valid?)
    .map(&:valid?)
    .all?
end
attributes_that_respond_to(message) click to toggle source
# File lib/rectify/form.rb, line 147
def attributes_that_respond_to(message)
  attributes
    .each_value
    .select { |f| f.respond_to?(message) }
end
form_attributes_valid?() click to toggle source
# File lib/rectify/form.rb, line 135
def form_attributes_valid?
  attributes_that_respond_to(:valid?)
    .map(&:valid?)
    .all?
end