Simple SVNHelper for Rails Applications

原创:黄文斌
来自:http://github.com/kudelabs/SVNHelper/tree/master 这个小程序是专门写给使用SVN版本控制的Rails应用程序。
有了它,我们可以在某个特定的网页上面直接看到我们的程序revision版本,还有最后的修改日期。
它的实现原理很简单,就是查看.svn/entries文件里面的修改记录。
一般用法,把这个文件复制到root/lib目录下面,然后在environment.rb里面用全局变量记录它:
$current_version = SVNHelper.version(RAILS_ROOT)
这样子,你可以在erb页面里面调用这个变量,比如:
程序版本: $current_version.rev_with_date
同时,它会在每次服务器重启的时候重新记录最新修改的svn版本。
This little library is specialized for those Rails Applications which are using SVN as version control. It allows us to view the revision and the last modified date of our application on the website. It’s rather simple to implement such function, we just look into‘.svn/enries’file in RAILS_ROOT and record the things we want inside.
How to use it?
Please copy the file into root/lib, and set up a global variable in config/environment.rb:
 $current_version = SVNHelper.version(RAILS_ROOT) 
And then you can use this variable everywhere you wish to see the revision and last modified date. Also, its value may be changed each time you restart the server if any changes to the SVN found. If you are interested in this, you can see the source code from here: http://github.com/kudelabs/SVNHelper/tree/master

Posted by Mysen Sun, 12 Jul 2009 04:05:00 GMT


来自asciicasts.com文本形式的Rails动态短片

这对于我们早期贴出的Rails动态短片是一个很好的更新。

来自英国的Rails程序员Eifion Bedford正在收集Ryan Bates系列的文本形式的免费动态短片。这意味着他是精心抄写Ryan的著作,整理截图,您可以学习这些代码示例并复制和粘贴它们。这是一个很好的学习这些材料的途径,尤其对于非英语母语的人来说。他所称的ASCII Casts,是一个巨大的免费学习Ruby and Rails的额外材料。我推荐安全系列性能提示。感谢Eifon努力为我们描绘生动的网页开发世界。

Posted by 钟宇平 Wed, 25 Feb 2009 16:11:00 GMT


Rails 2.x Named Scope

我在实际项目中发现 named_scope 的好处,下面翻译了 What’s New in Edge Rails: Has Finder Functionality 这篇文章,同时结合自己使用的心得与 Rails 爱好者们共享。

原文

Rails 2.x 整合了 Nick Kallen 的 has_finder plugin,改名为 named_scope。首先,在 User Model 中定义 named_scope:

class User < ActiveRecord::Base
  named_scope :active, :conditions => {:active => true}
  named_scope :inactive, :conditions => {:active => false}
  named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
end

标准的使用:

User.active    # 相当于 User.find(:all, :conditions => {:active => true})
User.inactive # 相当于 User.find(:all, :conditions => {:active => false})
User.recent   # 相当于 User.find(:all, :conditions => ['created_at > ?', 1.week.ago])
不同的 named_scope 之间可以嵌套使用:
User.active.recent
这相当于下面的用法:
  # User.with_scope(:conditions => {:active => true}) do
  #   User.find(:all, :conditions => ['created_at > ?', 1.week.ago])
  # end

高级使用:

