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

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

ITEM_TYPES =
{
  check_box: Form::Item::SingleOption,
  radio_button: Form::Item::SingleOption,
  text_area: Form::Item::SingleOption,
  text_field: Form::Item::SingleOption,
  password_field: Form::Item::SingleOption,
  hidden_field: Form::Item::SingleOption,
  file_field: Form::Item::SingleOption,
  color_field: Form::Item::SingleOption,
  number_field: Form::Item::SingleOption,
  telephone_field: Form::Item::SingleOption,

  time_select: Form::Item::TwoOptions,
  date_select: Form::Item::TwoOptions,
  datetime_select: Form::Item::TwoOptions,
  collection_select: Form::Item::TwoOptions,
  grouped_collection_select: Form::Item::TwoOptions,

  collection_check_boxes: Form::Item::TwoOptionsWithBlock,
  collection_radio_buttons: Form::Item::TwoOptionsWithBlock,

  block: Form::Item::Block,

  carrierwave_image: Form::Item::CarrierWaveImage,

  globalize_fields: Form::Item::GlobalizeFields
}.freeze

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.



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

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: [] }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dynamic_scaffold/form/item/base.rb', line 34

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

  if ITEM_TYPES[type] == Form::Item::Block
    ITEM_TYPES[type].new(config, type, *args, block)
  else
    ITEM_TYPES[type].new(config, type, *args)
  end
end

Instance Method Details

#default(value = nil) ⇒ Object



138
139
140
141
# File 'lib/dynamic_scaffold/form/item/base.rb', line 138

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

#errors(record) ⇒ Object



152
153
154
155
156
157
# File 'lib/dynamic_scaffold/form/item/base.rb', line 152

def errors(record)
  msg = record.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(record.errors.full_messages_for(rel.name)) if rel.present?
  msg
end

#extract_parameters(permitting) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/dynamic_scaffold/form/item/base.rb', line 102

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

#if(&block) ⇒ Object



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

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

#insert(position, &block) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/dynamic_scaffold/form/item/base.rb', line 143

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

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



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dynamic_scaffold/form/item/base.rb', line 88

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

Returns:

  • (Boolean)


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

def label?
  !@label.nil?
end

#needs_rendering?(view) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
# File 'lib/dynamic_scaffold/form/item/base.rb', line 120

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



68
69
70
71
# File 'lib/dynamic_scaffold/form/item/base.rb', line 68

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

#notes?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/dynamic_scaffold/form/item/base.rb', line 64

def notes?
  !@notes.empty?
end

#proxy(attribute_name) ⇒ Object



126
127
128
129
# File 'lib/dynamic_scaffold/form/item/base.rb', line 126

def proxy(attribute_name)
  @proxy = attribute_name
  self
end

#proxy_fieldObject



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

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



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

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

#type?(type) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/dynamic_scaffold/form/item/base.rb', line 80

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

#unless(&block) ⇒ Object



115
116
117
118
# File 'lib/dynamic_scaffold/form/item/base.rb', line 115

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