liquid-validations 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +39 -0
- data/LICENSE.txt +22 -0
- data/README.md +84 -0
- data/Rakefile +11 -0
- data/lib/liquid-validations.rb +56 -0
- data/lib/liquid-validations/version.rb +3 -0
- data/liquid-validations.gemspec +26 -0
- data/spec/liquid_validations_spec.rb +64 -0
- data/spec/spec_helper.rb +40 -0
- metadata +150 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
liquid-validations (1.0.1)
|
5
|
+
activerecord
|
6
|
+
liquid
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (3.2.12)
|
12
|
+
activesupport (= 3.2.12)
|
13
|
+
builder (~> 3.0.0)
|
14
|
+
activerecord (3.2.12)
|
15
|
+
activemodel (= 3.2.12)
|
16
|
+
activesupport (= 3.2.12)
|
17
|
+
arel (~> 3.0.2)
|
18
|
+
tzinfo (~> 0.3.29)
|
19
|
+
activesupport (3.2.12)
|
20
|
+
i18n (~> 0.6)
|
21
|
+
multi_json (~> 1.0)
|
22
|
+
arel (3.0.2)
|
23
|
+
builder (3.0.4)
|
24
|
+
i18n (0.6.4)
|
25
|
+
liquid (2.5.0)
|
26
|
+
minitest (4.6.2)
|
27
|
+
multi_json (1.6.1)
|
28
|
+
rake (10.0.3)
|
29
|
+
sqlite3 (1.3.7)
|
30
|
+
tzinfo (0.3.37)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
liquid-validations!
|
37
|
+
minitest
|
38
|
+
rake
|
39
|
+
sqlite3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Big Cartel, LLC
|
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,84 @@
|
|
1
|
+
# Liquid Validations [![Build Status](https://travis-ci.org/bigcartel/liquid-validations.png)](https://travis-ci.org/bigcartel/liquid-validations)
|
2
|
+
|
3
|
+
ActiveRecord style validations for Liquid content in your ActiveRecord models.
|
4
|
+
|
5
|
+
This gem makes 2 class methods available to your models:
|
6
|
+
* `validates_liquid_of` - Ensures that the liquid content is valid and has all opening/closing tags.
|
7
|
+
* `validates_presence_of_liquid_variable` - Useful to ensure your user content contains the specified Liquid variable(s).
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
`gem 'liquid-validations'` in your Gemfile.
|
12
|
+
|
13
|
+
Then `bundle install` and you should be all set.
|
14
|
+
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
In it's simplest form:
|
19
|
+
|
20
|
+
``` ruby
|
21
|
+
class Article < ActiveRecord::Base
|
22
|
+
validates_liquid_of :content
|
23
|
+
validates_presence_of_liquid_variable :content, variable: 'very_important_variable'
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
`validates_presence_of_liquid_variable` takes an optional `container` option like this:
|
28
|
+
|
29
|
+
``` ruby
|
30
|
+
validates_presence_of_liquid_variable :content,
|
31
|
+
variable: 'head_content',
|
32
|
+
container: 'head' # <html><head>{{ head_content }}</head>...
|
33
|
+
```
|
34
|
+
|
35
|
+
Which you may find useful for scoping. Or not.
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
Each of these methods make use of ActiveModel's underlying [`validates_each`](http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_each) method so any valid options passed in will be handed off.
|
40
|
+
|
41
|
+
A fully tricked out example would be:
|
42
|
+
|
43
|
+
``` ruby
|
44
|
+
validates_liquid_of :content, :on => :create,
|
45
|
+
:message => 'is invalid liquid.',
|
46
|
+
:allow_nil => true,
|
47
|
+
:allow_blank => true,
|
48
|
+
:if => :needs_validation?,
|
49
|
+
:unless => Proc.new { |record| record.new_record? }
|
50
|
+
```
|
51
|
+
|
52
|
+
Albeit, that's going a little overboard.
|
53
|
+
|
54
|
+
|
55
|
+
## Compatibility
|
56
|
+
|
57
|
+
Liquid Validations has been tested (used) with the following:
|
58
|
+
|
59
|
+
#### Rails
|
60
|
+
* 2.3 and up
|
61
|
+
* 3
|
62
|
+
|
63
|
+
#### Ruby
|
64
|
+
* 1.8
|
65
|
+
* 1.9
|
66
|
+
* 2.0
|
67
|
+
|
68
|
+
That's not to say it won't work with Rails 1.2 or 4, but we haven't yet tested either of those.
|
69
|
+
|
70
|
+
## What about Sinatra
|
71
|
+
|
72
|
+
This *should* work outside of Rails as long as your models are using ActiveRecord as the database mapper. However, this is just in theory, and hasn't been tested yet.
|
73
|
+
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
If you do find that something is busted, or think improvements can be made
|
78
|
+
|
79
|
+
* Fork it
|
80
|
+
* Create a topic branch - `git checkout -b fix_rails_1-0`
|
81
|
+
* Push to your branch - `git push origin fix_rails_1-0`
|
82
|
+
* Create a Pull Request from your branch
|
83
|
+
|
84
|
+
That's it!
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'liquid-validations/version'
|
2
|
+
|
3
|
+
module LiquidValidations
|
4
|
+
def validates_liquid_of(*attr_names)
|
5
|
+
configuration = { :message => I18n.translate('activerecord.errors.messages')[:invalid] }
|
6
|
+
configuration.update(attr_names.extract_options!)
|
7
|
+
|
8
|
+
validates_each attr_names, configuration do |record, attr_name, value|
|
9
|
+
errors = []
|
10
|
+
|
11
|
+
begin
|
12
|
+
template = Liquid::Template.parse(value.to_s)
|
13
|
+
errors += template.errors
|
14
|
+
rescue Exception => e
|
15
|
+
errors << e.message
|
16
|
+
end
|
17
|
+
|
18
|
+
for error in errors
|
19
|
+
record.errors.add(attr_name, friendly_liquid_error(error))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def validates_presence_of_liquid_variable(*attr_names)
|
25
|
+
configuration = { :message => I18n.translate('activerecord.errors.messages')[:invalid], :variable => nil, :container => nil }
|
26
|
+
configuration.update(attr_names.extract_options!)
|
27
|
+
|
28
|
+
raise(ArgumentError, "You must supply a variable to check for") if configuration[:variable].blank?
|
29
|
+
|
30
|
+
validates_each attr_names, configuration do |record, attr_name, value|
|
31
|
+
value = value.to_s
|
32
|
+
variable = configuration[:variable].to_s
|
33
|
+
variable_re = /\{\{\s*#{ variable }( .*)?\}\}/
|
34
|
+
container = configuration[:container].to_s
|
35
|
+
container_re = /<\s*#{ container }.*>.*#{ variable_re }.*<\/\s*#{ container }\s*>/im
|
36
|
+
|
37
|
+
if container.blank? && !(value =~ variable_re)
|
38
|
+
record.errors.add(attr_name, "You must include {{ #{ variable } }} in your #{ friendly_attr_name(attr_name) }")
|
39
|
+
elsif !container.blank? && !(value =~ container_re)
|
40
|
+
record.errors.add(attr_name, "You must include {{ #{ variable } }} inside the <#{ container }> tag of your #{ friendly_attr_name(attr_name) }")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def friendly_attr_name(attr_name)
|
48
|
+
attr_name.to_s.humanize.downcase
|
49
|
+
end
|
50
|
+
|
51
|
+
def friendly_liquid_error(error)
|
52
|
+
error.gsub(/liquid/i, '').gsub(/terminated with regexp:.+/, 'closed')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
ActiveRecord::Base.extend(LiquidValidations)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'liquid-validations/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'liquid-validations'
|
8
|
+
gem.version = LiquidValidations::VERSION
|
9
|
+
gem.authors = ['Matt Wigham', 'Joshua Abbott']
|
10
|
+
gem.email = ['dev@bigcartel.com']
|
11
|
+
gem.description = %q{ ActiveRecord style validations for Liquid content in your ActiveRecord models. See the README to get the lowdown. }
|
12
|
+
gem.summary = %q{ ActiveRecord style validations for Liquid content in your ActiveRecord models. }
|
13
|
+
gem.homepage = 'https://github.com/bigcartel/liquid-validations'
|
14
|
+
gem.license = 'MIT'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_dependency 'liquid'
|
22
|
+
gem.add_dependency 'activerecord'
|
23
|
+
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'sqlite3'
|
25
|
+
gem.add_development_dependency 'minitest'
|
26
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Mixin < ActiveRecord::Base
|
4
|
+
end
|
5
|
+
|
6
|
+
describe LiquidValidations do
|
7
|
+
it 'should provide the validates_liquid_of method to ActiveRecord subclasses' do
|
8
|
+
Mixin.must_respond_to(:validates_liquid_of)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should provide the validates_presence_of_liquid_variable method to ActiveRecord subclasses' do
|
12
|
+
Mixin.must_respond_to(:validates_presence_of_liquid_variable)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.validates_liquid_of' do
|
16
|
+
before do
|
17
|
+
Mixin.instance_eval do
|
18
|
+
validates_liquid_of :content
|
19
|
+
end
|
20
|
+
|
21
|
+
@mixin = Mixin.new
|
22
|
+
end
|
23
|
+
|
24
|
+
[ ' {{ Bad liquid ',
|
25
|
+
' {% Bad liquid ',
|
26
|
+
'{% for %}{% endfor' ].each do |bad_liquid|
|
27
|
+
it "the record should be invalid when there is a liquid parsing error for #{ bad_liquid }" do
|
28
|
+
@mixin.content = bad_liquid
|
29
|
+
@mixin.valid?.must_equal false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should include the errors in the errors object' do
|
34
|
+
@mixin.content = '{{ unclosed variable '
|
35
|
+
@mixin.valid?
|
36
|
+
@mixin.errors.must_include(:content)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '.validates_presence_of_liquid_variable' do
|
41
|
+
before do
|
42
|
+
Mixin.instance_eval do
|
43
|
+
validates_presence_of_liquid_variable :content, :variable => 'josh_is_awesome'
|
44
|
+
end
|
45
|
+
|
46
|
+
@mixin = Mixin.new
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'must be configured properly' do
|
50
|
+
proc { Mixin.instance_eval { validates_presence_of_liquid_variable :content } }.must_raise ArgumentError
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'the record should be invalid when the specified variable is not present' do
|
54
|
+
@mixin.content = '{{ josh_is_not_awesome }}'
|
55
|
+
@mixin.valid?.must_equal false
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should include the errors in the errors object' do
|
59
|
+
@mixin.content = '{{ josh_is_not_awesome }}'
|
60
|
+
@mixin.valid?
|
61
|
+
@mixin.errors.must_include(:content)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_record'
|
5
|
+
require 'liquid'
|
6
|
+
require 'liquid-validations'
|
7
|
+
require 'minitest/spec'
|
8
|
+
require 'minitest/autorun'
|
9
|
+
require 'minitest/pride'
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:', :database => 'liquid_validations_test.db')
|
12
|
+
|
13
|
+
ActiveRecord::Migration.verbose = false
|
14
|
+
|
15
|
+
def setup_db
|
16
|
+
ActiveRecord::Schema.define(:version => 1) do
|
17
|
+
create_table :mixins do |t|
|
18
|
+
t.column :content, :text
|
19
|
+
t.column :created_at, :datetime
|
20
|
+
t.column :updated_at, :datetime
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def teardown_db
|
26
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
27
|
+
ActiveRecord::Base.connection.drop_table(table)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class MiniTest::Spec
|
32
|
+
before do
|
33
|
+
setup_db
|
34
|
+
end
|
35
|
+
|
36
|
+
after do
|
37
|
+
teardown_db
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: liquid-validations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matt Wigham
|
14
|
+
- Joshua Abbott
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2013-03-11 00:00:00 -06:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: liquid
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activerecord
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rake
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
type: :development
|
63
|
+
version_requirements: *id003
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
name: sqlite3
|
66
|
+
prerelease: false
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
type: :development
|
77
|
+
version_requirements: *id004
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: minitest
|
80
|
+
prerelease: false
|
81
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
type: :development
|
91
|
+
version_requirements: *id005
|
92
|
+
description: " ActiveRecord style validations for Liquid content in your ActiveRecord models. See the README to get the lowdown. "
|
93
|
+
email:
|
94
|
+
- dev@bigcartel.com
|
95
|
+
executables: []
|
96
|
+
|
97
|
+
extensions: []
|
98
|
+
|
99
|
+
extra_rdoc_files: []
|
100
|
+
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- .travis.yml
|
104
|
+
- Gemfile
|
105
|
+
- Gemfile.lock
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/liquid-validations.rb
|
110
|
+
- lib/liquid-validations/version.rb
|
111
|
+
- liquid-validations.gemspec
|
112
|
+
- spec/liquid_validations_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
has_rdoc: true
|
115
|
+
homepage: https://github.com/bigcartel/liquid-validations
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
hash: 3
|
129
|
+
segments:
|
130
|
+
- 0
|
131
|
+
version: "0"
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
hash: 3
|
138
|
+
segments:
|
139
|
+
- 0
|
140
|
+
version: "0"
|
141
|
+
requirements: []
|
142
|
+
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 1.6.2
|
145
|
+
signing_key:
|
146
|
+
specification_version: 3
|
147
|
+
summary: ActiveRecord style validations for Liquid content in your ActiveRecord models.
|
148
|
+
test_files:
|
149
|
+
- spec/liquid_validations_spec.rb
|
150
|
+
- spec/spec_helper.rb
|