class Basepack::Forms::Base

Attributes

association_chain[R]
factory[R]
groups[R]
nested_in[R]
partial[RW]
resource[RW]
resource_class[R]
view[RW]

Public Class Methods

new(factory, chain, options = {}) click to toggle source
# File lib/basepack/forms/base.rb, line 15
def initialize(factory, chain, options = {})
  @factory     = factory
  @fields_hash = {}
  @groups      = []
  @nested_in   = options[:nested_in]

  chain = Array.wrap(chain)
  @association_chain = chain[0...-1].freeze

  if chain.last.is_a? Class
    @resource_class = chain.last
  else
    @resource = chain.last
    @resource_class = @resource.class
  end
end

Public Instance Methods

build_from_factory() click to toggle source
# File lib/basepack/forms/base.rb, line 32
def build_from_factory
  factory.build_form(self)
end
chain() click to toggle source
# File lib/basepack/forms/base.rb, line 58
def chain
  @association_chain + [resource]
end
chain_with_class() click to toggle source
# File lib/basepack/forms/base.rb, line 62
def chain_with_class
  @association_chain + [resource_class]
end
configure() { |self| ... } click to toggle source
# File lib/basepack/forms/base.rb, line 45
def configure(&block)
  yield(self)
  self
end
content_for_field(field_name, &block) click to toggle source
# File lib/basepack/forms/base.rb, line 164
def content_for_field(field_name, &block)
  field(field_name) do |f|
    f.content(&block)
  end
end
default_group() click to toggle source
# File lib/basepack/forms/base.rb, line 142
def default_group
  @groups.first
end
default_partial() click to toggle source
# File lib/basepack/forms/base.rb, line 154
def default_partial
end
field(name, delegate_or_attributes = nil) { |field| ... } click to toggle source
# File lib/basepack/forms/base.rb, line 98
def field(name, delegate_or_attributes = nil)
  if delegate_or_attributes or block_given?
    field = @fields_hash[name]
    if field
      field.update_attributes(delegate_or_attributes) unless delegate_or_attributes.nil?
    else
      field = factory.new_field(name, self, delegate_or_attributes)
    end
    yield(field) if block_given?
    @fields_hash[name] = field
  else
    @fields_hash[name]
  end
end
field_names() click to toggle source
# File lib/basepack/forms/base.rb, line 78
def field_names
  @fields_hash.keys
end
fields() click to toggle source
# File lib/basepack/forms/base.rb, line 82
def fields
  @fields_hash.values
end
fields_hash() click to toggle source
# File lib/basepack/forms/base.rb, line 86
def fields_hash
  @fields_hash.dup
end
group(attributes = nil) { |group| ... } click to toggle source
# File lib/basepack/forms/base.rb, line 135
def group(attributes = nil)
  group = factory.new_group(self, attributes)
  yield(group) if block_given?
  @groups << group
  group
end
has_field?(name) click to toggle source
# File lib/basepack/forms/base.rb, line 94
def has_field?(name)
  @fields_hash.has_key? name
end
hide_field(field_name) click to toggle source
# File lib/basepack/forms/base.rb, line 118
def hide_field(field_name)
  @fields_hash[field_name].try :visible=, false
end
hide_fields(*field_names) click to toggle source
# File lib/basepack/forms/base.rb, line 122
def hide_fields(*field_names)
  field_names.each {|f| hide_field(f)}
end
inspect() click to toggle source
# File lib/basepack/forms/base.rb, line 239
def inspect
  "#<#{self.class.name}[#{resource_class.name}] #{field_names}>"
end
inverse_of_nested_in?(field) click to toggle source
# File lib/basepack/forms/base.rb, line 74
def inverse_of_nested_in?(field) # TODO remove and should be automatical (test in visible_fields or in visible?)
  field.inverse_of_nested_in?
end
label() click to toggle source
# File lib/basepack/forms/base.rb, line 50
def label
  @label ||= resource_class.model_name.human
end
label_plural() click to toggle source
# File lib/basepack/forms/base.rb, line 54
def label_plural
  @label_plural ||= resource_class.model_name.human(count: 'other', default: label.pluralize)
