matsuda
7/14/2010 - 4:08 AM

Rails' utility helpers

Rails' utility helpers

module ApplicationHelper
  
  # 
  # JavaScriptコードを<head>タグに挿入する
  # 
  def content_for_javascript_tag(content_or_options_with_block = nil, html_options = {}, &block)
    content_for :javascript do
      javascript_tag(content_or_options_with_block, html_options, &block)
    end
  end

  def simple_format(text)
    return text if text.blank?
    st = text.to_s.dup
    st = st.gsub(/(\r\n|\n|\r)/, "\n")
    st = st.gsub(/\n/, '<br />')
    st
  end

  # 
  # htmlオプション
  # 
  def html_id_and_name(object_name, column_name, idx = nil, nested_object_name = nil)
    {
      :id => html_id(object_name, column_name, idx, nested_object_name),
      :name => html_name(object_name, column_name, idx, nested_object_name)
    }
  end

  def html_id(object_name, column_name, idx = nil, nested_object_name = nil)
    i = idx ? "_#{idx}" : ''
    nested = nested_object_name ? "_#{nested_object_name}" : ''
    %Q{#{object_name}#{nested}_#{column_name}#{i}}
  end

  def html_name(object_name, column_name, idx = nil, nested_object_name = nil)
    i = idx ? "[#{idx}]" : ''
    nested = nested_object_name ? "[#{nested_object_name}]" : ''
    %Q{#{object_name}#{nested}#{i}[#{column_name}]}
  end

end