Class: Utilisable::Form

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/utilisable/form.rb

Overview

Form object to move validations from models. Usually used when similar model needs different validations on different forms. Can be used to build attributes for model to save. Example:

class UserForm < ApplicationForm

attribute :user_id, Integer
attribute :user_name, String
attribute :sibling_name, String, remove_from_hash: true

include_in_hash :sibling_id

validates :user_id, :user_name, presence: true
validate :is_kyle

private

def is_kyle
  return true if user_name == 'Kyle'

  errors.add(:user_name, 'should be Kyle')
end

def sibling_id
  @sibling_id ||= User.find_by(name: sibling_name)
end

end

user_params = 1, user_name: 'Kyle', sibling_name: 'John' form = UserForm.new(user_params) form.valid? - true form = UserForm.new(user_params.merge(user_name: 'Steve')) form.valid? - false form.errors.full_messages # [“User name should be Kyle”] form = UserForm.new(user_params.merge(user_id: 'test')) form.valid? # false form.errors.full_messages # [“User can't be blank”] form = UserForm.new(user_params.merge(user_id: '1')) # will convert user_id into Integer form.to_h # { user_id: 1, user_name: 'Kyle', sibling_id: 12 }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Form

Returns a new instance of Form.


50
51
52
53
54
55
# File 'lib/utilisable/form.rb', line 50

def initialize(hash)
  hash.keys.each do |key|
    attribute = self.class.attributes.find { |attr| attr.attribute_name == key.to_sym }
    set_attribute(attribute, hash[key]) if attribute.present?
  end
end

Class Method Details

.from_model(model) ⇒ Object


57
58
59
# File 'lib/utilisable/form.rb', line 57

def self.from_model(model)
  new(model.attributes)
end

Instance Method Details

#errors_to_stringObject


91
92
93
# File 'lib/utilisable/form.rb', line 91

def errors_to_string
  errors.full_messages.to_sentence
end

#set(name, value) ⇒ Object


74
75
76
77
78
79
80
81
82
# File 'lib/utilisable/form.rb', line 74

def set(name, value)
  attribute = self.class.attributes.find { |attr| attr.attribute_name.to_s == name.to_s }
  if attribute.present?
    set_attribute(attribute, value)
  else
    instance_variable_set("@#{name}", value)
  end
  self
end

#set_attributes(hash) ⇒ Object


84
85
86
87
88
89
# File 'lib/utilisable/form.rb', line 84

def set_attributes(hash)
  hash.to_h.to_a.each do |attr_array|
    set(attr_array.first, attr_array.second)
  end
  self
end

#to_hObject


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/utilisable/form.rb', line 61

def to_h
  hash = {}.with_indifferent_access
  self.instance_variables.each do |var|
    var_name = var.to_s.delete('@')
    attribute = self.class.attributes.find { |attr| attr.attribute_name == var_name.to_sym }
    hash[var_name] = self.instance_variable_get(var) if attribute&.remove_from_hash == false
  end
  self.class.methods_for_hash.each do |method|
    hash[method] = self.send(method)
  end
  hash
end