Class: HocUtils::Generators::ApiScaffoldGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- HocUtils::Generators::ApiScaffoldGenerator
- Includes:
- Rails::Generators::ModelHelpers, Rails::Generators::ResourceHelpers
- Defined in:
- lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb
Instance Method Summary collapse
- #api_version ⇒ Object
-
#generate_admin_controllers ⇒ Object
Generates admin controllers in app/admin.
-
#generate_api_controller ⇒ Object
Scaffolds the api controller.
-
#generate_model ⇒ Object
Generates the model and the migration.
-
#generate_routes ⇒ Object
Generates routes in config/routes.rb.
-
#generate_specs ⇒ Object
Generates spec stubs for swagger.
- #migrate ⇒ Object
- #salute ⇒ Object
- #update_swagger ⇒ Object
Instance Method Details
#api_version ⇒ Object
22 23 24 |
# File 'lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb', line 22 def api_version .api_version.downcase end |
#generate_admin_controllers ⇒ Object
Generates admin controllers in app/admin
88 89 90 91 92 |
# File 'lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb', line 88 def generate_admin_controllers return unless .admin? say "Generates app/admin/#{plural_table_name}_admin.rb", :bold generate "trestle:resource #{singular_table_name}" end |
#generate_api_controller ⇒ Object
Scaffolds the api controller
45 46 47 48 |
# File 'lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb', line 45 def generate_api_controller say "Generates app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb", :bold template "api_controller.rb.tt", "app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb" end |
#generate_model ⇒ Object
Generates the model and the migration. Overrides the model definition to add acts_as_api block
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb', line 27 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 "\n acts_as_api\n api_accessible :basic do |t|\n \#{attributes_names.map { |name| \"\\tt.add :\#{name}\" }.join(\"\\n\")}\n t.add :created_at\n t.add :updated_at\n end\n\n" end end |
#generate_routes ⇒ Object
Generates routes in config/routes.rb. Will namespace resources to api/.
51 52 53 54 55 |
# File 'lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb', line 51 def generate_routes return unless .routes? 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 ⇒ Object
Generates spec stubs for swagger.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb', line 58 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"}, } }, } end end |
#migrate ⇒ Object
94 95 96 97 |
# File 'lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb', line 94 def migrate say "Migrating...", :bold rails_command 'db:migrate' end |
#salute ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb', line 104 def salute msg " Generation complete.\n Next step is to customize the generated code.\n * Open 'spec/swagger_helper.rb' and change the definitions \#{singular_table_name}_input and \#{singular_table_name}.\n * Run 'rails rswag:specs:swaggerize' to update swagger.\n * Make sure any referenced models are updated with eg. has_many :\#{plural_table_name}\n * Customize the table and form definition in 'app/admin/\#{plural_table_name}_admin.rb'\n * #beAwesome\n\n MSG\n say msg, :green, :bold\nend\n" |
#update_swagger ⇒ Object
99 100 101 102 |
# File 'lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb', line 99 def update_swagger say "Updating swagger documentation...", :bold rails_command 'rswag:specs:swaggerize' end |