晒晒我们的开源项目 ITEYE开源代码

我们的研发团队是一支mini型研发团队,目前共有研发人员13人。由于网站产品维护的历史原因,这13人的研发团队分为4支小组,分别是:Ruby研发小组5人;PHP研发小组4人;.net研发小组2人,Java搜索小组2人。

别看我们研发人员这么少,但是我们研发人员战斗力很强,我们维护和开发着十多条产品线。在我们开发自身产品的过程中,也积累了一些比较通用的组件,一些对大家来说有用的工具。因此从今年下半年开始,我们陆续将一些组件和工具开源出来,哪怕这些项目的质量并不是那么高,也希望能够给社区带来一点点自己的贡献。

以下简要介绍一下我们已经开源的项目,今后我们还会努力开源更多的东西和大家交流和分享:

1、Ansj中文分词 – 开源的高准确率Java中文分词器

项目Github地址:https://github.com/ansjsun/ansj_seg
这是基于大名鼎鼎中科院的Ictclas中文分词算法编写的Java实现版本,比常用的开源mmseg4j的分词准确率高。目前我们自己站内的搜索将逐渐从mmseg4j算法替换成Ansj中文分词算法。

2、ServiceFramework – 开源的羽量级Java Web服务框架

项目Github地址:https://github.com/allwefantasy/ServiceFramework
我们基于Java的开源搜索框架lucene编写了网站的分布式搜索和Tag文章分类服务。因为需要向前端的Web应用程序提供搜索和Tag服务接口API,所以我们编写了这个羽量级的Java框架软件。它的优点就是羽量级,自身集成了Jetty服务器,MVC,IoC和ORM,让你只需要编写非常少的代码,就可以快速将你的业务逻辑组件以Web API的方式提供服务。

3、ExportBlog – 开源的通用博客导出工具

项目Github地址:https://github.com/sqzhuyi/ExportBlog
这是一个基于.net Winforms编写的通用博客导出工具,支持导出网站包括:CSDN、ITEYE、博客园、新浪、搜狐、和讯、ChinaUnix、网易、51CTO、开源中国、百度空间、QQ空间等等。导出格式支持CHM、PDF、HTML、TXT和EPUB 5种格式文档。详细介绍:http://blog.csdn.net/sq_zhuyi/article/details/7924776

4、Secode_level_cache – 开源的Rails对象缓存插件

项目Github地址:https://github.com/csdn-dev/second_level_cache
Rails的ActiveRecord自身没有带强大的对象缓存功能,这是AR的一个重大的遗憾。早在2008年开始,我们就借鉴了Java强大的ORM框架Hibernate的二级对象缓存编写了这个Rails的AR对象缓存插件,并且一直作为JavaEye网站缓存优化的秘密武器来使用,取得了非常理想的效果。
现在我们将这个插件从Rails2.x的版本升级到了3.x版本,并且抽取成了一个通用插件,开始应用于新的Rails3.2的项目之上。有志于AR对象缓存优化的ruby程序员不容错过。

5、limiter - 网站反爬虫和DOS攻击的利器

项目Github地址:https://github.com/csdn-dev/limiter
早年的JavaEye网站曾经深受DOS攻击和爬虫海量抓取造成的负载过高的困扰,我曾经和这个问题进行了为期几年不懈的斗争,并且在总结几年斗争经验后写了一篇总结性博客文章:互联网网站的反爬虫策略浅析 。当时我基于这个反爬虫策略编写了JavaEye网站的智能防火墙插件,取得了良好的效果。
现在我们将这个插件从JavaEye的源代码中剥离出来,抽取成一个通用的rackware,便于应用于普通的Rails3.x的项目当中。

Posted in Uncategorized

Single sign-on 单点登录 sso

from wiki

Single sign-on (SSO) is a property of access control of multiple related, but independent software systems. With this property a user logs in once and gains access to all systems without being prompted to log in again at each of them. Conversely, Single sign-off is the property whereby a single action of signing out terminates access to multiple software systems.

As different applications and resources support different authentication mechanisms, single sign-on has to internally translate to and store different credentials compared to what is used for initial authentication.

from baike

单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。

企业应用集成(EAI, Enterprise Application Integration)。企业应用集成可以在不同层面上进行:例如在数据存储层面上的“数据大集中”,在传输层面上的“通用数据交换平台”,在应用层面上的“业务流程整合”,和用户界面上的“通用企业门户”等等。事实上,还有一个层面上的集成变得越来越重要,那就是“身份认证”的整合,也就是“单点登录”。

现在普遍使用 oauth 来实现 多个系统的授权 认证

https://github.com/songkick/oauth2-provider

Posted in Uncategorized

Sharing A Devise User Session Across Subdomains With Rails 3

to andersen
  仔细测了一下 Askjane::Application.config.session_store :active_record_store, key: '_askjane_session', :domain => ".bbtang.com"在 server上
  是ok的(效果上也是能共享bbtang.com 和www.bbtang.com的会话的只要端口一致)就是不知道在本地如何因为本地一般不会设置host绑定域名(经测试答案是不能的),如何(所以需要:all这个设置项)
to 客服 和 jojo
 提一点 你们一些 首页源码的时候 不要把 域名加上去  注意 尼玛 端口不一致 也是不能share 会话的
 简而言之 你们写一些html静态源码的时候除非不在 bbtang.com的 项目上 没有必要加 域名的 这会加大 大家的成本(如果说端口不一致就会导致会话丢失,如果没有做多域名兼容也会导致会话丢失能免则免)
over

Francis.J(864248765) 13:57:28

https://github.com/rails/rails/issues/2483

Francis.J(864248765) 13:57:55
尼玛 rails 大爷又 踩雷了
Francis.J(864248765) 13:59:42

https://github.com/rails/rails/issues/2483

Francis.J(864248765) 14:02:27

https://github.com/rails/rails/pull/7316

Francis.J(864248765) 14:08:37
然后 我们看看 究竟用 :cookie_store + :domain => :all
还是  :active_record_store + ".bbtang.com" (这里垮子域名估计不会很合适)
还是 升一下 rails  用  :active_record_store + :domain => :all

Recently I’ve been working on a Rails application that supports subdomains. I’m using Devise for user authentication and need the user to choose a subdomain to use upon registration.

Similar to the 37signals applications, I want a single sign-on to be persistent across subdomains. Since I didn’t have a clue where to begin with subdomains, I followed this tutorial on my new Rails 3.1 beta 1 application. This tutorial worked like a charm and I omitted the friendly_id and tweaked a few things to my liking.

The gist of it is simple. Create a User model like you would normally do with Devise. You add a Subdomain model that is linked to the Users (in my case I only wanted a single subdomain per user). Configuring the routes is pretty simple as you can simply create a constraint that will match the root and fire it off to the right action and let the rest fall through.

The trick comes into sharing the session between domains. Browsers, of course, will separate out the cookies and store them by separated out by subdomain. What you want to do is edit your config/initializers/session_store.rb file to look like this

APPNAMEGOESHERE::Application.config.session_store :cookie_store, :key => '_tourlyapp_session', :domain => "lvh.me"

The trick here is the :domain option. What this does is sets the level of the TLD (top level domain) and tells Rails how long the domain is. The part you want to watch out for here is that if you set :domain => :all like is recommend in some places, it simply won’t work unless you’re using localhost. :all defaults to a TLD length of 1, which means if you’re testing with Pow (myapp.dev) it won’t work either because that is a TLD of length 2.

You might get weird things like halfway Devise sessions sharing, but only allowing you to create and destroy the session on the root domain. Using :all works great if you’re using localhost, but when I started using lvh.me:3000 for testing I had those problems (lvh.me stands for local vhost me and is a domain that simply points to localhost which makes for zero-config subdomain development. It’s super handy.).

The best option might be to comment out this line and put it into your individual environment configurations. This way you can keep things configured easily as the :all option. Once you’ve got your domain string added everything should work like a charm.

BONUS PROTIP: The normal route variables you see used end with _path. These don’t include the domain and therefore ignore the :subdomain option you pass into them. url_for, on the other hand, does support subdomains so you should get into the habit of using root_url instead of root_path and so on.

Posted in Uncategorized

