Class: Formily::Input

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/formily/input.rb

Instance Method Summary collapse

Instance Method Details

#conditioned_tagObject



33
34
35
36
37
38
39
40
41
# File 'app/models/formily/input.rb', line 33

def conditioned_tag
  tag_attr = JSON.parse(self.tag_attributes) rescue {}

  if tag_attr['formily_conditioned'].present?
    tag_attr['formily_conditioned']
  else
    ''
  end
end

#editor_typeObject



51
52
53
# File 'app/models/formily/input.rb', line 51

def editor_type
  'input'
end

#get_default_value(val, controller) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/formily/input.rb', line 66

def get_default_value(val, controller)

  if val.blank?
    if self.default_value.present?

      case self.default_value

        when 'Date'
          DateTime.now.strftime('%Y/%m/%d')
        when 'DateTime'
          DateTime.now.strftime('%Y/%m/%d %H:%M')
        when 'current_user_name'
          controller.current_user.name rescue ''
        when 'device_location'
          'device_location'
        when 'related_value'
          tag_attr = JSON.parse(self.tag_attributes) rescue {}
          val_arr = tag_attr['data-related'].split('.')

          @obj_rel = if val_arr.first =~ /->/
                       m = val_arr.first.sub(/.*?->/, '').safe_constantize
                       user_id = controller.instance_variable_get('@user_id')
                       if user_id
                         m.find(user_id)
                       else
                         m.order('RANDOM()').first rescue nil
                       end
                     else
                       m = controller.instance_variable_get('@obj_rel')
                       m || val_arr.first.safe_constantize
                     end

          val_arr[1..-1].inject(@obj_rel){|obj, t| obj.send(t) } rescue ''

        else
          self.default_value
      end

    else
      self.tag_attributes =~ /"readonly":"readonly"/ ? self.label : val
    end
  else
    val
  end
end

#get_value_from_submit(submit, controller = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'app/models/formily/input.rb', line 55

def get_value_from_submit(submit, controller = nil)
  val = if submit.present?
          submit.input_values.where(input_id: self.id).pluck(:input_value).first || nil rescue nil
        else
          nil
        end


  get_default_value(val, controller)
end

#partial_pathObject



43
44
45
# File 'app/models/formily/input.rb', line 43

def partial_path
  'formily/partial/inputs/' + self.class.name.demodulize.underscore
end

#render_attributesObject



19
20
21
22
23
24
25
26
27
# File 'app/models/formily/input.rb', line 19

def render_attributes
  tag_attr = JSON.parse(self.tag_attributes) rescue {}

  if tag_attr['formily_conditioned'].present?
    tag_attr.delete('formily_conditioned')
  end

  tag_attr.map{|k, v| "#{k}=\"#{v}\""}.join(' ').html_safe
end

#render_attributes_hashObject



29
30
31
# File 'app/models/formily/input.rb', line 29

def render_attributes_hash
  JSON.parse(self.tag_attributes).except('formily_conditioned') rescue {}
end

#to_htmlObject



47
48
49
# File 'app/models/formily/input.rb', line 47

def to_html
  "<label>#{self.label}<input type=\"text\" name=\"formily_inputs[#{self.name}]\"/></label>"
end