acts_as_formatted 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acts_as_formatted/acts_as_formatted.rb +126 -0
- data/lib/acts_as_formatted/formatters/link_formatter.rb +9 -0
- data/lib/acts_as_formatted/formatters/noop_formatter.rb +5 -0
- data/lib/acts_as_formatted/formatters/repair_html_formatter.rb +7 -0
- data/lib/acts_as_formatted/formatters/simple_formatter.rb +8 -0
- data/lib/acts_as_formatted/hash.rb +5 -0
- data/lib/acts_as_formatted/timestamps.rb +15 -0
- data/lib/acts_as_formatted.rb +10 -0
- metadata +74 -0
@@ -0,0 +1,126 @@
|
|
1
|
+
module ActsAsFormatted
|
2
|
+
mattr_accessor :default_options
|
3
|
+
self.default_options = {
|
4
|
+
:fields => :text,
|
5
|
+
:formatters => :simple
|
6
|
+
}
|
7
|
+
|
8
|
+
module ActsMethods
|
9
|
+
def acts_as_formatted(options = {})
|
10
|
+
return if acts_as_formatted?
|
11
|
+
|
12
|
+
initialize_format_configuration
|
13
|
+
set_format_configuration(options)
|
14
|
+
|
15
|
+
define_format_hooks
|
16
|
+
extend_class_for_acts_as_formatted
|
17
|
+
end
|
18
|
+
|
19
|
+
def acts_as_formatted?
|
20
|
+
self.included_modules.include?(ActsAsFormatted::InstanceMethods)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def initialize_format_configuration
|
25
|
+
cattr_accessor :format_configuration
|
26
|
+
self.format_configuration = {}
|
27
|
+
end
|
28
|
+
|
29
|
+
def set_format_configuration(options)
|
30
|
+
self.format_configuration[:fields] = normalized_format_configuration(options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def normalized_format_configuration(options)
|
34
|
+
config_options = ActsAsFormatted.default_options.merge(options)
|
35
|
+
fields = convert_fields_option_to_array(config_options[:fields])
|
36
|
+
[fields].flatten.inject({}) do |hash, field|
|
37
|
+
key, value = normalized_format_configuration_for(field, config_options)
|
38
|
+
hash[key] = value
|
39
|
+
hash
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def normalized_format_configuration_for(field, options)
|
44
|
+
field_name = field.is_a?(Hash) ? field.keys.first.to_sym : field.to_sym
|
45
|
+
field_options = field.is_a?(Hash) ? field[field_name] : (ActsAsFormatted.default_options.merge(options)).without(:fields)
|
46
|
+
formatters = field_options.is_a?(Hash) ? field_options[:formatters] : field_options
|
47
|
+
formatters ||= options[:formatters]
|
48
|
+
formatters = [formatters].flatten
|
49
|
+
|
50
|
+
return field_name, {
|
51
|
+
:formatters => formatters,
|
52
|
+
:formatted_field => options[:formatted_field] || :"formatted_#{field_name}"
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def convert_fields_option_to_array(option)
|
57
|
+
if option.is_a?(Hash)
|
58
|
+
option.collect { |key, value| { key => value } }
|
59
|
+
else
|
60
|
+
option
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def define_format_hooks
|
65
|
+
self.before_save :update_formatted_content
|
66
|
+
end
|
67
|
+
|
68
|
+
def extend_class_for_acts_as_formatted
|
69
|
+
self.extend ActsAsFormatted::ClassMethods
|
70
|
+
self.send :include, ActsAsFormatted::InstanceMethods
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
module ClassMethods
|
75
|
+
def update_all_formatted_content!(timestamps = false)
|
76
|
+
timestamps ? update_all_formatted_content_with_timestamps! : update_all_formatted_content_without_timestamps!
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def update_all_formatted_content_with_timestamps!
|
82
|
+
all.each do |instance|
|
83
|
+
instance.update_formatted_content!
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def update_all_formatted_content_without_timestamps!
|
88
|
+
without_timestamps do
|
89
|
+
update_all_formatted_content_with_timestamps!
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
module InstanceMethods
|
95
|
+
def formatters_for(field_name)
|
96
|
+
@formatters ||= {}
|
97
|
+
@formatters[field_name] ||= begin
|
98
|
+
formatters = self.class.format_configuration[:fields][field_name][:formatters]
|
99
|
+
formatters.collect { |formatter| "#{formatter}_formatter".camelize.constantize.new }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def reformat(field_name)
|
104
|
+
text = attributes[field_name.to_s]
|
105
|
+
formatters_for(field_name).each do |formatter|
|
106
|
+
text = formatter.format_text(text)
|
107
|
+
end unless text.nil?
|
108
|
+
text
|
109
|
+
end
|
110
|
+
|
111
|
+
def update_formatted_content
|
112
|
+
self.class.format_configuration[:fields].each do |field_name, field_options|
|
113
|
+
send(:"#{field_options[:formatted_field]}=", reformat(field_name))
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def update_formatted_content!
|
118
|
+
update_formatted_content
|
119
|
+
save
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.included(receiver)
|
124
|
+
receiver.extend(ActsAsFormatted::ActsMethods)
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveRecord::Timestamps
|
2
|
+
def without_timestamps(&block)
|
3
|
+
rec_ts = ActiveRecord::Base.record_timestamps
|
4
|
+
applied_to = self.is_a?(Class) ? self : self.class
|
5
|
+
applied_to.record_timestamps = false
|
6
|
+
begin
|
7
|
+
yield
|
8
|
+
ensure
|
9
|
+
applied_to.record_timestamps = rec_ts
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveRecord::Base.send(:extend, ActiveRecord::Timestamps)
|
15
|
+
ActiveRecord::Base.send(:include, ActiveRecord::Timestamps)
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Include hook code here
|
2
|
+
require 'acts_as_formatted/timestamps'
|
3
|
+
require 'acts_as_formatted/acts_as_formatted'
|
4
|
+
require 'acts_as_formatted/hash'
|
5
|
+
|
6
|
+
Dir.glob(File.dirname(__FILE__) + '/acts_as_formatted//formatters/*.rb').sort.each do |formatter|
|
7
|
+
require formatter
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveRecord::Base.send :include, ActsAsFormatted
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_formatted
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- imedo GmbH
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-25 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description:
|
23
|
+
email: entwickler@imedo.de
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/acts_as_formatted/acts_as_formatted.rb
|
32
|
+
- lib/acts_as_formatted/formatters/link_formatter.rb
|
33
|
+
- lib/acts_as_formatted/formatters/noop_formatter.rb
|
34
|
+
- lib/acts_as_formatted/formatters/repair_html_formatter.rb
|
35
|
+
- lib/acts_as_formatted/formatters/simple_formatter.rb
|
36
|
+
- lib/acts_as_formatted/hash.rb
|
37
|
+
- lib/acts_as_formatted/timestamps.rb
|
38
|
+
- lib/acts_as_formatted.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://www.imedo.de
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Save processed version of a text in the database.
|
73
|
+
test_files: []
|
74
|
+
|