IE, iframe, P3P, Cookies, oh my

测试新浪微游戏接口时,发现一个问题:当使用IE浏览器的时候,rails的session无法保存。之前在自己开发服务器上测试时,验证过IE和Firefox都能正常使用session的,觉得很是奇怪。

通 过抓包发现,正常情况下,服务器端在响应客户端访问请求后,在返回的http头中会有Set-Cookies这样的参数,同时在接下来的客户端的http 请求头中,会加上Cookie这样的参数;上述不能正常保存session情况下的抓包分析发现,客户端的http请求头中浏览器没有设置Cookie参 数。

解决方法一:修改IE的默认Cookie设置,设置IE隐私设置中的高级隐私设置,勾选“总是允许回话Cookie”,这样session的值就能正常保存了。不过这样肯定不是最好的解决办法,对大多数用户来说,这样做不合理。

解决方法二:参考方法来自: http://www.sympact.net/2008/07/rails-and-ifram.html ,文中描述了具体原因,是因为IFrame中打开的链接和主页面的链接不在同一个域,所以IE默认会认为是不可信任的,则不允许使用Cookie。解决办 法正如文中所示,在controller中的before_filter中增加一个方法,此方法中设置响应的http相应头中增加P3P参数,问题即可解 决。
大致代码如下:


