public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
simplified filtered_column so only one filter is processed at a time

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@1865 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Mon Aug 21 12:40:39 -0700 2006
commit  abc8fbb55c54b901b173f4c6894b2353830b7250
tree    36a0c6c1a74c009fa8fa414733a1c665ccad487b
parent  db94381279c32d84567a081c9a16c40916c3ef7d
...
1
2
 
3
4
5
6
7
8
9
10
11
12
13
14
15
...
1
 
2
3
4
5
 
 
 
 
 
 
 
 
6
7
0
@@ -1,14 +1,6 @@
0
 class Content < ActiveRecord::Base
0
- filtered_column :body, :excerpt, :only => :textile_filter
0
+ filtered_column :body, :excerpt
0
   validates_presence_of :body
0
   belongs_to :user, :with_deleted => true
0
   belongs_to :site
0
-
0
- def filters=(filters)
0
- write_attribute :filter, (filters.blank? ? nil : filters.first.to_s)
0
- end
0
-
0
- def filters
0
- filter.blank? ? [] : [filter.to_sym]
0
- end
0
 end
0
\ No newline at end of file
...
11
12
13
14
15
16
17
 
 
18
19
20
...
22
23
24
25
26
27
28
29
30
31
32
 
33
34
35
36
37
38
39
 
 
 
 
 
40
41
42
...
11
12
13
 
14
15
16
17
18
19
20
21
...
23
24
25
 
 
 
 
26
27
28
 
29
30
31
32
 
 
 
 
33
34
35
36
37
38
39
40
0
@@ -11,10 +11,11 @@ module FilteredColumn
0
           send :include, InstanceMethods
0
           class_inheritable_accessor :filtered_attributes, :filtered_options
0
           before_save :process_filters
0
- serialize :filters, Array
0
         end
0
         
0
         options = names.last.is_a?(Hash) ? names.pop : {}
0
+ options[:only] = [options[:only]].flatten.compact
0
+ options[:except] = [options[:except]].flatten.compact
0
         names.each do |name|
0
           (self.filtered_options ||= {})[name] = options
0
           (self.filtered_attributes ||= []) << name
0
@@ -22,21 +23,18 @@ module FilteredColumn
0
       end
0
 
0
       module InstanceMethods
0
- def filters=(value)
0
- write_attribute :filters, [value].flatten.collect { |v| v.blank? ? nil : v.to_sym }.compact.uniq
0
- end
0
-
0
         protected
0
           def process_filters
0
             filtered_attributes.each do |attr_name|
0
- send "#{attr_name}_html=", FilteredColumn::Processor.process_filters(filters_for_attribute(attr_name), send(attr_name).to_s.dup)
0
+ send "#{attr_name}_html=", FilteredColumn::Processor.process_filter(filter_for_attribute(attr_name), send(attr_name).to_s.dup)
0
             end
0
           end
0
           
0
- def filters_for_attribute(attr_name)
0
- filters = self.filters
0
- filters ||= filtered_options[attr_name][:only]
0
- filters ||= (FilteredColumn.filters.keys - [filtered_options[attr_name][:except]].flatten || [])
0
+ def filter_for_attribute(attr_name)
0
+ return nil if filter.blank? ||
0
+ (!filtered_options[attr_name][:only].blank? && !filtered_options[attr_name][:only].include?(filter.to_sym)) ||
0
+ (!filtered_options[attr_name][:except].blank? && filtered_options[attr_name][:except].include?(filter.to_sym))
0
+ filter
0
           end
0
       end
0
     end
...
10
11
12
13
 
14
15
16
 
17
18
19
20
 
 
 
 
21
22
23
...
10
11
12
 
13
14
15
 
16
17
18
19
 
20
21
22
23
24
25
26
0
@@ -10,14 +10,17 @@ module FilteredColumn
0
       ].freeze
0
 
0
     class << self
0
- def process_filters(filters, text)
0
+ def process_filter(filter_name, text)
0
         return '' if text.blank?
0
         process_macros(text) if FilteredColumn.macros.any?
