Module: DefaultForm::Builder::Default

Included in:
Helper
Defined in:
lib/default_form/builder/default.rb

Constant Summary collapse

VALIDATIONS =
[
  :required,
  :pattern,
  :min, :max, :step,
  :maxlength
].freeze

Instance Method Summary collapse

Instance Method Details

#default_help(method) ⇒ Object



18
19
20
21
22
# File 'lib/default_form/builder/default.rb', line 18

def default_help(method)
  if object.is_a?(ActiveRecord::Base)
    object.class.help_i18n(method)
  end
end

#default_label(method) ⇒ Object



12
13
14
15
16
# File 'lib/default_form/builder/default.rb', line 12

def default_label(method)
  if object.is_a?(ActiveRecord::Base)
    object.class.human_attribute_name(method)
  end
end

#default_options(method = nil, 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
# File 'lib/default_form/builder/default.rb', line 51

def default_options(method = nil, options = {})
  # search 应返回默认 params 中对应的 value
  on = options.slice(:label, :placeholder, :autocomplete, :autofilter)
  on.reverse_merge! on_options
  if on[:autofilter] && !(options.key?(:value) || on[:autocomplete])
    options[:value] = default_value(method)
  end

  if on[:placeholder] && !options.key?(:placeholder)
    options[:placeholder] = default_label(method)
  end

  if on[:label] && !options.key?(:label)
    options[:label] = default_label(method)
  end

  valid_key = options.keys.map(&:to_sym) & VALIDATIONS
  if valid_key.present?
    action_str = 'default_valid#clear blur->default_valid#check invalid->default_valid#notice'
    options[:data] ||= {}
    if options[:data][:action].present?
      options[:data][:action] += " #{action_str}"
    else
      options[:data][:action] = action_str
    end unless options[:data].key?(:valid)
  end
end

#default_step(method) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/default_form/builder/default.rb', line 37

def default_step(method)
  if object.is_a?(ActiveRecord::Base)
    col = object.class.column_for_attribute(method)

    if col.type == :decimal
      0.01
    else
      1
    end
    #scale = col.scale || col.limit
    #0.1.to_d.power(scale.to_i)
  end
end

#default_value(method) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/default_form/builder/default.rb', line 24

def default_value(method)
  if object.is_a?(ActiveRecord::Base)
    r = object.respond_to?(method) && object.send(method)
    return r if r
  end

  if object_name.present?
    params.dig(object_name, method)
  else
    params[method]
  end
end

#default_without_method(options = {}) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/default_form/builder/default.rb', line 79

def default_without_method(options = {})
  origin = (options.delete(:origin) || {}).with_defaults!(origin_css)
  wrap = (options.delete(:wrap) || {}).with_defaults!(wrap_css)
  error = (options.delete(:error) || {}).with_defaults!(error_css)

  [origin, wrap, error]
end