Class: DynamicScaffold::Form::Item::Base
- Inherits:
-
Object
- Object
- DynamicScaffold::Form::Item::Base
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.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 19
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
#name ⇒ Object
Returns the value of attribute name.
18
19
20
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 18
def name
@name
end
|
Class Method Details
.create(config, type, *args, &block) ⇒ Object
Instance Method Details
#default(value = nil, &block) ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 130
def default(value = nil, &block)
if block_given?
@default = block
else
@default = value
end
end
|
#default_value(view) ⇒ Object
138
139
140
141
142
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 138
def default_value(view)
return view.instance_exec(&@default) if @default.is_a? Proc
@default
end
|
#errors(form) ⇒ Object
153
154
155
156
157
158
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 153
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.concat(form.object.errors.full_messages_for(rel.name)) if rel.present?
msg
end
|
92
93
94
95
96
97
98
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 92
def (permitting)
if @multiple
permitting << { @name => [] }
else
permitting << @name
end
end
|
#if(&block) ⇒ Object
100
101
102
103
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 100
def if(&block)
@if_block = block
self
end
|
#insert(position, &block) ⇒ Object
144
145
146
147
148
149
150
151
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 144
def insert(position, &block)
if block_given?
@inserts[position] << block
self
else
@inserts[position]
end
end
|
#label(*args, &block) ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 58
def label(*args, &block)
return @label || @config.model.human_attribute_name(@name) if args.empty? && block.nil?
@label_attributes = args.
@label = args.first unless args.empty?
@label_block = block if block_given?
self
end
|
#label? ⇒ Boolean
54
55
56
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 54
def label?
!@label.nil?
end
|
#needs_rendering?(view) ⇒ Boolean
110
111
112
113
114
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 110
def needs_rendering?(view)
return true unless @if_block || @unless_block
return view.instance_exec(view.controller.params, &@if_block) if @if_block
return !view.instance_exec(view.controller.params, &@unless_block) if @unless_block
end
|
#note(&block) ⇒ Object
38
39
40
41
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 38
def note(&block)
@notes << block if block_given?
self
end
|
#notes? ⇒ Boolean
34
35
36
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 34
def notes?
!@notes.empty?
end
|
#proxy(attribute_name) ⇒ Object
116
117
118
119
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 116
def proxy(attribute_name)
@proxy = attribute_name
self
end
|
#proxy_field ⇒ Object
121
122
123
124
125
126
127
128
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 121
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 68
def render_label(view, depth)
if @label_block.present?
label = view.instance_exec(
proxy_field.label,
depth,
@label_attributes,
&@label_block
)
return label unless label.nil?
end
view.tag.label(proxy_field.label, @label_attributes)
end
|
#render_notes(record, view) ⇒ Object
43
44
45
46
47
48
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 43
def render_notes(record, view)
htmls = @notes.map do |note|
view.instance_exec(record, ¬e)
end
view.safe_join(htmls)
end
|
#type?(*args) ⇒ Boolean
50
51
52
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 50
def type?(*args)
args.include?(@type)
end
|
#unless(&block) ⇒ Object
105
106
107
108
|
# File 'lib/dynamic_scaffold/form/item/base.rb', line 105
def unless(&block)
@unless_block = block
self
end
|