class Wedge::Plugins::Form

Attributes

_accessor_options[RW]
_accessors[RW]
_aliases[RW]

Private Class Methods

_attr_accessors() click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 302
def self._attr_accessors
  @_attr_accessors ||= []
end
_form() click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 306
def self._form
  @_form || {}
end
alias_model(alias_name, original_name)
Alias for: model_alias
attr_accessor(*attrs, &block) click to toggle source
# File lib/wedge/plugins/form.rb, line 181
def attr_accessor(*attrs, &block)
  attrs.each_with_index do |att, i|
    if att.is_a? Hash
      # remove the hash from the attrs, use them as options
      options = attrs.delete_at i
      # set the type class to aa string so it's not turned into an
      # anonymous class
      if type = options.delete(:type)
        options[:type] = type.to_s
      end
      # merge and att them to the accessor options
      attrs.each do |a|
        ((@_accessor_options ||= IndifferentHash.new)[a] ||= {}).merge! options
      end
    else
      # issue: OPAL is not using the alias method original_attr_reader
      # correctly.  It's still somehow getting in here when called below.
      next if %w'_atts _options _atts_keys'.include? att.to_s
      ###################################################################

      # set empty options if need be
      (@_accessor_options ||= IndifferentHash.new)[att] ||= {}
      # store the accessors
      ((@_accessors ||= []) << att).uniq!
      define_method(att) { _atts.send att }
    end
  end

  _delegates(*attrs)
end
Also aliased as: original_attr_accessor
attr_reader(*attrs, &block) click to toggle source
# File lib/wedge/plugins/form.rb, line 161
def attr_reader(*attrs, &block)
  default_opts = { read_only: true }
  opts = attrs.pop

  if opts.is_a? Hash
    default_opts.merge! opts
    attrs << default_opts.merge!(opts)
  else
    attrs << opts
    attrs << default_opts
  end

  attr_accessor(*attrs, &block)
end
Also aliased as: original_attr_reader
form_accessor(name, options = {}) click to toggle source
# File lib/wedge/plugins/form.rb, line 176
def form_accessor name, options = {}
  attr_accessor *[name, { form: name }.merge(options)]
end
inherited(subclass) click to toggle source

We need to set instance variables on the inherited class

# File lib/wedge/plugins/form.rb, line 213
def inherited(subclass)
  return if name == 'Wedge::Plugins::Form'

  subclass.instance_variable_set :@_accessors, @_accessors.deep_dup
  subclass.instance_variable_set :@_accessor_options, @_accessor_options.deep_dup
  subclass.instance_variable_set :@_aliases, @_aliases.deep_dup
end
model_alias(alias_name, original_name) click to toggle source
# File lib/wedge/plugins/form.rb, line 221
def model_alias alias_name, original_name
  @_aliases ||= IndifferentHash.new
  @_aliases[original_name] = alias_name
  # discuss: should we also alias_method. right now I'm think no, reason
  # being it's just a model alias and shouldn't allow people to call
  # that method on the form to avoid some people using one name and some
  # another.
  # alias_method alias_name, original_name
end
Also aliased as: alias_model
new(atts = {}, options = {}) click to toggle source

Initialize with a hash of attributes and values. Extra attributes are discarded.

@example

class EditPost < Scrivener
  attr_accessor :title
  attr_accessor :body

  def validate
    assert_present :title
    assert_present :body
  end
end

edit = EditPost.new(title: "Software Tools")

edit.valid? #=> false

edit.errors[:title] #=> []
edit.errors[:body]  #=> [:not_present]

edit.body = "Recommended reading..."

edit.valid? #=> true

# Now it's safe to initialize the model.
post = Post.new(edit.attributes)
post.save
# File lib/wedge/plugins/form.rb, line 265
def initialize(atts = {}, options = {})
  atts      = atts.deep_dup
  @_options = options.indifferent
  @_atts    = Atts.new atts, _accessors, _aliases, _accessor_options
  @_atts    = @_atts.set_defaults self

  _set_atts atts
