aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/generators/rails/resource/resource_generator.rb
blob: 9c57eda7de685f3b63b670693b9c8b29dde382d8 (plain) (blame)
1
2
3
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
36
37
38
39
40
41
42
43
require 'generators/rails/model/model_generator'

module Rails
  module Generators
    class ResourceGenerator < ModelGenerator
      hook_for :resource_controller do |base, controller|
        base.invoke controller, [ base.name.pluralize, base.options[:actions] ]
      end

      class_option :actions, :type => :array, :default => [], :banner => "ACTION ACTION",
                             :desc => "Actions for the resource controller", :aliases => "-a"

      class_option :singleton, :type => :boolean, :default => false, :aliases => "-i",
                               :desc => "Supply to create a singleton controller"

      class_option :force_plural, :type => :boolean, :default => false, :aliases => "-u",
                                  :desc => "Forces the use of a plural ModelName"

      def initialize(args=[], options={}, config={})
        super
        if args[0] == args[0].pluralize && !self.options[:force_plural]
          say "Plural version of the model detected, using singularized version. Override with --force-plural."
          args[0] = args[0].singularize
        end
      end

      def add_resource_route
        route "map.resource#{:s unless options[:singleton]} :#{pluralize?(file_name)}"
      end

      protected

        def pluralize?(name)
          if options[:singleton]
            name
          else
            name.pluralize
          end
        end

    end
  end
end