Module: Hibiki::Rails::ReactiveForm::ClassMethods

Defined in:
lib/hibiki/rails/reactive_form.rb

Instance Method Summary collapse

Instance Method Details

#from(record) ⇒ Object

Hydrate from a record — the only supported constructor. Extra arguments are forwarded to #initialize, so a form with its own constructor still works.



50
# File 'lib/hibiki/rails/reactive_form.rb', line 50

def from(record, ...) = new(...).hydrate(record)

#hibiki_attributesObject



67
68
69
# File 'lib/hibiki/rails/reactive_form.rb', line 67

def hibiki_attributes
  @hibiki_attributes || __hibiki_inherited(:hibiki_attributes) || []
end

#hibiki_modelObject



71
72
73
74
75
76
# File 'lib/hibiki/rails/reactive_form.rb', line 71

def hibiki_model
  model = @hibiki_model || __hibiki_inherited(:hibiki_model)
  raise "#{self} has no reactive_attributes declaration" if model.nil?

  model.is_a?(Module) ? model : model.to_s.constantize
end

#hibiki_type(name) ⇒ Object

Channel action params arrive as strings; casting through the model's own attribute type is the difference between done = "false" meaning false and meaning true.



81
# File 'lib/hibiki/rails/reactive_form.rb', line 81

def hibiki_type(name) = hibiki_model.type_for_attribute(name.to_s)

#reactive_attributes(model, *names) ⇒ Object

reactive_attributes Todo, :title, :done

The model may be a Class or a String/Symbol; a name is resolved on every use, so a form class under app/ never pins a constant across a Zeitwerk reload.



57
58
59
60
61
62
63
64
65
# File 'lib/hibiki/rails/reactive_form.rb', line 57

def reactive_attributes(model, *names)
  @hibiki_model = model
  @hibiki_attributes = names.map(&:to_sym)
  casts = __hibiki_casts
  @hibiki_attributes.each do |name|
    state name
    casts.define_method(:"#{name}=") { |value| super(self.class.hibiki_type(name).cast(value)) }
  end
end