end
original_attr_accessor(*attrs, &block)
Alias for: attr_accessor
original_attr_reader(*attrs, &block)
Alias for: attr_reader

Private Instance Methods

_accessor_options() click to toggle source
# File lib/wedge/plugins/form.rb, line 313
def _accessor_options
  @_accessor_options ||= (self.class._accessor_options || IndifferentHash.new).deep_dup
end
_accessors() click to toggle source
# File lib/wedge/plugins/form.rb, line 309
def _accessors
  @_accessors ||= (self.class._accessors || IndifferentHash.new).deep_dup
end
_aliases() click to toggle source
# File lib/wedge/plugins/form.rb, line 317
def _aliases
  @_aliases || (self.class._aliases || IndifferentHash.new).deep_dup
end
_attr_accessors() click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 314
def _attr_accessors
  self.class._attr_accessors
end
_attributes() click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 288
def _attributes
  @_attributes ||= {}
end
_atts_keys() click to toggle source
# File lib/wedge/plugins/form.rb, line 293
def _atts_keys
  @_atts_keys ||= []
end
_data() click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 298
def _data
  @_data ||= {}
end
_dom() click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 322
def _dom
  @_dom ||= @_options[:dom]
end
_error_name(key, error) click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 326
def _error_name key, error
  validate_msg(error.to_sym, key.to_sym) || case error.to_s.to_sym
  when :not_email
    'Email Isn\'t Valid.'
  when :not_present
    'Required.'
  when :not_equal
    'Password does not match.'
  else
    !error[/\s/] ? error.to_s.gsub(/_/, ' ').titleize : error
  end
end
_form() click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 310
def _form
  self.class._form
end
_keys() click to toggle source
# File lib/wedge/plugins/form.rb, line 305
def _keys
  ((_options[:atts] || _accessors) + _with_atts).reject { |k| _without_atts.include? k }
end
_options() click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 318
def _options
  @_options
end
Also aliased as: options
_set_atts(atts) click to toggle source
# File lib/wedge/plugins/form.rb, line 274
def _set_atts atts
  return unless atts

  atts.each do |key, val|
    # grab the original key if alias is given
    _atts_keys << (key = _aliases.invert[key] || key)

    if (_accessor_options[key] || {})[:form]
      send(key)._set_atts val
    else
      accessor = "#{key}="

      if respond_to?(accessor)
        send(accessor, val)
      end
    end
  end
end
_with_atts() click to toggle source
# File lib/wedge/plugins/form.rb, line 297
def _with_atts
  _accessor_options[:with_atts] || []
end
_without_atts() click to toggle source
# File lib/wedge/plugins/form.rb, line 301
def _without_atts
  _accessor_options[:without_atts] || []
end
attributes(for_model = false) click to toggle source

Return hash of attributes and values.

# File lib/wedge/plugins/form.rb, line 334
def attributes for_model = false
  IndifferentHash.new.tap do |atts|
    _options[:_attributes]       = true
    _options[:_model_attributes] = for_model

    _keys.each do |att|
      opts = _accessor_options[att].indifferent
      if ((for_model && !opts[:read_only]) || !for_model) && _atts.can_read?(att) && (!opts[:hidden] || opts[:hidden].is_a?(Proc) && !self.instance_exec(&opts[:hidden]))
        is_form   = opts[:form]
        key       = for_model ? _aliases[att] || att : att
        key       = (for_model && is_form)? "#{key}_attributes" : key
        atts[key] = is_form ? send(att).send(for_model ? 'model_attributes' : 'attributes') : (for_model ? _atts.send(att) : send(att))
      end
    end
  end
end
attributes?() click to toggle source
# File lib/wedge/plugins/form.rb, line 325
def attributes?
  @_options[:_attributes] ? true : false
