Class: Manifest::DataTypeGenerator

Inherits:
ActiveRecord::Generators::Base
  • Object
show all
Defined in:
lib/generators/manifest/data_type/data_type_generator.rb

Overview

Creates templates and migrations for custom Manifest data types. Example:

rails generate manifest:data_type Person first_name:string last_name:string

Instance Method Summary collapse

Instance Method Details

#create_controller_fileObject



55
56
57
58
59
# File 'lib/generators/manifest/data_type/data_type_generator.rb', line 55

def create_controller_file
  plural_name = ActiveSupport::Inflector.pluralize(name)

  template 'controller.rb', "#{Rails.root}/app/controllers/manifest/#{plural_name.underscore}_controller.rb"
end

#create_index_viewObject



26
27
28
# File 'lib/generators/manifest/data_type/data_type_generator.rb', line 26

def create_index_view
  template 'index.html.erb', "#{Rails.root}/app/views/manifest/#{table_name}/index.html.erb"
end

#create_manifest_namespace_in_routes_if_it_does_not_existObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/manifest/data_type/data_type_generator.rb', line 30

def create_manifest_namespace_in_routes_if_it_does_not_exist
  route_file = "#{Rails.root}/config/routes.rb"
  File.open(route_file, 'r') do |f|
    if f.lines.to_a.find_index { |l| l =~ /^\s*namespace :manifest do/ }
      return
    else
      sentinel = /\.routes.draw do\s*$/
      inject_into_file route_file, "\n  namespace :manifest do\n  end", { after: sentinel }
    end
  end
end

#create_model_fileObject



51
52
53
# File 'lib/generators/manifest/data_type/data_type_generator.rb', line 51

def create_model_file
  template 'model.rb', "#{Rails.root}/app/models/#{name.underscore}.rb"
end

#generate_data_type_edit_viewObject



22
23
24
# File 'lib/generators/manifest/data_type/data_type_generator.rb', line 22

def generate_data_type_edit_view
  template 'edit.html.erb', "#{Rails.root}/app/views/manifest/#{table_name}/edit.html.erb"
end

#generate_data_type_form_partialObject



14
15
16
# File 'lib/generators/manifest/data_type/data_type_generator.rb', line 14

def generate_data_type_form_partial
  template '_form.html.erb', "#{Rails.root}/app/views/manifest/#{table_name}/_form.html.erb"
end

#generate_data_type_migrationObject



10
11
12
# File 'lib/generators/manifest/data_type/data_type_generator.rb', line 10

def generate_data_type_migration
  migration_template 'migration.rb', "#{Rails.root}/db/migrate/create_#{table_name}.rb"
end

#generate_data_type_new_viewObject



18
19
20
# File 'lib/generators/manifest/data_type/data_type_generator.rb', line 18

def generate_data_type_new_view
  template 'new.html.erb', "#{Rails.root}/app/views/manifest/#{table_name}/new.html.erb"
end

#inject_data_type_route_into_routesObject



42
43
44
45
46
47
48
49
# File 'lib/generators/manifest/data_type/data_type_generator.rb', line 42

def inject_data_type_route_into_routes
  route_file = "#{Rails.root}/config/routes.rb"
  sentinel = /^\s*namespace :manifest do/

  plural_name = ActiveSupport::Inflector.pluralize(name.underscore)

  inject_into_file route_file, "\n    resources :#{plural_name}", { after: sentinel}
end