end
new_form(*args) click to toggle source
# File lib/basepack/forms/base.rb, line 36
def new_form(*args)
  form = factory.new_form(*args)
  form.view = view
  if configure_nested_form = form.nested_in.try(:configure_nested_form)
    form.configure(&configure_nested_form)
  end
  form
end
new_record?() click to toggle source
# File lib/basepack/forms/base.rb, line 70
def new_record?
  !@resource or @resource.new_record?
end
path() click to toggle source
# File lib/basepack/forms/base.rb, line 170
def path
  view.url_for(chain)
end
permit_params(params) click to toggle source
# File lib/basepack/forms/base.rb, line 174
def permit_params(params)
  sanitize_params(params).permit!
end
render_field(field) click to toggle source
# File lib/basepack/forms/base.rb, line 157
def render_field(field)
  field.render
end
render_field!(field) click to toggle source
# File lib/basepack/forms/base.rb, line 161
def render_field!(field)
end
sanitize_params(params, add_to_allwed = []) click to toggle source
# File lib/basepack/forms/base.rb, line 178
def sanitize_params(params, add_to_allwed = [])
  new_params = params.dup
  allowed = add_to_allwed.dup

  visible_fields.each do |f|
    next if f.read_only? or f.inverse_of_nested_in?

    f.parse_input(new_params)

    f.allowed_methods.each do |m|
      if f.type == :serialized or (new_params[m].is_a?(Array) or new_params[m].is_a?(Hash)) ^ !f.multiple?
        allowed << m
      end
    end

    if nform = f.nform and nparams = new_params[f.method_name]
      # nested_form with accepts_nested_attributes_for needs :id and
      # :_destroy params in some cases

      nested_allowed = []
      field_nested_params = f.nested_form
      if field_nested_params
        nested_allowed << :id unless field_nested_params[:update_only]
        nested_allowed << :_destroy if field_nested_params[:allow_destroy] or nform.new_record?
      end

      new_params[f.method_name] = case nparams
      when Array
        nparams.map {|p| p.is_a?(Hash) ? nform.sanitize_params(p, nested_allowed) : p.to_s}
      when Hash
        if f.multiple?
          Hash[nparams.map do |k, p|
            [k, p.is_a?(Hash) ? nform.sanitize_params(p, nested_allowed) : p.to_s]
          end]
        else
          allowed << f.method_name
          nform.sanitize_params(nparams, nested_allowed)
        end
      else
        nparams
      end
    end
  end

  unpermitted = new_params.slice!(*allowed)
  if unpermitted.present?
    ActiveSupport::Notifications.instrument("unpermitted_parameters.action_controller", keys: unpermitted.keys)
  end

  new_params
end
show_fields(*field_names) click to toggle source
# File lib/basepack/forms/base.rb, line 126
def show_fields(*field_names)
  field_names.each {|name| @fields_hash[name].try :visible=, true }
end
show_only_fields(*field_names) click to toggle source
# File lib/basepack/forms/base.rb, line 130
def show_only_fields(*field_names)
  visible_fields.each { |field| hide_field(field.name) }
  show_fields(*field_names)
end
translate(resource, action, subaction = "menu") click to toggle source

use more lookup paths for translations

# File lib/basepack/forms/base.rb, line 244
def translate(resource, action, subaction = "menu")
  Utils.translate(resource, action, subaction)
end
visible_field(name) click to toggle source
# File lib/basepack/forms/base.rb, line 113
def visible_field(name)
  field = @fields_hash[name]
  field && field.visible? ? field : nil
end
visible_fields() click to toggle source
# File lib/basepack/forms/base.rb, line 90
def visible_fields
  @fields_hash.values.find_all {|f| f.visible? }
end
visible_groups() click to toggle source
# File lib/basepack/forms/base.rb, line 146
def visible_groups
  groups.find_all {|g| g.field_names.present? }
end
with_resource(resource, *args) { |resource, *args| ... } click to toggle source
# File lib/basepack/forms/base.rb, line 230
def with_resource(resource, *args, &block)
  orig_resource, @resource = @resource, resource
  begin
    yield(resource, *args)
  ensure
    @resource = orig_resource
  end
end