Sha256: 36652512176c70b57770c769ebe832d8e83aff3a7ee5686a405fb3811d32d1fe

Contents?: true

Size: 1.96 KB

Versions: 51

Compression:

Stored size: 1.96 KB

Contents

require "rails/generators"

# Most projects don't have the inflection "REST" as an acronym, so this is a helper class to prevent
# this generator from being namespaced as `"r_e_s_t_framework"`.
# :nocov:
class RESTFrameworkCustomGeneratorControllerNamespace < String
  def camelize
    return "RESTFramework"
  end
end
# :nocov:

class RESTFramework::Generators::ControllerGenerator < Rails::Generators::Base
  PATH_REGEX = %r{^[a-z0-9][a-z0-9_/]+$}

  desc <<~END
    Description:
      Generates a new REST Framework controller.

      Specify the controller as a path, including the module, if needed, like:
      'parent_module/controller_name'.

    Example:
      `rails generate rest_framework:controller user_api/groups`

      Generates a controller at `app/controllers/user_api/groups_controller.rb` named
      `UserApi::GroupsController`.
  END

  argument :path, type: :string
  class_option(
    :parent_class, type: :string, default: "ApplicationController", desc: "Inheritance parent"
  )
  class_option(
    :include_base,
    type: :boolean,
    default: false,
    desc: "Include `BaseControllerMixin`, not `ModelControllerMixin`",
  )

  # 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.
  def self.namespace
    return RESTFrameworkCustomGeneratorControllerNamespace.new("rest_framework:controller")
  end

  def create_rest_controller_file
    unless PATH_REGEX.match?(self.path)
      raise StandardError, "Path isn't valid."
    end

    # Remove '_controller' from end of path, if it exists.
    cleaned_path = self.path.delete_suffix("_controller")

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

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
rest_framework-1.0.0.beta2 lib/rest_framework/generators/controller_generator.rb
rest_framework-1.0.0.beta1 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.11.0 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.10.0 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.16 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.15 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.14 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.12.pre.1.pre.g39306cb lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.11.pre.40.pre.g5dcad32 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.11 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.10 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.9 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.8 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.7 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.6 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.5 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.4 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.3 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.2 lib/rest_framework/generators/controller_generator.rb
rest_framework-0.9.1 lib/rest_framework/generators/controller_generator.rb