Class: AutoRspecGenerator

Inherits:
Thor::Group
  • Object
show all
Includes:
ActiveRecordParser, Thor::Actions
Defined in:
lib/auto_rspec/generators/auto_rspec/auto_rspec_generator.rb

Instance Method Summary collapse

Methods included from ActiveRecordParser

#parse_active_record

Instance Method Details

#create_spec_fileObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/auto_rspec/generators/auto_rspec/auto_rspec_generator.rb', line 19

def create_spec_file
  # error message if not appropriate path provided
  return puts "#{file_path} doesn't exist!" unless File.exist?(file_path)

  # todo replace
  file_name = File.basename(file_path)

  directories = File.dirname(file_path).split('/').drop(1)
  spec_path = 'spec/' + directories.join('/') + "/#{File.basename(file_path, '.*')}_spec.rb"

  # todo replace
  class_name = converted_string = file_path.dup.split('/').map(&:capitalize).join('::')

  # parse classes
  @class_name = class_name.split('.').first
  until @class_name.empty? || Object.const_defined?(@class_name)
    @class_name = @class_name.split('::').drop(1).join('::')
  end
  the_class = Module.const_get(@class_name) unless @class_name.empty?

  if the_class.nil?
    puts "Spec generation for provided file doesn't supported. Please use it only for ApplicationRecord/ActiveModel classes"
  elsif the_class < ActiveRecord::Base
    create_spec_directory(directories)
    @active_record_matchers = parse_active_record(the_class)
    template "application_record.erb", spec_path
  elsif the_class < ActiveModel
    create_spec_directory(directories)
    @active_model_matchers = parse_active_model(the_class)
    template "active_model.erb", spec_path
  elsif the_class < ApplicationController
    create_spec_directory(directories)
    @application_controller_matchers = parse_application_controller(the_class)
    template "application_controller.erb", spec_path
  else
    puts "Spec generation for provided file doesn't supported. Please use it only for ApplicationRecord/ActiveModel classes"
  end
end