Class: TaxonWorks::TaskGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/taxon_works/task/task_generator.rb

Overview

Generate new TaxonWorks tasks

Instance Method Summary collapse

Instance Method Details

#add_scopes_to_routesObject



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/generators/taxon_works/task/task_generator.rb', line 59

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/tasks.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}  get :index, as: 'index_#{controller_base_name}_task'\n" if options[:vue]

  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/tasks.rb', route_str, after: "#{scopes[scope_index - 1]} do\n")
end

#add_to_user_taskObject



115
116
117
118
119
120
121
122
# File 'lib/generators/taxon_works/task/task_generator.rb', line 115

def add_to_user_task
  user_tasks_str = "\n"

  @route_names.each_with_index do |name, index|
    next if @route_actions[index] != 'get'
    add_user_task(name)
  end
end

#add_user_task(name) ⇒ Object (private)



176
177
178
179
180
181
182
183
184
185
# File 'lib/generators/taxon_works/task/task_generator.rb', line 176

def add_user_task(name)
  user_tasks_str = "\n#{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"
  append_to_file 'config/interface/hub/user_tasks.yml', user_tasks_str
end

#bad_action_name?(array) ⇒ Boolean (private)

Returns:

  • (Boolean)


204
205
206
# File 'lib/generators/taxon_works/task/task_generator.rb', line 204

def bad_action_name?(array)
  array[0].blank? or array[1].blank?
end

#check_argsObject

This is a Thor task, all methods are invoked in order



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/taxon_works/task/task_generator.rb', line 14

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 Rainbow(error_str).red
    abort
  end

  if options[:vue] && methods_actions_names.present?
    puts Rainbow('--vue is incompatible with method action names, an index is created by default').red
    abort
  end
end

#controller_class_nameObject (private)



208
209
210
# File 'lib/generators/taxon_works/task/task_generator.rb', line 208

def controller_class_name
  controller_base_name.titleize.tr(' ', '')
end

#create_appObject (private)



187
188
189
# File 'lib/generators/taxon_works/task/task_generator.rb', line 187

def create_app
  template 'app', "app/javascript/vue/tasks/#{path_to_controller}#{controller_base_name}/app.vue"
end

#create_controllerObject



133
134
135
# File 'lib/generators/taxon_works/task/task_generator.rb', line 133

def create_controller
  template 'controller', "app/controllers/tasks/#{path_to_controller}#{controller_base_name}_controller.rb"
end

#create_controller_foldersObject



124
125
126
127
128
129
130
131
# File 'lib/generators/taxon_works/task/task_generator.rb', line 124

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

#create_mainObject (private)



191
192
193
# File 'lib/generators/taxon_works/task/task_generator.rb', line 191

def create_main
  template 'main', "app/javascript/vue/tasks/#{path_to_controller}#{controller_base_name}/main.js"
end

#create_view_foldersObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/generators/taxon_works/task/task_generator.rb', line 137

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

#create_viewsObject



148
149
150
151
152
# File 'lib/generators/taxon_works/task/task_generator.rb', line 148

def create_views
  @route_methods.each do |method|
    create_file "app/views/tasks/#{path_to_controller}#{controller_base_name}/#{method}.html.erb"
  end
end

#create_vue_indexObject (private)



195
196
197
# File 'lib/generators/taxon_works/task/task_generator.rb', line 195

def create_vue_index
  template 'vue_index', "app/views/tasks/#{path_to_controller}#{controller_base_name}/index.html.erb"
end

#full_controller_class_nameObject (private)



212
213
214
215
216
217
218
219
220
# File 'lib/generators/taxon_works/task/task_generator.rb', line 212

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

#handle_vueObject



154
155
156
157
158
159
160
161
162
# File 'lib/generators/taxon_works/task/task_generator.rb', line 154

def handle_vue
  if options[:vue]
    create_app
    create_main
    create_vue_index
    register_vue_in_application_js
    add_user_task("index_#{controller_base_name}_task" )
  end
end

#instruct_next_stepsObject



164
165
166
167
168
169
170
171
172
# File 'lib/generators/taxon_works/task/task_generator.rb', line 164

def instruct_next_steps
  if options[:vue]

    puts "Next steps:"
    puts Rainbow("* Restart your rails development server and confirm the new 'TODO: Task name' card exists and resolves").blue
    puts Rainbow('* Update the generated code throught changing "vue-task" to something specific').blue
  end

end

#process_argsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/taxon_works/task/task_generator.rb', line 36

def process_args

  @paths = path_to_controller.split('/')
  @route_methods = []
  @route_actions = []
  @route_names = []

  if methods_actions_names.present?
    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}"

      abort(message = "ERROR: malformed method action name\n USAGE: method_name:action_verb:route_name\n") if bad_action_name?(split_str)

      @route_methods.push(method)
      @route_actions.push(action)
      @route_names.push(name + '_task')
    end
  end
end

#register_vue_in_application_jsObject (private)



199
200
201
202
# File 'lib/generators/taxon_works/task/task_generator.rb', line 199

def register_vue_in_application_js
  str =  "import '../vue/tasks/#{path_to_controller}#{controller_base_name}/main.js'"
  append_to_file 'app/javascript/packs/application.js', str
end