Module: Shaf::Formable

Defined in:
lib/shaf/formable.rb,
lib/shaf/formable/form.rb,
lib/shaf/formable/field.rb,
lib/shaf/formable/builder.rb

Defined Under Namespace

Classes: Builder, Field, Form

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_class_reader(clazz, name, form) ⇒ Object



5
6
7
# File 'lib/shaf/formable.rb', line 5

def self.add_class_reader(clazz, name, form)
  clazz.define_singleton_method(name) { form.dup }
end

.add_instance_reader(clazz, name, form, prefill) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/shaf/formable.rb', line 9

def self.add_instance_reader(clazz, name, form, prefill)
  clazz.define_method(name) do
    form.dup.tap do |f|
      f.resource = self
      f.fill! if prefill
    end
  end
end

Instance Method Details

#forms_for(clazz, &block) ⇒ Object Also known as: form_for



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shaf/formable.rb', line 18

def forms_for(clazz, &block)
  builder = Formable::Builder.new(&block)
  builder.forms.each do |form_wrapper|
    method_name = form_wrapper.method_name
    next unless method_name

    form = form_wrapper.form

    Formable.add_class_reader(clazz, method_name, form)
    next unless form_wrapper.instance_accessor?

    Formable.add_instance_reader(clazz, method_name, form, form_wrapper.prefill?)
  end
end