| 1 |
module ActionController |
|---|
| 2 |
module Resources |
|---|
| 3 |
class Resource |
|---|
| 4 |
attr_reader :collection_methods, :member_methods, :new_methods |
|---|
| 5 |
attr_reader :path_prefix, :new_name_prefix |
|---|
| 6 |
attr_reader :plural, :singular |
|---|
| 7 |
attr_reader :options |
|---|
| 8 |
|
|---|
| 9 |
def initialize(entities, options) |
|---|
| 10 |
@plural = entities |
|---|
| 11 |
@singular = options[:singular] || plural.to_s.singularize |
|---|
| 12 |
|
|---|
| 13 |
@options = options |
|---|
| 14 |
|
|---|
| 15 |
arrange_actions |
|---|
| 16 |
add_default_actions |
|---|
| 17 |
set_prefixes |
|---|
| 18 |
end |
|---|
| 19 |
|
|---|
| 20 |
def controller |
|---|
| 21 |
@controller ||= (options[:controller] || plural).to_s |
|---|
| 22 |
end |
|---|
| 23 |
|
|---|
| 24 |
def path |
|---|
| 25 |
@path ||= "#{path_prefix}/#{plural}" |
|---|
| 26 |
end |
|---|
| 27 |
|
|---|
| 28 |
def new_path |
|---|
| 29 |
@new_path ||= "#{path}/new" |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
def member_path |
|---|
| 33 |
@member_path ||= "#{path}/:id" |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
def nesting_path_prefix |
|---|
| 37 |
@nesting_path_prefix ||= "#{path}/:#{singular}_id" |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
def deprecate_name_prefix? |
|---|
| 41 |
@name_prefix.blank? && !@new_name_prefix.blank? |
|---|
| 42 |
end |
|---|
| 43 |
|
|---|
| 44 |
def name_prefix |
|---|
| 45 |
deprecate_name_prefix? ? @new_name_prefix : @name_prefix |
|---|
| 46 |
end |
|---|
| 47 |
|
|---|
| 48 |
def old_name_prefix |
|---|
| 49 |
@name_prefix |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
def nesting_name_prefix |
|---|
| 53 |
"#{new_name_prefix}#{singular}_" |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
def action_separator |
|---|
| 57 |
@action_separator ||= Base.resource_action_separator |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
protected |
|---|
| 61 |
def arrange_actions |
|---|
| 62 |
@collection_methods = arrange_actions_by_methods(options.delete(:collection)) |
|---|
| 63 |
@member_methods = arrange_actions_by_methods(options.delete(:member)) |
|---|
| 64 |
@new_methods = arrange_actions_by_methods(options.delete(:new)) |
|---|
| 65 |
end |
|---|
| 66 |
|
|---|
| 67 |
def add_default_actions |
|---|
| 68 |
add_default_action(member_methods, :get, :edit) |
|---|
| 69 |
add_default_action(new_methods, :get, :new) |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
def set_prefixes |
|---|
| 73 |
@path_prefix = options.delete(:path_prefix) |
|---|
| 74 |
@name_prefix = options.delete(:name_prefix) |
|---|
| 75 |
@new_name_prefix = options.delete(:new_name_prefix) |
|---|
| 76 |
end |
|---|
| 77 |
|
|---|
| 78 |
def arrange_actions_by_methods(actions) |
|---|
| 79 |
(actions || {}).inject({}) do |flipped_hash, (key, value)| |
|---|
| 80 |
(flipped_hash[value] ||= []) << key |
|---|
| 81 |
flipped_hash |
|---|
| 82 |
end |
|---|
| 83 |
end |
|---|
| 84 |
|
|---|
| 85 |
def add_default_action(collection, method, action) |
|---|
| 86 |
(collection[method] ||= []).unshift(action) |
|---|
| 87 |
end |
|---|
| 88 |
end |
|---|
| 89 |
|
|---|
| 90 |
class SingletonResource < Resource |
|---|
| 91 |
def initialize(entity, options) |
|---|
| 92 |
@plural = @singular = entity |
|---|
| 93 |
@options = options |
|---|
| 94 |
arrange_actions |
|---|
| 95 |
add_default_actions |
|---|
| 96 |
set_prefixes |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
alias_method :member_path, :path |
|---|
| 100 |
alias_method :nesting_path_prefix, :path |
|---|
| 101 |
end |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
def resources(*entities, &block) |
|---|
| 250 |
options = entities.last.is_a?(Hash) ? entities.pop : { } |
|---|
| 251 |
entities.each { |entity| map_resource entity, options.dup, &block } |
|---|
| 252 |
end |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
def resource(*entities, &block) |
|---|
| 310 |
options = entities.last.is_a?(Hash) ? entities.pop : { } |
|---|
| 311 |
entities.each { |entity| map_singleton_resource entity, options.dup, &block } |
|---|
| 312 |
end |
|---|
| 313 |
|
|---|
| 314 |
private |
|---|
| 315 |
def map_resource(entities, options = {}, &block) |
|---|
| 316 |
resource = Resource.new(entities, options) |
|---|
| 317 |
|
|---|
| 318 |
with_options :controller => resource.controller do |map| |
|---|
| 319 |
map_collection_actions(map, resource) |
|---|
| 320 |
map_default_collection_actions(map, resource) |
|---|
| 321 |
map_new_actions(map, resource) |
|---|
| 322 |
map_member_actions(map, resource) |
|---|
| 323 |
|
|---|
| 324 |
if block_given? |
|---|
| 325 |
with_options(:path_prefix => resource.nesting_path_prefix, :new_name_prefix => resource.nesting_name_prefix, &block) |
|---|
| 326 |
end |
|---|
| 327 |
end |
|---|
| 328 |
end |
|---|
| 329 |
|
|---|
| 330 |
def map_singleton_resource(entities, options = {}, &block) |
|---|
| 331 |
resource = SingletonResource.new(entities, options) |
|---|
| 332 |
|
|---|
| 333 |
with_options :controller => resource.controller do |map| |
|---|
| 334 |
map_collection_actions(map, resource) |
|---|
| 335 |
map_default_singleton_actions(map, resource) |
|---|
| 336 |
map_new_actions(map, resource) |
|---|
| 337 |
map_member_actions(map, resource) |
|---|
| 338 |
|
|---|
| 339 |
if block_given? |
|---|
| 340 |
with_options(:path_prefix => resource.nesting_path_prefix, :new_name_prefix => resource.nesting_name_prefix, &block) |
|---|
| 341 |
end |
|---|
| 342 |
end |
|---|
| 343 |
end |
|---|
| 344 |
|
|---|
| 345 |
def map_collection_actions(map, resource) |
|---|
| 346 |
resource.collection_methods.each do |method, actions| |
|---|
| 347 |
actions.each do |action| |
|---|
| 348 |
action_options = action_options_for(action, resource, method) |
|---|
| 349 |
|
|---|
| 350 |
unless resource.old_name_prefix.blank? |
|---|
| 351 |
map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.old_name_prefix}#{action}_#{resource.plural}") |
|---|
| 352 |
map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.old_name_prefix}#{action}_#{resource.plural}") |
|---|
| 353 |
end |
|---|
| 354 |
|
|---|
| 355 |
if resource.deprecate_name_prefix? |
|---|
| 356 |
map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{action}_#{resource.plural}") |
|---|
| 357 |
map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{action}_#{resource.plural}") |
|---|
| 358 |
end |
|---|
| 359 |
|
|---|
| 360 |
map.named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}", action_options) |
|---|
| 361 |
map.connect("#{resource.path};#{action}", action_options) |
|---|
| 362 |
map.connect("#{resource.path}.:format;#{action}", action_options) |
|---|
| 363 |
map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}.:format", action_options) |
|---|
| 364 |
end |
|---|
| 365 |
end |
|---|
| 366 |
end |
|---|
| 367 |
|
|---|
| 368 |
def map_default_collection_actions(map, resource) |
|---|
| 369 |
index_action_options = action_options_for("index", resource) |
|---|
| 370 |
map.named_route("#{resource.name_prefix}#{resource.plural}", resource.path, index_action_options) |
|---|
| 371 |
map.named_route("formatted_#{resource.name_prefix}#{resource.plural}", "#{resource.path}.:format", index_action_options) |
|---|
| 372 |
|
|---|
| 373 |
if resource.deprecate_name_prefix? |
|---|
| 374 |
map.deprecated_named_route("#{resource.name_prefix}#{resource.plural}", "#{resource.plural}") |
|---|
| 375 |
map.deprecated_named_route("formatted_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.plural}") |
|---|
| 376 |
end |
|---|
| 377 |
|
|---|
| 378 |
create_action_options = action_options_for("create", resource) |
|---|
| 379 |
map.connect(resource.path, create_action_options) |
|---|
| 380 |
map.connect("#{resource.path}.:format", create_action_options) |
|---|
| 381 |
end |
|---|
| 382 |
|
|---|
| 383 |
def map_default_singleton_actions(map, resource) |
|---|
| 384 |
create_action_options = action_options_for("create", resource) |
|---|
| 385 |
map.connect(resource.path, create_action_options) |
|---|
| 386 |
map.connect("#{resource.path}.:format", create_action_options) |
|---|
| 387 |
end |
|---|
| 388 |
|
|---|
| 389 |
def map_new_actions(map, resource) |
|---|
| 390 |
resource.new_methods.each do |method, actions| |
|---|
| 391 |
actions.each do |action| |
|---|
| 392 |
action_options = action_options_for(action, resource, method) |
|---|
| 393 |
if action == :new |
|---|
| 394 |
|
|---|
| 395 |
unless resource.old_name_prefix.blank? |
|---|
| 396 |
map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "#{resource.old_name_prefix}new_#{resource.singular}") |
|---|
| 397 |
map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.old_name_prefix}new_#{resource.singular}") |
|---|
| 398 |
end |
|---|
| 399 |
|
|---|
| 400 |
if resource.deprecate_name_prefix? |
|---|
| 401 |
map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "new_#{resource.singular}") |
|---|
| 402 |
map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_new_#{resource.singular}") |
|---|
| 403 |
end |
|---|
| 404 |
|
|---|
| 405 |
map.named_route("new_#{resource.name_prefix}#{resource.singular}", resource.new_path, action_options) |
|---|
| 406 |
map.named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}.:format", action_options) |
|---|
| 407 |
|
|---|
| 408 |
else |
|---|
| 409 |
|
|---|
| 410 |
unless resource.old_name_prefix.blank? |
|---|
| 411 |
map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.old_name_prefix}#{action}_new_#{resource.singular}") |
|---|
| 412 |
map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.old_name_prefix}#{action}_new_#{resource.singular}") |
|---|
| 413 |
end |
|---|
| 414 |
|
|---|
| 415 |
if resource.deprecate_name_prefix? |
|---|
| 416 |
map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{action}_new_#{resource.singular}") |
|---|
| 417 |
map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{action}_new_#{resource.singular}") |
|---|
| 418 |
end |
|---|
| 419 |
|
|---|
| 420 |
map.named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}", action_options) |
|---|
| 421 |
map.connect("#{resource.new_path};#{action}", action_options) |
|---|
| 422 |
map.connect("#{resource.new_path}.:format;#{action}", action_options) |
|---|
| 423 |
map.named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}.:format", action_options) |
|---|
| 424 |
|
|---|
| 425 |
end |
|---|
| 426 |
end |
|---|
| 427 |
end |
|---|
| 428 |
end |
|---|
| 429 |
|
|---|
| 430 |
def map_member_actions(map, resource) |
|---|
| 431 |
resource.member_methods.each do |method, actions| |
|---|
| 432 |
actions.each do |action| |
|---|
| 433 |
action_options = action_options_for(action, resource, method) |
|---|
| 434 |
|
|---|
| 435 |
unless resource.old_name_prefix.blank? |
|---|
| 436 |
map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.old_name_prefix}#{action}_#{resource.singular}") |
|---|
| 437 |
map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.old_name_prefix}#{action}_#{resource.singular}") |
|---|
| 438 |
end |
|---|
| 439 |
|
|---|
| 440 |
if resource.deprecate_name_prefix? |
|---|
| 441 |
map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{action}_#{resource.singular}") |
|---|
| 442 |
map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{action}_#{resource.singular}") |
|---|
| 443 |
end |
|---|
| 444 |
|
|---|
| 445 |
map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}", action_options) |
|---|
| 446 |
map.connect("#{resource.member_path};#{action}", action_options) |
|---|
| 447 |
map.connect("#{resource.member_path}.:format;#{action}", action_options) |
|---|
| 448 |
map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}.:format", action_options) |
|---|
| 449 |
|
|---|
| 450 |
end |
|---|
| 451 |
end |
|---|
| 452 |
|
|---|
| 453 |
show_action_options = action_options_for("show", resource) |
|---|
| 454 |
map.named_route("#{resource.name_prefix}#{resource.singular}", resource.member_path, show_action_options) |
|---|
| 455 |
map.named_route("formatted_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}.:format", show_action_options) |
|---|
| 456 |
|
|---|
| 457 |
if resource.deprecate_name_prefix? |
|---|
| 458 |
map.deprecated_named_route("#{resource.name_prefix}#{resource.singular}", "#{resource.singular}") |
|---|
| 459 |
map.deprecated_named_route("formatted_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.singular}") |
|---|
| 460 |
end |
|---|
| 461 |
|
|---|
| 462 |
update_action_options = action_options_for("update", resource) |
|---|
| 463 |
map.connect(resource.member_path, update_action_options) |
|---|
| 464 |
map.connect("#{resource.member_path}.:format", update_action_options) |
|---|
| 465 |
|
|---|
| 466 |
destroy_action_options = action_options_for("destroy", resource) |
|---|
| 467 |
map.connect(resource.member_path, destroy_action_options) |
|---|
| 468 |
map.connect("#{resource.member_path}.:format", destroy_action_options) |
|---|
| 469 |
end |
|---|
| 470 |
|
|---|
| 471 |
def conditions_for(method) |
|---|
| 472 |
{ :conditions => method == :any ? {} : { :method => method } } |
|---|
| 473 |
end |
|---|
| 474 |
|
|---|
| 475 |
def action_options_for(action, resource, method = nil) |
|---|
| 476 |
default_options = { :action => action.to_s } |
|---|
| 477 |
require_id = resource.kind_of?(SingletonResource) ? {} : { :requirements => { :id => Regexp.new("[^#{Routing::SEPARATORS.join}]+") } } |
|---|
| 478 |
case default_options[:action] |
|---|
| 479 |
when "index", "new" : default_options.merge(conditions_for(method || :get)) |
|---|
| 480 |
when "create" : default_options.merge(conditions_for(method || :post)) |
|---|
| 481 |
when "show", "edit" : default_options.merge(conditions_for(method || :get)).merge(require_id) |
|---|
| 482 |
when "update" : default_options.merge(conditions_for(method || :put)).merge(require_id) |
|---|
| 483 |
when "destroy" : default_options.merge(conditions_for(method || :delete)).merge(require_id) |
|---|
| 484 |
else default_options.merge(conditions_for(method)) |
|---|
| 485 |
end |
|---|
| 486 |
end |
|---|
| 487 |
end |
|---|
| 488 |
end |
|---|
| 489 |
|
|---|
| 490 |
ActionController::Routing::RouteSet::Mapper.send :include, ActionController::Resources |
|---|