Skip to content

Commit

Permalink
active_model: Support ActiveModel::* mix-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Feb 4, 2024
1 parent 6b4d6c5 commit 2c09a4f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/rbs_rails/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def generate
private def klass_decl
<<~RBS
#{header}
def self.attribute: (Symbol name, ?Symbol? cast_type, ?untyped default, **untyped) -> void
#{mixins}
#{attributes}
#{footer}
Expand All @@ -69,6 +69,22 @@ def self.attribute: (Symbol name, ?Symbol? cast_type, ?untyped default, **untype
end.join("\n")
end

private def mixins
mixins = []
if klass < ::ActiveModel::Model
mixins << "include ::ActiveModel::Model"
end
if klass < ::ActiveModel::Attributes
mixins << "include ::ActiveModel::Attributes"
mixins << "extend ::ActiveModel::Attributes::ClassMethods"
end
if klass < ::ActiveModel::Validations
mixins << "include ::ActiveModel::Validations"
mixins << "extend ::ActiveModel::Validations::ClassMethods"
end
mixins.join("\n")
end

private def attributes
# @type var model: untyped
model = klass
Expand Down
1 change: 1 addition & 0 deletions sig/rbs_rails/active_model.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module RbsRails

def klass_decl: () -> String
def header: () -> String
def mixins: () -> String
def attributes: () -> String
def footer: () -> String

Expand Down
6 changes: 5 additions & 1 deletion test/expectations/article.rbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Article < ::Object
def self.attribute: (Symbol name, ?Symbol? cast_type, ?untyped default, **untyped) -> void
include ::ActiveModel::Model
include ::ActiveModel::Attributes
extend ::ActiveModel::Attributes::ClassMethods
include ::ActiveModel::Validations
extend ::ActiveModel::Validations::ClassMethods

def blog_id: () -> Integer?
def blog_id=: (Integer? value) -> Integer?
Expand Down

0 comments on commit 2c09a4f

Please sign in to comment.