Class: Cfror::Field

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cfror/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#value_objectObject

Returns the value of attribute value_object.



5
6
7
# File 'app/models/cfror/field.rb', line 5

def value_object
  @value_object
end

Instance Method Details

#save_option!(model, value) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/models/cfror/field.rb', line 37

def save_option!(model, value)
  option_data = self.options.find_by(dataable: model)
  if option_data
    option_data.update_attribute(:select_option, SelectOption.find(value))
  else
    options.create!(dataable: model, select_option: SelectOption.find(value))
  end
end

#save_value!(model, value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/cfror/field.rb', line 22

def save_value!(model, value)
  type = field_type.to_sym

  if type == :option
    save_option!(model, value)
  else
    data = send("#{field_type}s").find_by(dataable: model)
    if data
      data.update_attribute(:body, value)
    else
      send("#{field_type}s").create!(dataable: model, body: value)
    end
  end
end

#set_value_for(model) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'app/models/cfror/field.rb', line 46

def set_value_for(model)
  type = field_type.to_sym

  self.value_object = if type == :option
                self.options.find_by(dataable: model).try(:select_option)
              else
                send("#{field_type}s").find_by(dataable: model)
              end
end

#valueObject



56
57
58
# File 'app/models/cfror/field.rb', line 56

def value
  self.value_object.try(:body)
end