module Formalism::Form::Filling

Module for filling forms with data

Private Instance Methods

fill_depends(*depends_on) click to toggle source
# File lib/formalism/form/filling.rb, line 33
def fill_depends(*depends_on)
        depends_on.each do |depends_name|
                next unless self.class.fields_and_nested_forms.key?(depends_name)

                fill_field_or_nested_form depends_name
        end
end
fill_field(name, options) click to toggle source
# File lib/formalism/form/filling.rb, line 41
def fill_field(name, options)
        key = options.fetch(:key, name)
        setter = "#{name}="

        if @params.key?(key)
                send setter, @params[key]
        elsif instance_respond_to?(key)
                send setter, instance_public_send(key)
        elsif options.key?(:default) && !fields.include?(key)
                send setter, process_default(options[:default])
        end
end
fill_field_or_nested_form(name) click to toggle source
# File lib/formalism/form/filling.rb, line 17
def fill_field_or_nested_form(name)
        return if @filled_fields_and_nested_forms.include? name

        options = self.class.fields_and_nested_forms[name]

        fill_depends(*options[:depends_on])

        if options.key?(:form)
                fill_nested_form name, options
        else
                fill_field name, options
        end

        @filled_fields_and_nested_forms.push name
end
fill_fields_and_nested_forms() click to toggle source
# File lib/formalism/form/filling.rb, line 9
def fill_fields_and_nested_forms
        @filled_fields_and_nested_forms = []

        self.class.fields_and_nested_forms.each_key do |name|
                fill_field_or_nested_form name
        end
end
fill_nested_form(name, options) click to toggle source
# File lib/formalism/form/filling.rb, line 54
def fill_nested_form(name, options)
        return unless (form = initialize_nested_form(name, options))

        nested_forms[name] = form
end
initialize_nested_form(name, options) click to toggle source
# File lib/formalism/form/filling.rb, line 64
def initialize_nested_form(name, options)
        args =
                if @params.key?(name) then [send("params_for_nested_#{name}")]
                elsif instance_respond_to?(name) then [instance_public_send(name)]
                elsif options.key?(:default) then [process_default(options[:default])]
                else []
                end

        result =
                instance_exec options[:form], &options.fetch(:initialize, ->(form) { form.new(*args) })
        result.runnable = false unless runnable
        result
end
process_default(default) click to toggle source
# File lib/formalism/form/filling.rb, line 60
def process_default(default)
        default.is_a?(Proc) ? instance_exec(&default) : default
end