class DynamicScaffold::Form::Item::Base

Attributes

multiple[R]
name[R]
parent_item[RW]

Public Class Methods

create(config, type, *args, &block) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 6
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
new(config, type, name, html_attributes = {}) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 20
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

Public Instance Methods

default(value = nil, &block) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 147
def default(value = nil, &block)
  if block_given?
    @default = block
  else
    @default = value
  end
  self
end
default_value(view) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 156
def default_value(view)
  return view.instance_exec(&@default) if @default.is_a? Proc

  @default
end
errors(form) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 171
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
extract_parameters(permitting) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 109
def extract_parameters(permitting)
  if @multiple
    permitting << { @name => [] }
  else
    permitting << @name
  end
end
if(&block) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 117
def if(&block)
  @if_block = block
  self
end
insert(position, &block) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 162
def insert(position, &block)
  if block_given?
    @inserts[position] << block
    self
  else
    @inserts[position]
  end
end
label(*args, &block) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 68
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?() click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 64
def label?
  !@label.nil?
end
needs_rendering?(view, record) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 127
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) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 46
def note(&block)
  @notes << block if block_given?
  self
end
notes?() click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 42
def notes?
  !@notes.empty?
end
proxy(attribute_name) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 133
def proxy(attribute_name)
  @proxy = attribute_name
  self
end
proxy_field() click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 138
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) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 78
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) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 51
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) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 60
def type?(*args)
  args.include?(@type)
end
unique_name() click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 35
def unique_name
  results = [@config.model.table_name]
  results << parent_item.name if parent_item.present?
  results << name
  results.join('_')
end
unless(&block) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 122
def unless(&block)
  @unless_block = block
  self
end

Protected Instance Methods

build_args(view, args) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 188
def build_args(view, args)
  args.map do |arg|
    next arg unless arg.is_a? Proc

    view.instance_exec(&arg)
  end
end
build_html_attributes(classnames) click to toggle source
# File lib/dynamic_scaffold/form/item/base.rb, line 180
def build_html_attributes(classnames)
  classnames_list = @classnames_list
  classnames_list = [*classnames_list, classnames] if classnames
  options = @html_attributes.dup
  options[:class] = classnames_list.join(' ') unless classnames_list.empty?
  options
end