rails3 gem:acts_as_something [英] rails3 gem: acts_as_something

查看:147
本文介绍了rails3 gem:acts_as_something的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些通用代码提取到一个gem中。



我认为 acts_as_something 是简单重用的好策略。

有没有一个很好的教程来讨论这个rails3 gems?我发现有几个讨论rails2(例如 http://guides.rubyonrails.org/plugins.html ),但这是特定于rails2



以下是我已阅读的一些教程/博客:





谢谢

解决方案

UPDATE:博客文章基于这个答案,但有更多的细节: http: // thoughtsi ncomputation.com/posts/coding-an-acts_as-gem-for-rails-3



-



我还没有意识到另一个教程的源头,但是这里有一些一般的提示。



Rails 3使用了真正的称为Railtie的有用功能 - 请参阅 http://api.rubyonrails.org/classes/Rails/Railtie .html



所以,如果我正在实施一个acts_as_ * gem,我会从那里开始。我的railtie可能看起来像这样:

 #lib / acts_as_awesome / railtie.rb 
需要'rails'
需要'acts_as_awesome'

模块ActsAsAwesome
class Railtie< Rails :: Railtie
config.to_prepare do
ApplicationController.send(:extend,ActsAsAwesome :: Hook)
end
end
end

和ActsAsAwesome :: Hook代码:

 #lib / acts_as_awesome / hook.rb 
模块ActsAsAwesome :: Hook
def acts_as_awesome(* args)
options = args.extract_options!
#可以让控制器真棒。
包括ActsAsAwesome :: InstanceMethods
before_filter:an_awesome_filter
end
end

我觉得这里的概念很完善,并且之前使用过类似的过程。基本上,它会告诉Rails在生产期间和开发中的每个请求之前执行一次to_prepare块(我们希望这是因为ApplicationController将在那些时间重新加载,可能会消除我们的钩子方法);钩子就是这样的:它为所有控制器(或扩展ApplicationController的所有控制器)添加一个钩子,以允许用户将真正的Awesome代码引入到其控制器中,而不会影响不需要它的控制器。

#acts_as_awesome钩子本身并不传达Awesome功能。这是因为并非所有的控制器都可能需要此功能。相反,该方法负责通过ActsAsAwesome :: InstanceMethods模块介绍真正的超棒的东西。这样,如果用户明确调用acts_as_awesome方法,用户只能获得Awesome功能。它还在控制器中添加了一个before过滤器,以证明此方法中的代码与其目标控制器类本身的代码完全相同。



技术应该完全相同,如果你的目标是模型而不是控制器:只需将你的钩子注入到ActiveRecord :: Base中即可。由于AR:B仅在Rails启动时加载,所以您应该可以将其加入到初始化程序中(请参阅Railtie文档),但我保留在此输入的权利。



有关railtie的问题:文档看起来应该是自动检测的,但我经常遇到这方面的问题。为了解决它,只需要从你的gem的主源文件中获得railtie(在上面的例子中,这将是lib / acts_as_awesome.rb)。



你可以看到完整的ActsAs令人惊叹的源在我的github帐户的所有荣耀:
http://github.com/sinisterchipmunk/acts_as_awesome



我希望这有帮助。你的问题有点高水平,所以高层的回应是我能做的最好的。



-Colin MacKenzie IV



http://thoughtsincomputation.com



@ sinisterchipmnk

I'm trying to extract some common code into a gem.

I'm thinking that acts_as_somethingis a good strategy for simple re-use.

Is there a good tutorial that discusses this for rails3 gems? I've found several that discuss rails2 (such as http://guides.rubyonrails.org/plugins.html) but that is specific to rails2

here are some of the tutorials/blogs I've already read:

thanks

解决方案

UPDATE: I've added a blog post based on this answer, but with much more detail: http://thoughtsincomputation.com/posts/coding-an-acts_as-gem-for-rails-3

--

I'm not aware of another tutorial source off the top of my head, but here are some general tips.

Rails 3 makes use of a really useful feature called Railtie - see http://api.rubyonrails.org/classes/Rails/Railtie.html .

So, if I were implementing an acts_as_* gem, I'd start there. My railtie might look something like:

# lib/acts_as_awesome/railtie.rb
require 'rails'
require 'acts_as_awesome'

module ActsAsAwesome
  class Railtie < Rails::Railtie
    config.to_prepare do
      ApplicationController.send(:extend, ActsAsAwesome::Hook)
    end
  end
end

and the ActsAsAwesome::Hook code:

# lib/acts_as_awesome/hook.rb
module ActsAsAwesome::Hook
  def acts_as_awesome(*args)
    options = args.extract_options!
    # do the things that make the controller awesome.
    include ActsAsAwesome::InstanceMethods
    before_filter :an_awesome_filter
  end
end

I feel the concepts here are sound and have used similar processes before. Basically, it would tell Rails to execute the to_prepare block once during production and before each request in development (we want that because ApplicationController will be reloaded at those times, potentially wiping out our hook method); and the hook is just that: it adds a hook to all controllers (or rather, all controllers that extend ApplicationController) to allow the user to introduce the real "Awesome" code into their controllers without otherwise affecting controllers that don't need it.

The #acts_as_awesome hook doesn't, in itself, convey the Awesome functionality. That's because not all controllers might need this functionality. Instead, the method is responsible for introducing the real awesome stuff, via the ActsAsAwesome::InstanceMethods module. This way, the user only gets the Awesome functionality if they explicitly call the acts_as_awesome method. It also adds a before filter to the controller to demonstrate that the code in this method would be evaluated exactly the same as if it were in the target controller class itself.

This technique should work exactly the same if you're targeting models instead of controllers: just inject your hook into ActiveRecord::Base. As AR:B is only loaded at Rails boot, you should probably be able to put that into an initializer (refer to the Railtie docs), but I reserve the right to be mistaken here.

A gotcha concerning the railtie: the documentation reads as if it should have been autodetected, but I often have problems with this. To get around it, simply require the railtie from your gem's main source file (in the above example, that would be lib/acts_as_awesome.rb).

You can see the entire ActsAsAwesome source in all its glory at my github account: http://github.com/sinisterchipmunk/acts_as_awesome

I hope this is helpful. Your question was somewhat high-level so a high-level response is the best I can do.

-Colin MacKenzie IV

http://thoughtsincomputation.com

@sinisterchipmnk

这篇关于rails3 gem:acts_as_something的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