mistiq 0.0.89 → 0.0.91

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODg3ZjBjZDdkZjkzNjA4ODFhMzk4ZjE5NmM5YjIzNTZiMDdjNzJmMQ==
4
+ ZmFhOTQ3NDUyZjBlYzBjMzBlMjNlZjNmOGUxYWUwMTA0ZGVlNTJiNQ==
5
5
  data.tar.gz: !binary |-
6
- ZTc5MmU4YjY5YTQzZjVlMWNmMTc0ZWJhOGRhMTZlZWI5ZTAxMzM2Yg==
6
+ NDI0NjAyYTEyZjA1YjA3NGFiMGI1N2NmYWNkMTFlNDI4YjQzMDY0NQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDY3NTNhNDc3OTM5MTliNjQwZTg4ZDgzMzFkMzQ3ZTQ4NTNlMzkzMzVhOWMy
10
- ODA3NzU1YmEyMTk3OGQyYzM4ZTU5Yjk4ZjhjZjQzN2E1NTBlM2I0MjRlYjEw
11
- M2M0YWZmYTFlNWVlYmRlMWZlMTM1ZWFmZGM2ZTYxZmZmNGJhYzE=
9
+ ZDc3ZWU0ZDQyZTkyMDdmNzFmZjk5NzM1YTgzYjI5MmVhY2NiYTAyM2E1OWM2
10
+ Njc5NjE5NDc3ZGE0MGZlZDY1NWYwZmE0OTEwMTlhNDJhNTI1M2UzNzE2ODVl
11
+ NjRjMWMxNmQ2MGNhOWFlZDY2NGQ4NWFiMTJlYjBhNTc1OGQ3MTM=
12
12
  data.tar.gz: !binary |-
13
- NGQyYTUwNzE3N2ViMzE0ZmQzYzEwNGFkYzAzMzQwYzI2NjBhNzYxMTU0YjAy
14
- MTVlMDZkNzM5MzliMzc3YjA4MmQyMWJlZjk4MjliNGM1MzEyMTc4ODkzZmE5
15
- ZjdkMTZjYzQ1MjMzMWMzODdmOTcyZmExNTdlZjc5MDlkMDg3Y2Q=
13
+ NDVkMjk2MDc4MGY0OTU4YzVjOWQyYjkxZmMwZjJkNWMzZTU5Y2VlMjE3ZjEx
14
+ OTIxNDVjODcwMTc5Nzc5NzE4MjZiZmNiY2ZiYzZlYTYzNjhlYmJmODAxMmY0
15
+ N2Y1NzRkZTdiMmQ0ODVlZWJkYjM1NTk0ODEzMWEzNzQ3OWU2ZWQ=
@@ -13,7 +13,7 @@ module Mistiq
13
13
  def disable_create_ops()
