Rails Rails 5.1 beta 发布了 - 关键词 Loving JavaScript

huacnlee · February 24, 2017 · Last by grantbb replied at March 03, 2017 · 5117 hits

Rails 5.0 was released just some eight months ago, and now, some 3500 commits later, we’re already close to the next big release. And what release this version 5.1 is lining up to be! We’ve made great strides on long-term promises and key ergonomics while also spring cleaning a bunch of deprecated code.

Loving JavaScript

  • 现在你可以通过 Yarn 来管理 NPM 的 JavaScript 依赖了;
  • 你可以选择用 Webpack 来编译 JavaScript,with rails new project_name --webpack;
  • jQuery 不再是默认的依赖,jquery-ujs 变成了 rails-ujs

System tests

Capybara 默认引入,增加 ApplicationSystemTestCase 类型:

class Users::CreateTest < ApplicationSystemTestCase
  test "adding a new user" do
    visit users_path
    click_on 'New User'

    fill_in 'Name', with: 'Arya'
    click_on 'Create User'

    assert_text 'Arya'
  end
end

似乎不太感兴趣,一直没这么写测试...

Encrypted secrets

看起来是为了解决 secrets 总会无意中被带入版本管理里面,成为安全漏洞的问题。

于是 config/secrets.yml 现在起将变成加密的内容,在项目初始化的时候会有一个 key (私钥),独立保存 key,然后 config/secrets.yml 就可以放心放入版本库了。

以后编辑 secrets 需要执行 rails secrets:edit

production 环境的时候需要用私钥才能读取,或者配置环境变量 RAILS_MASTER_KEY=xxxx

也就是说以后那些各类需要保密用的私钥,例如 OAuth 2 的 key、三方平台的秘钥、密码加密的 salt 什么的都可以统统放入 config/secrets.yml 里面统一管理。

https://github.com/rails/rails/blob/master/railties/lib/rails/commands/secrets/USAGE

Parameterized mailers

看起来大致就是 ActionMailer 编写可以支持 before_actionrails/rails/actionmailer/lib/action_mailer/parameterized.rb#2dadf73:

class InvitationsMailer < ApplicationMailer
  before_action { @inviter, @invitee = params[:inviter], params[:invitee] }
  before_action { @account = params[:inviter].account }

  default to:       -> { @invitee.email_address },
          from:     -> { common_address(@inviter) },
          reply_to: -> { @inviter.email_address_with_name }

  def account_invitation
    mail subject: "#{@inviter.name} invited you to their Basecamp (#{@account.name})"
  end
end

Direct & resolved routes

看起来是一个很大的改动,routes.rb 新增 directresolve DSL:

# config/routes.rb
resource :profile
namespace :admin do
  resources :users
end

direct(:apple) { "http://www.apple.com" }
resolve("User") { [:profile] }

direct(class: "User") { [:profile] }
# resolve 的效果
app_url # http://www.apple.com

<%= form_for @user do |form| %>
<% end %>
# <form action="/profile/1"></form>

# 然后 View 里面使用 link_to 会有这样的结果
link_to 'Profile', @uesr # /profile
link_to 'Profile', [:admin, @uesr] # /admin/users/1

关于 Action Pack 的 CHANGELOG

统一 form_tag 和 form_for,变成 form_with

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

以及其他更多内容,详见原文:

http://weblog.rubyonrails.org/2017/2/23/Rails-5-1-beta1/

huacnlee in [Topic was deleted] mention this topic. 24 Feb 11:54

3 年前 DHH 提倡的 system test 终于整合进来了,但是不觉得这测试是由开发工程师来写,还是适合测试工程师/QA 去做。

如何将 Rails 5.0 升级 5.1

修改 Gemfile

gem 'rails', '~> 5.1.0.beta' 

and run bundle update rails

执行 rails app:update 检查 Rails 标准的几个文件的改动

Overwrite config/secrets.yml? (enter "h" for help) [Ynaqdh] 

遇到这类提示的时候,选 d,查看 Diff,根据情况手工修改你的文件。

看起来重要的是 config/environments/production.rb 里面新增了一个配置:

# Attempt to read encrypted secrets from `config/secrets.yml.enc`.
# Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or
# `config/secrets.yml.key`.
config.read_encrypted_secrets = true
DEPRECATION WARNING: The behavior of `attribute_change` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_change_to_attribute` instead. (called from block (3 levels) in <top (required)> at spec/models/user_spec.rb:30)

大量的这种 Warnning,看了一下,要清理需要非一些功夫了... attribute_changed? 这种用法很多地方都在用。

Change ActionView ERB Handler from Erubis to Erubi

Erubi offers the following advantages for Rails:

* Works with ruby's --enable-frozen-string-literal option
* Has 88% smaller memory footprint
* Does no freedom patching (Erubis adds a method to Kernel)
* Has simpler internals (1 file, <150 lines of code)
* Has an open development model (Erubis doesn't have a
  public source control repository or bug tracker)
* Is not dead (Erubis hasn't been updated since 2011)

Erubi is a simplified fork of Erubis that contains just the
parts that are generally needed (which includes the parts
that Rails uses).  The only intentional difference in
behavior is that it does not include support for <%=== tags
for debug output.  That could be added to the ActionView ERB
handler if it is desired.

The Erubis template handler remains in a deprecated state
so that code that accesses it directly does not break.  It
can be removed after Rails 5.1.

https://github.com/rails/rails/commit/7da8d76206271bdf200ea201f7e5a49afb9bc9e7

感谢版主,一个大大的赞!

Loving JavaScript 👍

早上看到了新闻,感觉以后 ng vue react 配合 webpack 直接整合到 rails 项目也挺好

最近在琢磨 ng2,还在考虑如何将它整合到 rails 中。ng2 自己可以起服务实现前后端分离,不知道有什么好一些的办法,整合到一个 rails 项目中?

Ruby China 的升级了一下,居然没错误,就多了些 warning

有个主要的是 Warning 是 attribute_changed? 这种用法不能在 after_save 的 callbacks 里面做了(Rails 5.2 将会更改行为)需要改用 saved_change_to_attribute? 代替。

而这类用法可能会大量存在,例如 Devise 里面,需要调整花不少时间。

@huacnlee

Parameterized Mailers 是说可以通过 with 方法传递一个 params 给 mailer,这样就可以像一般 controller-actions 的风格组织代码了(在 before_action 里准备数据,就像脚手架生成的代码那样) 参考源码:https://github.com/rails/rails/blob/2dadf73891431ff16709ae41ccb552a56ce34c32/actionmailer/lib/action_mailer/parameterized.rb#L98-L100

ActionMailer 里 一直是可以写 before_action 等的。

这样对于大项目的静态文件管理就灵活多了。

升级很顺畅,一把过 +1

Action View 采用 erubi 代替之前的 erubis 作为 ERB 的引擎。

你知道么!erubis 居然是一个自从 2011 年就没有更新过的 Gem。

JS 这个隔壁老王总算得逞了 😂

默默地开始学习 webpack

连 Rails 都开始拥抱 JS,看来 JS 统一前端不可阻挡啊

默默的等着

Reply to huacnlee

就知道你会第一时间升级 😄

Reply to martin91

太多 Warning,等等 Devise 先解决 😄

这些改进都还挺喜人的,感觉走在争取的方向上。下个项目 5.1 走起。

You need to Sign in before reply, if you don't have an account, please Sign up first.