Skip to content
Browse files

added possibility generate thor task with rails generator

  • Loading branch information...
1 parent bfc6c17 commit ca6d4affe716b3e4d0b0167582be60d59dee9dc6 Tima Maslyuchenko committed Oct 20, 2012
View
3 railties/lib/rails/generators.rb
@@ -57,7 +57,8 @@ module Generators
stylesheets: true,
stylesheet_engine: :css,
test_framework: false,
- template_engine: :erb
+ template_engine: :erb,
+ task_engine: :rake
}
}
View
13 railties/lib/rails/generators/rails/task/USAGE
@@ -1,9 +1,18 @@
Description:
- Stubs out a new Rake task. Pass the namespace name, and a list of tasks as arguments.
+ Stubs out a new Rake or Thor task.
+ Pass the name, a list of tasks as arguments and a task_engine as option.
This generates a task file in lib/tasks.
Example:
`rails generate task feeds fetch erase add`
- Task: lib/tasks/feeds.rake
+ Task: lib/tasks/feeds.rake
+
+ `rails generate task feeds fetch erase add --task_engine thor`
+
+ Task: lib/tasks/feeds.thor
+
+ `rails generate task feeds fetch erase add -t thor`
+
+ Task: lib/tasks/feeds.thor
View
7 railties/lib/rails/generators/rails/task/task_generator.rb
@@ -2,9 +2,14 @@ module Rails
module Generators
class TaskGenerator < NamedBase # :nodoc:
argument :actions, type: :array, default: [], banner: "action action"
+ class_option :task_engine, type: :string, default: 'rake', aliases: '-t'
def create_task_files
- template 'task.rb', File.join('lib/tasks', "#{file_name}.rake")
+ engine = options[:task_engine].to_sym
+ unless [:rake, :thor].include? engine
+ raise Error, 'Task engine should be rake or thor'
+ end
+ template "#{engine}_task.rb", File.join('lib/tasks', "#{file_name}.#{engine}")
end
end
View
0 ...s/generators/rails/task/templates/task.rb → ...erators/rails/task/templates/rake_task.rb
File renamed without changes.
View
10 railties/lib/rails/generators/rails/task/templates/thor_task.rb
@@ -0,0 +1,10 @@
+class <%= file_name.camelize %> < Thor
+
+<% actions.each do |action| -%>
+ desc "<%= action %>", "TODO"
+ def <%= action %>
+ require './config/environment'
+ end
+
+<% end -%>
+end
View
7 railties/test/generators/task_generator_test.rb
@@ -5,8 +5,13 @@ class TaskGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper
arguments %w(feeds foo bar)
- def test_controller_skeleton_is_created
+ def test_creating_rake_task_by_default
run_generator
assert_file "lib/tasks/feeds.rake", /namespace :feeds/
end
+
+ def test_creating_thor_task
+ run_generator ['feeds', 'foo', 'bar', '-t', 'thor']
+ assert_file "lib/tasks/feeds.thor", /class Feeds < Thor/
+ end
end

0 comments on commit ca6d4af

Please sign in to comment.
Something went wrong with that request. Please try again.