Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# typed: strict
# frozen_string_literal: true

return unless defined?(ActiveSupport::EnvironmentInquirer)

module Tapioca
module Dsl
module Compilers
# `Tapioca::Dsl::Compilers::ActiveSupportEnvironmentInquirer` decorates an RBI file for non-default environment
# files in the `config/environments` directory.
#
# For example, in a Rails application with the following files:
#
# - config/environments/development.rb
# - config/environments/demo.rb
# - config/environments/production.rb
# - config/environments/staging.rb
# - config/environments/test.rb
#
# this compiler will produce an RBI file with the following content:
# ~~~rbi
# # typed: true
#
# class ActiveSupport::EnvironmentInquirer
# sig { returns(T::Boolean) }
# def demo?; end
#
# sig { returns(T::Boolean) }
# def staging?; end
# end
# ~~~
#: [ConstantType = singleton(::ActiveSupport::EnvironmentInquirer)]
class ActiveSupportEnvironmentInquirer < Compiler
extend T::Sig

# @override
#: -> void
def decorate
envs = Rails.root.glob("config/environments/*.rb").map { |f| f.basename(".rb").to_s }.sort
envs -= ::ActiveSupport::EnvironmentInquirer::DEFAULT_ENVIRONMENTS
return if envs.none?

root.create_path(::ActiveSupport::EnvironmentInquirer) do |mod|
envs.each do |env|
mod.create_method("#{env}?", return_type: "T::Boolean")
end
end
end

class << self
extend T::Sig

# @override
#: -> T::Enumerable[T::Module[top]]
def gather_constants
return [] unless defined?(Rails.application) && Rails.application

[::ActiveSupport::EnvironmentInquirer]
end
end
end
end
end
end
25 changes: 25 additions & 0 deletions manual/compiler_activesupportenvironmentinquirer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## ActiveSupportEnvironmentInquirer

`Tapioca::Dsl::Compilers::ActiveSupportEnvironmentInquirer` decorates an RBI file for non-default environment
files in the `config/environments` directory.

For example, in a Rails application with the following files:

- config/environments/development.rb
- config/environments/demo.rb
- config/environments/production.rb
- config/environments/staging.rb
- config/environments/test.rb

this compiler will produce an RBI file with the following content:
~~~rbi
# typed: true

class ActiveSupport::EnvironmentInquirer
sig { returns(T::Boolean) }
def demo?; end

sig { returns(T::Boolean) }
def staging?; end
end
~~~
1 change: 1 addition & 0 deletions manual/compilers.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ In the following section you will find all available DSL compilers:
* [ActiveStorage](compiler_activestorage.md)
* [ActiveSupportConcern](compiler_activesupportconcern.md)
* [ActiveSupportCurrentAttributes](compiler_activesupportcurrentattributes.md)
* [ActiveSupportEnvironmentInquirer](compiler_activesupportenvironmentinquirer.md)
* [ActiveSupportTimeExt](compiler_activesupporttimeext.md)
* [Config](compiler_config.md)
* [FrozenRecord](compiler_frozenrecord.md)
Expand Down
9 changes: 9 additions & 0 deletions spec/rails_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ module RailsSpecHelper
extend Tapioca::Helpers::Test::Content

class << self
#: (String path) -> void
def define_fake_rails_app(path)
base_folder = Pathname.new(path)
config_class = Struct.new(:root)
config = config_class.new(base_folder)
app_class = Struct.new(:config)
Rails.application = app_class.new(config)
end

