浏览代码

[rails] Use ActionController::API for actions that return JSON

API::Controller is a lightweight version of ActionController::Base,
useful for controllers that only return JSON.

+----------------------+------+-----+-----+------+-------+---------+--------------+
|           branch_name|  json|   db|query|update|fortune|plaintext|weighted_score|
+----------------------+------+-----+-----+------+-------+---------+--------------+
|                master|147603|32970|26658| 15330|  18044|   173519|          1934|
|rails/metal-controller|159296|33277|30173| 15743|  19999|   177191|          2059|
+----------------------+------+-----+-----+------+-------+---------+--------------+
Petrik 6 月之前
父节点
当前提交
ee37ca41a6

+ 0 - 9
frameworks/Ruby/rails/app/controllers/application_controller.rb

@@ -1,13 +1,4 @@
 # frozen_string_literal: true
 
 class ApplicationController < ActionController::Base
-  if defined?(Agoo) || defined?(Falcon) || defined?(Puma)
-    before_action :add_date_header
-  end
-
-  private
-
-  def add_date_header
-    response.set_header('Date', Time.now.httpdate)
-  end
 end

+ 15 - 0
frameworks/Ruby/rails/app/controllers/concerns/date_header.rb

@@ -0,0 +1,15 @@
+module DateHeader
+  extend ActiveSupport::Concern
+
+  included do
+    if defined?(Agoo) || defined?(Falcon) || defined?(Puma)
+      before_action :add_header
+    end
+  end
+
+  private
+
+  def add_header
+    response.set_header('Date', Time.now.httpdate)
+  end
+end

+ 12 - 0
frameworks/Ruby/rails/app/controllers/fortunes_controller.rb

@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+class FortunesController < ApplicationController
+  include DateHeader
+
+  def index
+    @fortunes = Fortune.all.to_a
+    @fortunes << Fortune.new(id: 0, message: 'Additional fortune added at request time.')
+    @fortunes.sort_by!(&:message)
+    render :fortune
+  end
+end

+ 3 - 8
frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

@@ -1,6 +1,8 @@
 # frozen_string_literal: true
 
-class HelloWorldController < ApplicationController
+class HelloWorldController < ActionController::API
+  include DateHeader
+
   QUERY_RANGE = 1..10_000    # range of IDs in the Fortune DB
   ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
   MIN_QUERIES = 1            # min number of records that can be retrieved
@@ -26,13 +28,6 @@ class HelloWorldController < ApplicationController
     render json: items.values
   end
 
-  def fortune
-    @fortunes = Fortune.all.to_a
-    @fortunes << Fortune.new(id: 0, message: 'Additional fortune added at request time.')
-    @fortunes.sort_by!(&:message)
-    render :fortune
-  end
-
   def update
     worlds = ALL_IDS.sample(query_count).map do |id|
       world = World.find(id)

+ 0 - 0
frameworks/Ruby/rails/app/views/hello_world/fortune.html.erb → frameworks/Ruby/rails/app/views/fortunes/fortune.html.erb


+ 1 - 1
frameworks/Ruby/rails/config/routes.rb

@@ -46,7 +46,7 @@ Rails.application.routes.draw do
   get "json", to: JsonApp
   get "db", to: "hello_world#db"
   get "queries", to: "hello_world#query"
-  get "fortunes", to: "hello_world#fortune"
+  get "fortunes", to: "fortunes#index"
   get "updates", to: "hello_world#update"
   get "plaintext", to: PlaintextApp
   get "cached", to: "hello_world#cached_query"