Module: ActionDispatch::Routing::Mapper::Resources

Included in:
ActionDispatch::Routing::Mapper
Defined in:
lib/action_dispatch/routing/mapper.rb

Defined Under Namespace

Classes: Resource, SingletonResource

Constant Summary collapse

VALID_ON_OPTIONS =

CANONICAL_ACTIONS holds all actions that does not need a prefix or a path appended since they fit properly in their scope level.

[:new, :collection, :member]
RESOURCE_OPTIONS =
[:as, :controller, :path, :only, :except]
CANONICAL_ACTIONS =
%w(index create new show update destroy)

Instance Method Summary collapse

Instance Method Details

#collectionObject



611
612
613
614
615
616
617
618
619
# File 'lib/action_dispatch/routing/mapper.rb', line 611

def collection
  unless @scope[:scope_level] == :resources
    raise ArgumentError, "can't use collection outside resources scope"
  end

  collection_scope do
    yield
  end
end

#initialize(*args) ⇒ Object

:nodoc:



543
544
545
546
# File 'lib/action_dispatch/routing/mapper.rb', line 543

def initialize(*args) #:nodoc:
  super
  @scope[:path_names] = @set.resources_path_names
end

#match(*args) ⇒ Object



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/action_dispatch/routing/mapper.rb', line 681

def match(*args)
  options = args.extract_options!.dup
  options[:anchor] = true unless options.key?(:anchor)

  if args.length > 1
    args.each { |path| match(path, options.dup) }
    return self
  end

  on = options.delete(:on)
  if VALID_ON_OPTIONS.include?(on)
    args.push(options)
    return send(on){ match(*args) }
  elsif on
    raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
  end

  if @scope[:scope_level] == :resources
    args.push(options)
    return nested { match(*args) }
  elsif @scope[:scope_level] == :resource
    args.push(options)
    return member { match(*args) }
  end

  action = args.first
  path = path_for_action(action, options.delete(:path))

  if action.to_s =~ /^[\w\/]+$/
    options[:action] ||= action unless action.to_s.include?("/")
    options[:as] = name_for_action(action, options[:as])
  else
    options[:as] = name_for_action(options[:as])
  end

  super(path, options)
end

#memberObject



621
622
623
624
625
626
627
628
629
# File 'lib/action_dispatch/routing/mapper.rb', line 621

def member
  unless resource_scope?
    raise ArgumentError, "can't use member outside resource(s) scope"
  end

  member_scope do
    yield
  end
end

#namespace(path, options = {}) ⇒ Object



663
664
665
666
667
668
669
# File 'lib/action_dispatch/routing/mapper.rb', line 663

def namespace(path, options = {})
  if resource_scope?
    nested { super }
  else
    super
  end
end

#nestedObject



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/action_dispatch/routing/mapper.rb', line 641

def nested
  unless resource_scope?
    raise ArgumentError, "can't use nested outside resource(s) scope"
  end

  with_scope_level(:nested) do
    if shallow?
      with_exclusive_scope do
        if @scope[:shallow_path].blank?
          scope(parent_resource.nested_scope, nested_options) { yield }
        else
          scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do
            scope(parent_resource.nested_scope, nested_options) { yield }
          end
        end
      end
    else
      scope(parent_resource.nested_scope, nested_options) { yield }
    end
  end
end

#newObject



631
632
633
634
635
636
637
638
639
# File 'lib/action_dispatch/routing/mapper.rb', line 631

def new
  unless resource_scope?
    raise ArgumentError, "can't use new outside resource(s) scope"
  end

  new_scope do
    yield
  end
end

#resource(*resources, &block) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/action_dispatch/routing/mapper.rb', line 552

def resource(*resources, &block)
  options = resources.extract_options!

  if apply_common_behavior_for(:resource, resources, options, &block)
    return self
  end

  resource_scope(SingletonResource.new(resources.pop, options)) do
    yield if block_given?

    collection_scope do
      post :create
    end if parent_resource.actions.include?(:create)

    new_scope do
      get :new
    end if parent_resource.actions.include?(:new)

    member_scope  do
      get    :edit if parent_resource.actions.include?(:edit)
      get    :show if parent_resource.actions.include?(:show)
      put    :update if parent_resource.actions.include?(:update)
      delete :destroy if parent_resource.actions.include?(:destroy)
    end
  end

  self
end

#resources(*resources, &block) ⇒ Object



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/action_dispatch/routing/mapper.rb', line 581

def resources(*resources, &block)
  options = resources.extract_options!

  if apply_common_behavior_for(:resources, resources, options, &block)
    return self
  end

  resource_scope(Resource.new(resources.pop, options)) do
    yield if block_given?

    collection_scope do
      get  :index if parent_resource.actions.include?(:index)
      post :create if parent_resource.actions.include?(:create)
    end

    new_scope do
      get :new
    end if parent_resource.actions.include?(:new)

    member_scope  do
      get    :edit if parent_resource.actions.include?(:edit)
      get    :show if parent_resource.actions.include?(:show)
      put    :update if parent_resource.actions.include?(:update)
      delete :destroy if parent_resource.actions.include?(:destroy)
    end
  end

  self
end

#resources_path_names(options) ⇒ Object



548
549
550
# File 'lib/action_dispatch/routing/mapper.rb', line 548

def resources_path_names(options)
  @scope[:path_names].merge!(options)
end

#root(options = {}) ⇒ Object



719
720
721
722
723
724
725
726
727
728
729
# File 'lib/action_dispatch/routing/mapper.rb', line 719

def root(options={})
  if @scope[:scope_level] == :resources
    with_scope_level(:root) do
      scope(parent_resource.path) do
        super(options)
      end
    end
  else
    super(options)
  end
end

#shallowObject



671
672
673
674
675
# File 'lib/action_dispatch/routing/mapper.rb', line 671

def shallow
  scope(:shallow => true) do
    yield
  end
end

#shallow?Boolean

Returns:

  • (Boolean)


677
678
679
# File 'lib/action_dispatch/routing/mapper.rb', line 677

def shallow?
  parent_resource.instance_of?(Resource) && @scope[:shallow]
end