[rails IE, IFRAME, P3P, COOKIES](https://gist.github.com/3719568)
#userful gem [rack-p3p](https://github.com/hoopla/rack-p3p)
# encoding: utf-8
class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :set_p3p
  def set_p3p
    headers['P3P'] = "policyref="/w3c/p3p.xml", CP="ALL DSP COR CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV INT""
  end
end

I was just banging my head against the wall trying to figure out why internet explorer wasn’t remembering my user’s sessions. Turns out it’s something that has bitten me in the past.

IE doesn’t allow you to set cookies when your site is in an iframe unless your site has set P3P headers. Also, ordering matters – the P3P header must be set *before* the cookie is set.

If you’re using ruby, this gem works pretty well: https://github.com/hoopla/rack-p3p

Further reading: http://stackoverflow.com/questions/389456/cookie-blocked-not-saved-in-iframe-in-internet-explorer

All the articles I read about setting headers, etags, etc were all really old. Hopefully, if you’re using rails you found this article. Just install the gem and add the line from the README to your application.rb – no monkey patching. Good luck.

rails IE frame ActionController::InvalidAuthenticityToken

Same problem here with a rails application launched in an iframe I get:

“the change you wanted was rejected”

In log:

ActionController::InvalidAuthenticityToken

Seems that the problem occur in IE when you are developing in an iframe situation where the master page is at a different domain than the inner page. (es: iframed Facebook applications)

This is because IE’s default “medium” privacy setting has an issue with cookies in that situation.

A possible solution is to set a P3P header (try to google: p3p iframe internet explorer) Example, in application_controller.rb:

<code>before_filter  <wbr>:set_p3p

def set_p3p
 <wbr> response.headers["P3P"]='CP="CAO PSA OUR"'
end</wbr></wbr></code>

 

php版站内应用在ie浏览器下获取到session值(失效)[已解决]

php版站内应用在ie浏览器下获取到session值(失效)
首页授权证后保存的session在其它页面获取不到。例如:
在站内应用的iframe下:
a.php
<?php
session_start();
$_SESSION["user"]=”abc”;
echo $_SESSION["user"];
?>
<a href=”b.php”>b.php</a>

b.php
<?php
session_start();
echo $_SESSION["user"];
?>

运行a.php后正常显示abc,跳转到b.php显示为空。

这问题纠缠了我很久了,不继地调试,直到今晚终于找到答案了:
原来这也属于是跨域访问的问题。

以下是解决方案:
“用P3P header解决iframe跨域访问cookie/session”的问题
理论很简单,而且模式也和大多请求返回状态的SSO差不多.但是有几个地方是要注意一下的.
1.页面里的COOKIE不能是浏览器进程的COOKIE(包括验证票和不设置超时时间的COOKIE),否则跨域会取不到.这点做跨域COOKIE的人比较少提到.不过实际上留意下几家大学做的方案,有细微的提到他们的验证模块里的COOKIE是有设置超时时间的.
2.当利用IFRAME时,记得要在相应的动态页的页头添加一下P3P的信息,否则IE会自觉的把IFRAME框里的COOKIE给阻止掉,产生问题.本身不保存自然就取不到了.这个其实是FRAMESET和COOKIE的问题,用FRAME或者IFRAME都会遇到.
3.测试时输出TRACE,会减少很多测试的工作量.
只需要设置 P3P HTTP Header,在隐含 iframe 里面跨域设置 cookie 就可以成功。他们所用的内容是:
P3P: CP=’CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR’

ASP直接在头部加了头部申明,测试有效。
<%Response.AddHeader “P3P”, “CP=CAO PSA OUR”%>

php的话,我没去试,应该是如下写法:
header(‘P3P: CP=CAO PSA OUR’);

ASP.NET的话
通过在代码上加Response.AddHeader(“P3P”, “CP=CAO PSA OUR”)或者在Window服务中将ASP.NET State Service 启动。
JSP:response.setHeader(“P3P”,”CP=CAO PSA OUR”)

如何在静态页面加头信息 P3P: CP=”CAO PSA OUR 来解决框架与cookie的问题?
IIS站点管理器允许你给所有输出的内容都加上任意HTTP Header,你只要在Custom Header里面加上P3P: CP=”CAO PSA OUR”
就ok了

Posted in Uncategorized

在 Cloud Foundry 上使用 JRuby for Rails 应用程序

[译注]本文翻译自Cloud Foundry英文博客站点,原文题为“Using JRuby for Rails Applications on Cloud Foundry”,文章发表时间是 2012 年 4 月 19 日。

如今,只需进行一些简单的配置更改,即可将 JRuby Rails 应用程序部署到 CloudFoundry.com。JRuby 应用程序常常通过创建一个包含 Rails 应用程序的 .war 文件来部署到 servlet 容器中。对于 Cloud Foundry,我们将采取同样的做法,同时对数据库配置进行一些更改,以便应用程序还可以访问 CloudFoundry.com 上的数据库服务。

将 JRuby on Rails 应用程序部署到 Cloud Foundry 上时需进行的更改

要使 JRuby 应用程序在 CloudFoundry.com 上运行,我们需要完成两项任务。首先,我们需通过修改 configuration 目录中的 database.yml 文件将该应用程序配置为连接到 CloudFoundry.com 上的数据库服务。当我们部署该应用程序时,我们还需要运行“rake db:migrate”的等效命令,以便创建数据库表。我们可以通过在 config/initializers 目录中添加一个初始化程序来做到这一点。

环境变量 VCAP_SERVICES 中提供了我们在配置数据库连接时所需的信息。我们可以通过编程方式分析该变量,也可以使用方便的 Cloud Foundry 运行时 gem(请参见 Cloud Foundry 服务与 Ruby 搭配使用:第 2 部分 –  Ruby 应用程序的实时支持博文),在本篇博文中我们将采用后者。要使用该 gem,我们需要将它包含在我们的 Gemfile 中:

...
gem  'cf-runtime'
...

既然我们已经添加了该 gem,我们就可以向 database.yml 文件中添加一些代码段,用以访问生产环境的数据库服务信息。下面是一个 database.yml 文件中的 production 部分,在这个文件中我们使用的是 MySQL 数据库:

config/database.yml

production:
    adapter: mysql
    <% require 'cfruntime/properties' %>
    <% db_svc = CFRuntime::CloudApp.service_props('mysql') %>
    database: <%= db_svc[:database] rescue 'bookshelf_production' %>
    username: <%= db_svc[:username] rescue 'root' %>
    password: <%= db_svc[:password] rescue '' %>
    host: <%= db_svc[:host] rescue 'localhost' %>
    port: <%= db_svc[:port] rescue '3306' %>

正如您可以看到的那样,我们添加了一个 require 语句来获取 cfruntime/properties,然后我们通过调用 service_props 方法并在调用时传入我们所使用的服务类型来获取服务属性的哈希值。如果仅有一项属于该类型的服务绑定到该应用程序,则无需指定此服务的实际名称。如果您将多项属于同一类型的服务绑定到您的应用程序,您将需要指定实际服务名称。服务属性的哈希值存储在一个名为 db_svc 的变量中,代码会将对应的值提取出来以用作数据库、用户名、密码、主机和端口。其中的每一条语句都有一个 rescue 子句,如果我们并非在 Cloud Foundry 环境中操作,则该语句将提供要使用的值,这种情况下 db_svc 将为 Nil。

另外,如果使用的是 PostgreSQL,则该 database.yml 文件的 production 部分将大致如下:

production:
   adapter: postgresql
   encoding: unicode
   <% require 'cfruntime/properties' %>
   <% db_svc = CFRuntime::CloudApp.service_props('postgresql') %>
   database: <%= db_svc[:database] rescue 'bookshelf_production' %>
   username: <%= db_svc[:username] rescue 'bookshelf' %>
   password: <%= db_svc[:password] rescue '' %>
   host: <%= db_svc[:host] rescue 'localhost' %>
   port: <%= db_svc[:port] rescue '5432' %>

接下来,我们将注意力转向创建供我们的应用程序使用的表上面。为此,我们在部署该应用程序时需向 config/initializers 目录中添加以下初始化程序。我将该初始化程序命名为 cf_db_migrate.rb:

config/initializers/cf_db_migrate.rb

require 'cfruntime/properties'
# Run the equivalent of rake db:migrate on startup
if CFRuntime::CloudApp.running_in_cloud?
  migrations = Rails.root.join('db','migrate')
  if migrations.directory?
    ActiveRecord::Migrator.migrate(migrations)
  end
end

我们再次使用 cfruntime/properties 来检查我们当前是否在云中运行。接下来,我们将检查 db/migrate 目录是否存在;如果它存在,我们将使用该目录中的迁移文件来运行数据库迁移 (ActiveRecord::Migrator.migrate(migrations))。

我们还必须进行的一项更改就是对 warble 配置的更改。默认情况下此配置在生成的 war 文件中不包含 db/migrate 目录,因此我们需要通过指定 config.includes = FileList["db/migrate/*"] 将此目录添加到此配置中。下面是 config/warble.rb 文件的相关内容:

config/warble.rb

# Warbler web application assembly configuration file
Warbler::Config.new do |config|
  # Application directories to be included in the webapp.
  config.dirs = %w(app config lib log vendor tmp)
  # Additional files/directories to include, above those in config.dirs
  config.includes = FileList["db/migrate/*"]
end

一个完整示例

我们从上文中已经了解到需做出哪些更改,那我们就来快速生成一个 Rails 应用程序并做出所需的更改,然后将该应用程序部署到 CloudFoundry.com。如果您尚未安装 JRuby,建议首先参阅 JRuby 入门

创建 JRuby Rails 应用程序

首先,我们创建新应用程序,并创建具有完整基架的第一个域对象。

<code>jruby -S rails new bookshelf -d mysql cd bookshelf jruby -S rails generate scaffold Book title:string category:string published:integer price:decimal{10.2} isbn:string </code>

接下来,我们删除生成的 public/index.html,然后修改 config/routes.rb 以使用“books”作为根目录:

<code>rm public/index.html vi config/routes.rb</code>

下面是我在 config/routes.rb 中添加的路由:

Bookshelf::Application.routes.draw do
  resources :books
  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => 'welcome#index'
  root :to => 'books#index'
  # See how all your routes lay out with "rake routes"
end

现在,我们将在本地运行此应用程序以确保它正常运行:

<code>jruby -S rake db:create jruby -S rake db:migrate jruby -S rails server</code>

Book 实体的 Rails 空列表视图表明它确实正常运行。现在,我可以向我的书架添加新书。

修改 JRuby Rails 应用程序以便部署到 CloudFoundry

我们来首先进行我们在上文中提到的以下更改:

  • 将 gem cf-runtime 添加到 Gemfile
  • 修改 config/database.yml 文件的“production:”节,具体见上文
  • 添加一个名为 config/initializers/cf_db_migrate.rb 的文件,文件内容见上文

接下来我们需要生成 Warbler 配置文件,因此我们将运行:

<code>jruby -S warble config </code>

现在,我们就可以:

  • 修改 config/warble.rb 以添加 db/migrate 目录,具体见上文

这些便是需要做出的全部更改,我们现在就已万事俱备,可以打包和部署此应用程序了。

打包 JRuby Rails 应用程序并将其部署到 CloudFoundry

我们将使用 Warbler 来将此应用程序打包成一个 war,并使用 CloudFoundry vmc 命令行实用程序来部署此应用程序。

我们用来将此应用程序打包成 war 文件的流程十分简单:捆绑,预编译资产,然后运行 Warbler:

<code>jruby -S bundle install jruby -S rake assets:precompile jruby -S warble</code>

这会在我们的 Rails 应用程序的根目录中创建一个 bookshelf.war。目前,将 vmc 命令与 JRuby 一起运行时会存在一些问题,不过我们正在致力于加以修复。同时,我们还可以将此 war 文件移至其他目录,这样我就可以更轻松地改用常规的“C”Ruby。我将创建一个“deploy”目录,并将该目录配置为使用 Ruby 1.9.2-p290(我当前使用的是 rbenv,但您也可以使用 RVM):

<code>mkdir deploy mv bookshelf.war deploy/. cd deploy rbenv local 1.9.2-p290 # (if you use RVM the command should be 'rvm ruby-1.9.2-p290')</code>

现在,我们已准备就绪,可以登录 CloudFoundry 并部署我们的应用程序了。对于此部分,您需要安装vmc

<code>vmc target api.cloudfoundry.com vmc login cloud@mycompany.com Password: ***** Successfully logged into [http://api.cloudfoundry.com] vmc push bookshelf Would you like to deploy from the current directory? [Yn]: Y Application Deployed URL [bookshelf.cloudfoundry.com]: mybookshelf.cloudfoundry.com Detected a Java Web Application, is this correct? [Yn]: Y Memory reservation (128M, 256M, 512M, 1G, 2G) [512M]: 512M How many instances? [1]: 1 Bind existing services to 'bookshelf'? [yN]: N Create services to bind to 'bookshelf'? [yN]: Y 1: mongodb 2: mysql 3: postgresql 4: rabbitmq 5: redis What kind of service?: 2 Specify the name of the service [mysql-a4fd7]: mysql-books Create another? [yN]: N Would you like to save this configuration? [yN]: N Creating Application: OK Creating Service [mysql-books]: OK Binding Service [mysql-books]: OK Uploading Application: Checking for available resources: OK Processing resources: OK Packing application: OK Uploading (707K): OK Push Status: OK Staging Application 'bookshelf': OK Starting Application 'bookshelf': OK</code>

上面已将 vmc 命令突出显示出来。除 URL 以及是否应创建服务外,已接受大部分默认设置。我使用的 URL 为“mybookshelf.cloudfoundry.com”而非默认 URL,以免与现有的书架应用程序冲突。对于是否创建新服务的问题,我回答“Y”;此外我还选择了 (2) mysql 并将其命名为“mysql-books”。

现在我们应看到此应用程序正在运行:

<code>vmc apps +-------------+----+---------+---------------------------------+---------------+ | Application | # | Health | URLS | Services | +-------------+----+---------+---------------------------------+---------------+ | bookshelf | 1 | RUNNING | mybookshelf.cloudfoundry.com | mysql-books | +-------------+----+---------+---------------------------------+---------------+</code>

因此,我们现在输入“http://mybookshelf.cloudfoundry.com/”,就可以看到此 Bookshelf 应用程序惟妙惟肖地展示出来,我们可以添加一些书籍。

您可以在 cloudfoundry-samples/jruby-rails-bookshelf 位置查看和下载用于此示例的完整源代码;如果您只想了解为在 Cloud Foundry 中部署而需进行的更改,请查看此提交

结论

我们已经证明了可以将一个简单的 JRuby on Rails 应用程序部署到 cloudfoundry 并使用 MySQL 服务作为支持它的数据存储。只需对数据库的应用程序配置进行一些修改,即可做到。

我们将发布一篇博文来阐述对于使用 DataMapper 实现持久保留的 JRuby Sinatra 应用程序我们需要进行的类似更改。

- Cloud Foundry 团队
Thomas Risberg

Posted in Uncategorized

你可能错过的 Rails 技巧 –Posted by bigcircle

记得前段时间 RailsConf2012 之后看过一个不错的pdf,10 things you didn’t know rails could do

说是10个,但是给出了42个实例,这几天抽空又回味了下,料很多,写的很好,顺便总结学习下

Pass 掉第一个 fridayhug,我们是开心拥抱每一天

 

%w(action_controller/railtie coderay markaby).map &method(:require)

run TheSmallestRailsApp ||= Class.new(Rails::Application) {
  config.secret_token = routes.append {
    root to: proc {
      [200, {"Content-Type" => "text/html"}, [Markaby::Builder.new.html {
        title @title = "The Smallest Rails App"
        h3 "I am #@title!"
        p "Here is my source code:"
        div { CodeRay.scan_file(__FILE__).div(line_numbers: :table) }
        p { a "Make me smaller", href: "//goo.gl/YdRpy" }
      }]]
    }
  }.to_s
  initialize!
}

2 – 提醒功能 TODO

class UsersController < ApplicationController
  # TODO:  Make it possible to create new users.
end

class User < ActiveRecord::Base
  # FIXME: Should token really  be accessible?
  attr_accessible :bil, :email, :name, :token
end

执行 rake notes

 

app/controllers/users_controller.rb:
  * [ 2] [TODO] Make it possible to create new users.

app/models/user.rb:
  * [ 2] [FIXME] Should token really be accessible?

app/views/articles/index.html.erb:
  * [ 1] [OPTIMIZE] Paginate this listing.

查看单独的 TODO / FIXME / OPTIMIZE

rake notes:todo

app/controllers/users_controller.rb:
  * [ 2] Make it possible to create new users.

可以自定义提醒名称

class Article < ActiveRecord::Base
  belongs_to :user
  attr_accessible :body, :subject
  # JEG2: Add that code from your blog here.
end

不过需要敲一长串,可以alias个快捷键

rake notes:custom ANNOTATION=JEG2

app/models/article.rb:
  * [ 4]Add that code from your blog here.

3 – 沙箱模式执行 rails c

rails c --sandbox

沙箱模式会有回滚事务机制,对数据库的操作在退出之前都会自动回滚到之前未修改的数据

4 – 在 rails c 控制台中使用 rails helper 方法

$ rails c
Loading development environment (Rails 3.2.3)
>> helper.number_to_currency(100)
=> "$100.00"
>> helper.time_ago_in_words(3.days.ago)
=> "3 days"

5 – 开发模式用 thin 代替 webrick

group :development do
  gem 'thin'
end

rails s thin / thin start

6 – 允许自定义配置

 - lib/custom/railtie.rb

 module Custom
   class Railtie < Rails::Railtie
     config.custom = ActiveSupport::OrderedOptions.new
   end
 end

 - config/application.rb

 require_relative "../lib/custom/railtie"

 module Blog
   class Application < Rails::Application
     # ...
     config.custom.setting = 42
   end
 end

7 – keep funny

作者给出了个介绍 ruby 以及一些相关 blog的网站 rubydramas,搞笑的是这个网站右上角标明

Powered by PHP

用 isitrails.com 检查了下,果然不是用 rails 做的,这个应该是作者分享 ppt 过程中的一个小插曲吧

8 -理解简写的迁移文件

rails g resources user name:string email:string token:string bio:text

字段会被默认为 string 属性,查看了下 源码,果然有初始化定义

rails g resources user name email token:string{6} bio:text

会生成用样的 migration 文件

class CreateUsers < ActiveRecord::Migration   def change     create_table :users do |t|       t.string :name       t.string :email       t.string :token, :limit => 6
      t.text :bio
      t.timestamps
    end
  end
end

9 – 给 migration 添加索引

rails g resource user name:index email:uniq token:string{6} bio:text

会生成 name 和 email 的索引,同时限定 email 唯一

class CreateUsers < ActiveRecord::Migration   def change     create_table :users do |t|       t.string :name       t.string :email       t.string :token, :limit => 6
      t.text :bio
      t.timestamps
    end

    add_index :users, :name
    add_index :users, :email, :unique => true
  end
end

10 – 添加关联关系

rails g resource article user:references subject body:text

会自动关联生成对应的 belongs_to 和 外键,并添加索引

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.references :user
      t.string :subject
      t.text :body
      t.timestamps
    end
    add_index :articles, :user_id
  end
end
class Article < ActiveRecord::Base
  belongs_to :user
  attr_accessible :body, :subject
end

11 – 显示数据迁移记录

rake db:migrate:status

会输出 migration 的状态,这在解决迁移版本冲突的时候很有用

database: db/development.sqlite3

 status   Migration ID    Migration Name
 ---------------------------------------
   up     20120414155612  Create users
   up     20120414160528  Create articles
  down    20120414161355  Create comments

12 – 导入 csv 文件

csv 文件内容如下

Name,Email
James,james@example.com
Dana,dana@example.com
Summer,summer@example.com

创建 rake 任务导入 users 表

require 'csv'
namespace :users do
  desc "Import users from a CSV file"
  task :import => :environment do
    path = ENV.fetch("CSV_FILE") {
      File.join(File.dirname(__FILE__), *%w[.. .. db data users.csv])
    }
    CSV.foreach(path, headers: true, header_converters: :symbol) do |row|
      User.create(row.to_hash)
    end
  end
end

13 – 数据库中储存 csv

class Article <  ActiveRecord::Base
  require 'csv'
  module CSVSerializer
    module_function
    def load(field); field.to_s.parse_csv; end
    def dump(object); Array(object).to_csv; end
  end
  serialize :categories, CSVSerializer

  attr_accessible :body, :subject, :categories
end

serialize 用于在文本字段储存序列化的值,如序列,Hash,Array等,例如

user = User.create(:preferences => { "background" => "black", "display" => large })
User.find(user.id).preferences # => { "background" => "black", "display" => large }

这个例子中将 CSVSerializer to_csv序列化之后储存在 categories 这个文本类型字段中

14 – 用 pluck 查询

$ rails c
loading development environment(Rails 3.2.3)

>> User.select(:email).map(&:email)
  User Load(0.1ms) SELECT email FROM "users"
=> ["james@example.com", "dana@example.com", "summer@example.com"]
>> User.pluck(:email)
   (0.2ms) SELECT email FROM "users"
=> ["james@example.com", "dana@example.com", "summer@example.com"]
>> User.uniq.pluck(:email)
   (0.2ms) SELECT DISTINCT email FROM "users"
=> ["james@example.com", "dana@example.com", "summer@example.com"]

pluck 的实现方式其实也是 select 之后再 map

def pluck(column_name)
  column_name = column_name.to_s
  klass.connection.select_all(select(column_name).arel).map! do |attributes|
    klass.type_cast_attribute(attributes.keys.first, klass.initialize_attributes(attributes))
  end
end

15 – 使用 group count

创建 article 关联 model event

rails g resource event article:belongs_to triggle

创建3条 edit 记录和10条 view 记录。 Event.count 标明有13条记录,
group(:triggle).count 表示统计 triggle group 之后的数量,也可以用 count(:group => :trigger)

$ rails c

&gt;&gt; article = Article.last
=&gt; #<article id:1="">&gt;&gt; {edit:3, view:10}.each do |trigger, count| ?&gt; count.times do ?&gt; Event.new(trigger: trigger).tap{ |e| e.article= article; e.save! } ?&gt; end =&gt; {:edit =&gt; 3, :view =&gt; 10} &gt;&gt; Event.count =&gt; 13 &gt;&gt; Event.group(:trigger).count =&gt; {"edit" =&gt; 3, "view" =&gt; 10}</article>

16 – 覆盖关联关系

class Car < ActiveRecord::Base
  belongs_to :owner
  belongs_to :previous_owner, class_name: "Owner"

  def owner=(new_owner)
    self.previous_owner = owner
    super
  end
end

car更改 owner 时,如果有了 new_owner,就把原 owner 赋给 previous_owner,注意要加上 super

17 – 构造示例数据

$ rails c
Loading development environment (Rails 3.2.3)
>> User.find(1)
=> #
>> jeg2 = User.instantiate("id" => 1, "email" => "
=> #
>> jeg2.name = "James Edward Gray II"
=> "James Edward Gray II"
>> jeg2.save!
=> true
>> User.find(1)
james@example.com", ...>

伪造一条记录,并不是数据库中的真实数据,也不会把原有数据覆盖

18 – PostgreSQL 中使用无限制的 string

去掉适配器中对 string 长度的限制,这个应该是 PostgreSQL 数据库的特性

module PsqlApp
  class Application < Rails::Application
    # Switch to limitless strings
    initializer "postgresql.no_default_string_limit" do
      ActiveSupport.on_load(:active_record) do
        adapter = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
        adapter::NATIVE_DATABASE_TYPES[:string].delete(:limit)
      end
    end
 end
end

创建 user 表,bio 字符串

rails g resource user bio
$ rails c
Loading development environment (Rails 3.2.3)
>> very_long_bio = "X" * 10_000; :set
=> :set
>> User.create!(bio: very_long_bio)
=> #
>> User.last.bio.size
=> 10000

19 – PostgreSQL 中使用全文搜

rails g resource article subject body:text

更改迁移文件,添加索引和 articles_search_update 触发器

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :subject
      t.text   :body
      t.column :search, "tsvector"
      t.timestamps
    end
    execute <

Model 中添加 search 方法

class Article < ActiveRecord::Base
  attr_accessible :body, :subject
  def self.search(query)
    sql = sanitize_sql_array(["plainto_tsquery('english', ?)", query])
    where(
      "search @@ #{sql}"
    ).order(
      "ts_rank_cd(search, #{sql}) DESC"
    )
  end
end

PostgreSQL 数据库没用过,这段看例子吧

$ rails c
Loading development environment (Rails 3.2.3)
&gt;&gt; Article.create!(subject: "Full Text Search")
=&gt; #<article id:="" 1="">&gt;&gt; Article.create!(body: "A stemmed search.") =&gt; #<article id:="" 2="">&gt;&gt; Article.create!(body: "You won't find me!") =&gt; #<article id:="" 3="">&gt;&gt; Article.search("search").map { |a| a.subject || a.body } =&gt; ["Full Text Search", "A stemmed search."] &gt;&gt; Article.search("stemming").map { |a| a.subject || a.body } =&gt; ["A stemmed search."]</article></article></article>

21 – 每个用户使用不同的数据库

- user_database.rb

def connect_to_user_database(name)
  config = ActiveRecord::Base.configurations["development"].merge("database" => "db/#{name}.sqlite3")
  ActiveRecord::Base.establish_connection(config)
end

创建 rake 任务:新增用户数据库和创建

require "user_database"

namespace :db do
  desc "Add a new user database"
  task :add => %w[environment load_config] do
    name = ENV.fetch("DB_NAME") { fail "DB_NAME is required" }
    connect_to_user_database(name)
    ActiveRecord::Base.connection
  end

  namespace :migrate do
    desc "Migrate all user databases"
    task :all => %w[environment load_config] do
      ActiveRecord::Migration.verbose = ENV.fetch("VERBOSE", "true") == "true"
      Dir.glob("db/*.sqlite3") do |file|
        next if file == "db/test.sqlite3"
        connect_to_user_database(File.basename(file, ".sqlite3"))
        ActiveRecord::Migrator.migrate(
          ActiveRecord::Migrator.migrations_paths,
          ENV["VERSION"] && ENV["VERSION"].to_i
        ) do |migration|
          ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope)
        end
      end
    end
  end
end

执行几个rake 任务创建不同的数据库

$ rails g resource user name
$ rake db:add DB_NAME=ruby_rogues
$ rake db:add DB_NAME=grays
$ rake db:migrate:all
==  CreateUsers: migrating ==================================
-- create_table(:users)
   -> 0.0008s
==  CreateUsers: migrated (0.0008s) =========================
==  CreateUsers: migrating ==================================
-- create_table(:users)
   -> 0.0007s
==  CreateUsers: migrated (0.0008s) =========================

创建几条记录,为不同的数据库创建不同的数据

$ rails c
>> connect_to_user_database("ruby_rogues")
=> #
>> User.create!(name: "Chuck")
=> #
>> User.create!(name: "Josh")
=> #
>> User.create!(name: "Avdi")
=> #
...
>> connect_to_user_database("grays")
=> #
>> User.create!(name: "James")
=> #
>> User.create!(name: "Dana")
=> #
>> User.create!(name: "Summer")
=> #

ApplicationController 里面添加 before_filter 根据登陆的二级域名判断连接哪个数据库

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :connect_to_database
private
  def connect_to_database
    connect_to_user_database(request.subdomains.first)
  end
end

中场休息时,去找了下 RailsConf 2012 的视频看了看,刚好看到关于 这篇 的介绍,片子还挺长,41分钟,演讲者长相和声音都很不符合大家对 Rails 的认知,大家有兴趣的可以去听听

21 – 自动写文件

class Comment < ActiveRecord::Base
  # ...
  Q_DIR = (Rails.root + "comment_queue").tap(&:mkpath)
  after_save :queue_comment
  def queue_comment
    File.atomic_write(Q_DIR + "#{id}.txt") do |f|
      f.puts "Article: #{article.subject}"
      f.puts "User:    #{user.name}"
      f.puts body
    end
  end
end

找了下 Api 是 Rails 对 Ruby 基础类的扩展

22 – 合并嵌套 Hash

$ rails c
Loading development environment (Rails 3.2.3)
>> {nested: {one: 1}}.merge(nested: {two: 2})
=> {:nested=>{:two=>2}}
>> {nested: {one: 1}}.deep_merge(nested: {two: 2})
=> {:nested=>{:one=>1, :two=>2}}

主要是用到了 deep_merge 合并相同的 key

23 – Hash except

$ rails c
Loading development environment (Rails 3.2.3)
>> params = {controller: "home", action: "index", from: "Google"}
=> {:controller=>"home", :action=>"index", :from=>"Google"}
>> params.except(:controller, :action)
=> {:from=>"Google"}

这个方法经常会用到,可能用的人也很多

24 – add defaults to Hash

$ rails c
Loading development environment (Rails 3.2.3)
>> {required: true}.merge(optional: true)
=> {:required=>true, :optional=>true}
>> {required: true}.reverse_merge(optional: true)
=> {:optional=>true, :required=>true}
>> {required: true, optional: false}.merge(optional: true)
=> {:required=>true, :optional=>true}
>> {required: true, optional: false}.reverse_merge(optional: true)
=> {:optional=>false, :required=>true}

这几个都是对 Hash 类的增强,merge 会替换原有相同 key 的值,但 reverse_merge 不会

从源码就可以看出,会事先 copy 一份 default hash

def reverse_merge(other_hash)
  super
  self.class.new_from_hash_copying_default(other_hash)
end

25 – String.value? 方法

看下面的几个例子

$ rails c
Loading development environment (Rails 3.2.3)
>> env = Rails.env
=> “development”
>> env.development?
=> true
>> env.test?
=> false
>> “magic”.inquiry.magic?
=> true
>> article = Article.first
=> #

>> article.draft?
=> true
>> article.published?
=> false

 

env, “magic” 可以直接使用 value? 的方法,这个扩展是 String#inquiry 方法

def inquiry
  ActiveSupport::StringInquirer.new(self)
end

# 用method_missing 实现
def method_missing(method_name, *arguments)
  if method_name.to_s[-1,1] == "?"
    self == method_name.to_s[0..-2]
  else
    super
  end
end

类型的一个例子,同样用到了 inquiry 方法

class Article < ActiveRecord::Base
  # ...
  STATUSES = %w[Draft Published]
  validates_inclusion_of :status, in: STATUSES
  def method_missing(method, *args, &block)
    if method =~ /A#{STATUSES.map(&:downcase).join("|")}?z/
      status.downcase.inquiry.send(method)
    else
      super
    end
  end
end

 

26 – 让你成为杂志的封面 (暖场之用)

搞笑哥拿出了 DHH 当选 Linux journal 杂志封面的图片,会场也是哄堂大笑 ^.^

27 – 隐藏注释

<h1>Home Page</h1>

# 生成的 html<!-- HTML comments stay in the rendered content -->

<h1>Home Page</h1>

这个一下没看懂。。试了下项目里面的代码,原来是隐藏的意思。。 28 – 理解更短的 erb 语法

# ...
module Blog
  class Application < Rails::Application

    # Broken:  config.action_view.erb_trim_mode = '%'
    ActionView::Template::Handlers::ERB.erb_implementation =
      Class.new(ActionView::Template::Handlers::Erubis) do
        include ::Erubis::PercentLineEnhancer
      end
    end
  end
end

接着在 view 页面替换用 % 表示原来,有点像 slim

% if current_user.try(:admin?)

% end

29 – 用 block 避免视图层赋值


<table>
  <% @cart.products.each do |product| %>
    <tr>
      <td><%= product.name %></td>
      <td><%= number_to_currency product.price %></td>
    </tr>
  <% end %>
  <tr>
    <td>Subtotal</td>
    <td><%= number_to_currency @cart.total %></td>
  </tr>
  <tr>
    <td>Tax</td>
    <td><%= number_to_currency(tax = calculate_tax(@cart.total)) %></td>
  </tr>
  <tr>
    <td>Total</td>
    <td><%= number_to_currency(@cart.total + tax) %></td>
  </tr>
</table>


tax = calculate_tax(@cart.total)
 会先被赋值再被下面引用 用 block 重构下,把逻辑代码放到 helper 里面

module CartHelper
  def calculate_tax(total, user = current_user)
    tax = TaxTable.for(user).calculate(total)
    if block_given?
      yield tax
    else
      tax
    end
  end
end
<table>
  <% @cart.products.each do |product| %>
    <tr>
      <td><%= product.name %></td>
      <td><%= number_to_currency product.price %></td>
    </tr>
  <% end %>
  <tr>
    <td>Subtotal</td>
    <td><%= number_to_currency @cart.total %></td>
  </tr>
  <% calculate_tax @cart.total do |tax| %>
    <tr>
      <td>Tax</td>
      <td><%= number_to_currency tax %></td>
    </tr>
    <tr>
      <td>Total</td>
      <td><%= number_to_currency(@cart.total + tax) %></td>
    </tr>
  <% end %>
</table>

 

30 – 同时生成多个标签

<h1>Articles</h1>
  <% @articles.each do |article| %>
    <%= content_tag_for(:div, article) do %>
    <h2><%= article.subject %></h2>
  <% end %>
<% end %>
<%= content_tag_for(:div, @articles) do |article| %>
  <h2><%= article.subject %></h2>
<% end %>

 

 

content_tag_for 具体用法可以参考 Api,意思比较明白 to_partial_path 是 ActiveModel 內建的实例方法,返回一个和可识别关联对象路径的字符串,原文是这么说的,目前还没看明白这么用的目的在哪

这篇 blog 介绍了4个最喜欢的 Rails3.2 隐藏特性,

 

这4条都在这个系列中,作者可能也是从这学来的吧

31 – Render Any Object

class Event < ActiveRecord::Base
  # ...
  def to_partial_path
    "events/#{trigger}"  # events/edit or events/view
  end
end

to_partial_path 是 ActiveModel 內建的实例方法,返回一个和可识别关联对象路径的字符串,原文是这么说的,目前还没看明白这么用的目的在哪

 

Returns a string identifying the path associated with the object.
ActionPack uses this to find a suitable partial to represent the object.

32 – 生成 group option

 %w[One Two Three],
  "Group B" => %w[One Two Three]
) ) %>