named_scope 允许在执行时,传递参数来定义查询条件。
class User < ActiveRecord::Base
  named_scope :registered, lambda { |time_ago| { :conditions => ['created_at > ?', time_ago] }
end

User.registered 7.days.ago # 相当于 User.find(:all, :conditions => [‘created_at > ?’, 7.days.ago])

named_scope 扩展

与 association extensions 相似, named_scope 也可以扩展。

class User < ActiveRecord::Base
  named_scope :inactive, :conditions => {:active => false} do
    def activate
      each { |i| i.update_attribute(:active, true) }
    end
  end
end
# 重新激活所有未激活用户 
User.inactive.activate

匿名 Scopes

通过对类对象 class objects 使用 scoped 方法,你可以快速地建立 scope。

# 定义 named scopes
active = User.scoped(:conditions => {:active => true})
recent = User.scoped(:conditions => ['created_at > ?', 7.days.ago])

# 嵌套使用
recent_active = recent.active

# 对返回的对象进行操作
recent_active.each { |u| ... }

=============================================================================

单纯的 scope ,例如 User.active, User.inactive 等只是提供简单的快捷方法,并不具备强大的功能特性。我们在实际项目中,很多时候是得益于 scope 的嵌套使用。我认为嵌套使用主要有五个好处:

1. scope 的嵌套使用,能够多层次(只要符合逻辑,几乎为无限)的组合查询条件。例如User.active.recent, User.active.recent.female, User.active.recent.female.adult……

2. 能够与 association 结合使用,限定外部 scope。举个例子,假如 User Belongs To Club,你可以将查询限制到具体一个 Club 的范围,如 Club.first.users.active.recent。

3. 用面向对象的方式,使得复杂查询在一个 SQL 语句中完成,减少编写复杂 SQL 语句的次数。例如 User.active.recent.female.adult,该查询只会触发一次 SQL 语句,而且非常简洁。在过去我们只能通过类似下面的语句来完成该查询: User.find(:all, :conditions => [“active = true AND created_at > ? AND gender = ‘female’ AND age > 25”, 1.week.ago]。

4. 符合 DRY 精神,精心定义的 scope,可以避免很多重复的 SQL conditions,其复用性相当高!

5. scope 返回的对象仍然具备所有 SQL 查询功能。例如,User.active.rencet.find_by_first_name(“Jim”),或User.active.recent.find_all_by_last_name(“Green”),同样可以工作,而且仍然只触发一次 SQL 查询。

Named scope 需要注意的问题:

1. 嵌套 scope 只能提供交集查询,如果要查询多个 scope 的并集,只能通过数组的 + 操作。例如,User.active + User.recent。但是这样做的一个问题是返回的结果为普通的数组,我们无法再使用上述第5个好处了。针对该问题,我们的处理方法是在必要时候,定义更灵活的 scope。例如,使用 scope 的参数传递功能:


named_scope :active_or_recent, lambda { |args*|
  time_ago = args.first

  if time_ago
    { :conditions => [‘active = true OR created_at > ?’, time_ago] }
  else
    { :conditions => [‘active = true’] }
  end
}

这样,User.active_or_recent(7.days.ago) 使用的条件为{ :conditions => [‘active = true OR created_at > ?’, time_ago] },而 User.active_or_recent 使用的条件为{ :conditions => [‘active = true’] }。

2. 突然忘了,请等待更新:)

Posted by Shaokun Thu, 18 Sep 2008 20:46:00 GMT


railscasts(Rails短片)帮助你成为ruby高手

click here to read the English vesion of this article
点击这里浏览本篇文章的英文版

随着Rails越来越受欢迎,用于学习Rails的资源也越来越多。其中最好的一种学习方式就是看那些关于Ruby和Rails的视频短片(ScreenCasts)。这些短片让你在听讲授者解释概念的同时可以看到相应的代码。通常,这些短片对应一些经典的教材,但是通过这种视频短片的方式,这些内容变得更容易理解,你可以更加直观的感受到那些枯燥的概念背后的真正的含义。

首先,有一些优秀而且免费的由Ryan Bates讲授的关于Rails的短片可以从这里下载:railscasts.com。他们短小精悍,常常重点突出某一个新的特征。你可以用iTunes把这些短片自动下载到你的电脑里(但是这在中国大陆行不通,因为Feedburner被拦截了),或者你可以直接从网站下载。这些短片里面有一组是专门讲述Rails 2.1 的新特征的,很值得一看。

其次, Screencast之父, @topfunky a.k.a Geoffrey Grosenbach 也已经整理好了一组出色的关于Rails的培训教材,并把它们放在了peepcode.com上。这些教材是收费的,但是并不算贵,每个视频只要9美元即可(考虑到美国的消费水平,的确是相当便宜了)。这些教材很优秀,含金量很高,物超所值。我们已经学习过这些短片,它们帮助我们跟上了新版本rails发展的速度,此外我们也从那里下载了一些关于Rails的PDF书籍。我们极力推荐从这些教材入手学习Rails。

Rails运行在Ruby环境,我也找到了一些很好的关于如何使用Ruby编程的视频短片(ScreenCasts)。Dave Thomas of The Pragmatic Programmers(也就是Web开发敏捷之道(Agile Web Development with Rails)的作者)亲自在短片中讲授了Ruby对象模型,类继承和元编程(meta-programming)的基本概念。它们能够帮助你充分发挥Ruby的强大威力,并帮你建立理解Rails框架的基础知识背景。这些视频短片(ScreenCast)只要5美元一个(别误会,我可不是他们的推销员,如果他们付给我报酬的话,我倒是很乐意帮他们推销一下)。

如果你真的对Rails和Ruby感兴趣,我强烈推荐你使用以上这些网络上的学习资源,当然你要需要下载相关的工具,并准备好开始写你自己的Rails代码。

尽情享受Rails把,未来的Ruby黑客们!

Posted by 张晓峰 Mon, 16 Jun 2008 20:10:00 GMT


Javascript 通用筛选功能

如何对 HTML 上的元素,如表格,列表或者 DIV 等等按照关键字进行筛选呢?最简单的方法是通过循环遍历各个元素,对比关键字与元素的 inner html 内容。但是,这个方法有一个很大的缺陷。例如,如果对跨行的表格 tr 进行筛选呢?在显示上,跨行的 tr 与其他 tr 显示在同一行,但是它们的 inner html 往往却不同。而代码上,它们又在不同的 tr 上。所以,隐藏了跨行的 tr 会破坏整个表格的布局。对于这个问题,我们的解决方案是对任何可筛选的元素添加匹配字符串变量,这样,筛选功能就变得更加通用和灵活了。因为,就算 tr 的内容不一样,我们仍然可以在代码中设置相同的匹配字符串变量,使其在筛选中被同样处理,显示或者隐藏。下面我们简单介绍筛选功能的 javascript:

示范例子


<table>

<thead>
  <tr>
    <th>菜系</th>
    <th>菜名</th>
    <th>价钱</th>
  </tr>
</thead>

<tbody id="food">
  <tr id="tr_01"><td rowspan="3">粤菜</td><td>烧汁茄子</td><td>10.00</td></tr>
  <tr id="tr_02"><td>烧鸭</td><td>20.00</td></tr>
  <tr id="tr_03"><td>西芹炒百合</td><td>15.00</td></tr>

  <tr id="tr_04"><td rowspan="2">东北菜</td><td>酱骨架</td><td>35.00</td></tr>
  <tr id="tr_05"><td>火龙鱼</td><td>70.00</td></tr>

  <tr id="tr_06"><td rowspan="x">湘菜</td><td>xx</td><td>xx.xx</td></tr>
  ...

  <tr id="tr_0x"><td rowspan="x">潮菜</td><td>xx</td><td>xx.xx</td></tr>
  ...

  <tr id="tr_0x"><td rowspan="x">越菜</td><td>xx</td><td>xx.xx</td></tr>
  ...

  <tr id="tr_0x"><td rowspan="x">寿司</td><td>xx</td><td>xx.xx</td></tr>
  ...

  <tr id="tr_0x"><td rowspan="x">清真</td><td>xx</td><td>xx.xx</td></tr>
  ...
</tbody>

</table>

function strip_spaces(stripee) {  
    return stripee.replace(/(^[ ]*) | ([ ]*$)/g,'');
}

function filter(id, criteria) {
    var t = $(id);
    if (!t) { return; }

    t.getElementsByClassName('no_matched_notice').each(function(elem) {
        elem.remove();
    });

    var notice_msg = 'No matching data found!';
    criteria = strip_spaces(criteria);

    empty_input = criteria.length === 0;

    var matched_none = true;
    t.childElements().each(function(elem) {
        // if the user doesn't input anything, then show all the elements
        if (!elem.matched_strs || empty_input) {
            Element.show(elem);
        } else {
        // else find the element
            matched = false;

            elem.matched_strs.each(function(str) {            
                if (str.toUpperCase().indexOf(criteria.toUpperCase()) != -1) {
                    matched = true;
                    matched_none = false;
                    return;
                }
            });

            matched ? Element.show(elem) : Element.hide(elem);
        }
    });

    if (matched_none && !empty_input) {
        var tag = t.tagName;
        var ins = null;
        switch (tag) {
            case "TBODY":
            case "TABLE":
                ins = new Insertion.Bottom(t, "<tr class=\"no_matched_notice\"><td colspan='100'>" + notice_msg + "</td></tr>");
                break;
            case "OL":
            case "UL":
                ins = new Insertion.Bottom(t, "<li class=\"no_matched_notice\">" + notice_msg + "</li>");
                break;
            default:
                ins = new Insertion.Bottom(t, "<" + tag + " class=\"no_matched_notice\">" + notice_msg + "</" + tag + ">");
                break;
        }
    }
}

HTML中,我们需要做的工作是:

1) 添加匹配字符串到所有可匹配元素上,为了能够匹配多个关键字,将以数组的形式存储:
$('tr_01').matched_strs = ["粤菜", "烧汁茄子", "烧鸭", "西芹炒百合"];
$('tr_02').matched_strs = ["粤菜", "烧汁茄子", "烧鸭", "西芹炒百合"];
$('tr_03').matched_strs = ["粤菜", "烧汁茄子", "烧鸭", "西芹炒百合"];

$('tr_04').matched_strs = ["东北菜", "酱骨架", "火龙鱼"];
$('tr_05').matched_strs = ["东北菜", "酱骨架", "火龙鱼"];
...

2) 添加筛选关键字的输入框:
<input id="keyword" type="text" />
3) 绑定 filter 函数到输入框事件上:

$('keyword').onkeyup = function() {filter('food', this.value);};
$('keyword').onchange = function() {filter('food', this.value);};

恭喜你,完成了!

Posted by Shaokun Tue, 04 Dec 2007 16:33:00 GMT