end
display_errors(options = {}) click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 196
def display_errors options = {}, &block
  dom = options.delete(:dom) || _dom
  d_errors = errors

  if override_errors = options[:override_errors]
    d_errors = override_errors
  end

  keys = options.delete(:keys) || (_options[:key] ? [_options[:key]] : [])

  if extra_errors = options.delete(:errors)
    extra_errors.each do |key, value|
      d_errors[key] = value
    end
  end

  d_errors.each do |key, error|
    d_keys = (keys.dup << key)

    error = error.first

    if error.is_a?(Hash)
      d_options = options.dup
      d_options[:keys] = d_keys
      d_options[:override_errors] = d_errors[key].first

      display_errors d_options, &block
    elsif !block_given? || block.call(d_keys, error) == false
      name = d_keys.each_with_index.map do |field, i|
        i != 0 ? "[#{field}]" : field
      end.join

      if tmpl = options[:tmpl]
        if client?
          field_error_dom = DOM.new(`#{tmpl.dom}[0].outerHTML`)
        else
          field_error_dom = DOM.new(tmpl.dom.to_html)
        end
      else
        field_error_dom = DOM.new('<span class="field-error"><span>')
      end

      field_error_dom.html _error_name(key, error)

      field = dom.find("[name='#{name}']")
      field.before field_error_dom.dom
    end
  end
end
Also aliased as: render_errors
empty?() click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 339
def empty?
  _attributes.empty?
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method Wedge::Component::method_missing
# File lib/wedge/plugins/form_backup.rb, line 136
def method_missing method, *args, &block
  # respond_to?(symbol, include_all=false)
  if _data.respond_to? method, true
    _data.send method, *args, &block
  else
    return if method[/\=\z/]

    super
  end
end
model_attributes() click to toggle source
# File lib/wedge/plugins/form.rb, line 351
def model_attributes
  attributes true
end
model_attributes?() click to toggle source
# File lib/wedge/plugins/form.rb, line 329
def model_attributes?
  @_options[:_model_attributes] ? true : false
end
nested?() click to toggle source
# File lib/wedge/plugins/form.rb, line 321
def nested?
  @_options[:_nested] ? true : false
end
options()
Alias for: _options
render_errors(options = {})
Alias for: display_errors
render_values(dom = false, key = false, data = false) click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 247
def render_values dom = false, key = false, data = false
  dom = _options[:dom] unless dom
  key = _options[:key] if !key && _options.key?(:key)

  dom.find('input, select, textarea') do |element|
    name  = element['name']
    next if name.nil?
    name  = name.gsub(/\A#{key}/, '') if key
    keys  = name.gsub(/\A\[/, '').gsub(/[^a-z0-9_]/, '|').gsub(/\|\|/, '|').gsub(/\|$/, '').split('|')
    value = false

    keys.each do |k|
      begin
        value = value != false ? value.send(k) : send(k)
      rescue
        value = ''
      end
    end

    case element.name
    when 'select'
      element.find('option') do |x|
        x['selected'] = true if x['value'] == value.to_s
      end
    when 'input'
      if %w(radio checkbox).include? element['type']
        if element['value'] == value.to_s
          element['checked'] = true
        else
          element.delete 'checked'
        end
      else
        value = sprintf('%.2f', value) if value.is_a? BigDecimal
        element['value'] = value.to_s
      end
    when 'textarea'
      element.val value.to_s
    end
  end
end
slice(*keys) click to toggle source
# File lib/wedge/plugins/form.rb, line 358
def slice(*keys)
  IndifferentHash.new.tap do |atts|
    keys.each do |att|
      atts[att] = send(att)
    end
  end
end
validate_msg(error, column) click to toggle source
# File lib/wedge/plugins/form_backup.rb, line 292
def validate_msg error, column
  false
end
wedge_config() click to toggle source
Calls superclass method Wedge::Component::wedge_config
# File lib/wedge/plugins/form_backup.rb, line 343
def wedge_config
  @wedge_config ||= begin
    c = super
    c.skip_method_wrap
    c
  end
end