这个其实就是用到了 grouped_options_for_select ,我在前面的 博文 提到过这几个 select 的用法

33 -定制你自己喜欢的 form 表单

class LabeledFieldsWithErrors < ActionView::Helpers::FormBuilder
  def errors_for(attribute)
    if (errors = object.errors[attribute]).any?
      @template.content_tag(:span, errors.to_sentence, class: "error")
    end
  end
  def method_missing(method, *args, &block)
    if %r{ A (?labeled_)?
              (?w+?)
              (?_with_errors)? z }x =~ method and
       respond_to?(wrapped) and [labeled, with_errors].any?(&:present?)
      attribute, tags = args.first, [ ]
      tags           << label(attribute) if labeled.present?
      tags           << send(wrapped, *args, &block)
      tags           << errors_for(attribute) if with_errors.present?
      tags.join(" ").html_safe
    else
      super
    end
  end
end

定义了几个不想去看懂的 method_missing 方法。。 修改 application.rb,添加配置

class Application < Rails::Application
  # ...
  require "labeled_fields_with_errors"
  config.action_view.default_form_builder = LabeledFieldsWithErrors
  config.action_view.field_error_proc     = ->(field, _) { field }
end

创建 form 表单可以这样书写


<%= form_for @article do |f| %>
  <p><%= f.text_field
  <p><%= f.labeled_text_field
  <p><%= f.text_field_with_errors
  <p><%= f.labeled_text_field_with_errors :subject %></p>
  <%= f.submit %>
