err_supply 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35673a76dbd68c64bdc2f49222403480c6bb2dcb
4
- data.tar.gz: 45ef7b26dc7d7857138e4aac2da0509614b690d9
3
+ metadata.gz: e26b294d388b3d14d24bc7f245d01c5c1318eafb
4
+ data.tar.gz: 81f3b1a2556498463ba3e77aa5e495f25d29c441
5
5
  SHA512:
6
- metadata.gz: 45ad911a10d3dd8e6620a444cd0b1a09974c9f5beb89bec8de7a083702c832f7848b8d28df8279c4c9a070a99b95dca2360db7c52c7b2924a9988dfadc309409
7
- data.tar.gz: cdc3f2d847d3c1973ea1c3b0bba7212a5697de6004bc1f2e555a0d0687ebd4b4cdd51f1c656c3c3cd273e03a463b46e1f0635c91656a771b60813df5aeb7768f
6
+ metadata.gz: 1160167253407821bd833fc9b50f59ec1f5774c9f8996fc804b11472f6bd25534ce0cecc511ac1c9488e5e11fa266f87b35b44d116ba99abc27c66fdb7b0edc2
7
+ data.tar.gz: 8169d4e862ba8e0f5880e78d02e085bdccb1d89b24a1a1704f84948dcef464ba1bb9833d4c3d04a123cdeeb4c6c415c62de1becd43676183a0dddb47bad9ac9d
@@ -0,0 +1,90 @@
1
+ module ErrSupply
2
+ module ControllerHelpers
3
+
4
+ # Declare our method as a helper method so it is accessible to views.
5
+ ActionController::Base.helper_method :err_supply_hash
6
+
7
+ # This method can be called recursively to unpack a rails object's error hash
8
+ # into something more manageable for the view. Basically, it uses the keys in the
9
+ # object's error hash to traverse the object graph and expand the errors into a
10
+ # flattened hash, keyed using the dom ids rails will generate for the
11
+ # associated objects.
12
+ #
13
+ # Because the method is called recursively, it only pays attention to errors for
14
+ # the given object and its immediate child collections. More remote descendents
15
+ # will be processed in subsequent passes.
16
+ #
17
+ def err_supply_hash(obj, options={})
18
+
19
+ #---------------------------------------------
20
+ # set up main variables
21
+ #---------------------------------------------
22
+
23
+ prefix = options[:prefix]
24
+ errors = obj.errors
25
+ h = {}
26
+
27
+
28
+ #---------------------------------------------
29
+ # apply options to local attrs
30
+ #---------------------------------------------
31
+
32
+ # get keys
33
+ attrs = errors.keys.select { |k| k.to_s.split(".").size == 1 }
34
+
35
+ # whitelist/blacklist keys
36
+ if options.has_key?(:only)
37
+ attrs = attrs.select { |k| Array(options[:only]).flatten.include?(k) }
38
+ end
39
+ if options.has_key?(:except)
40
+ attrs = attrs.reject { |k| Array(options[:except]).flatten.include?(k) }
41
+ end
42
+
43
+ # apply errors that match our list (slightly inefficient, but a touch easier to read)
44
+ attrs.each do |attr|
45
+ o = options[attr.to_sym] || {}
46
+ id = "#{prefix}_#{(o[:key] || attr).to_s}"
47
+
48
+ unless h.has_key?(id)
49
+ h[id] = {
50
+ "label" => obj.class.human_attribute_name(attr),
51
+ "messages" => []
52
+ }
53
+ end
54
+
55
+ h[id]["label"] = o[:label].to_s.underscore.humanize unless o[:label].nil?
56
+ h[id]["messages"] = (h[id]["messages"] + errors[attr].map { |msg| "#{msg}.".gsub("..", ".") }).flatten
57
+ end
58
+
59
+
60
+ #---------------------------------------------
61
+ # apply options to children
62
+ #---------------------------------------------
63
+
64
+ # get keys
65
+ assoc_names = errors.keys.map { |k| k.to_s.split(".") }.select { |a| a.many? }.map { |a| a[0] }.compact.uniq
66
+
67
+ # if child has errors or is invalid (i.e., we prefer explicitly declared errrors),
68
+ # call function recursively and merge results
69
+ assoc_names.each do |assoc_name|
70
+ c_options = options[assoc_name.to_sym] || {}
71
+ obj.send("#{assoc_name}").each_with_index do |child, index|
72
+ if !child.errors.empty? or child.invalid?
73
+ c_prefix = "#{prefix}_#{assoc_name}_attributes_#{index}"
74
+ c_hash = err_supply_hash(child, c_options.merge({ :prefix => c_prefix }))
75
+
76
+ h.merge!(c_hash)
77
+ end
78
+ end
79
+ end
80
+
81
+
82
+ #---------------------------------------------
83
+ # return the hash
84
+ #---------------------------------------------
85
+ h
86
+
87
+ end
88
+
89
+ end
90
+ end
@@ -1,3 +1,3 @@
1
1
  module ErrSupply
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
@@ -10,92 +10,6 @@ module ErrSupply
10
10
 
