#33 Making a Plugin
引用
注意 这种手法,可能对 rails 4 不再起作用
rails4 http://guides.rubyonrails.org/plugins.html
关于废弃插件的说明 https://github.com/rails/rails/commit/dad7fdc5734a3813246f238ac5760b2076932216
rails 3及以下
After loading the framework and any gems and plugins in your application, Rails turns to loading initializers. An initializer is any Ruby file stored under +config/initializers+ in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks, plugins and gems are loaded, such as options to configure settings for these parts.
rails4
After loading the framework and any gems in your application, Rails turns to loading initializers. An initializer is any Ruby file stored under +config/initializers+ in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and gems are loaded, such as options to configure settings for these parts.
rails4 不再有 vendor/plugins
Rails 4 将删除 Rails::Plugins 类,所以将不会再加载 vender/plugins 目录下的任何代码。
大多数应用应该依赖于 gems 而不是插件。但如果你在 vender/plugins 中还有一些代码,你有两种选择:
改用 gem 方式实现,多数插件已经有了 gem 版本,如果没有你可以在 Gemfile 中通过 :gitor:pathoptions 来引用插件
移到 lib/your\_pluginand ,然后在 conconfig/initializers 初始化
rails4 http://guides.rubyonrails.org/plugins.html
关于废弃插件的说明 https://github.com/rails/rails/commit/dad7fdc5734a3813246f238ac5760b2076932216
rails 3及以下
After loading the framework and any gems and plugins in your application, Rails turns to loading initializers. An initializer is any Ruby file stored under +config/initializers+ in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks, plugins and gems are loaded, such as options to configure settings for these parts.
rails4
After loading the framework and any gems in your application, Rails turns to loading initializers. An initializer is any Ruby file stored under +config/initializers+ in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and gems are loaded, such as options to configure settings for these parts.
rails4 不再有 vendor/plugins
Rails 4 将删除 Rails::Plugins 类,所以将不会再加载 vender/plugins 目录下的任何代码。
大多数应用应该依赖于 gems 而不是插件。但如果你在 vender/plugins 中还有一些代码,你有两种选择:
改用 gem 方式实现,多数插件已经有了 gem 版本,如果没有你可以在 Gemfile 中通过 :gitor:pathoptions 来引用插件
移到 lib/your\_pluginand ,然后在 conconfig/initializers 初始化

简介:将get/set等做成一个plugin 减少重复 You can sometimes remove a lot of duplication by generating methods dynamic. In this episode I will show you how to create a plugin which does exactly that. 目的: class Task < ActiveRecord::Base belongs_to :project def due_at_string due_at.to_s(:db) end def due_at_string=(due_at_str) self.due_at = Time.parse(due_at_str) rescue ArgumentError @due_at_invalid = true end end 将以上代码简化成 class Task < ActiveRecord::Base stringify_time :due_at end 手法: 1、script/generate plugin stringify_time 2、init.rb require 'stringify_time' class ActiveRecord::Base extend StringifyTime end 3、stringify_time.rb module StringifyTime def stringify_time(*names) names.each do |name| define_method "#{name}_string" do read_attribute(name).to_s(:db) end define_method "#{name}_string=" do |time_str| begin write_attribute(name, Time.parse(time_str)) rescue ArgumentError instance_variable_set("@#{name}_invalid", true) end end define_method "#{name}_is_invalid?" do return instance_variable_get("@#{name}_invalid") end end end end 4、 最终,我们的task model可以这样写 class Task < ActiveRecord::Base belongs_to :project stringify_time :due_at def validate errors.add(:due_at, "is invalid") if @due_at_invalid end end
发表评论
-
rails 小技巧
2014-02-23 22:55 629#Rails Tip 1 Actions Are Method ... -
ruby 小技巧
2014-02-23 21:18 987#Track 1: The C in MVC #irb Mi ... -
rails 小代码合集 view controller model
2014-02-23 13:18 1641Rails Create an image with link ... -
rails bash
2014-02-22 21:48 6261、一个完整的rails app自动生成bash 引用#!/ ... -
实用工具--rails 命令、generator
2014-02-22 20:52 10221、rails 基本 rails new rails-boo ... -
rails 版本 更新/升级 release note
2014-02-22 14:02 593升级到 Rails 4,你的应用需要准备什么? 升 ... -
日期 、路由辅助方法
2014-02-22 11:48 578#31 Formatting Time 方法一: Task ... -
rails 调试
2014-02-23 22:57 575#24 The Stack Trace A plugin c ... -
Authentication 用户登录 用户权限相关
2014-02-21 21:20 680引用 # 19Where Administration Goe ... -
ActiveRecord
2014-02-21 20:39 1079ActiveRecord 4新特性 http://www.os ... -
工作相关
2014-02-21 20:27 550# 工作经历: 2年制造业ERP开发及管理经验 2年旅游信息化 ... -
rails 开发工具相关
2014-02-21 20:14 487#使用TextMate进行Ruby On Rails开发推荐插 ... -
rails view session layout
2014-02-21 19:00 681#208 erb-blocks 简介:在erb中使用block ...
相关推荐
Rails 2.0 API 文档是一个非常宝贵的资源,它为开发者提供了全面的指南,以便于在使用Ruby on Rails 2.0版本时更好地理解和利用其框架功能。Ruby on Rails(简称Rails)是一个开源的Web应用框架,它遵循MVC(模型-...
综上所述,《RAILS2.1的中文版资料》为Ruby on Rails开发者提供了丰富的资源和深入的技术细节,不仅有助于开发者更好地理解2.1版本的新特性,还能帮助他们利用这些特性构建更高效、更稳定的Web应用程序。
- `rake rails:unfreeze`: 解锁应用程序,使其不再使用冻结的 gems 或 Edge Rails,返回到系统 gems 的流动使用状态。 - **更新 Rails**: - `rake rails:update`: 更新应用程序中的 scripts 和 public/...
5. **Plugins**:类似现在的 gems,允许扩展Rails功能。 6. ** erb** 视图模板引擎,用Ruby代码嵌入HTML中。 深入研究Rails 2的源码,我们可以了解到早期版本的代码组织方式、设计模式以及与后来版本的差异。例如,...
本文将详细介绍一些常用的Rails命令及其用途,帮助开发者更好地理解和掌握Rails的使用方法。 #### 1. 创建一个Rails应用程序 创建一个新的Rails应用程序是开发流程的第一步。可以通过以下命令来完成这一操作: ``...
1)做一个项目,广泛替代的app-name 2)做一个项目,广泛取代的AppName确保Ruby / Rails是最新的 # rbenv and ruby-build are installed from git, not from brewcd ~ /.rbenv && git pullcd ~ /.rbenv/plugins/...
在`Available Plugins`选项卡中找到`Ruby and Rails`,并勾选它来安装Ruby支持。 2. **安装Ruby插件**: - 可选地,你可以勾选`JRuby and Rails Distribution`以安装JRuby和Ruby on Rails框架。 - `Ruby ...