tabular_form_builder.rb 1.59 KB
Newer Older
1
#-- encoding: UTF-8
2 3
#-- copyright
# ChiliProject is a project management system.
4
#
Holger Just's avatar
Holger Just committed
5
# Copyright (C) 2010-2012 the ChiliProject Team
6
#
7 8 9 10
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
11
#
12 13 14
# See doc/COPYRIGHT.rdoc for more details.
#++

15 16 17
require 'action_view/helpers/form_helper'

class TabularFormBuilder < ActionView::Helpers::FormBuilder
18
  include Redmine::I18n
19

20 21
  def initialize(object_name, object, template, options, proc)
    set_language_if_valid options.delete(:lang)
22
    super
23 24
  end

25
  (field_helpers.map(&:to_s) - %w(radio_button hidden_field fields_for) + %w(date_select)).each do |selector|
26
    src = <<-END_SRC
27
    def #{selector}(field, options = {})
28
      label_for_field(field, options) + super
29 30 31 32
    end
    END_SRC
    class_eval src, __FILE__, __LINE__
  end
33 34

  def select(field, choices, options = {}, html_options = {})
35 36
    label_for_field(field, options) + super
  end
37

38 39 40
  # Returns a label tag for the given field
  def label_for_field(field, options = {})
      return '' if options.delete(:no_label)
Jean-Philippe Lang's avatar
Jean-Philippe Lang committed
41
      text = options[:label].is_a?(Symbol) ? l(options[:label]) : options[:label]
42
      text ||= l(("field_" + field.to_s.gsub(/\_id$/, "")).to_sym)
43
      text += @template.content_tag("span", " *", :class => "required") if options.delete(:required)
44 45
      @template.label(@object_name, field.to_s, text,
                                     :class => (@object && @object.errors[field] ? "error" : nil))
46 47
  end
end