def load_active_storage
add_ruby_file("application.rb", <<~RUBY)
ENV["DATABASE_URL"] = "sqlite3::memory:"
Expand Down
3 changes: 3 additions & 0 deletions spec/tapioca/cli/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2858,6 +2858,7 @@ class PostCompiler < Tapioca::Dsl::Compiler
Tapioca::Dsl::Compilers::ActiveRecordStore enabled
Tapioca::Dsl::Compilers::ActiveSupportConcern enabled
Tapioca::Dsl::Compilers::ActiveSupportCurrentAttributes enabled
Tapioca::Dsl::Compilers::ActiveSupportEnvironmentInquirer enabled
Tapioca::Dsl::Compilers::MixedInClassAttributes enabled
Tapioca::Dsl::Compilers::SidekiqWorker enabled
Tapioca::Dsl::Compilers::SmartProperties enabled
Expand Down Expand Up @@ -2893,6 +2894,7 @@ class PostCompiler < Tapioca::Dsl::Compiler
Tapioca::Dsl::Compilers::ActiveRecordStore enabled
Tapioca::Dsl::Compilers::ActiveSupportConcern enabled
Tapioca::Dsl::Compilers::ActiveSupportCurrentAttributes enabled
Tapioca::Dsl::Compilers::ActiveSupportEnvironmentInquirer enabled
Tapioca::Dsl::Compilers::MixedInClassAttributes enabled
Tapioca::Dsl::Compilers::SidekiqWorker enabled
Tapioca::Dsl::Compilers::SmartProperties disabled
Expand Down Expand Up @@ -2928,6 +2930,7 @@ class PostCompiler < Tapioca::Dsl::Compiler
Tapioca::Dsl::Compilers::ActiveRecordStore disabled
Tapioca::Dsl::Compilers::ActiveSupportConcern disabled
Tapioca::Dsl::Compilers::ActiveSupportCurrentAttributes disabled
Tapioca::Dsl::Compilers::ActiveSupportEnvironmentInquirer disabled
Tapioca::Dsl::Compilers::MixedInClassAttributes disabled
Tapioca::Dsl::Compilers::SidekiqWorker disabled
Tapioca::Dsl::Compilers::SmartProperties enabled
Expand Down
14 changes: 1 addition & 13 deletions spec/tapioca/dsl/compilers/active_record_fixtures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class User
before do
require "rails"

define_fake_rails_app
Tapioca::RailsSpecHelper.define_fake_rails_app(tmp_path("lib"))
end

it "gathers only the ActiveSupport::TestCase base class" do
Expand Down Expand Up @@ -231,18 +231,6 @@ def posts(fixture_name = nil, *other_fixtures); end
end
end
end

private

#: -> void
def define_fake_rails_app
base_folder = Pathname.new(tmp_path("lib"))

config_class = Struct.new(:root)
config = config_class.new(base_folder)
app_class = Struct.new(:config)
Rails.application = app_class.new(config)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# typed: strict
# frozen_string_literal: true

require "spec_helper"

module Tapioca
module Dsl
module Compilers
class ActiveSupportEnvironmentInquirerSpec < ::DslSpec
describe "Tapioca::Dsl::Compilers::ActiveSupportEnvironmentInquirer" do
describe "without a Rails app" do
it "gathers nothing if not in a Rails application" do
add_default_environment_files

assert_empty(gathered_constants)
end
end

describe "with a Rails app" do
before do
require "rails"
Tapioca::RailsSpecHelper.define_fake_rails_app(tmp_path("lib"))
add_default_environment_files
end

describe "gather_constants" do
it "gathers only `ActiveSupport::EnvironmentInquirer` as a constant" do
assert_equal(["ActiveSupport::EnvironmentInquirer"], gathered_constants)
end
end

describe "decorate" do
it "generates nothing if there are only default environments" do
expected = <<~RBI
# typed: strong
RBI

assert_equal(expected, rbi_for("ActiveSupport::EnvironmentInquirer"))
end

it "generates boolean predicate methods for non-default environments" do
add_content_file("config/environments/staging.rb", "")
add_content_file("config/environments/demo.rb", "")

expected = <<~RBI
# typed: strong

class ActiveSupport::EnvironmentInquirer
sig { returns(T::Boolean) }
def demo?; end

sig { returns(T::Boolean) }
def staging?; end
end
RBI

assert_equal(expected, rbi_for("ActiveSupport::EnvironmentInquirer"))
end
end
end
end

private

#: -> void
def add_default_environment_files
add_content_file("config/environments/development.rb", "")
add_content_file("config/environments/test.rb", "")
add_content_file("config/environments/production.rb", "")
end
end
end
end
end
Loading