html_safe_attribute 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +49 -0
- data/Rakefile +7 -0
- data/html_safe_attribute.gemspec +25 -0
- data/lib/html_safe_attribute.rb +10 -0
- data/lib/html_safe_attribute/hook.rb +13 -0
- data/lib/html_safe_attribute/instance_methods.rb +13 -0
- data/lib/html_safe_attribute/railtie.rb +15 -0
- data/lib/html_safe_attribute/version.rb +3 -0
- data/spec/html_safe_attribute_spec.rb +32 -0
- data/spec/spec_helper.rb +18 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# HtmlSaveAttribute
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
This `html_safe_attribute` extension provides the capabilities of
|
6
|
+
declaring attributes as html_safe in your Rails models.
|
7
|
+
|
8
|
+
Sometimes information coming from the DB or other sources contains plain
|
9
|
+
HTML and the content is completely trustable. This gem solves the
|
10
|
+
problem of repeating `.html_safe` everywhere in your views for specific
|
11
|
+
model attributes.
|
12
|
+
|
13
|
+
It only applies when content in attribute is a String. When it is
|
14
|
+
Numeric, it just does nothing.
|
15
|
+
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
In your Gemfile:
|
20
|
+
|
21
|
+
gem 'html_safe_attribute'
|
22
|
+
|
23
|
+
Or, from the command line:
|
24
|
+
|
25
|
+
gem install html_safe_attribute
|
26
|
+
|
27
|
+
## Example
|
28
|
+
|
29
|
+
Use `html_safe` method in the your model:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Job < ActiveRecord::Base
|
33
|
+
html_safe :body
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing to `html_safe_attribute`
|
38
|
+
|
39
|
+
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
40
|
+
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
41
|
+
- Fork the project
|
42
|
+
- Start a feature/bugfix branch
|
43
|
+
- Commit and push until you are happy with your contribution
|
44
|
+
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
45
|
+
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
46
|
+
|
47
|
+
## Copyright
|
48
|
+
|
49
|
+
Copyright (c) 2013 Santi Bel, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "html_safe_attribute/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "html_safe_attribute"
|
7
|
+
s.version = HtmlSafeAttribute::VERSION
|
8
|
+
s.authors = ["Santi Bel"]
|
9
|
+
s.email = ["santiago.bel@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Declare html_safe attributes in your Rails models}
|
12
|
+
s.description = %q{Use 'html_safe :attribute' to declare html_safe attributes in your Rails models}
|
13
|
+
|
14
|
+
s.rubyforge_project = "html_safe_attribute"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
|
22
|
+
s.add_development_dependency "rspec", "~> 2.13"
|
23
|
+
|
24
|
+
s.add_runtime_dependency "rails", "~> 3.2"
|
25
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "html_safe_attribute/version"
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), "html_safe_attribute/railtie.rb")
|
5
|
+
|
6
|
+
module HtmlSafeAttribute
|
7
|
+
autoload :Hook, File.join(File.dirname(__FILE__), "html_safe_attribute/hook")
|
8
|
+
autoload :InstanceMethods, File.join(File.dirname(__FILE__), "html_safe_attribute/instance_methods")
|
9
|
+
end
|
10
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module HtmlSafeAttribute::Hook
|
2
|
+
|
3
|
+
def html_safe(attr_name)
|
4
|
+
|
5
|
+
define_method(:"#{attr_name.to_s}_with_html_safe") do
|
6
|
+
html_safe_if_string(send(:"#{attr_name}_without_html_safe"))
|
7
|
+
end
|
8
|
+
alias_method_chain attr_name.to_sym, :html_safe
|
9
|
+
|
10
|
+
include HtmlSafeAttribute::InstanceMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'html_safe_attribute'
|
3
|
+
|
4
|
+
begin
|
5
|
+
module HtmlSafeAttribute
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
config.to_prepare do
|
8
|
+
Object.send(:extend, HtmlSafeAttribute::Hook)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
rescue
|
13
|
+
p $!, $!.message
|
14
|
+
raise $!
|
15
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
Object.send(:extend, HtmlSafeAttribute::Hook)
|
4
|
+
|
5
|
+
class BasicModel
|
6
|
+
attr_accessor :my_attribute
|
7
|
+
html_safe :my_attribute
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
describe HtmlSafeAttribute do
|
12
|
+
|
13
|
+
describe "html_safe attributes" do
|
14
|
+
|
15
|
+
subject do
|
16
|
+
model = BasicModel.new
|
17
|
+
model.my_attribute = "line 1 <br /> line 2"
|
18
|
+
model
|
19
|
+
end
|
20
|
+
|
21
|
+
it "must be html_safe for text" do
|
22
|
+
expect(subject.my_attribute.html_safe?).to be_true
|
23
|
+
expect(subject.my_attribute).to eq("line 1 <br /> line 2")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "must be normal for numeric value" do
|
27
|
+
subject.my_attribute = 2
|
28
|
+
expect(subject.my_attribute).to eq(2)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'bundler'
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
|
8
|
+
require 'active_support'
|
9
|
+
require 'html_safe_attribute'
|
10
|
+
require 'rspec'
|
11
|
+
require 'rspec/autorun'
|
12
|
+
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.color_enabled = true
|
16
|
+
config.formatter = 'documentation'
|
17
|
+
end
|
18
|
+
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: html_safe_attribute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Santi Bel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.13'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.13'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rails
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.2'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.2'
|
46
|
+
description: Use 'html_safe :attribute' to declare html_safe attributes in your Rails
|
47
|
+
models
|
48
|
+
email:
|
49
|
+
- santiago.bel@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- html_safe_attribute.gemspec
|
59
|
+
- lib/html_safe_attribute.rb
|
60
|
+
- lib/html_safe_attribute/hook.rb
|
61
|
+
- lib/html_safe_attribute/instance_methods.rb
|
62
|
+
- lib/html_safe_attribute/railtie.rb
|
63
|
+
- lib/html_safe_attribute/version.rb
|
64
|
+
- spec/html_safe_attribute_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: ''
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project: html_safe_attribute
|
86
|
+
rubygems_version: 1.8.25
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Declare html_safe attributes in your Rails models
|
90
|
+
test_files: []
|