<% end %>

生成如下的 html 页面

<p><input id="article_subject" name="article[subject]" size="30" type="text" value="" /></p>
<p><label for="article_subject">Subject</label>
   <input id="article_subject" name="article[subject]" size="30" type="text" value="" /></p>
<p><input id="article_subject" name="article[subject]" size="30" type="text" value="" />
   <span class="error">can't be blank</span></p>
<p><label for="article_subject">Subject</label>
   <input id="article_subject" name="article[subject]" size="30" type="text" value="" />
   <span class="error">can't be blank</span></p>
<!-- ... -->

不是很喜欢这种方式,反而把简单的html搞复杂了,让后来维护的人增加额外的学习成本     不是很喜欢这种方式,反而把简单的html搞复杂了,让后来维护的人增加额外的学习成本

34 – Inspire theme songs about your work (再次暖场时刻)

2011年 Farmhouse Conf 上主持人 Ron Evans 专门用口琴演奏了为大神 Tenderlove 写的歌 – Ruby Hero Tenderlove! ,听了半天不知道唱的啥。。 想找下有没有美女 Rubist, 看了下貌似没有,都是大妈,这位 Meghann Millard 尚可远观,大姐装束妖娆,手握纸条,蚊蝇环绕,不时微笑,长的真有点像 gossip girl 里面的 Jenny Humphrey

