Class: DynamicScaffold::Form::Item::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_scaffold/form/item/base.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, type, name, html_attributes = {}) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dynamic_scaffold/form/item/base.rb', line 22

def initialize(config, type, name, html_attributes = {})
  @config = config
  @name = name
  @type = type
  @html_attributes = html_attributes
  classnames = @html_attributes.delete(:class)
  @classnames_list = []
  @classnames_list.push(classnames) if classnames
  @notes = []
  @multiple = type == :collection_check_boxes || html_attributes[:multiple]
  @inserts = { before: [], after: [] }
  @label_attributes = {}
  @label_block = nil
end

Instance Attribute Details

#multipleObject (readonly)

Returns the value of attribute multiple.



21
22
23
# File 'lib/dynamic_scaffold/form/item/base.rb', line 21

def multiple
  @multiple
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/dynamic_scaffold/form/item/base.rb', line 21

def name
  @name
end

#parent_itemObject

Returns the value of attribute parent_item.



20
21
22
# File 'lib/dynamic_scaffold/form/item/base.rb', line 20

def parent_item
  @parent_item
end

Class Method Details

.create(config, type, *args, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/dynamic_scaffold/form/item/base.rb', line 8

def create(config, type, *args, &block)
  if Form::Item::Type::LIST[type].nil?
    raise(
      DynamicScaffold::Error::InvalidParameter,
      "Unknown form item type #{type}. supported: #{Form::Item::Type::LIST.keys.join(', ')}"
    )
  end

  Form::Item::Type::LIST[type].new(config, type, *args, &block)
end

Instance Method Details

#default(value = nil, &block) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/dynamic_scaffold/form/item/base.rb', line 148

def default(value = nil, &block)
  if block_given?
    @default = block
  else
    @default = value
  end
  self
end

#default_value(view) ⇒ Object



157
158
159
160
161
# File 'lib/dynamic_scaffold/form/item/base.rb', line 157

def default_value(view)
  return view.instance_exec(&@default) if @default.is_a? Proc

  @default
end

#errors(form) ⇒ Object



172
173
174
175
176
177
# File 'lib/dynamic_scaffold/form/item/base.rb', line 172

def errors(form)
  msg = form.object.errors.full_messages_for(proxy_field.name)
  rel = @config.model.reflect_on_all_associations.find {|r| r.foreign_key.to_s == name.to_s }
  msg = msg.dup.concat(form.object.errors.full_messages_for(rel.name)) if rel.present?
  msg
end

#extract_parameters(permitting) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/dynamic_scaffold/form/item/base.rb', line 110

def extract_parameters(permitting)
  if @multiple
    permitting << { @name => [] }
  else
    permitting << @name
  end
end

#if(&block) ⇒ Object



118
119
120
121
# File 'lib/dynamic_scaffold/form/item/base.rb', line 118

def if(&block)
  @if_block = block
  self
end

#insert(position, &block) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/dynamic_scaffold/form/item/base.rb', line 163

def insert(position, &block)
  if block_given?
    @inserts[position] << block
    self
  else
    @inserts[position]
  end
end

#label(*args, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/dynamic_scaffold/form/item/base.rb', line 70

def label(*args, &block)
  return @label || @config.model.human_attribute_name(@name) if args.empty? && block.nil?

  @label_attributes = args.extract_options!
  @label = args.first unless args.empty?
  @label_block = block if block_given?

  self
end

#label?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dynamic_scaffold/form/item/base.rb', line 66

def label?
  !@label.nil?
end

#needs_rendering?(view, record) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
# File 'lib/dynamic_scaffold/form/item/base.rb', line 128

def needs_rendering?(view, record)
  return true unless @if_block || @unless_block
  return view.instance_exec(view.controller.params, record, &@if_block) if @if_block
  return !view.instance_exec(view.controller.params, record, &@unless_block) if @unless_block
end

#note(&block) ⇒ Object



48
49
50
51
# File 'lib/dynamic_scaffold/form/item/base.rb', line 48

def note(&block)
  @notes << block if block_given?
  self
end

#notes?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/dynamic_scaffold/form/item/base.rb', line 44

def notes?
  !@notes.empty?
end

#proxy(attribute_name) ⇒ Object



134
135
136
137
# File 'lib/dynamic_scaffold/form/item/base.rb', line 134

def proxy(attribute_name)
  @proxy = attribute_name
  self
end

#proxy_fieldObject



139
140
141
142
143
144
145
146
# File 'lib/dynamic_scaffold/form/item/base.rb', line 139

def proxy_field
  return self unless @proxy

  proxy_target = @config.form.items.select {|f| f.name == @proxy }
  raise DynamicScaffold::Error::InvalidParameter, "Missing proxy target element: #{@proxy}" if proxy_target.empty?

  proxy_target.first
end

#render_label(view, depth) ⇒ Object



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
# File 'lib/dynamic_scaffold/form/item/base.rb', line 80

def render_label(view, depth)
  label_attrs = @label_attributes.dup
  if label_attrs[:class].present?
    label_attrs[:class] = "ds-label #{label_attrs[:class]}"
  else
    label_attrs[:class] = 'ds-label'
  end

  if @label_block.present?
    label = view.instance_exec(
      proxy_field.label,
      depth,
      label_attrs,
      &@label_block
    )
    return label unless label.nil?
  end

  # if DynamicScaffold.config.form.label.present?
  #   label = view.instance_exec(
  #     proxy_field.label,
  #     depth,
  #     label_attrs,
  #     &DynamicScaffold.config.form.label
  #   )
  #   return label unless label.nil?
  # end
  view.tag.label(proxy_field.label, **label_attrs) if proxy_field.label.present?
end

#render_notes(record, view) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/dynamic_scaffold/form/item/base.rb', line 53

def render_notes(record, view)
  htmls = @notes.map do |note|
    view.capture do
      view.instance_exec(record, &note)
    end
  end
  view.safe_join(htmls)
end

#type?(*args) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/dynamic_scaffold/form/item/base.rb', line 62

def type?(*args)
  args.include?(@type)
end

#unique_nameObject



37
38
39
40
41
42
# File 'lib/dynamic_scaffold/form/item/base.rb', line 37

def unique_name
  results = [@config.model.table_name]
  results << parent_item.name if parent_item.present?
  results << name
  results.join('_')
end

#unless(&block) ⇒ Object



123
124
125
126
# File 'lib/dynamic_scaffold/form/item/base.rb', line 123

def unless(&block)
  @unless_block = block
  self
end