liquid_loader 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/liquid_loader.rb CHANGED
@@ -4,3 +4,4 @@ require 'active_support/dependencies'
4
4
  require "liquid_loader/version"
5
5
  require 'liquid_loader/context'
6
6
  require 'liquid_loader/liquid_model'
7
+ require 'liquid_loader/liquid_validations'
@@ -0,0 +1,62 @@
1
+ module LiquidValidations
2
+ def validates_liquid_of(*attr_names)
3
+ configuration = { :message => I18n.translate('activerecord.errors.messages')[:invalid], :on => :save }
4
+ configuration.update(attr_names.extract_options!)
5
+
6
+ validates_each(attr_names, configuration) do |record, attr_name, value|
7
+ errors = []
8
+
9
+ begin
10
+ template = Liquid::Template.parse(value.to_s)
11
+ errors += template.errors
12
+ rescue Exception => e
13
+ errors << e.message
14
+ end
15
+
16
+ for error in errors
17
+ record.errors.add_to_base(friendly_liquid_error(error) + " in your #{ friendly_attr_name(attr_name) }")
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ def validates_presence_of_liquid_variable(*attr_names)
24
+ configuration = { :message => I18n.translate('activerecord.errors.messages')[:invalid], :on => :save, :variable => nil, :container => nil }
25
+ configuration.update(attr_names.extract_options!)
26
+
27
+ raise(ArgumentError, "You must supply a variable to check for") if configuration[:variable].blank?
28
+
29
+ validates_each(attr_names, configuration) do |record, attr_name, value|
30
+
31
+ value = value.to_s
32
+
33
+ variable = configuration[:variable].to_s
34
+ variable_re = /\{\{\s*#{ variable }( .*)?\}\}/
35
+
36
+ container = configuration[:container].to_s
37
+ container_re = /<\s*#{ container }.*>.*#{ variable_re }.*<\/\s*#{ container }\s*>/im
38
+
39
+ if container.blank? && !(value =~ variable_re)
40
+
41
+ record.errors.add_to_base("You must include {{ #{ variable } }} in your #{ friendly_attr_name(attr_name) }")
42
+
43
+ elsif !container.blank? && !(value =~ container_re)
44
+
45
+ record.errors.add_to_base("You must include {{ #{ variable } }} inside the <#{ container }> tag of your #{ friendly_attr_name(attr_name) }")
46
+
47
+ end
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def friendly_attr_name(attr_name)
54
+ attr_name.to_s.humanize.downcase
55
+ end
56
+
57
+ def friendly_liquid_error(error)
58
+ error.gsub('liquid', '').
59
+ gsub('Liquid', '').
60
+ gsub(/terminated with regexp:.+/, 'closed')
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module LiquidLoader
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liquid_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-13 00:00:00.000000000Z
12
+ date: 2011-12-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: liquid
16
- requirement: &19229460 !ruby/object:Gem::Requirement
16
+ requirement: &11484040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *19229460
24
+ version_requirements: *11484040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &19343800 !ruby/object:Gem::Requirement
27
+ requirement: &11483520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 3.1.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *19343800
35
+ version_requirements: *11483520
36
36
  description: See Summary
37
37
  email:
38
38
  - JGW Maxwell
@@ -47,6 +47,7 @@ files:
47
47
  - lib/liquid_loader.rb
48
48
  - lib/liquid_loader/context.rb
49
49
  - lib/liquid_loader/liquid_model.rb
50
+ - lib/liquid_loader/liquid_validations.rb
50
51
  - lib/liquid_loader/version.rb
51
52
  - lib/rails/generators/liquid/drop_generator.rb
52
53
  - lib/rails/generators/liquid/templates/drop.rb