3 module ActiveSupport
#:nodoc:
4 module CoreExtensions
#:nodoc:
7 # Converts the array to a comma-separated sentence where the last element is joined by the connector word. Options:
8 # * <tt>:connector</tt> - The word used to join the last element in arrays with two or more elements (default: "and")
9 # * <tt>:skip_last_comma</tt> - Set to true to return "a, b and c" instead of "a, b, and c".
10 def to_sentence(options
= {})
11 options
.assert_valid_keys(:connector, :skip_last_comma, :locale)
13 default
= I18n
.translate(:'support.array.sentence_connector', :locale => options
[:locale])
14 default_skip_last_comma
= I18n
.translate(:'support.array.skip_last_comma', :locale => options
[:locale])
15 options
.reverse_merge
! :connector => default
, :skip_last_comma => default_skip_last_comma
16 options
[:connector] = "#{options[:connector]} " unless options
[:connector].nil? || options
[:connector].strip
== ''
24 "#{self[0]} #{options[:connector]}#{self[1]}"
26 "#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}"
31 # Calls <tt>to_param</tt> on all its elements and joins the result with
32 # slashes. This is used by <tt>url_for</tt> in Action Pack.
34 collect
{ |e
| e
.to_param
}.join
'/'
37 # Converts an array into a string suitable for use as a URL query string,
38 # using the given +key+ as the param name.
40 # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
43 collect
{ |value
| value
.to_query(prefix
) }.join
'&'
46 def self.included(base
) #:nodoc:
48 alias_method
:to_default_s, :to_s
49 alias_method
:to_s, :to_formatted_s
53 # Converts a collection of elements into a formatted string by calling
54 # <tt>to_s</tt> on all elements and joining them:
56 # Blog.find(:all).to_formatted_s # => "First PostSecond PostThird Post"
58 # Adding in the <tt>:db</tt> argument as the format yields a prettier
61 # Blog.find(:all).to_formatted_s(:db) # => "First Post,Second Post,Third Post"
62 def to_formatted_s(format
= :default)
65 if respond_to
?(:empty?) && self.empty
?
68 collect
{ |element
| element
.id
}.join(",")
75 # Returns a string that represents this array in XML by sending +to_xml+
76 # to each element. Active Record collections delegate their representation
77 # in XML to this method.
79 # All elements are expected to respond to +to_xml+, if any of them does
80 # not an exception is raised.
82 # The root node reflects the class name of the first element in plural
83 # if all elements belong to the same type and that's not Hash:
85 # customer.projects.to_xml
87 # <?xml version="1.0" encoding="UTF-8"?>
88 # <projects type="array">
90 # <amount type="decimal">20000.0</amount>
91 # <customer-id type="integer">1567</customer-id>
92 # <deal-date type="date">2008-04-09</deal-date>
96 # <amount type="decimal">57230.0</amount>
97 # <customer-id type="integer">1567</customer-id>
98 # <deal-date type="date">2008-04-15</deal-date>
103 # Otherwise the root element is "records":
105 # [{:foo => 1, :bar => 2}, {:baz => 3}].to_xml
107 # <?xml version="1.0" encoding="UTF-8"?>
108 # <records type="array">
110 # <bar type="integer">2</bar>
111 # <foo type="integer">1</foo>
114 # <baz type="integer">3</baz>
118 # If the collection is empty the root element is "nil-classes" by default:
122 # <?xml version="1.0" encoding="UTF-8"?>
123 # <nil-classes type="array"/>
125 # To ensure a meaningful root element use the <tt>:root</tt> option:
127 # customer_with_no_projects.projects.to_xml(:root => "projects")
129 # <?xml version="1.0" encoding="UTF-8"?>
130 # <projects type="array"/>
132 # By default root children have as node name the one of the root
133 # singularized. You can change it with the <tt>:children</tt> option.
135 # The +options+ hash is passed downwards:
137 # Message.all.to_xml(:skip_types => true)
139 # <?xml version="1.0" encoding="UTF-8"?>
142 # <created-at>2008-03-07T09:58:18+01:00</created-at>
145 # <updated-at>2008-03-07T09:58:18+01:00</updated-at>
146 # <user-id>1</user-id>
150 def to_xml(options
= {})
151 raise "Not all elements respond to to_xml" unless all
? { |e
| e
.respond_to
? :to_xml }
153 options
[:root] ||= all
? { |e
| e
.is_a
?(first
.class) && first
.class.to_s
!= "Hash" } ? first
.class.to_s
.underscore
.pluralize
: "records"
154 options
[:children] ||= options
[:root].singularize
155 options
[:indent] ||= 2
156 options
[:builder] ||= Builder
::XmlMarkup.new(:indent => options
[:indent])
158 root
= options
.delete(:root).to_s
159 children
= options
.delete(:children)
161 if !options
.has_key
?(:dasherize) || options
[:dasherize]
162 root
= root
.dasherize
165 options
[:builder].instruct
! unless options
.delete(:skip_instruct)
167 opts
= options
.merge({ :root => children
})
169 xml
= options
[:builder]
171 xml
.tag
!(root
, options
[:skip_types] ? {} : {:type => "array"})
173 xml
.tag
!(root
, options
[:skip_types] ? {} : {:type => "array"}) {
174 yield xml
if block_given
?
175 each
{ |e
| e
.to_xml(opts
.merge({ :skip_instruct => true })) }