Class: Effective::FormInputs::HasMany

Inherits:
Effective::FormInput show all
Defined in:
app/models/effective/form_inputs/has_many.rb

Constant Summary collapse

BLANK =
''.html_safe

Constants inherited from Effective::FormInput

Effective::FormInput::DEFAULT_FEEDBACK_OPTIONS, Effective::FormInput::DEFAULT_INPUT_GROUP_OPTIONS, Effective::FormInput::EMPTY_HASH, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES, Effective::FormInput::HORIZONTAL_LABEL_OPTIONS, Effective::FormInput::HORIZONTAL_WRAPPER_OPTIONS, Effective::FormInput::INLINE_LABEL_OPTIONS, Effective::FormInput::VERTICAL_WRAPPER_OPTIONS

Instance Attribute Summary

Attributes inherited from Effective::FormInput

#name, #options

Instance Method Summary collapse

Methods inherited from Effective::FormInput

#feedback_options, #hint_options, #initialize, #input_group_options, #label_options, #wrapper_options

Constructor Details

This class inherits a constructor from Effective::FormInput

Instance Method Details

#add?Boolean

add: true

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'app/models/effective/form_inputs/has_many.rb', line 45

def add?
  return @add unless @add.nil?

  @add ||= begin
    add = options[:input].delete(:add)
    add.nil? ? true : add
  end
end

#build?Boolean

build: true

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
# File 'app/models/effective/form_inputs/has_many.rb', line 35

def build?
  return @build unless @build.nil?

  @build ||= begin
    build = options[:input].delete(:build)
    build.nil? ? true : build
  end
end

#can_remove_methodObject



80
81
82
83
# File 'app/models/effective/form_inputs/has_many.rb', line 80

def can_remove_method
  return @can_remove_method unless @can_remove_method.nil?
  @can_remove_method = (options[:input].delete(:can_remove_method) || false)
end

#collectionObject



25
26
27
# File 'app/models/effective/form_inputs/has_many.rb', line 25

def collection
  Array(options[:input][:collection] || object.send(name))
end

#displayObject

cards: false



30
31
32
# File 'app/models/effective/form_inputs/has_many.rb', line 30

def display
  @display ||= (options[:input].delete(:cards) ? :cards : :rows)
end

#errors?Boolean

errors: true

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# File 'app/models/effective/form_inputs/has_many.rb', line 55

def errors?
  return @errors unless @errors.nil?

  @errors ||= begin
    errors = options[:input].delete(:errors)
    errors.nil? ? true : errors
  end
end

#input_html_optionsObject



17
18
19
# File 'app/models/effective/form_inputs/has_many.rb', line 17

def input_html_options
  { class: 'form-has-many mb-4' }
end

#input_js_optionsObject



21
22
23
# File 'app/models/effective/form_inputs/has_many.rb', line 21

def input_js_options
  { sortable: true }
end

#remove?Boolean

remove: true

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/effective/form_inputs/has_many.rb', line 65

def remove?
  return @remove unless @remove.nil?

  @remove ||= begin
    remove = options[:input].delete(:remove)

    if remove != nil
      remove
    else
      opts = (object.class.nested_attributes_options[name] || {})
      opts[:update_only] != true && opts[:allow_destroy] != false
    end
  end
end

#reorder?Boolean

reorder: true

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/effective/form_inputs/has_many.rb', line 86

def reorder?
  return @reorder unless @reorder.nil?

  @reorder ||= begin
    reorder = options[:input].delete(:reorder)

    if reorder != nil
      reorder
    else
      build_resource().class.columns_hash['position']&.type == :integer
    end
  end
end

#to_html(&block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'app/models/effective/form_inputs/has_many.rb', line 6

def to_html(&block)
  object.send(name).build() if build? && collection.blank?

  errors = (@builder.error(name) if errors?) || BLANK
  can_remove_method

  errors + (:div, options[:input].except(:collection)) do
    has_many_fields_for(block) + has_many_links_for(block)
  end
end