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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/infold/controller_generator.rb', line 43

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



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

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

  return if name.pluralize.underscore == 'admin_users'
  in_file = File.readlines(file).grep(/^\s+authenticated :admin_user do root/)
  if in_file.blank?
    inject_into_file file, after: "resources :admin_users" do
      "\n  authenticated :admin_user do root :to => '#{name.pluralize.underscore}#index', as: :root end"
    end
  end
end

#create_controller_fileObject



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

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
# File 'lib/generators/infold/controller_generator.rb', line 11

def setup
  resource_name = name.camelize.singularize
  db_schema = DbSchema.new(File.read(Rails.root.join('db/schema.rb')))
  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