从ideasfactorychina看到有人说wealink进不去,貌似wealink挂了。登录网站看看,wealink(若邻) 此刻确实挂了,现在时间是2007年3月29日23:50.
Archive for 三月, 2007
我想找到一个类似acts_as_commentable和acts_as_rateable的plugins,给任何一个object统计阅读次数,结果没有找到。于是开始动手写一个acts_as_countable,由于编写时间较短,该插件虽然实现了counter的功能,但效率低下,而且在page_cache时无法使用,以下是公开的源代码,有需要的朋友可以加以改进。
一、Create Table
CREATE TABLE `counts` (
`id` int(11) NOT NULL auto_increment,
`count` int(11) default ‘1′,
`created_at` datetime NOT NULL,
`countable_id` int(11) NOT NULL default ‘0′,
`countable_type` varchar(15) NOT NULL default ‘’,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
二、Create plugin
ruby script/generate plugin acts_as_countable
三、Open /vendor/plugins/act_as_countable/lib/act_as_countable.rb and put this code inside:
# ActsAsCountable
module Jesse
module Acts #:nodoc:
module Countable #:nodoc:def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def acts_as_countable
has_many :counts, :as => :countable, :dependent => :destroy,rder => ‘created_at DESC’
include Jesse::Acts::Countable::InstanceMethods
extend Jesse::Acts::Countable::SingletonMethods
end
end
module SingletonMethods
def find_counts_for(obj)
countable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
Count.find(:first,
:conditions => [”countable_id = ? and countable_type = ?”, obj.id, countable])
end
end
module InstanceMethods
def counter
Count.find(:first,
:conditions => [”countable_id = ? and countable_type = ?”, id, self.type.name])
end
# Helper method that defaults the submitted time.
def add_count(count)
counter = Count.find(:first, :conditions => [”countable_id = ? and countable_type = ?”, count.countable_id, self.type.name ])
if counter.nil?
counts << count
else
counter.update_attribute(:count, counter.count + 1)
end
end
endend
end
end
四、Create /vendor/plugins/act_as_countable/lib/count.rb and put this code inside:
class Count < ActiveRecord::Base
belongs_to :countable, :polymorphic => truedef self.find_counts_for_countable(countable_str, countable_id)
find(:first,
:conditions => [”countable_type = ? and countable_id = ?”, countable_str, countable_id]
)
enddef self.find_countable(countable_str, countable_id)
countable_str.constantize.find(countable_id)
end
end
五、put this code inside our /vendor/plugins/act_as_countable/init.rb
require ‘acts_as_countable’
ActiveRecord::Base.send(:include, Jesse::Acts::Countable)
六、How to Use
1. put acts_as_countable inside model, like acts_as_commentable
2. in view action:
count = Count.new
count.countable_id = @obj.id
@obj.add_count(count)
Resources:



我恨透了Windows Live Mail的垃圾,几乎天天被骚扰,我每次都很认真的帮助他们报告垃圾,但从来不能看到任何改进。如果不是因为MSN上的朋友,真想删除了我的Hotmail邮件帐号。在垃圾制造者面前,微软帝国显得如此可怜。
Ruby 1.8.6 Release了,想看详细情形,或是下载 Source 请看这里。根据 fixneo 先生的提醒,我花了时间去找了一下 Bug Report ,发现到这篇文章。里面写到 Time.to_date 跟 MD5.new 会出现问题。不过这些 Bug 会在 Rails 1.2.3 做修正,也就是应该是 Rails 的问题?
隔天早上,Rails Team 也立刻发表了 Rails 1.2.3,所以要升级 Ruby 1.8.6也请顺便升级 Rails 1.2.3。
为什么我得升级 Ruby 1.8.6?会跟 Rails 冲突,还得一定要升到 Rails 1.2.3 才能够跑 Rails。那么麻烦的 Ruby 版本,为啥我一定要升级?我给你一个原因
Ruby 1.8.6 已经将 FastThread包进去了
也就是说灌 Mongrel 应该不用加装 FastThread 了。我们可以合理推测 Ruby Native 支持总比 Gem 支持来的好不是吗 :p
还有一点,随着 Ruby 1.8.6 的推出,看来下一版的 Mongrel 除了 fastThread,也不用装 cgi_multipart_eof_fix 了。 Ruby 的整合度越高,狗皮膏药越来越少总是一件好事。






