module Cocooned::Helpers

Public Instance Methods

Private Instance Methods

cocooned_default_label(action, association = nil) click to toggle source
# File lib/cocooned/helpers.rb, line 228
def cocooned_default_label(action, association = nil)
  # TODO: Remove in 3.0
  if I18n.exists?(:cocoon)
    msg = Cocooned::Helpers::Deprecate.deprecate_release_message('the :cocoon i18n scope', ':cocooned')
    warn msg
  end

  keys = ["cocooned.defaults.#{action}", "cocoon.defaults.#{action}"]
  keys.unshift("cocooned.#{association}.#{action}", "cocoon.#{association}.#{action}") unless association.nil?
  keys.collect!(&:to_sym)
  keys << action.to_s.humanize

  I18n.translate(keys.take(1).first, default: keys.drop(1))
end
cocooned_extract_builder_options!(html_options) click to toggle source
# File lib/cocooned/helpers.rb, line 271
def cocooned_extract_builder_options!(html_options)
  %i[wrap_object force_non_association_create].each_with_object({}) do |option_name, opts|
    opts[option_name] = html_options.delete(option_name) if html_options.key?(option_name)
  end
end
cocooned_extract_data!(html_options) click to toggle source
# File lib/cocooned/helpers.rb, line 295
def cocooned_extract_data!(html_options)
  data = {
    count: [
      html_options.delete(:count).to_i,
      (html_options.key?(:data) ? html_options[:data].delete(:count) : 0).to_i,
      1
    ].compact.max,
    association_insertion_node: cocooned_extract_single_data!(html_options, :insertion_node),
    association_insertion_method: cocooned_extract_single_data!(html_options, :insertion_method),
    association_insertion_traversal: cocooned_extract_single_data!(html_options, :insertion_traversal)
  }

  # Compatibility with the old way to pass data attributes to Rails view helpers
  # Has we build a :data key, they will not be looked up.
  html_options.keys.select { |k| k.to_s.match?(/data[_-]/) }.each_with_object(data) do |data_key, d|
    key = data_key.to_s.gsub(/^data[_-]/, '')
    d[key] = html_options.delete(data_key)
  end
  data.compact
end
cocooned_extract_render_options!(html_options) click to toggle source
# File lib/cocooned/helpers.rb, line 277
def cocooned_extract_render_options!(html_options)
  render_options = { form_name: :f }

  # TODO: Remove in 2.0
  if html_options.key?(:render_options)
    msg = Cocooned::Helpers::Deprecate.deprecate_release_message(':render_options', :none)
    warn msg

    options = html_options.delete(:render_options)
    render_options[:locals] = options.delete(:locals) if options.key?(:locals)
    render_options[:form_options] = options
  end

  %i[locals partial form_name form_options].each_with_object(render_options) do |option_name, opts|
    opts[option_name] = html_options.delete(option_name) if html_options.key?(option_name)
  end
end
cocooned_extract_single_data!(hash, key) click to toggle source
# File lib/cocooned/helpers.rb, line 316
def cocooned_extract_single_data!(hash, key)
  k = key.to_s
  return hash.delete(k) if hash.key?(k)

  # Compatibility alternatives
  # TODO: Remove in 2.0
  return hash.delete("association_#{k}") if hash.key?("association_#{k}")
  return hash.delete("data_association_#{k}") if hash.key?("data_association_#{k}")
  return hash.delete("data-association-#{k.tr('_', '-')}") if hash.key?("data-association-#{k.tr('_', '-')}")
  return nil unless hash.key?(:data)

  d = hash[:data].with_indifferent_access
  return d.delete("association_#{k}") if d.key?("association_#{k}")
  return d.delete("association-#{k.tr('_', '-')}") if d.key?("association-#{k.tr('_', '-')}")

  nil
end
cocooned_form_method(form) click to toggle source
# File lib/cocooned/helpers.rb, line 260
def cocooned_form_method(form)
  ancestors = form.class.ancestors.map(&:to_s)
  if ancestors.include?('SimpleForm::FormBuilder')
    :simple_fields_for
  elsif ancestors.include?('Formtastic::FormBuilder')
    :semantic_fields_for
  else
    :fields_for
  end
end
cocooned_render_association(builder, options = {}) click to toggle source
# File lib/cocooned/helpers.rb, line 243
def cocooned_render_association(builder, options = {})
  render_options = options.dup
  locals = (render_options.delete(:locals) || {}).symbolize_keys
  partial = render_options.delete(:partial) || "#{builder.singular_name}_fields"
  form_name = render_options.delete(:form_name)
  form_options = (render_options.delete(:form_options) || {}).symbolize_keys
  form_options.reverse_merge!(child_index: "new_#{builder.association}")

  builder.form.send(cocooned_form_method(builder.form),
                    builder.association,
                    builder.build_object,
                    form_options) do |form_builder|
    partial_options = { form_name.to_sym => form_builder, :dynamic => true }.merge(locals)
    render(partial, partial_options)
  end
end