class HocUtils::Generators::ApiScaffoldGenerator

Public Instance Methods

api_version() click to toggle source
# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 37
def api_version
  options.api_version.downcase
end
generate_admin_controllers() click to toggle source

Generates admin controllers in app/admin

# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 120
def generate_admin_controllers
  return unless options.admin?
  say "Generates app/admin/#{plural_table_name}_admin.rb", :bold
  generate "trestle:resource #{singular_table_name}"
end
generate_api_controller() click to toggle source

Scaffolds the api controller

# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 60
def generate_api_controller
  say "Generates app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb", :bold
  if is_nested?
    template "nested_api_controller.rb.tt", "app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb"
  else
    template "api_controller.rb.tt", "app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb"
  end
end
generate_model() click to toggle source

Generates the model and the migration. Overrides the model definition to add acts_as_api block

# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 42
      def generate_model
        say "Generates app/models/#{singular_table_name}.rb", :bold
        invoke :model
        inject_into_class "app/models/#{singular_table_name}.rb", singular_table_name.camelize do
<<-ACTS

  acts_as_api
  api_accessible :basic do |t|
  #{attributes_names.map { |name| "\tt.add :#{name}" }.join("\n")}
    t.add :created_at
    t.add :updated_at
  end

ACTS
        end
      end
generate_routes() click to toggle source

Generates routes in config/routes.rb. Will namespace resources to api/.

# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 70
def generate_routes
  return unless options.routes?

  if is_nested?
    say "Sorry you have to generate the route manually. because I don't know how to do it when the resource is nested!", :yellow, :bold
    return
  end
  say "Generates routes. You may want to merge api/#{api_version} namespaces in config/routes.rb", :bold
  generate "resource_route api/#{api_version.downcase}/#{plural_table_name}"
end
generate_specs() click to toggle source

Generates spec stubs for swagger.

# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 82
def generate_specs
  say "Generates spec/integration/#{plural_table_name}_spec.rb", :bold
  template "spec.rb.tt", "spec/integration/#{plural_table_name}_spec.rb"
  say "Adds definitions to spec/swagger_helper.rb", :bold
  insert_into_file "spec/swagger_helper.rb", :after => "definitions: {\n" do
    %{
    # AUTO GENERATED STUB TODO: update with correct fields
    #{singular_table_name}_input: {
      description: 'TODO: replace with correct description',
      type: 'object',
      properties: {

      },
      required: [] #TODO require
    },
    # AUTO GENERATED STUB TODO: update with correct fields
    #{singular_table_name}: {
      description: 'TODO: replace with correct description',
      type: 'object',
      properties: {
        id: { type: "integer"},
        created_at: { type: "string"},
        updated_at: { type: "string"},
      }
    },
    # AUTO GENERATED STUB TODO: update with correct fields
    #{plural_table_name}: {
      type: 'object',
      properties: {
        meta: { "$ref": "#/definitions/meta" },
        #{plural_table_name}: { type: 'array', items: { "$ref": "#/definitions/#{singular_table_name}" },},
      }
    },
  }
  end
end
is_nested?() click to toggle source
# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 21
def is_nested?
  !options.nested_to.nil?
end
migrate() click to toggle source
# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 126
def migrate
  say "Migrating...", :bold
  rails_command 'db:migrate'
end
parent_class_name() click to toggle source
# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 33
def parent_class_name
  singular_parent_name.try(:classify)
end
plural_parent_name() click to toggle source
# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 29
def plural_parent_name
  options.nested_to.try(:pluralize)
end
salute() click to toggle source
# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 136
def salute
  say("Generation complete.", :green, :bold)
  say("Next step is to customize the generated code.", :green)
  say("* Open 'spec/swagger_helper.rb' and change the definitions #{singular_table_name}_input and #{singular_table_name}.", :green)
  say("* Run 'rails rswag:specs:swaggerize' to update swagger.", :green)
  say("* Make sure any referenced models are updated with eg. has_many :#{plural_table_name}", :green)
  say("* Customize the table and form definition in 'app/admin/#{plural_table_name}_admin.rb'", :green)
  say("* Setup nested route") if is_nested?
  say("* #beAwesome", :green)
end
singular_parent_name() click to toggle source
# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 25
def singular_parent_name
  options.nested_to.try(:downcase)
end
update_swagger() click to toggle source
# File lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb, line 131
def update_swagger
  say "Updating swagger documentation...", :bold
  rails_command 'rswag:specs:swaggerize'
end