kelredd-resourceful 0.7.23 → 0.7.24
Sign up to get free protection for your applications and to get access to all the features.
@@ -134,12 +134,33 @@ module Resourceful
|
|
134
134
|
# # => "?id=thomas_hardy"
|
135
135
|
# {:id => 23423, :since => Time.now}.to_http_query_str
|
136
136
|
# # => "?since=Thu,%2021%20Jun%202007%2012:10:05%20-0500&id=23423"
|
137
|
+
# {:id => [1,2]}.to_http_query_str
|
138
|
+
# # => "?id[]=1&id[]=2"
|
139
|
+
# {:poo => {:foo => 1, :bar => 2}}.to_http_query_str
|
140
|
+
# # => "?poo[bar]=2&poo[foo]=1"
|
141
|
+
# {:poo => {:foo => 1, :bar => {:bar1 => 1, :bar2 => "nasty"}}}.to_http_query_str
|
142
|
+
# "?poo[bar][bar1]=1&poo[bar][bar2]=nasty&poo[foo]=1"
|
137
143
|
unless {}.respond_to?(:to_http_query_str)
|
138
144
|
def to_http_query_str(opts = {})
|
139
|
-
require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
|
145
|
+
require 'cgi' unless defined?(::CGI) && defined?(::CGI::escape)
|
140
146
|
opts[:prepend] ||= '?'
|
141
147
|
opts[:append] ||= ''
|
142
|
-
|
148
|
+
opts[:key_ns] ||= nil
|
149
|
+
opt_strings = self.collect do |key, val|
|
150
|
+
key_s = opts[:key_ns] ? "#{opts[:key_ns]}[#{key.to_s}]" : key.to_s
|
151
|
+
if val.kind_of?(::Array)
|
152
|
+
val.collect{|i| "#{key_s}[]=#{::CGI.escape(i.to_s)}"}.join('&')
|
153
|
+
elsif val.kind_of?(::Hash)
|
154
|
+
val.to_http_query_str({
|
155
|
+
:prepend => '',
|
156
|
+
:key_ns => key_s,
|
157
|
+
:append => ''
|
158
|
+
})
|
159
|
+
else
|
160
|
+
"#{key_s}=#{::CGI.escape(val.to_s)}"
|
161
|
+
end
|
162
|
+
end
|
163
|
+
self.empty? ? '' : "#{opts[:prepend]}#{opt_strings.join('&')}#{opts[:append]}"
|
143
164
|
end
|
144
165
|
end
|
145
166
|
|
@@ -16,7 +16,7 @@ module Resourceful
|
|
16
16
|
assoc_data = get_association_data(klass, clean_association_name)
|
17
17
|
assoc_klass = klass.ancestors.include?(Resourceful::Model::Base) ? klass.get_namespaced_klass(assoc_data[:class_name]) : assoc_data[:class_name].resourceful_constantize
|
18
18
|
fk_values = collection.inject([]) {|vals, item| vals << item.send(assoc_data[:foreign_key_method])}
|
19
|
-
fk_results = assoc_klass.send(assoc_data[:find_method_name], :all, {assoc_data[:foreign_key_name] => fk_values
|
19
|
+
fk_results = assoc_klass.send(assoc_data[:find_method_name], :all, {assoc_data[:foreign_key_name] => fk_values}, true)
|
20
20
|
collection.each do |item|
|
21
21
|
item_results = fk_results.reject{|result| result.send(assoc_data[:foreign_key_name]) != item.send(assoc_data[:foreign_key_method])}
|
22
22
|
item.instance_variable_set("@#{clean_association_name}", [:belongs_to, :has_one].include?(assoc_data[:type]) ? item_results.first : item_results)
|
data/lib/resourceful/version.rb
CHANGED