Class: Infold::ControllerGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/infold/controller_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_menuObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/infold/controller_generator.rb', line 39

def add_menu
  return if name.pluralize.underscore == 'admin_users'
  file = Rails.root.join('app/views/admin/common/_header_menu.html.haml')
  return unless File.exist?(file)
  menu = "\n  %li.nav-item\n    = link_to '#{@writer.resource.app_title}', #{@writer.index_path}, class: 'nav-link'"
  in_file = File.readlines(file).grep(/, #{@writer.index_path},/)
  if in_file.blank?
    append_to_file file do
      menu
    end
  end
end

#add_routesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/infold/controller_generator.rb', line 24

def add_routes
  file = Rails.root.join('config/routes/admin.rb')
  route = "resources :#{name.pluralize.underscore}"
  return unless File.exist?(file)
  in_file = File.readlines(file).grep(/^\s+#{route}$/)
  if in_file.blank?
    inject_into_file file, after: "namespace 'admin' do" do
      "\n  #{route}"
    end
  end

  gsub_file file, "root :to => 'admin_users#index'",
            "root :to => '#{name.pluralize.underscore}#index'"
end

#create_controller_fileObject



20
21
22
# File 'lib/generators/infold/controller_generator.rb', line 20

def create_controller_file
  template "controller.rb", Rails.root.join("app/controllers/admin/#{name.pluralize.underscore}_controller.rb"), force: true
end

#setupObject



11
12
13
14
15
16
17
18
# File 'lib/generators/infold/controller_generator.rb', line 11

def setup
  resource_name = name.camelize.singularize
  db_schema_file = Rails.root.join('db/schema.rb')
  db_schema = DbSchema.new(File.exist?(db_schema_file) ? File.read(db_schema_file) : nil)
  yaml = YAML.load_file(Rails.root.join("config/infold/#{resource_name.underscore}.yml"))
  resource = YamlReader.generate_resource(resource_name, yaml, db_schema)
  @writer = ControllerWriter.new(resource)
end