Class: ApiScaffold::Generators::ApiScaffoldGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
GeneratorHelpers, Rails::Generators::ResourceHelpers
Defined in:
lib/generators/api_scaffold/api_scaffold_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GeneratorHelpers

#apipie_installed?, #apipie_param, #attributes_hash, #attributes_string, #error_serializer_created?, #fixture_name, #fixture_replacement, #gem_available?, #prefix, #prefixed_class_name, #prefixed_controller_class_name, #prefixed_url, #test_framework

Constructor Details

#initialize(args, *options) ⇒ ApiScaffoldGenerator

:nodoc:



16
17
18
19
20
# File 'lib/generators/api_scaffold/api_scaffold_generator.rb', line 16

def initialize(args, *options) #:nodoc:
  @generator_args = args.first
  raise "Nested scaffolds are not yet supported." if @generator_args.split("/").size > 1
  super
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



11
12
13
# File 'lib/generators/api_scaffold/api_scaffold_generator.rb', line 11

def attributes
  @attributes
end

Instance Method Details

#add_routesObject



42
43
44
45
46
47
48
49
# File 'lib/generators/api_scaffold/api_scaffold_generator.rb', line 42

def add_routes
  # Include tabs and line break for proper formatting
  routes_string = "      resources :#{controller_file_name}, except: [:new, :edit]\n"
  # Inject into file following the api scope and v1 namespace
  inject_into_file 'config/routes.rb', after: "  namespace :api, defaults: { format: :json } do\n    namespace :#{prefix} do\n" do
    routes_string
  end
end

#create_controller_filesObject



26
27
28
29
30
31
32
# File 'lib/generators/api_scaffold/api_scaffold_generator.rb', line 26

def create_controller_files
  if gem_available?('fast_jsonapi') || gem_available?('active_model_serializers')
    template "controllers/serializer_controller.rb", File.join("app/controllers/api/", prefix, "#{controller_file_name}_controller.rb")
  else
    template "controllers/controller.rb", File.join("app/controllers/api", prefix, "#{controller_file_name}_controller.rb")
  end
end

#create_controller_test_filesObject



34
35
36
37
38
39
40
# File 'lib/generators/api_scaffold/api_scaffold_generator.rb', line 34

def create_controller_test_files
  if test_framework == :rspec
    template "tests/rspec/controller_spec.rb", File.join("spec/controllers/api", prefix, "#{controller_file_name}_controller_spec.rb")
  else
    template "tests/test_unit/controller_spec.rb", File.join("test/controllers/api", prefix, "#{controller_file_name}_controller_test.rb")
  end
end

#create_serializer_filesObject



51
52
53
54
55
56
# File 'lib/generators/api_scaffold/api_scaffold_generator.rb', line 51

def create_serializer_files
  if gem_available?('fast_jsonapi') || gem_available?('active_model_serializers')
    invoke "serializer"
    template "serializers/error_serializer.rb", File.join("app/serializers", "error_serializer.rb") unless error_serializer_created?
  end
end