Module: CustomFieldAnswer::Field

Extended by:
ActiveSupport::Concern
Included in:
CustomFieldAnswer
Defined in:
app/models/custom_field_answer/field.rb

Defined Under Namespace

Modules: DateField, NumberField, TextAnswerField

Instance Method Summary collapse

Instance Method Details

#after_initializeObject

We need to override after_initialize module to force instance to extend proper module ActiveRecord instances are created sometimes using malloc which will not invoke the initialize but they will call after_initialize



30
31
32
# File 'app/models/custom_field_answer/field.rb', line 30

def after_initialize
  extend_module
end

#custom_field=(custom_field) ⇒ Object



5
6
7
8
9
# File 'app/models/custom_field_answer/field.rb', line 5

def custom_field=(custom_field)
  set_custom_field(custom_field)

  extend_module
end

#custom_field_id=(question_id) ⇒ Object

when question_id is assigned extend the module



12
13
14
15
16
# File 'app/models/custom_field_answer/field.rb', line 12

def custom_field_id=(question_id)
  super

  extend_module
end

#extend_moduleObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/custom_field_answer/field.rb', line 34

def extend_module
  if target_module = module_to_extend
    # Don't include module twice
    kind_of?(target_module) || extend(target_module)

    # Force value coersion for extended module
    #
    # For example
    # if answer is created as shown below
    # case(1)
    # q = NumberQuestion.new
    # a = Answer.new(:question => q, :value => 23.45)
    # a.float_value ==> nil
    # a.value  ==> '23.45'
    #
    # case(2)
    # a = Answer.new(:question => q)
    # a.value = 23.45
    # a.float_value ==> 23.45
    # a.value  ==> '23.45'
    #
    # Above case appears bcoz modules are extended after
    # attributes are assigned. reassign_value method will
    # give the modules to fix this problem please refer
    # to NumberResponse reassign_value
    #
    # Note: currently both cases will work as expected
    reassign_value
  end
end

#initialize(attributes = nil, options = {}) ⇒ Object

Override Answer initialize method to extend proper Response module For example Answer instance whose question is TextFieldQuestion will extend TextFieldResponse



21
22
23
24
25
# File 'app/models/custom_field_answer/field.rb', line 21

def initialize(attributes = nil, options = {})
  super

  extend_module
end

#module_to_extendObject

This method will determine which module to extend Answer instances which are created using select might throw exception For example Answer.find(:conditions => [], :select => “value”) retrieve_module_name will throw exception when it tries to access question field This is the only case Answer will fail to extend proper module



70
71
72
73
74
75
76
# File 'app/models/custom_field_answer/field.rb', line 70

def module_to_extend
  begin
    CustomFieldAnswer::Field.const_get("#{custom_field.field_type.camelize}Field")
  rescue
    nil
  end
end

#reassign_valueObject



78
79
80
# File 'app/models/custom_field_answer/field.rb', line 78

def reassign_value
  self.value = self.value
end