public
Rubygem
Description: Aims to extend Ruby standard library, providing some useful tools that's not existed in the standard library, especially for functional programming.
Homepage: http://rubyforge.org/projects/ludy
Clone URL: git://github.com/godfat/ludy.git
ludy / test / ludy / test_rails_fields_filters.rb
100644 67 lines (51 sloc) 1.849 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
 
require File.join(File.dirname(__FILE__), '..', 'helper')
require 'ludy/rails_fields_filters'
require 'ludy/xhtml_formatter'
 
require 'rubygems'
require 'active_record'
 
File.delete 'tmp.sqlite3' if File.exists? 'tmp.sqlite3'
 
ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :database => 'tmp.sqlite3'
)
 
ActiveRecord::Schema.define(:version => 1) do
  create_table "articles", :force => true do |t|
    t.string "content"
    t.string "author"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
end
 
class Article < ActiveRecord::Base
  extend Ludy::RailsFieldsFilters
  rff_filters[:before_save] << [:content, Ludy::XhtmlFormatter.method(:format_autolink)]
  rff_filters[:before_save] << [:author, :titleize.to_proc]
 
  rff_filters[:after_save] << [:author, [:reverse.to_proc, :downcase.to_proc]]
 
  rff_filters[:after_save] << [
    [:content, :author],
    [lambda{ |input|
      Ludy::XhtmlFormatter.format_article input,
        :a, :pre, :object, :img, :b, :strong, :em, :li, :ul, :ol, :i
    }, :append_id]
  ]
 
  rff_setup
 
  def append_id content
    "#{content} #{id}"
  end
end
 
Article.create :author => 'lin jen-shin',
               :content => '今天是我一歲生日 http://godfat.org/ 真的嗎?'
 
class ArticleTest < Test::Unit::TestCase
  def test_article_fixture
    target = '今天是我一歲生日 <a href="http://godfat.org/" title="http://godfat.org/">http://godfat.org/</a> 真的嗎?'
 
    Article.find(:first).save
    assert_equal target, Article.find(:first).content
    assert Article.find_by_content(target).id == Article.find(:first).id
 
    article = Article.find(:first)
    assert_equal 'Lin Jen Shin', article.author
 
    article.save
    assert_equal 'nihs nej nil 1', article.author
    assert_equal "#{target} 1", article.content
  end
end