Skip to content
This repository has been archived by the owner on Oct 13, 2022. It is now read-only.

Commit

Permalink
增加 ActiveModel 的测试,README 增加相关说明; #3
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Aug 20, 2014
1 parent a1d6cc2 commit 2f16d08
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Gemfile
@@ -1,3 +1,5 @@
source 'https://rubygems.org'
gemspec
gem 'rspec'
gem 'rspec'
gem 'activesupport'
gem 'activemodel'
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -7,12 +7,16 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activemodel (4.1.4)
activesupport (= 4.1.4)
builder (~> 3.1)
activesupport (4.1.4)
i18n (~> 0.6, >= 0.6.9)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.1)
tzinfo (~> 1.1)
builder (3.2.2)
diff-lcs (1.2.4)
i18n (0.6.11)
json (1.8.1)
Expand All @@ -33,5 +37,7 @@ PLATFORMS
ruby

DEPENDENCIES
activemodel
activesupport
auto-correct!
rspec
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -36,6 +36,34 @@ irb> "bootstrap control-group对齐问题".auto_correct!
Bootstrap control-group 对齐问
```

## ActiveModel 的 changed? 相关提示

由于 auto_correct 是直接修改原始变量值的,你可能会遇到由于没有引发 `ActiveModel::Dirty` 的相关 callback 事件而导致下面这种场景 ActiveModel 不会将相关的字段写入到数据库。

比如下面的情况:

```ruby
class Topic < ActiveRecord::Base
before_save do
self.title.auto_correct!
end
end
```

正确的方式:

```ruby
class Topic < ActiveRecord::Base
before_save do
# 引发 ActiveModel::Dirty 的 change
self.title_will_change!
self.title.auto_correct!
end
end
```

具体请参见 [ActiveModel::Dirty 的文档](http://api.rubyonrails.org/classes/ActiveModel/Dirty.html)

## 性能

详见 Rakefile
Expand Down
21 changes: 21 additions & 0 deletions spec/text_spec.rb
@@ -1,5 +1,14 @@
# coding: utf-8
require "auto-correct"
require "active_support/all"
require "active_model"

class User
include ActiveModel::Dirty
attr_accessor :name

define_attribute_methods :name
end

describe "AutoCorrect" do
let(:text) { "" }
Expand Down Expand Up @@ -171,5 +180,17 @@
it { "全新的ruby web框架:lotus".auto_correct!.should == "全新的 Ruby web 框架:Lotus" }
it { "grape写纯 api,选择哪个oauth gem好呢?".auto_correct!.should == "Grape 写纯 API,选择哪个 OAuth gem 好呢?" }
end

context 'with ActiveModel field_changed?' do
it {
u = User.new
u.name = "foo的"
u.name_changed?.should == false
u.name_will_change!
u.name.auto_correct!.should == "foo 的"
u.name_change.should == ["foo的","foo 的"]
u.name_changed?.should == true
}
end
end
end

0 comments on commit 2f16d08

Please sign in to comment.