Module: Incline::Extensions::ResourceRouteGenerator

Defined in:
lib/incline/extensions/resource_route_generator.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Overrides the add_resource_route method.



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
44
45
46
47
48
# File 'lib/incline/extensions/resource_route_generator.rb', line 9

def self.included(base)
  base.class_eval do
    undef add_resource_route

    ##
    # Adds a resource route with additional :api and :locate actions.
    def add_resource_route
      return if options[:actions].present?

      # iterates over all namespaces and opens up blocks
      regular_class_path.each_with_index do |namespace, index|
        write("namespace :#{namespace} do", index + 1)
      end

      # inserts the primary resource with api routes as well.
      pad = '  ' * (route_length + 1)
      write "\#{pad}resources :\#{file_name.pluralize} do\n\#{pad}  member do\n\#{pad}    post :locate\n\#{pad}  end\n\#{pad}  collection do\n\#{pad}    match :api, via: [ :get, :post ]\n\#{pad}  end\n\#{pad}end\n      EOR\n\n      # ends blocks\n      regular_class_path.each_index do |index|\n        write(\"end\", route_length - index)\n      end\n\n      # route prepends two spaces onto the front of the string that is passed, this corrects that.\n      # Also it adds a \\n to the end of each line, as route already adds that\n      # we need to correct that too.\n      route route_string[2..-2]\n    end\n\n  end\nend\n", 0