14
14
  Railtie.get_regex_hash.each {
15
15
  |r,p|
16
- set_guard_rule(true,r) if r.match(/.*#add/) || r.match(/.*#create/) || r.match(/.*#new/)
16
+ set_guard_rule(true,r) if r.match(/.*#new/) || r.match(/.*#add/)
17
17
  }
18
18
  end
19
19
 
data/lib/mistiq/init.rb CHANGED
@@ -5,64 +5,49 @@ module Mistiq
5
5
  #to redact links in the views
6
6
  Rails.application.config.after_initialize do
7
7
  @LINK_REGEX_HASH = Hash.new
8
- clashes = Hash.new
9
8
 
10
9
  #pre-load routes
11
10
  Rails.application.reload_routes!
12
11
 
13
12
  #get routes
14
13
  routes = Rails.application.routes.routes
14
+ inspector = RoutesInspector.new(routes)
15
+ routes = inspector.get_routes
15
16
 
16
17
  routes.each {
17
18
  |r|
18
- controller = r.defaults[:controller]
19
- action = r.defaults[:action]
20
-
21
- route = r.path.spec.to_s
22
19
 
23
- #removes (.:format)
24
- if route.match("(.:format)")
25
- pattern = route.sub("(.:format)","")
26
- else
27
- pattern = route
28
- end
29
-
30
- #if there are any parameters in
31
- #the route use regex to ignore them
32
- if pattern.match(/(:.*\/)/)
33
- pattern.gsub!(/(:.*\/)/,".*/")
34
- end
35
-
36
- #if there is a parameter at the end
37
- if pattern.match(/(:.*)/)
38
- pattern.gsub!(/(:.*)/,"[^\/\"]*")
39
- end
20
+ reqs = r[:reqs].split('#')
21
+
22
+ controller = reqs[0]
23
+ action = reqs[1]
40
24
 
41
- if action == "destroy"
42
- destroy_regex = "data-method=(\"|')delete(\"|')";
43
- pattern = "<a.*#{destroy_regex}.*href=(\"|')#{pattern}(\"|').*>.*<\/a>"
44
- #also check if destroy_regex occurs after the href attribute
45
- pattern = pattern+"@@@<a.*href=(\"|')#{pattern}(\"|').*#{destroy_regex}.*>.*<\/a>"
46
- else
47
- pattern = "<a.*href=(\"|')#{pattern}(\"|').*>.*<\/a>"
25
+ route = r[:path]
26
+ verb = r[:verb]
27
+
28
+ #get regex-ed route
29
+ regex_len = r[:regexp].length
30
+ regex = r[:regexp].slice(1..regex_len-2)
31
+
32
+ #if action#controller's data method is not GET and not POST
33
+ if verb != "GET" && verb != "POST" && verb != ""
34
+ method_regex = "data-method=(\"|')#{verb.downcase}(\"|')";
35
+ pattern = "<a.*#{method_regex}.*href=(\"|')#{regex}(\"|').*>.*<\/a>"
36
+
37
+ #also check if method_regex occurs after the href attribute
38
+ pattern = pattern+"@@@<a.*href=(\"|')#{regex}(\"|').*#{method_regex}.*>.*<\/a>"
39
+ elsif verb == "GET"
40
+ #if action#controller's data method is GET
41
+ #pattern = "<a(.*href=(\"|')#{pattern}(\"|'))(?!.*data\-method)[^<]*<\/a>"
42
+ pattern = "<a(?!.*data\-method)(.*href=(\"|')#{regex}(\"|'))[^<]*<\/a>"
48
43
  end
49
44
 
50
- #espace / characters
51
- pattern.gsub!(/\//,"\\/")
52
-
53
- #check if the current route clashes with another one
54
- if !clashes[route].nil?
55
- puts "Route clash at #{controller}##{action} for route #{route} with route for #{clashes[route]}"
56
- else
57
- #add route to the clashes hash to detect
58
- #any future route clashes
59
- clashes[route] = "#{controller}##{action}"
45
+ if verb == "GET" || (verb != "POST" && verb != "")
46
+ @LINK_REGEX_HASH["#{controller}##{action}"] = pattern
60
47
  end
61
-
62
- @LINK_REGEX_HASH["#{controller}##{action}"] = pattern
63
48
  }
64
49
 
65
- puts "Link REGEX hash has been computed. #{clashes.size} route clashes have been found."
50
+ puts "Link REGEX hash has been computed. #{@LINK_REGEX_HASH.size} routes were hashed."
66
51
  end
67
52
  end
68
53
 
@@ -70,4 +55,128 @@ module Mistiq
70
55
  @LINK_REGEX_HASH
71
56
  end
72
57
  end
58
+
59
+ class RouteWrapper < SimpleDelegator
60
+ def endpoint
61
+ rack_app ? rack_app.inspect : "#{controller}##{action}"
62
+ end
63
+
64
+ def constraints
65
+ requirements.except(:controller, :action)
66
+ end
67
+
68
+ def rack_app(app = self.app)
69
+ @rack_app ||= begin
70
+ class_name = app.class.name.to_s
71
+ if class_name == "ActionDispatch::Routing::Mapper::Constraints"
72
+ rack_app(app.app)
73
+ elsif ActionDispatch::Routing::Redirect === app || class_name !~ /^ActionDispatch::Routing/
74
+ app
75
+ end
76
+ end
77
+ end
78
+
79
+ def verb
80
+ super.source.gsub(/[$^]/, '')
81
+ end
82
+
83
+ def path
84
+ super.spec.to_s
85
+ end
86
+
87
+ def name
88
+ super.to_s
89
+ end
90
+
91
+ def regexp
92
+ __getobj__.path.to_regexp
93
+ end
94
+
95
+ def json_regexp
96
+ str = regexp.inspect.
97
+ sub('\\A' , '^').
98
+ sub('\\Z' , '$').
99
+ sub('\\z' , '$').
100
+ sub(/^\// , '').
101
+ sub(/\/[a-z]*$/ , '').
102
+ gsub(/\(\?#.+\)/ , '').
103
+ gsub(/\(\?-\w+:/ , '(').
104
+ gsub(/\s/ , '')
105
+ Regexp.new(str).source
106
+ end
107
+
108
+ def reqs
109
+ @reqs ||= begin
110
+ reqs = endpoint
111
+ reqs += " #{constraints.to_s}" unless constraints.empty?
112
+ reqs
113
+ end
114
+ end
115
+
116
+ def controller
117
+ requirements[:controller] || ':controller'
118
+ end
119
+
120
+ def action
121
+ requirements[:action] || ':action'
122
+ end
123
+
124
+ def internal?
125
+ controller.to_s =~ %r{\Arails/(info|welcome)} || path =~ %r{\A#{Rails.application.config.assets.prefix}}
126
+ end
127
+
128
+ def engine?
129
+ rack_app && rack_app.respond_to?(:routes)
130
+ end
131
+ end
132
+
133
+ class RoutesInspector
134
+ def initialize(routes)
135
+ @engines = {}
136
+ @routes = routes
137
+ end
138
+
139
+ def get_routes
140
+ routes_to_display = filter_routes(nil)
141
+ routes = collect_routes(routes_to_display)
142
+ return routes
143
+ end
144
+
145
+ private
146
+
147
+ def filter_routes(filter)
148
+ if filter
149
+ @routes.select { |route| route.defaults[:controller] == filter }
150
+ else
151
+ @routes
152
+ end
153
+ end
154
+
155
+ def collect_routes(routes)
156
+ routes.collect do |route|
157
+ RouteWrapper.new(route)
158
+ end.reject do |route|
159
+ route.internal?
160
+ end.collect do |route|
161
+ collect_engine_routes(route)
162
+
163
+ { name: route.name,
164
+ verb: route.verb,
165
+ path: route.path,
166
+ reqs: route.reqs,
167
+ regexp: route.json_regexp }
168
+ end
169
+ end
170
+
171
+ def collect_engine_routes(route)
172
+ name = route.endpoint
173
+ return unless route.engine?
174
+ return if @engines[name]
175
+
176
+ routes = route.rack_app.routes
177
+ if routes.is_a?(ActionDispatch::Routing::RouteSet)
178
+ @engines[name] = collect_routes(routes.routes)
179
+ end
180
+ end
181
+ end
73
182
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mistiq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.89
4
+ version: 0.0.91
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Papancea