Module: FreeForm::Property::ClassMethods

Includes:
Forwardable
Defined in:
lib/freeform/form/property.rb

Instance Method Summary collapse

Instance Method Details

#allow_destroy_on_saveObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/freeform/form/property.rb', line 17

def allow_destroy_on_save
  # Define _destroy method for marked-for-destruction handling
  attr_accessor :_destroy
  define_method(:_destroy=) do |value|
    false_values  = [nil, 0 , false, "0", "false"]
    true_values  = [1 , true, "1", "true"]

    #TODO: Test this!!
    @_destroy = !false_values.include?(value)  # Mark initially
    # if (@_destroy == false) # Can only switch if false
    #   @_destroy = true if true_values.include?(value)
    # end
  end
  alias_method :marked_for_destruction?, :_destroy
  define_method(:mark_for_destruction) do
    @_destroy = true
  end
end

#property(attribute, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/freeform/form/property.rb', line 36

def property(attribute, options={})
  @property_mappings ||= Hash.new
  model = options[:on] ? options [:on] : :self
  field = options[:as] ? options[:as] : attribute.to_sym
  ignore_blank = options[:ignore_blank] ? options[:ignore_blank] : false
  @property_mappings.merge!({field => {:model => model, :field => attribute.to_sym, :ignore_blank => ignore_blank}})

  if model == :self
    attr_accessor attribute
  else
    define_method("#{field}") do
      send("#{model}").send("#{attribute}")
    end

    define_method("#{field}=") do |value|
      send("#{model}").send("#{attribute}=", value)
    end
  end
end

#property_mappingsObject



12
13
14
15
# File 'lib/freeform/form/property.rb', line 12

def property_mappings
  # Take the form of {:property => {:model => model, :field => field, :ignore_blank => false}}
  @property_mappings ||= Hash.new
end