Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional enum methods #276

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions lib/rbs_rails/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def generate
#{delegated_type_instance}
#{delegated_type_scope(singleton: true)}
#{enum_instance_methods}
#{enum_scope_methods(singleton: true)}
#{enum_class_methods(singleton: true)}
#{scopes(singleton: true)}

#{generated_relation_methods_decl}
Expand All @@ -63,7 +63,7 @@ def generate
private def generated_relation_methods_decl
<<~RBS
module #{generated_relation_methods_name(abs: false)}
#{enum_scope_methods(singleton: false)}
#{enum_class_methods(singleton: false)}
#{scopes(singleton: false)}
#{delegated_type_scope(singleton: false)}
end
Expand Down Expand Up @@ -333,16 +333,20 @@ def authenticate_#{attribute}: (String) -> (#{klass_name} | false)
methods.join("\n")
end

private def enum_scope_methods(singleton:)
private def enum_class_methods(singleton:)
# @type var methods: Array[String]
methods = []
enum_definitions.each do |hash|
hash.each do |name, values|
next if IGNORED_ENUM_KEYS.include?(name)

class_name = sql_type_to_class(klass.columns_hash[name.to_s].type)
methods << "def #{singleton ? 'self.' : ''}#{name.to_s.pluralize}: () -> ::ActiveSupport::HashWithIndifferentAccess[::String, #{class_name}]"

values.each do |label, value|
value_method_name = enum_method_name(hash, name, label)
methods << "def #{singleton ? 'self.' : ''}#{value_method_name}: () -> #{relation_class_name}"
methods << "def #{singleton ? 'self.' : ''}not_#{value_method_name}: () -> #{relation_class_name}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion sig/rbs_rails/active_record.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class RbsRails::ActiveRecord::Generator

def enum_instance_methods: () -> String

def enum_scope_methods: (singleton: untyped `singleton`) -> String
def enum_class_methods: (singleton: untyped `singleton`) -> String

def enum_definitions: () -> Array[Hash[Symbol, untyped]]

Expand Down
5 changes: 5 additions & 0 deletions test/app/db/migrate/20240226015501_add_status_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToUser < ActiveRecord::Migration[6.1]
def change
add_column :users, :status, :integer, null: false
end
end
3 changes: 2 additions & 1 deletion test/app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_04_14_110904) do
ActiveRecord::Schema.define(version: 2024_02_26_015501) do

create_table "blogs", force: :cascade do |t|
t.string "title", null: false
Expand All @@ -31,6 +31,7 @@
t.integer "age", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "status", null: false
end

end
45 changes: 45 additions & 0 deletions test/expectations/user.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,42 @@ class User < ::ApplicationRecord
def restore_updated_at!: () -> void

def clear_updated_at_change: () -> void

def status: () -> ::String

def status=: (::String) -> ::String

def status?: () -> bool

def status_changed?: () -> bool

def status_change: () -> [ ::String?, ::String? ]

def status_will_change!: () -> void

def status_was: () -> ::String?

def status_previously_changed?: () -> bool

def status_previous_change: () -> ::Array[::String?]?

def status_previously_was: () -> ::String?

def status_before_last_save: () -> ::String?

def status_change_to_be_saved: () -> ::Array[::String?]?

def status_in_database: () -> ::String?

def saved_change_to_status: () -> ::Array[::String?]?

def saved_change_to_status?: () -> bool

def will_save_change_to_status?: () -> bool

def restore_status!: () -> void

def clear_status_change: () -> void
end
include GeneratedAttributeMethods

Expand Down Expand Up @@ -225,17 +261,26 @@ class User < ::ApplicationRecord
def temporary?: () -> bool
def accepted!: () -> bool
def accepted?: () -> bool
def self.statuses: () -> ::ActiveSupport::HashWithIndifferentAccess[::String, ::Integer]
def self.temporary: () -> ::User::ActiveRecord_Relation
def self.not_temporary: () -> ::User::ActiveRecord_Relation
def self.accepted: () -> ::User::ActiveRecord_Relation
def self.not_accepted: () -> ::User::ActiveRecord_Relation
def self.all_kind_args: (untyped type, ?untyped m, ?untyped n, *untyped rest, untyped x, ?k: untyped, **untyped untyped) { (*untyped) -> untyped } -> ::User::ActiveRecord_Relation
def self.no_arg: () -> ::User::ActiveRecord_Relation
def self.with_attached_avatar: () -> ::User::ActiveRecord_Relation

module GeneratedRelationMethods
def statuses: () -> ::ActiveSupport::HashWithIndifferentAccess[::String, ::Integer]

def temporary: () -> ::User::ActiveRecord_Relation

def not_temporary: () -> ::User::ActiveRecord_Relation

def accepted: () -> ::User::ActiveRecord_Relation

def not_accepted: () -> ::User::ActiveRecord_Relation

def all_kind_args: (untyped type, ?untyped m, ?untyped n, *untyped rest, untyped x, ?k: untyped, **untyped untyped) { (*untyped) -> untyped } -> ::User::ActiveRecord_Relation

def no_arg: () -> ::User::ActiveRecord_Relation
Expand Down
Loading