35 – 灵活的异常操作

修改 application.rb 定义

class Application < Rails::Application
# ...
  config.exceptions_app = routes
end

每次有异常时路由都会被调用,你可以用下面的方法简单 render 404 页面

match "/404", :to => "errors#not_found"

这个例子也在开头提到的那篇博文里面,感兴趣可以去自己研究下

36 – 给 Sinatra 添加路由

- Gemfile

source 'https://rubygems.org'
# ...
gem "resque", require: "resque/server"

module AdminValidator

  def matches?(request)
    if (id = request.env["rack.session"]["user_id"])
      current_user = User.find_by_id(id)
      current_user.try(:admin?)
    else
      false
    end
  end
end

挂载 Resque::Server 至 /admin/resqu

Blog::Application.routes.draw do
  # ...
  require "admin_validator"
  constraints AdminValidator do
    mount Resque::Server, at: "/admin/resque"
  end
end

这个也没有试验,不清楚具体用法,sinatra 平时也基本不用

37 – 导出CSV流

class ArticlesController < ApplicationController
  def index
    respond_to do |format|
      format.html do
        @articles = Article.all
      end
      format.csv do
        headers["Content-Disposition"] = %Q{attachment; filename="articles.csv"}
        self.response_body = Enumerator.new do |response|
          csv  = CSV.new(response, row_sep: "n")
          csv << %w[Subject Created Status]
          Article.find_each do |article|
            csv << [ article.subject,
                     article.created_at.to_s(:long),
                     article.status ]
        	end
        end
      end
    end
  end
# ...
end

导出 csv 是很常用的功能,很多时候报表都需要,这个还是比较实用的

38 - do some work in the background

给 articles 添加文本类型 stats 字段
rails g migration add_stats_to_articles stats:text

添加一个计算 stats 方法 和 一个 after_create 方法,在创建一条记录后,会把 calculate_stats 添加到 Queue 队列,当队列中有任务时,后台创建一个线程执行该 job

