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

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

Direct Known Subclasses

Block, SingleOption, TwoOptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



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

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]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/dynamic_scaffold/form/item/base.rb', line 5

def name
  @name
end

Instance Method Details

#default(value = nil) ⇒ Object



89
90
91
92
# File 'lib/dynamic_scaffold/form/item/base.rb', line 89

def default(value = nil)
  return @default if value.nil?
  @default = value
end

#if(&block) ⇒ Object



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

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

#label(label = nil, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dynamic_scaffold/form/item/base.rb', line 42

def label(label = nil, &block)
  if block_given?
    raise Error::InvalidParameter, 'Only the block type accepts block.' unless type? :block
    @block = block
  end
  if label
    @label = label
    self
  else
    return @label if @label
    @config.model.human_attribute_name @name
  end
end

#label?Boolean



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

def label?
  !@label.nil?
end

#needs_rendering?(view) ⇒ Boolean



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

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



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

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

#notes?Boolean



18
19
20
# File 'lib/dynamic_scaffold/form/item/base.rb', line 18

def notes?
  !@notes.empty?
end

#proxy(attribute_name) ⇒ Object



77
78
79
80
# File 'lib/dynamic_scaffold/form/item/base.rb', line 77

def proxy(attribute_name)
  @proxy = attribute_name
  self
end

#proxy_fieldObject

Raises:

  • (DynamicScaffold::Error::InvalidParameter)


82
83
84
85
86
87
# File 'lib/dynamic_scaffold/form/item/base.rb', line 82

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_notes(record, view) ⇒ Object



27
28
29
30
31
32
# File 'lib/dynamic_scaffold/form/item/base.rb', line 27

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

#strong_parameterObject



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

def strong_parameter
  return { @name => [] } if @multiple
  @name
end

#type?(type) ⇒ Boolean



34
35
36
# File 'lib/dynamic_scaffold/form/item/base.rb', line 34

def type?(type)
  @type == type
end

#unless(&block) ⇒ Object



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

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