Class: RubyLsp::Rails::FactoryBot::ServerExtension

Inherits:
Server::Extension
  • Object
show all
Defined in:
lib/ruby_lsp/rails/factory_bot/server_extension.rb

Class Method Summary collapse

Class Method Details

.block_for(attr) ⇒ Object



86
87
88
# File 'lib/ruby_lsp/rails/factory_bot/server_extension.rb', line 86

def block_for(attr)
  attr.instance_variable_get :@block
end

.block_source(attr) ⇒ Object



90
91
92
93
94
# File 'lib/ruby_lsp/rails/factory_bot/server_extension.rb', line 90

def block_source(attr)
  blk = block_for(attr)

  blk.source if blk.respond_to? :source
end

.guess_attribute_type(attribute, model_class) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ruby_lsp/rails/factory_bot/server_extension.rb', line 96

def guess_attribute_type(attribute, model_class)
  if model_class.respond_to? :attribute_types
    type = model_class.attribute_types[attribute.name.to_s]

    return case type
    when nil then nil
    when ActiveModel::Type::String, ActiveModel::Type::ImmutableString
      "String"
    when ActiveModel::Type::Float
      "Float"
    when ActiveModel::Type::Integer
      "Integer"
    when ActiveModel::Type::Boolean
      "boolean"
    when ActiveModel::Type::Date
      "Date"
    when ActiveModel::Type::Time
      "Time"
    when ActiveModel::Type::DateTime
      "DateTime"
    else
      type.type
    end
  end

  return unless model_class.respond_to? :reflections

  association = model_class.reflections[attribute.name.to_s]

  association&.klass
end