Skip to content

Commit

Permalink
Merge pull request #976 from Shopify/activerecord-typedstore-v1.5.0
Browse files Browse the repository at this point in the history
Support for activerecord-typedstore v1.5.0
  • Loading branch information
paracycle authored Jun 12, 2022
2 parents 0949329 + 7cd85b4 commit 3327062
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ GEM
activerecord (7.0.3)
activemodel (= 7.0.3)
activesupport (= 7.0.3)
activerecord-typedstore (1.4.0)
activerecord (>= 5.2)
activerecord-typedstore (1.5.0)
activerecord (>= 6.1)
activeresource (6.0.0)
activemodel (>= 6.0)
activemodel-serializers-xml (~> 1.0)
Expand Down
9 changes: 5 additions & 4 deletions lib/tapioca/dsl/compilers/active_record_typed_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,18 @@ class ActiveRecordTypedStore < Compiler
sig { override.void }
def decorate
stores = constant.typed_stores
return if stores.values.flat_map(&:accessors).empty?
return if stores.values.all? { |store| store.accessors.empty? }

root.create_path(constant) do |model|
stores.values.each do |store_data|
store_data.accessors.each do |accessor|
field = store_data.fields[accessor]
store_data.accessors.each do |accessor, name|
field = store_data.fields.fetch(accessor)
type = type_for(field.type_sym)
type = as_nilable_type(type) if field.null
name ||= field.name # support < 1.5.0

store_accessors_module = model.create_module("StoreAccessors")
generate_methods(store_accessors_module, field.name.to_s, type)
generate_methods(store_accessors_module, name.to_s, type)
model.create_include("StoreAccessors")
end
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions spec/tapioca/cli/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def self.application
@project.require_real_gem("smart_properties", "1.15.0")
@project.require_real_gem("sidekiq", "6.2.1")
@project.bundle_install
@gemfile = @project.read("Gemfile")
@gemfile_lock = @project.read("Gemfile.lock")
end

before do
@project.write("Gemfile", @gemfile)
@project.write("Gemfile.lock", @gemfile_lock)
end

after do
Expand Down Expand Up @@ -278,6 +285,7 @@ class Role
RB

@project.require_mock_gem(gem)
@project.bundle_install

result = @project.tapioca("dsl")

Expand Down Expand Up @@ -1184,6 +1192,87 @@ def self.gather_constants
refute_project_file_exist("sorbet/rbi/dsl/post.rbi")
refute_success_status(result)
end

it "generates RBIs for lower versions of activerecord-typedstore" do
@project.require_real_gem("activerecord-typedstore", "1.4.0")
@project.require_real_gem("sqlite3")
@project.bundle_install
@project.write("lib/post.rb", <<~RB)
require "active_record"
require "active_record/typed_store"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
class Post < ActiveRecord::Base
typed_store :metadata do |s|
s.string(:reviewer)
end
end
RB

result = @project.tapioca("dsl --only=ActiveRecordTypedStore Post")

assert_equal(<<~OUT, result.out)
Loading Rails application... Done
Loading DSL compiler classes... Done
Compiling DSL RBI files...
create sorbet/rbi/dsl/post.rbi
Done
Checking generated RBI files... Done
No errors found
All operations performed in working directory.
Please review changes and commit them.
OUT

assert_empty_stderr(result)

assert_project_file_equal("sorbet/rbi/dsl/post.rbi", <<~RBI)
# typed: true
# DO NOT EDIT MANUALLY
# This is an autogenerated file for dynamic methods in `Post`.
# Please instead update this file by running `bin/tapioca dsl Post`.
class Post
include StoreAccessors
module StoreAccessors
sig { returns(T.nilable(String)) }
def reviewer; end
sig { params(reviewer: T.nilable(String)).returns(T.nilable(String)) }
def reviewer=(reviewer); end
sig { returns(T::Boolean) }
def reviewer?; end
sig { returns(T.nilable(String)) }
def reviewer_before_last_save; end
sig { returns(T.nilable([T.nilable(String), T.nilable(String)])) }
def reviewer_change; end
sig { returns(T::Boolean) }
def reviewer_changed?; end
sig { returns(T.nilable(String)) }
def reviewer_was; end
sig { returns(T.nilable([T.nilable(String), T.nilable(String)])) }
def saved_change_to_reviewer; end
sig { returns(T::Boolean) }
def saved_change_to_reviewer?; end
end
end
RBI

assert_success_status(result)
end
end

describe "verify" do
Expand Down
26 changes: 26 additions & 0 deletions spec/tapioca/dsl/compilers/active_record_typed_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,32 @@ def rate=(rate); end

assert_includes(rbi_for(:Post), expected)
end

it "generates methods with prefix and suffix" do
add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
typed_store :metadata, prefix: true, suffix: true do |s|
s.string(:rate)
end
end
RUBY

expected = <<~RBI
# typed: strong
class Post
include StoreAccessors
module StoreAccessors
sig { returns(T.nilable(String)) }
def metadata_rate_metadata; end
sig { params(metadata_rate_metadata: T.nilable(String)).returns(T.nilable(String)) }
def metadata_rate_metadata=(metadata_rate_metadata); end
RBI

assert_includes(rbi_for(:Post), expected)
end
end
end
end
Expand Down

0 comments on commit 3327062

Please sign in to comment.