Class: RESTFramework::Generators::ControllerGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/rest_framework/generators/controller_generator.rb

Constant Summary collapse

PATH_REGEX =
/^\/*([a-z0-9_\/]*[a-z0-9_])(?:[\.a-z\/]*)$/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.namespaceObject

Some projects may not have the inflection “REST” as an acronym, which changes this generator to be namespaced in ‘r_e_s_t_framework`, which is weird.



48
49
50
# File 'lib/rest_framework/generators/controller_generator.rb', line 48

def self.namespace
  return RESTFrameworkCustomGeneratorControllerNamespace.new("rest_framework:controller")
end

Instance Method Details

#create_rest_controller_fileObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rest_framework/generators/controller_generator.rb', line 52

def create_rest_controller_file
  unless (path_match = PATH_REGEX.match(self.path))
    raise StandardError.new("Path isn't correct.")
  end

  cleaned_path = path_match[1]
  content = <<~END
    class #{cleaned_path.camelize}Controller < #{options[:parent_class]}
      include RESTFramework::#{
        options[:include_base] ? "BaseControllerMixin" : "ModelControllerMixin"
      }
    end
  END
  create_file("app/controllers/#{path}_controller.rb", content)
end