在rake任务中使用pluralize方法

时间:2021-07-08 08:01:41

I know this seems silly, but I would like to call some of Rails' Text Helpers in a rake task I am setting up. (Thinks like the pluralize and cycle method: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html)

我知道这看起来很傻,但我想在我正在设置的rake任务中调用一些Rails的Text Helpers。 (想象一下复数和循环方法:http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html)

How would you go about making these available in a rake task, or is it not easily possible?

你会如何在rake任务中提供这些服务,还是不容易实现?

4 个解决方案

#1


9  

It's rather messy to extend ActionView::Helpers in your rake task - that basically includes all helper methods in your Rake task.

在你的rake任务中扩展ActionView :: Helpers是相当混乱的 - 基本上包括你的Rake任务中的所有帮助方法。

ActionController::Base.helpers.pluralize(5, 'dog')

ActionController :: Base.helpers.pluralize(5,'dog')

If you don't have a count and you just want to pluralize a word:

如果你没有计数,你只想复数一个词:

ActiveSupport::Inflector.pluralize('dog')

::的ActiveSupport Inflector.pluralize( '狗')

#2


3  

Adding ActiveSupport and ActionPack as dependencies could be heavy handed to simply have the convenience of a text helper -- unless your Rakefile is already loading Rails -- but here's an example Rakefile:

添加ActiveSupport和ActionPack作为依赖关系可能很简单,只需要方便的文本助手 - 除非您的Rakefile已经加载Rails - 但这是一个示例Rakefile:

require 'rubygems'
require 'activesupport'
require 'actionpack'
require 'action_view/helpers'

extend ActionView::Helpers

task :plural do
  puts "I have #{pluralize(5, "dog")}"
  puts "You have #{pluralize(1, "dog")}"
end

#3


3  

In Rails 3:

require 'active_support/inflections'
require 'action_view/helpers'
extend ActionView::Helpers

task :plural do
  puts "I have #{pluralize(5, "dog")}"
  puts "You have #{pluralize(1, "dog")}"
end

#4


1  

If you just want the pluralized version of the string, you can just call pluralize on it directly without loading anything extra in your rake task:

如果你只想要字符串的复数形式,你可以直接调用pluralize,而不需要在你的rake任务中加载任何额外的东西:

"dog".pluralize

You could then just replace the pluralize helper with something like this:

然后你可以用这样的东西替换复数帮助器:

word = "dog"
if count == 1
  plural = "1 #{word}"
else
  plural = "#{count} #{word}".pluralize
end
puts plural

#1


9  

It's rather messy to extend ActionView::Helpers in your rake task - that basically includes all helper methods in your Rake task.

在你的rake任务中扩展ActionView :: Helpers是相当混乱的 - 基本上包括你的Rake任务中的所有帮助方法。

ActionController::Base.helpers.pluralize(5, 'dog')

ActionController :: Base.helpers.pluralize(5,'dog')

If you don't have a count and you just want to pluralize a word:

如果你没有计数,你只想复数一个词:

ActiveSupport::Inflector.pluralize('dog')

::的ActiveSupport Inflector.pluralize( '狗')

#2


3  

Adding ActiveSupport and ActionPack as dependencies could be heavy handed to simply have the convenience of a text helper -- unless your Rakefile is already loading Rails -- but here's an example Rakefile:

添加ActiveSupport和ActionPack作为依赖关系可能很简单,只需要方便的文本助手 - 除非您的Rakefile已经加载Rails - 但这是一个示例Rakefile:

require 'rubygems'
require 'activesupport'
require 'actionpack'
require 'action_view/helpers'

extend ActionView::Helpers

task :plural do
  puts "I have #{pluralize(5, "dog")}"
  puts "You have #{pluralize(1, "dog")}"
end

#3


3  

In Rails 3:

require 'active_support/inflections'
require 'action_view/helpers'
extend ActionView::Helpers

task :plural do
  puts "I have #{pluralize(5, "dog")}"
  puts "You have #{pluralize(1, "dog")}"
end

#4


1  

If you just want the pluralized version of the string, you can just call pluralize on it directly without loading anything extra in your rake task:

如果你只想要字符串的复数形式,你可以直接调用pluralize,而不需要在你的rake任务中加载任何额外的东西:

"dog".pluralize

You could then just replace the pluralize helper with something like this:

然后你可以用这样的东西替换复数帮助器:

word = "dog"
if count == 1
  plural = "1 #{word}"
else
  plural = "#{count} #{word}".pluralize
end
puts plural