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)


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

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)


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

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

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

#can_remove_methodObject



100
101
102
103
# File 'app/models/effective/form_inputs/has_many.rb', line 100

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



27
28
29
# File 'app/models/effective/form_inputs/has_many.rb', line 27

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

#displayObject

cards: false



32
33
34
# File 'app/models/effective/form_inputs/has_many.rb', line 32

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

#errors?Boolean

errors: true

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'app/models/effective/form_inputs/has_many.rb', line 71

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

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

#input_html_optionsObject



19
20
21
# File 'app/models/effective/form_inputs/has_many.rb', line 19

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

#input_js_optionsObject



23
24
25
# File 'app/models/effective/form_inputs/has_many.rb', line 23

def input_js_options
  { sortable: true }
end

#insertObject

insert: false



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

def insert
  return @insert unless @insert.nil?

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

#insert?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/effective/form_inputs/has_many.rb', line 66

def insert?
  !!insert
end

#removeObject

remove: true



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/effective/form_inputs/has_many.rb', line 81

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

#remove?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'app/models/effective/form_inputs/has_many.rb', line 96

def remove?
  !!remove
end

#reorder?Boolean

reorder: true

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/effective/form_inputs/has_many.rb', line 106

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



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

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