has_wysiwyg_content 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use @rubygems
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rails', '> 3.2.0'
5
+ gem 'sqlite3'
6
+ end
7
+
8
+ # Specify your gem's dependencies in has_wysiwyg_content.gemspec
9
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Philip Hallstrom
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # HasWysiwygContent
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/has_wysiwyg_content.png)](http://badge.fury.io/rb/has_wysiwyg_content)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'has_wysiwyg_content'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install has_wysiwyg_content
18
+
19
+ ## Usage
20
+
21
+ TBD...
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib'
6
+ test.libs << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'has_wysiwyg_content/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "has_wysiwyg_content"
8
+ gem.version = HasWysiwygContent::VERSION
9
+ gem.authors = ["Philip Hallstrom"]
10
+ gem.email = ["philip@pjkh.com"]
11
+ gem.description = %q{}
12
+ gem.summary = %q{}
13
+ gem.homepage = "https://github.com/phallstrom/has_wysiwyg_content"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,51 @@
1
+ require "has_wysiwyg_content/version"
2
+ require "has_wysiwyg_content/action_view_helper"
3
+
4
+ module HasWysiwygContent
5
+ OPTIONS = {
6
+ :url_regexp => nil
7
+ }
8
+
9
+ module Extensions
10
+
11
+ def self.included(base)
12
+ base.extend(ClassMethods)
13
+ end
14
+
15
+ module ClassMethods
16
+ def has_wysiwyg_content(*fields)
17
+ include HasWysiwygContent::Extensions::InstanceMethods
18
+
19
+ const_set('WYSIWYG_FIELDS', fields.map(&:to_s))
20
+
21
+ before_save {|o| cleanse_wysiwyg_content }
22
+
23
+ define_method("self.class.wysiwyg_attributes") { (self.class.column_names & fields.map(&:to_s)) }
24
+ class_eval <<-"EOV"
25
+ class << self
26
+ def wysiwyg_attributes
27
+ column_names & WYSIWYG_FIELDS
28
+ end
29
+ end
30
+ EOV
31
+ end
32
+ end
33
+
34
+ module InstanceMethods
35
+ def cleanse_wysiwyg_content
36
+ self.class.wysiwyg_attributes.each do |field|
37
+ regexp = HasWysiwygContent::OPTIONS[:url_regexp]
38
+ send("#{field}=", send(field).gsub(regexp, '\\1')) unless send(field).blank? || regexp.nil?
39
+ end
40
+ end
41
+
42
+ def update_wysiwyg_attributes(params = {})
43
+ wysiwyg_params = params.delete_if{|k,v| !self.class.wysiwyg_attributes.include? k.to_s}
44
+ update_attributes(wysiwyg_params)
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ ActiveRecord::Base.send :include, HasWysiwygContent::Extensions
@@ -0,0 +1,23 @@
1
+ module HasWysiwygContent
2
+ module TagHelper
3
+
4
+ def wysiwyg(field, substitutions = {}, &block)
5
+ block_captured = false
6
+ field.to_s.gsub(/\{\{(.*?)\}\}/) do |match|
7
+ str = $1
8
+ if str =~ /mailto\s*:\s*([^:\s]+)(?:\s*:\s*(.+))?/
9
+ mail_to($1, $2, :encode => :javascript)
10
+ elsif str == 'yield' && block_given?
11
+ block_captured = true
12
+ capture(&block)
13
+ else
14
+ var, method = str.split('.', 2)
15
+ substitutions.key?(var.to_sym) ? substitutions[var.to_sym].send(method) : match
16
+ end
17
+ end + ( block_given? && !block_captured ? capture(&block) : '' )
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ ActionView::Base.send :include, HasWysiwygContent::TagHelper
@@ -0,0 +1,3 @@
1
+ module HasWysiwygContent
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,92 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require 'action_controller'
5
+ require 'has_wysiwyg_content'
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
8
+
9
+ def setup_db
10
+ silence_stream(STDOUT) do
11
+ ActiveRecord::Schema.define(:version => 1) do
12
+ create_table :widgets do |t|
13
+ t.column :content, :text
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ def teardown_db
20
+ silence_stream(STDOUT) do
21
+ ActiveRecord::Base.connection.tables.each do |table|
22
+ ActiveRecord::Base.connection.drop_table(table)
23
+ end
24
+ end
25
+ end
26
+
27
+ class Widget < ActiveRecord::Base
28
+ has_wysiwyg_content :content
29
+ #def self.table_name() "widgets" end
30
+ end
31
+
32
+ ################################################################################
33
+
34
+ class TestHasWysiwygContent < ActiveSupport::TestCase
35
+ include ActionView::Helpers::TagHelper
36
+ include ActionView::Helpers::UrlHelper
37
+ include ActionView::Helpers::JavaScriptHelper
38
+ include HasWysiwygContent::TagHelper
39
+
40
+ def setup
41
+ setup_db
42
+ @widget = Widget.new(
43
+ :content => 'English. {{some_var.foo}}. {{invalid.foo}} {{{{mailto:philip@pjkh.com}}. <a href="http://www.pjkh.com/foo">click</a>. <a href=\'http://www.pjkh.com/bar\'>me</a>. The End.'
44
+ )
45
+ end
46
+
47
+ def teardown
48
+ teardown_db
49
+ end
50
+
51
+ def test_responds_to_update_wysiwyg_attributes
52
+ assert_respond_to @widget, :update_wysiwyg_attributes
53
+ end
54
+
55
+ def test_saved_content_is_not_cleansed_by_default
56
+ HasWysiwygContent::OPTIONS[:url_regexp] = nil
57
+ orig_content = @widget.content.dup
58
+ @widget.save!
59
+ assert_equal orig_content, @widget.content
60
+ end
61
+
62
+ def test_saved_content_is_cleansed_if_regexp_provided
63
+ HasWysiwygContent::OPTIONS[:url_regexp] = %r!http://www.pjkh.com!
64
+ @widget.save!
65
+ assert_equal 'English. {{some_var.foo}}. {{invalid.foo}} {{{{mailto:philip@pjkh.com}}. <a href="/foo">click</a>. <a href=\'/bar\'>me</a>. The End.', @widget.content
66
+ end
67
+
68
+ def test_wysiwyg_fields_only_contains_table_fields
69
+ assert_equal %w[content], Widget.wysiwyg_attributes
70
+ end
71
+
72
+ #
73
+ # Testing the helper...
74
+ #
75
+
76
+ def test_parsed_content_encodes_emails
77
+ assert_no_match /philip@pjkh.com/, wysiwyg(@widget.content)
78
+ end
79
+
80
+ def test_parsed_content_converts_valid_substitutions
81
+ @my_var = Object.new
82
+ def @my_var.foo; "VALUE_OF_MY_VAR"; end
83
+ assert_no_match /\{\{some_var.foo\}\}/, wysiwyg(@widget.content, :some_var => @my_var)
84
+ assert_match /VALUE_OF_MY_VAR/, wysiwyg(@widget.content, :some_var => @my_var)
85
+ end
86
+
87
+ def test_parsed_content_does_not_convert_nonexistent_substitutions
88
+ assert_match /\{\{invalid.foo\}\}/, wysiwyg(@widget.content)
89
+ end
90
+
91
+ end
92
+
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_wysiwyg_content
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philip Hallstrom
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ''
15
+ email:
16
+ - philip@pjkh.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rvmrc
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - has_wysiwyg_content.gemspec
28
+ - lib/has_wysiwyg_content.rb
29
+ - lib/has_wysiwyg_content/action_view_helper.rb
30
+ - lib/has_wysiwyg_content/version.rb
31
+ - test/test_has_wysiwyg_content.rb
32
+ homepage: https://github.com/phallstrom/has_wysiwyg_content
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ segments:
45
+ - 0
46
+ hash: 1817417977407933255
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ segments:
54
+ - 0
55
+ hash: 1817417977407933255
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.24
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: ''
62
+ test_files:
63
+ - test/test_has_wysiwyg_content.rb