Class: Taxonworks::TaskGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Taxonworks::TaskGenerator
- Defined in:
- lib/generators/taxonworks/task/task_generator.rb
Overview
Generate new TaxonWorks tasks
Instance Method Summary (collapse)
- - (Object) add_scopes_to_routes
- - (Object) add_to_user_task
- - (Object) check_args
- - (Object) controller_class_name private
- - (Object) create_controller
- - (Object) create_controller_folders
- - (Object) create_view_folders
- - (Object) create_views
- - (Object) full_controller_class_name private
- - (Object) process_args
Instance Method Details
- (Object) add_scopes_to_routes
46 47 48 49 50 51 52 53 54 55 56 57 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 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 46 def add_scopes_to_routes scopes = ['scope :tasks'] scope_index = 0 @paths.each do |path| scopes.push("scope :#{path}") end innermost_scope_str = "scope :#{controller_base_name}, controller: 'tasks/#{path_to_controller}#{controller_base_name}'" File.read('config/routes.rb').split("\n").each do |line| if line.include?(innermost_scope_str) puts "ERROR: \"#{innermost_scope_str}\" already exists!" abort elsif scope_index < scopes.length && line.include?(scopes[scope_index]) scope_index += 1 end end if scope_index == 0 puts "ERROR: Couldn't find 'task' scope!" abort end route_str = '' indent_str = ' ' scopes.each_with_index do |scope, index| if index >= scope_index route_str += "#{indent_str}#{scope} do\n" end indent_str += ' ' end route_str += "#{indent_str}#{innermost_scope_str} do\n" @route_actions.each_with_index do |action, index| route_str += "#{indent_str} #{action} '#{@route_methods[index]}', as: '#{@route_names[index]}'\n" end route_str += "#{indent_str}end\n" scopes.length.downto(scope_index + 1) do indent_str.chomp!(' ') route_str += "#{indent_str}end\n" end route_str += "\n" insert_into_file('config/routes.rb', route_str, after: "#{scopes[scope_index - 1]} do\n") end |
- (Object) add_to_user_task
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 98 def add_to_user_task user_tasks_str = "\n" @route_names.each_with_index do |name, index| next if @route_actions[index] != 'get' user_tasks_str += "#{name}:\n" user_tasks_str += " hub: true\n" user_tasks_str += " name: 'TODO: Task name'\n" user_tasks_str += " related:\n" user_tasks_str += " categories:\n" user_tasks_str += " status: prototype\n" user_tasks_str += " description: 'TODO: Task description'\n" end append_to_file 'config/interface/hub/user_tasks.yml', user_tasks_str end |
- (Object) check_args
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 11 def check_args error_str = '' if path_to_controller[0] == '/' error_str += "ERROR: 'path_to_controller' can't begin with a '/'\n" end if path_to_controller[path_to_controller.length - 1] != '/' error_str += "ERROR: 'path_to_controller' must end with a '/'\n" end if error_str.length > 0 puts error_str abort end end |
- (Object) controller_class_name (private)
148 149 150 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 148 def controller_class_name controller_base_name.titleize.tr(' ', '') end |
- (Object) create_controller
125 126 127 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 125 def create_controller template 'controller', "app/controllers/tasks/#{path_to_controller}#{controller_base_name}_controller.rb" end |
- (Object) create_controller_folders
116 117 118 119 120 121 122 123 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 116 def create_controller_folders directory_name = 'app/controllers/tasks' @paths.each do |path| directory_name += "/#{path}" Dir.mkdir(directory_name) unless File.directory?(directory_name) end end |
- (Object) create_view_folders
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 129 def create_view_folders directory_name = 'app/views/tasks' @paths.each do |path| directory_name += "/#{path}" Dir.mkdir(directory_name) unless File.directory?(directory_name) end Dir.mkdir(directory_name += "/#{controller_base_name}") unless File.directory?(directory_name) end |
- (Object) create_views
140 141 142 143 144 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 140 def create_views @route_methods.each do |method| create_file "app/views/tasks/#{path_to_controller}#{controller_base_name}/#{method}.html.erb" end end |
- (Object) full_controller_class_name (private)
152 153 154 155 156 157 158 159 160 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 152 def full_controller_class_name controller_name = 'Tasks::' controller_name += @paths.inject('') do |str, elem| str += "#{elem.titleize.tr(' ', '')}::" end controller_name += controller_class_name "#{controller_name.chomp("::")}Controller" end |
- (Object) process_args
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/generators/taxonworks/task/task_generator.rb', line 28 def process_args @paths = path_to_controller.split('/') @route_methods = Array.new @route_actions = Array.new @route_names = Array.new methods_actions_names.each do |str| split_str = str.split(':') method = split_str[0] action = split_str[1] name = split_str[2] || "#{method}_#{controller_base_name}" @route_methods.push(method) @route_actions.push(action) @route_names.push(name + '_task') end end |