class Article < ActiveRecord::Base
  # ...
  serialize :stats
  def calculate_stats
    words = Hash.new(0)
    body.to_s.scan(/S+/) { |word| words[word] += 1 }
    sleep 10  # simulate a lot of work
    self.stats = {words: words}
  end

  require "thread"
  def self.queue; @queue ||= Queue.new end
  def self.thread
    @thread ||= Thread.new do
      while job = queue.pop
        job.call
      end
    end
  end
  thread  # start the Thread

  after_create :add_stats
  def add_stats
    self.class.queue << -> { calculate_stats; save }
  end
end

添加一条记录,10秒后会自动给该记录 stats 字段添加 words Hash

$ rails c
Loading development environment (Rails 3.2.3)
>> Article.create!(subject: "Stats", body: "Lorem ipsum...");
Time.now.strftime("%H:%M:%S")
=> "15:24:10"
>> [Article.last.stats, Time.now.strftime("%H:%M:%S")]
=> [nil, "15:24:13"]
>> [Article.last.stats, Time.now.strftime("%H:%M:%S")]
=>[{:words=>{"Lorem"=>1, "ipsum"=>1, ...}, "15:24:26"]

39 – 用 Rails 生成静态站点

修改 config/environment/development.rb

Static::Application.configure do
  # ...
  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = !!ENV["GENERATING_SITE"]
  # ...
  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = !ENV["GENERATING_SITE"]
  # Generate digests for assets URLs
  config.assets.digest = !!ENV["GENERATING_SITE"]
  # ...
end

class ApplicationController < ActionController::Base
  protect_from_forgery
  if ENV["GENERATING_SITE"]
    after_filter do |c|
      c.cache_page(nil, nil, Zlib::BEST_COMPRESSION)
    end
  end
end

修改 rake static:generate 任务

require "open-uri"
namespace :static do
  desc "Generate a static copy of the site"
  task :generate => %w[environment assets:precompile] do
    site = ENV.fetch("RSYNC_SITE_TO") { fail "Must set RSYNC_SITE_TO" }
    server = spawn( {"GENERATING_SITE" => "true"},
                    "bundle exec rails s thin -p 3001" )
    sleep 10  # FIXME: start when the server is up

    # FIXME: improve the following super crude spider
    paths = %w[/]
    files = [ ]
    while path = paths.shift
      files << File.join("public", path.sub(%r{/z}, "/index") + ".html")
      File.unlink(files.last) if File.exist? files.last
      files << files.last + ".gz"
      File.unlink(files.last) if File.exist? files.last
      page = open("http://localhost:3001#{path}") { |url| url.read }
      page.scan(/]+href="([^"]+)"/) do |link|
        paths << link.first
      end
    end

    system("rsync -a public #{site}")

    Process.kill("INT", server)
    Process.wait(server)
    system("bundle exec rake assets:clean")
    files.each do |file|
      File.unlink(file)
    end
  end
end

生成到某个地方,去查看吧

rake static:generate RSYNC_SITE_TO=/Users/james/Desktop

后面几个都不感兴趣,没有测试,说好的42个,瞎扯了3个pass掉了,实在是吐血了

Over.

 

Posted in Uncategorized

Ruby Performance Tricks –Posted by Sergey Potapov

http://greyblake.com/blog/2012/09/02/ruby-perfomance-tricks/

I did some benchmarks to find out which alternatives to write code work faster. I wanna share it with you. All benchmarks are made against ruby 1.9.3p194 MRI.

Do not use exceptions for a control flow

The next example is pretty stupid but it shows how exceptions slow against conditional statements.

require 'benchmark'

class Obj
  def with_condition
    respond_to?(:mythical_method) ? self.mythical_method : nil
  end

  def with_rescue
    self.mythical_method
  rescue NoMethodError
    nil
  end
end

obj = Obj.new
N = 10_000_000

puts RUBY_DESCRIPTION

Benchmark.bm(15, "rescue/condition") do |x|
  rescue_report     = x.report("rescue:")    { N.times { obj.with_rescue  } }
  condition_report  = x.report("condition:") { N.times { obj.with_if      } }
  [rescue_report / condition_report]
end

MRI 1.9.3:

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
user system total real
rescue: 111.530000 2.650000 114.180000 (115.837103)
condition: 2.620000 0.010000 2.630000 ( 2.633154)
rescue/condition: 42.568702 265.000000 NaN ( 43.991767)

MRI 1.8.7 (REE has similar result):

ruby 1.8.7 (2011-12-28 patchlevel 357) [x86_64-linux]
                        user     system      total        real
rescue:            80.510000   0.940000  81.450000 ( 81.529022)
if:                 3.320000   0.000000   3.320000 (  3.330166)
rescue/condition:  24.250000        inf       -nan ( 24.481970)

String concatenation

Avoid using += to concatenate strings in favor of << method. The result is absolutely the same: add a string to the end of an existing one. What is the difference then?

See the example:

str1 = "first"
str2 = "second"
str1.object_id       # => 16241320

str1 += str2    # str1 = str1 + str2
str1.object_id  # => 16241240, id is changed

str1 << str2 str1.object_id  # => 16241240, id is the same

When you use += ruby creates a temporal object which is result of str1 + str2. Then it overrides str1 variable with reference to the new built object. On other hand << modifies existing one. As a result of using += you have the next disadvantages:

  • More calculation to join strings.
  • Redundant string object in memory (previous value of str1), which approximates time when GC will trigger.

How += is slow? Basically it depends on length of strings you have operation with.

require 'benchmark'

N = 1000
BASIC_LENGTH = 10