11
11
  "$('##{id}').trigger('err_supply:loaded', #{h.to_json});".html_safe
12
12
  end
13
-
14
-
15
-
16
- protected
17
-
18
- # This method can be called recursively to unpack a rails object's error hash
19
- # into something more manageable for the view. Basically, it uses the keys in the
20
- # object's error hash to traverse the object graph and expand the errors into a
21
- # flattened hash, keyed using the dom ids rails will generate for the
22
- # associated objects.
23
- #
24
- # Because the method is called recursively, it only pays attention to errors for
25
- # the given object and its immediate child collections. More remote descendents
26
- # will be processed in subsequent passes.
27
- #
28
- def err_supply_hash(obj, options={})
29
-
30
- #---------------------------------------------
31
- # set up main variables
32
- #---------------------------------------------
33
-
34
- prefix = options[:prefix]
35
- errors = obj.errors
36
- h = {}
37
-
38
-
39
- #---------------------------------------------
40
- # apply options to local attrs
41
- #---------------------------------------------
42
-
43
- # get keys
44
- attrs = errors.keys.select { |k| k.to_s.split(".").size == 1 }
45
-
46
- # whitelist/blacklist keys
47
- if options.has_key?(:only)
48
- attrs = attrs.select { |k| Array(options[:only]).flatten.include?(k) }
49
- end
50
- if options.has_key?(:except)
51
- attrs = attrs.reject { |k| Array(options[:except]).flatten.include?(k) }
52
- end
53
-
54
- # apply errors that match our list (slightly inefficient, but a touch easier to read)
55
- attrs.each do |attr|
56
- o = options[attr.to_sym] || {}
57
- id = "#{prefix}_#{(o[:key] || attr).to_s}"
58
-
59
- unless h.has_key?(id)
60
- h[id] = {
61
- "label" => obj.class.human_attribute_name(attr),
62
- "messages" => []
63
- }
64
- end
65
-
66
- h[id]["label"] = o[:label].to_s.underscore.humanize unless o[:label].nil?
67
- h[id]["messages"] = (h[id]["messages"] + errors[attr].map { |msg| "#{msg}.".gsub("..", ".") }).flatten
68
- end
69
-
70
-
71
- #---------------------------------------------
72
- # apply options to children
73
- #---------------------------------------------
74
-
75
- # get keys
76
- assoc_names = errors.keys.map { |k| k.to_s.split(".") }.select { |a| a.many? }.map { |a| a[0] }.compact.uniq
77
-
78
- # if child has errors or is invalid (i.e., we prefer explicitly declared errrors),
79
- # call function recursively and merge results
80
- assoc_names.each do |assoc_name|
81
- c_options = options[assoc_name.to_sym] || {}
82
- obj.send("#{assoc_name}").each_with_index do |child, index|
83
- if !child.errors.empty? or child.invalid?
84
- c_prefix = "#{prefix}_#{assoc_name}_attributes_#{index}"
85
- c_hash = err_supply_hash(child, c_options.merge({ :prefix => c_prefix }))
86
-
87
- h.merge!(c_hash)
88
- end
89
- end
90
- end
91
-
92
-
93
- #---------------------------------------------
94
- # return the hash
95
- #---------------------------------------------
96
- h
97
-
98
- end
99
13
 
100
14
  end
101
15
  end
data/lib/err_supply.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'err_supply/version'
2
+ require 'err_supply/controller_helpers'
2
3
  require 'err_supply/view_helpers'
3
4
 
4
5
  module ErrSupply
@@ -9,8 +10,15 @@ module ErrSupply
9
10
  #-------------------------------------------------------
10
11
  initializer "err_supply.start" do |app|
11
12
 
12
- # add method to views
13
- ActionView::Base.module_eval { include ErrSupply::ViewHelpers } if defined? ActionView
13
+ # extend views
14
+ ActiveSupport.on_load :action_view do
15
+ include ErrSupply::ViewHelpers
16
+ end
17
+
18
+ # extend controllers
19
+ ActiveSupport.on_load :action_controller do
20
+ include ErrSupply::ControllerHelpers
21
+ end
14
22
 
15
23
  end
16
24
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: err_supply
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Coroutine
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-01 00:00:00.000000000 Z
12
+ date: 2013-11-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -58,6 +58,7 @@ files:
58
58
  - err_supply.gemspec
59
59
  - lib/assets/javascripts/err_supply.js
60
60
  - lib/err_supply.rb
61
+ - lib/err_supply/controller_helpers.rb
61
62
  - lib/err_supply/version.rb
62
63
  - lib/err_supply/view_helpers.rb
63
64
  - vendor/assets/javascripts/jquery.qtip.js