0
- [filters].flatten.inject(text) { |txt, filter_name| filter_text filter_name, txt }
0
+ filter_text filter_name, text
0
       end
0
 
0
       def filter_text(filter_name, text_to_filter)
0
- FilteredColumn.filters[filter_name.to_sym].filter text_to_filter
0
+ return text_to_filter if filter_name.blank?
0
+ filter_class = FilteredColumn.filters[filter_name.to_sym]
0
+ return text_to_filter if filter_class.nil?
0
+ filter_class.filter text_to_filter
0
       end
0
       
0
       def process_macros(text_to_filter)
...
9
10
11
12
 
13
14
15
...
49
50
51
52
 
53
54
55
...
9
10
11
 
12
13
14
15
...
49
50
51
 
52
53
54
55
0
@@ -9,7 +9,7 @@ Test::Unit::TestCase.class_eval do
0
     FilteredColumn::Processor.called_filters = []
0
     filtered = yield
0
     filtered.save if filtered
0
- assert_equal filters.length, (FilteredColumn::Processor.called_filters & filters).length, "#{filters.join(', ')} expected, #{FilteredColumn::Processor.called_filters.join(', ')} called"
0
+ assert_equal filters.length, (FilteredColumn::Processor.called_filters & filters.collect(&:to_s)).length, "#{filters.join(', ')} expected, #{FilteredColumn::Processor.called_filters.join(', ')} called"
0
   end
0
 
0
   def assert_no_filters_called_on(klass, &block)
0
@@ -49,7 +49,7 @@ class Article < ActiveRecord::Base
0
   column :textile_and_markdown_body_html, :string
0
   column :no_textile_body, :string
0
   column :no_textile_body_html, :string
0
- column :filters, :text
0
+ column :filter, :string
0
   column :sample_macro_body, :string
0
   column :sample_macro_body_html, :string
0
 
...
11
12
13
14
 
15
16
17
...
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
68
69
70
71
72
73
74
75
76
77
78
...
11
12
13
 
14
15
16
17
...
37
38
39
 
 
 
 
 
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
0
@@ -11,7 +11,7 @@ class FilteredColumnTest < Test::Unit::TestCase
0
 
0
     define_method "test_should_filter_model_attribute_with_#{filter_name}" do
0
       assert_filters_called_on "#{filter_name}_filter".to_sym do
0
- a = Article.new :body => values[:input], :filters => "#{filter_name}_filter"
0
+ a = Article.new :body => values[:input], :filter => "#{filter_name}_filter"
0
         a.save
0
         assert_equal values[:output], a.body_html
0
       end
0
@@ -37,42 +37,7 @@ class FilteredColumnTest < Test::Unit::TestCase
0
     assert_equal :smartypants_filter, FilteredColumn::Filters::SmartypantsFilter.filter_key
0
   end
0
 
0
- def test_should_not_bomb_on_nil_filters
0
- a = Article.new :filters => nil
0
- assert_equal [], a.filters
0
- end
0
-
0
   def test_should_call_no_filters_with_no_data
0
     assert_no_filters_called_on(Article) { Article.new }
0
   end
0
-
0
- def test_should_call_all_default_filters
0
- assert_filters_called_on *FilteredColumn.filters.keys do
0
- Article.new(:body => 'foo')
0
- end
0
- end
0
-
0
- def test_should_call_only_textile
0
- assert_filters_called_on :textile_filter do
0
- Article.new(:textile_body => 'foo')
0
- end
0
- end
0
-
0
- def test_should_override_standard_filters
0
- assert_filters_called_on :textile_filter do
0
- Article.new(:textile_body => 'foo', :filters => [:textile_filter])
0
- end
0
- end
0
-
0
- def test_should_call_only_textile_and_markdown
0
- assert_filters_called_on :textile_filter, :markdown_filter do
0
- Article.new(:textile_and_markdown_body => 'foo')
0
- end
0
- end
0
-
0
- def test_should_not_call_textile
0
- assert_filters_called_on :markdown_filter do
0
- Article.new(:no_textile_body => 'foo')
0
- end
0
- end
0
 end

Comments

    No one has commented yet.