Browse Source

Deeply OpenStruct for better views

new-scoring
aeris 2 years ago
parent
commit
5f144af234
2 changed files with 31 additions and 10 deletions
  1. 17
    10
      app/controllers/check_controller.rb
  2. 14
    0
      config/initializers/fixture.rb

+ 17
- 10
app/controllers/check_controller.rb View File

@@ -7,23 +7,28 @@ class CheckController < ApplicationController
7 7
 		@host = SimpleIDN.to_unicode @host
8 8
 		respond_to do |format|
9 9
 			format.html do
10
-				return render :processing if @result.pending
10
+				return render :processing if @result[:pending]
11
+				@result = OpenStruct.deep @result
11 12
 			end
12 13
 			format.json do
13 14
 				render json: case
14
-					when @result.pending then :pending
15
-					else @result
16
-				end
15
+								 when @result[:pending] then
16
+									 :pending
17
+								 else
18
+									 JSON.pretty_generate @result
19
+							 end
17 20
 			end
18 21
 		end
19 22
 	end
20 23
 
21 24
 	def refresh
22 25
 		unless @result.pending
23
-			refresh_allowed = @result.date + Rails.configuration.refresh_delay
24
-			if Time.now < refresh_allowed
25
-				flash[:warning] = "Merci d’attendre au moins #{l refresh_allowed} pour rafraîchir"
26
-				return redirect_to action: :show, id: @host
26
+			if Rails.env == 'production'
27
+				refresh_allowed = @result.date + Rails.configuration.refresh_delay
28
+				if Time.now < refresh_allowed
29
+					flash[:warning] = "Merci d’attendre au moins #{l refresh_allowed} pour rafraîchir"
30
+					return redirect_to action: :show, id: @host
31
+				end
27 32
 			end
28 33
 			enqueue_host
29 34
 		end
@@ -31,10 +36,11 @@ class CheckController < ApplicationController
31 36
 	end
32 37
 
33 38
 	protected
39
+
34 40
 	def enqueue_host
35 41
 		Datastore.pending self.type, @host, @port
36 42
 		self.worker.perform_async *(@port.blank? ? [@host] : [@host, @port])
37
-		@result = OpenStruct.new pending: true , date: Time.now
43
+		@result = OpenStruct.new pending: true, date: Time.now
38 44
 	end
39 45
 
40 46
 	def check_host
@@ -47,11 +53,12 @@ class CheckController < ApplicationController
47 53
 
48 54
 		@host, @port = @id.split ':'
49 55
 		@host = SimpleIDN.to_ascii @host.downcase
50
-		if /[^a-zA-Z0-9.-]/.match @host
56
+		if /[^a-zA-Z0-9.-]/ =~ @host
51 57
 			flash[:danger] = "Hôte #{@host} invalide"
52 58
 			redirect_to action: :index
53 59
 			return false
54 60
 		end
61
+		@port = @port.to_i if @port
55 62
 		@result = Datastore.host self.type, @host, @port
56 63
 	end
57 64
 end

+ 14
- 0
config/initializers/fixture.rb View File

@@ -0,0 +1,14 @@
1
+require 'ostruct'
2
+
3
+class OpenStruct
4
+	def self.deep(value)
5
+		case value
6
+			when Hash
7
+				self.new value.collect { |k, v| [k, self.deep(v)] }.to_h
8
+			when Enumerable
9
+				value.collect { |v| self.deep v }
10
+			else
11
+				value
12
+		end
13
+	end
14
+end

Loading…
Cancel
Save