Module | ActiveSupport::CoreExtensions::Array::Conversions |
In: |
vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb
|
Converts a collection of elements into a formatted string by calling to_s on all elements and joining them:
Blog.find(:all).to_formatted_s # => "First PostSecond PostThird Post"
Adding in the :db argument as the format yields a prettier output:
Blog.find(:all).to_formatted_s(:db) # => "First Post,Second Post,Third Post"
# File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 74 74: def to_formatted_s(format = :default) 75: case format 76: when :db 77: if respond_to?(:empty?) && self.empty? 78: "null" 79: else 80: collect { |element| element.id }.join(",") 81: end 82: else 83: to_default_s 84: end 85: end
Converts an array into a string suitable for use as a URL query string, using the given key as the param name.
['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
# File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 53 53: def to_query(key) 54: prefix = "#{key}[]" 55: collect { |value| value.to_query(prefix) }.join '&' 56: end
Converts the array to a comma-separated sentence where the last element is joined by the connector word. Options:
# File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 9 9: def to_sentence(options = {}) 10: default_words_connector = I18n.translate('support.array.words_connector''support.array.words_connector', :locale => options[:locale]) 11: default_two_words_connector = I18n.translate('support.array.two_words_connector''support.array.two_words_connector', :locale => options[:locale]) 12: default_last_word_connector = I18n.translate('support.array.last_word_connector''support.array.last_word_connector', :locale => options[:locale]) 13: 14: # Try to emulate to_senteces previous to 2.3 15: if options.has_key?(:connector) || options.has_key?(:skip_last_comma) 16: ::ActiveSupport::Deprecation.warn(":connector has been deprecated. Use :words_connector instead", caller) if options.has_key? :connector 17: ::ActiveSupport::Deprecation.warn(":skip_last_comma has been deprecated. Use :last_word_connector instead", caller) if options.has_key? :skip_last_comma 18: 19: skip_last_comma = options.delete :skip_last_comma 20: if connector = options.delete(:connector) 21: options[:last_word_connector] ||= skip_last_comma ? connector : ", #{connector}" 22: else 23: options[:last_word_connector] ||= skip_last_comma ? default_two_words_connector : default_last_word_connector 24: end 25: end 26: 27: options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale) 28: options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector 29: 30: case length 31: when 0 32: "" 33: when 1 34: self[0].to_s 35: when 2 36: "#{self[0]}#{options[:two_words_connector]}#{self[1]}" 37: else 38: "#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}" 39: end 40: end
Returns a string that represents this array in XML by sending to_xml to each element. Active Record collections delegate their representation in XML to this method.
All elements are expected to respond to to_xml, if any of them does not an exception is raised.
The root node reflects the class name of the first element in plural if all elements belong to the same type and that‘s not Hash:
customer.projects.to_xml <?xml version="1.0" encoding="UTF-8"?> <projects type="array"> <project> <amount type="decimal">20000.0</amount> <customer-id type="integer">1567</customer-id> <deal-date type="date">2008-04-09</deal-date> ... </project> <project> <amount type="decimal">57230.0</amount> <customer-id type="integer">1567</customer-id> <deal-date type="date">2008-04-15</deal-date> ... </project> </projects>
Otherwise the root element is "records":
[{:foo => 1, :bar => 2}, {:baz => 3}].to_xml <?xml version="1.0" encoding="UTF-8"?> <records type="array"> <record> <bar type="integer">2</bar> <foo type="integer">1</foo> </record> <record> <baz type="integer">3</baz> </record> </records>
If the collection is empty the root element is "nil-classes" by default:
[].to_xml <?xml version="1.0" encoding="UTF-8"?> <nil-classes type="array"/>
To ensure a meaningful root element use the :root option:
customer_with_no_projects.projects.to_xml(:root => "projects") <?xml version="1.0" encoding="UTF-8"?> <projects type="array"/>
By default root children have as node name the one of the root singularized. You can change it with the :children option.
The options hash is passed downwards:
Message.all.to_xml(:skip_types => true) <?xml version="1.0" encoding="UTF-8"?> <messages> <message> <created-at>2008-03-07T09:58:18+01:00</created-at> <id>1</id> <name>1</name> <updated-at>2008-03-07T09:58:18+01:00</updated-at> <user-id>1</user-id> </message> </messages>
# File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 162 162: def to_xml(options = {}) 163: raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } 164: require 'builder' unless defined?(Builder) 165: 166: options = options.dup 167: options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records" 168: options[:children] ||= options[:root].singularize 169: options[:indent] ||= 2 170: options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) 171: 172: root = options.delete(:root).to_s 173: children = options.delete(:children) 174: 175: if !options.has_key?(:dasherize) || options[:dasherize] 176: root = root.dasherize 177: end 178: 179: options[:builder].instruct! unless options.delete(:skip_instruct) 180: 181: opts = options.merge({ :root => children }) 182: 183: xml = options[:builder] 184: if empty? 185: xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) 186: else 187: xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) { 188: yield xml if block_given? 189: each { |e| e.to_xml(opts.merge({ :skip_instruct => true })) } 190: } 191: end 192: end