From ee37ca41a63757318227a002cef39d8fbb6aea9b Mon Sep 17 00:00:00 2001 From: Petrik Date: Sun, 9 Mar 2025 18:46:43 +0100 Subject: [PATCH] [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| +----------------------+------+-----+-----+------+-------+---------+--------------+ --- .../app/controllers/application_controller.rb | 9 --------- .../rails/app/controllers/concerns/date_header.rb | 15 +++++++++++++++ .../rails/app/controllers/fortunes_controller.rb | 12 ++++++++++++ .../app/controllers/hello_world_controller.rb | 11 +++-------- .../{hello_world => fortunes}/fortune.html.erb | 0 frameworks/Ruby/rails/config/routes.rb | 2 +- 6 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 frameworks/Ruby/rails/app/controllers/concerns/date_header.rb create mode 100644 frameworks/Ruby/rails/app/controllers/fortunes_controller.rb rename frameworks/Ruby/rails/app/views/{hello_world => fortunes}/fortune.html.erb (100%) diff --git a/frameworks/Ruby/rails/app/controllers/application_controller.rb b/frameworks/Ruby/rails/app/controllers/application_controller.rb index 0ef204a6b6b..7944f9f993a 100644 --- a/frameworks/Ruby/rails/app/controllers/application_controller.rb +++ b/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 diff --git a/frameworks/Ruby/rails/app/controllers/concerns/date_header.rb b/frameworks/Ruby/rails/app/controllers/concerns/date_header.rb new file mode 100644 index 00000000000..d314038144c --- /dev/null +++ b/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 diff --git a/frameworks/Ruby/rails/app/controllers/fortunes_controller.rb b/frameworks/Ruby/rails/app/controllers/fortunes_controller.rb new file mode 100644 index 00000000000..0f937aff039 --- /dev/null +++ b/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 diff --git a/frameworks/Ruby/rails/app/controllers/hello_world_controller.rb b/frameworks/Ruby/rails/app/controllers/hello_world_controller.rb index 9e9ce8b8327..d4582e18c02 100644 --- a/frameworks/Ruby/rails/app/controllers/hello_world_controller.rb +++ b/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 @@ def cached_query 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) diff --git a/frameworks/Ruby/rails/app/views/hello_world/fortune.html.erb b/frameworks/Ruby/rails/app/views/fortunes/fortune.html.erb similarity index 100% rename from frameworks/Ruby/rails/app/views/hello_world/fortune.html.erb rename to frameworks/Ruby/rails/app/views/fortunes/fortune.html.erb diff --git a/frameworks/Ruby/rails/config/routes.rb b/frameworks/Ruby/rails/config/routes.rb index 33afbfbcdda..22c47dc8704 100644 --- a/frameworks/Ruby/rails/config/routes.rb +++ b/frameworks/Ruby/rails/config/routes.rb @@ -46,7 +46,7 @@ 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"