Skip to content

Commit 373860f

Browse files
Merge pull request #9674 from p8/rails/metal-controller
[rails] Use ActionController::API for actions that return JSON
2 parents faf49d1 + ee37ca4 commit 373860f

File tree

6 files changed

+31
-18
lines changed

6 files changed

+31
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
# frozen_string_literal: true
22

33
class ApplicationController < ActionController::Base
4-
if defined?(Agoo) || defined?(Falcon) || defined?(Puma)
5-
before_action :add_date_header
6-
end
7-
8-
private
9-
10-
def add_date_header
11-
response.set_header('Date', Time.now.httpdate)
12-
end
134
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module DateHeader
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
if defined?(Agoo) || defined?(Falcon) || defined?(Puma)
6+
before_action :add_header
7+
end
8+
end
9+
10+
private
11+
12+
def add_header
13+
response.set_header('Date', Time.now.httpdate)
14+
end
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class FortunesController < ApplicationController
4+
include DateHeader
5+
6+
def index
7+
@fortunes = Fortune.all.to_a
8+
@fortunes << Fortune.new(id: 0, message: 'Additional fortune added at request time.')
9+
@fortunes.sort_by!(&:message)
10+
render :fortune
11+
end
12+
end

frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

3-
class HelloWorldController < ApplicationController
3+
class HelloWorldController < ActionController::API
4+
include DateHeader
5+
46
QUERY_RANGE = 1..10_000 # range of IDs in the Fortune DB
57
ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
68
MIN_QUERIES = 1 # min number of records that can be retrieved
@@ -26,13 +28,6 @@ def cached_query
2628
render json: items.values
2729
end
2830

29-
def fortune
30-
@fortunes = Fortune.all.to_a
31-
@fortunes << Fortune.new(id: 0, message: 'Additional fortune added at request time.')
32-
@fortunes.sort_by!(&:message)
33-
render :fortune
34-
end
35-
3631
def update
3732
worlds = ALL_IDS.sample(query_count).map do |id|
3833
world = World.find(id)

frameworks/Ruby/rails/config/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
get "json", to: JsonApp
4747
get "db", to: "hello_world#db"
4848
get "queries", to: "hello_world#query"
49-
get "fortunes", to: "hello_world#fortune"
49+
get "fortunes", to: "fortunes#index"
5050
get "updates", to: "hello_world#update"
5151
get "plaintext", to: PlaintextApp
5252
get "cached", to: "hello_world#cached_query"

0 commit comments

Comments
 (0)