5.times do |factor|
  length = BASIC_LENGTH * (10 ** factor)
  puts "_" * 60 + "nLENGTH: #{length}"

  Benchmark.bm(10, '+= VS <

Output:

____________________________________________________________
LENGTH: 10
                 user     system      total        real
+=           0.000000   0.000000   0.000000 (  0.004671)
<<           0.000000   0.000000   0.000000 (  0.000176)
+= VS <<          NaN        NaN        NaN ( 26.508796)
____________________________________________________________
LENGTH: 100
                 user     system      total        real
+=           0.020000   0.000000   0.020000 (  0.022995)
<<           0.000000   0.000000   0.000000 (  0.000226)
+= VS <<          Inf        NaN        NaN (101.845829)
____________________________________________________________
LENGTH: 1000
                 user     system      total        real
+=           0.270000   0.120000   0.390000 (  0.390888)
<<           0.000000   0.000000   0.000000 (  0.001730)
+= VS <<          Inf        Inf        NaN (225.920077)
____________________________________________________________
LENGTH: 10000
                 user     system      total        real
+=           3.660000   1.570000   5.230000 (  5.233861)
<<           0.000000   0.010000   0.010000 (  0.015099)
+= VS <<          Inf 157.000000        NaN (346.629692)
____________________________________________________________
LENGTH: 100000
                 user     system      total        real
+=          31.270000  16.990000  48.260000 ( 48.328511)
<<           0.050000   0.050000   0.100000 (  0.105993)
+= VS <<   625.400000 339.800000        NaN (455.961373)

Be careful with calculation within iterators

Assume you need to write a function to convert an array into a hash where keys and values are same as elements of the array:

 

func([1, 2, 3])  # => {1 => 1, 2 => 2, 3 => 3}

The next solution would satisfy the requirements:

def func(array)
  array.inject({}) { |h, e| h.merge(e => e) }
end

And would be extremely slow with big portions of data because it contains nested methods (inject and merge), so it’s O(n2algorithm. But it’s obviously that it must be O(n) . Consider the next:

 

def func(array)
  array.inject({}) { |h, e| h[e] = e; h }
end

In this case we do only one iteration over an array without any hard calculation within the iterator.

See the benchmark:

require 'benchmark'

def n_func(array)
  array.inject({}) { |h, e| h[e] = e; h }
end

def n2_func(array)
  array.inject({}) { |h, e| h.merge(e => e) }
end

BASE_SIZE = 10

4.times do |factor|
  size   = BASE_SIZE * (10 ** factor)
  params = (0..size).to_a
  puts "_" * 60 + "nSIZE: #{size}"
  Benchmark.bm(10) do |x|
    x.report("O(n)" ) { n_func(params)  }
    x.report("O(n2)") { n2_func(params) }
  end
end

Output:

____________________________________________________________
SIZE: 10
                user     system      total        real
O(n)        0.000000   0.000000   0.000000 (  0.000014)
O(n2)       0.000000   0.000000   0.000000 (  0.000033)
____________________________________________________________
SIZE: 100
                user     system      total        real
O(n)        0.000000   0.000000   0.000000 (  0.000043)
O(n2)       0.000000   0.000000   0.000000 (  0.001070)
____________________________________________________________
SIZE: 1000
                user     system      total        real
O(n)        0.000000   0.000000   0.000000 (  0.000347)
O(n2)       0.130000   0.000000   0.130000 (  0.127638)
____________________________________________________________
SIZE: 10000
                user     system      total        real
O(n)        0.020000   0.000000   0.020000 (  0.019067)
O(n2)      17.850000   0.080000  17.930000 ( 17.983827)

It’s an obvious and trivial example. Just keep in mind to not do hard calculation within iterators if it’s possible.

Use bang! methods

In many cases bang methods do the same as there non-bang analogues but without duplication an object. The previous example with merge! would be much faster:

require 'benchmark'

def merge!(array)
  array.inject({}) { |h, e| h.merge!(e => e) }
end

def merge(array)
  array.inject({}) { |h, e| h.merge(e => e) }
end

N = 10_000
array = (0..N).to_a

Benchmark.bm(10) do |x|
  x.report("merge!") { merge!(array) }
  x.report("merge")  { merge(array)  }
end

Output:

                 user     system      total        real
merge!       0.010000   0.000000   0.010000 (  0.011370)
merge       17.710000   0.000000  17.710000 ( 17.840856)

 

Use instance variables

Accessing instance variable directly is about two times faster than accessing them with accessor methods:

require 'benchmark'

class Metric
  attr_accessor :var

  def initialize(n)
    @n   = n
    @var = 22
  end

  def run
    Benchmark.bm(10) do |x|
      x.report("@var") { @n.times { @var } }
      x.report("var" ) { @n.times { var  } }
      x.report("@var =")     { @n.times {|i| @var = i     } }
      x.report("self.var =") { @n.times {|i| self.var = i } }
    end
  end
end

metric = Metric.new(100_000_000)
metric.run

Output:

                 user     system      total        real
@var         6.980000   0.010000   6.990000 (  7.193725)
var         13.040000   0.000000  13.040000 ( 13.131711)
@var =       7.960000   0.000000   7.960000 (  8.242603)
self.var =  14.910000   0.010000  14.920000 ( 15.960125)

Parallel assignment is slower

require 'benchmark'

N = 10_000_000

Benchmark.bm(15) do |x|
  x.report('parallel') do
    N.times do
      a, b = 10, 20
    end
  end

  x.report('consequentially') do |x|
    N.times do
      a = 10
      b = 20
    end
  end
end

Output:

                      user     system      total        real
parallel          1.900000   0.000000   1.900000 (  1.928063)
consequentially   0.880000   0.000000   0.880000 (  0.879675)

 

Dynamic method defention

What is the faster way to define method dynamically: class_eval with a code string or using define_method? Which way generated methods work faster?

require 'benchmark'

class Metric
  N = 1_000_000

  def self.class_eval_with_string
    N.times do |i|
      class_eval(<

Output:

                             user     system      total        real
class_eval with string 219.840000   0.720000 220.560000 (221.933074)
define_method           61.280000   0.240000  61.520000 ( 62.070911)
string method            0.110000   0.000000   0.110000 (  0.111433)
dynamic method           0.150000   0.000000   0.150000 (  0.156537)

So class_eval works slower but it’s preferred since methods generated with class_evaland a string of code work faster.

Links

Danke.

Posted in Uncategorized

37signals Earns Millions Each Year. Its CEO’s Model? His Cleaning Lady

 

Don’t build a fast company, Jason Fried tells Fast Company. Build a slow one.

 

Jason Fried is a founder and CEO of 37signals, a software company based in Chicago. Fried also treats 37signals as something of a laboratory for innovative workplace practices–such as a recent experiment in shortening the summer workweek to just four days. We caught up with Fried to learn how employees are like fossil fuels, how a business can be like a cancer, and how one of the entrepreneurs he admires most is his cleaning lady.

FAST COMPANY: You have your employees only work four-day weeks in the summer.

JASON FRIED: Sometimes people are not really used to working just four days and actually want to stay to get more work done.

You’re saying you have people who actually want to stay the fifth day?

When we first started this a few years ago, there was a small sense of guilt in a few corners. People were like, “I have stuff to get done, it’s Thursday, so I’m gonna work Friday and just get it done. But we actually preferred that they didn’t. There are very few things that can’t wait till Monday.

How many employees would stay to work Fridays?

I don’t know.

Because you weren’t there!

We don’t track things in that way. I don’t look at that. I don’t want to encourage that kind of work. I want to encourage quality work.

As CEO, wouldn’t it simply be rational to let people work the fifth day for you if they wanted?

If you’re a short-term thinker you’d think so, but we’re long-term thinkers. We’re about being in business for the long haul and keeping the team together over the long haul. I would never trade a short-term burst for a long-term decline in morale. That happens a lot in the tech business: They burn people out and get someone else. I like the people who work here too much. I don’t want them to burn out. Lots of startups burn people out with 60, 70, 80 hours of work per week. They know that both the people or the company will flame out or be bought or whatever, and they don’t care, they just burn their resources. It’s like drilling for as much oil as you possibly can. You can look at people the same way.

Are we reaching “peak people”?

It seems like in a lot of companies we are. There’s a shortage of talent out there, and if there’s a shortage of resources, you want to conserve those resources.

So you think there’s a slash-and-burn mentality in the tech world?

For sure. I think there’s a lot of lottery-playing going on right now. Companies staffing up, raising a bunch of money, hiring a bunch of people, and burning them out in the hopes that they’ll hit the lottery.

You seem like too nice a guy to name names–but do you have certain companies in mind?

I won’t name names. I used to name names. But I think all you have to do is read TechCrunch. Look at what the top stories are, and they’re all about raising money, how many employees they have, and these are metrics that don’t matter. What matters is: Are you profitable? Are you building something great? Are you taking care of your people? Are you treating your customers well? In the coverage of our industry as a whole, you’ll rarely see stories about treating customers well, about people building a sustainable business. TechCrunch to me is the great place to look to see the sickness in our industry right now.

Our magazine is called Fast Company, but it sounds like you want to build a slow company.

I’m a fan of growing slowly, carefully, methodically, of not getting big just for the sake of getting big. I think that rapid growth is typically of symptom of… there’s a sickness there. There’s a great quote by a guy named Ricardo Semler, author of the book Maverick. He said that only two things grow for the sake of growth: businesses and tumors. We have 35 employees at 37signals. We could have hundreds of employees if we wanted to–our revenues and profits support that–but I think we’d be worse off.

What industries do you look to for inspiration, if not the tech world?

I take my inspiration from small mom-and-pop businesses that have been around for a long time. There are restaurants all over the place that I like to go to that have been around a long time, 30 years or more, and thinking about that, that’s an incredible run. I don’t know what percentage of tech companies have been around 30 years. The other interesting thing about restaurants is you could have a dozen Italian restaurants in the city and they can all be successful. It’s not like in the tech world, where everyone wants to beat each other up, and there’s one winner. Those are the businesses I find interesting–it could be a dry cleaner, a restaurant, a clothing store. Actually, my cleaning lady, for example, she’s great.

Your business icon is your cleaning lady?

She’s on her own, she cleans people’s homes, she’s incredibly nice. She brings flowers every time she cleans, and she’s just respectful and nice and awesome. Why can’t more people be like that? She’s been doing it some twenty-odd years, and that’s just an incredible success story. To me that’s far more interesting than a tech company that’s hiring a bunch of people, just got their fourth round of financing for 12 million dollars, and they’re still losing money. That’s what everyone talks about as being exciting, but I think that’s an absolutely disgusting scenario when it comes to business.

Posted in Uncategorized