Module: MonkeyForms::Form::InstanceMethods

Defined in:
lib/monkey_forms.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



28
29
30
# File 'lib/monkey_forms.rb', line 28

def attributes
  @attributes
end

Instance Method Details

#html_error_messagesObject

TODO not sure what’s best here



35
36
37
# File 'lib/monkey_forms.rb', line 35

def html_error_messages
  errors.full_messages.join("<br />")
end

#initialize(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/monkey_forms.rb', line 51

def initialize options = {}
  _run_initialize_callbacks do
    form_params = options.delete(:form) || {}
    @options    = options

    @options.each do |key, value|
      instance_variable_set "@#{key}", value
    end

    # Load the saved form from storage
    @attributes =
      if self.class.form_storage
        self.class.form_storage.load(@options)
      else
        ActiveSupport::HashWithIndifferentAccess.new
      end

    # Merge in this form's params
    DeepMerge.deep_merge!(form_params, @attributes)

    self.class.attributes.each do |a|
      @attributes[a] ||= ""
      if self.class.strip_attributes?
        if @attributes[a].class == String
          @attributes[a].strip!
        elsif @attributes[a].class == Array
          @attributes[a].each do |v|
            v.strip! if v.class == String
          end
        end
      end
    end
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/monkey_forms.rb', line 30

def persisted?
  false
end

#save_to_storage!Object



86
87
88
89
# File 'lib/monkey_forms.rb', line 86

def save_to_storage!
  @options[:attributes] = @attributes
  self.class.form_storage.save(@options)
end

#to_keyObject



47
48
49
# File 'lib/monkey_forms.rb', line 47

def to_key
  nil
end

#to_modelObject



39
40
41
# File 'lib/monkey_forms.rb', line 39

def to_model
  self
end

#to_paramObject



43
44
45
# File 'lib/monkey_forms.rb', line 43

def to_param
  nil
end