Class: ActionDispatch::Routing::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/routes.rb

Instance Method Summary collapse

Instance Method Details

#comments_on(*resources) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails/routes.rb', line 4

def comments_on(*resources)
  options = resources.extract_options!

  unless resources.count == 1
    raise "too many arguments passed to comments_on.  Format is comments_on :articles, :by => :users"
  end
  
  unless options.has_key?(:by)
    raise "comments_on must include an :by option.  Format is comments_on :articles, :by => :users"
  end
  
  unless options.has_key?(:controller)
    options[:controller] = 'loudmouth/comments'
  end
  
  topic_model = resources.first.to_s
  user_model = options[:by].to_s
  options.delete(:by)
  
  Loudmouth.treasure_map[topic_model.singularize.downcase.to_sym] = 
                                    { :by => user_model.singularize.downcase }
  
  constraint = lambda { |req| req.env["loudmouth_map"] = { :topic_model => topic_model, 
                                                           :user_model => user_model }; true }
  Rails.application.routes.draw do
    resources topic_model do
      constraints(constraint) do
        resources "comments", options
      end
    end
  end
end