aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_collections_helper.rb
blob: da89a23552fb18154518a0652284e3dac8a7eeee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module ActionView
  module Helpers
    module FormCollectionsHelper
      # Returns radio button tags for the collection of existing return values of +method+ for
      # +object+'s class. The value returned from calling +method+ on the instance +object+ will
      # be selected. If calling +method+ returns +nil+, no selection is made.
      #
      # The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are methods to be called on each member
      # of +collection+. The return values are used as the +value+ attribute and contents of each
      # radio button tag, respectively.
      #
      # Example object structure for use with this method:
      #   class Post < ActiveRecord::Base
      #     belongs_to :author
      #   end
      #   class Author < ActiveRecord::Base
      #     has_many :posts
      #     def name_with_initial
      #       "#{first_name.first}. #{last_name}"
      #     end
      #   end
      #
      # Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
      #   collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial)
      #
      # If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
      #   <span>
      #     <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
      #     <label class="collection_radio_buttons" for="post_author_id_1">D. Heinemeier Hansson</label>
      #   </span>
      #   <span>
      #     <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
      #     <label class="collection_radio_buttons" for="post_author_id_2">D. Thomas</label>
      #   </span>
      #   <span>
      #     <input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
      #     <label class="collection_radio_buttons" for="post_author_id_3">M. Clark</label>
      #   </span>
      def collection_radio_buttons(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
        Tags::CollectionRadioButtons.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block)
      end

      # Returns check box tags for the collection of existing return values of +method+ # for
      # +object+'s class. The value returned from calling +method+ on the instance +object+ will
      # be selected. If calling +method+ returns +nil+, no selection is made.
      #
      # The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are methods to be called on each member
      # of +collection+. The return values are used as the +value+ attribute and contents of each
      # check box tag, respectively.
      #
      # Example object structure for use with this method:
      #   class Post < ActiveRecord::Base
      #     has_and_belongs_to :author
      #   end
      #   class Author < ActiveRecord::Base
      #     has_and_belongs_to :posts
      #     def name_with_initial
      #       "#{first_name.first}. #{last_name}"
      #     end
      #   end
      #
      # Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
      #   collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
      #
      # If <tt>@post.author_ids</tt> is already <tt>[1]</tt>, this would return:
      #   <span>
      #     <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
      #     <label class="collection_check_boxes" for="post_author_ids_1">D. Heinemeier Hansson</label>
      #   </span>
      #   <span>
      #     <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="2" />
      #     <label class="collection_check_boxes" for="post_author_ids_1">D. Thomas</label>
      #   </span>
      #   <span>
      #     <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
      #     <label class="collection_check_boxes" for="post_author_ids_3">M. Clark</label>
      #   </span>
      #   <input name="post[author_ids][]" type="hidden" value="" />
      def collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
        Tags::CollectionCheckBoxes.new(object, method, self, collection, value_method, text_method, options, html_options).render(&block)
      end
    end

    class FormBuilder
      def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {})
        @template.collection_radio_buttons(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options))
      end

      def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {})
        @template.collection_check_boxes(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options))
      end
    end
  end
end