Class: Zeus::Rails

Inherits:
Plan
  • Object
show all
Defined in:
lib/zeus/rails.rb

Instance Method Summary collapse

Instance Method Details

#_monkeypatch_rakeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zeus/rails.rb', line 33

def _monkeypatch_rake
  if version = gem_is_bundled?('rake')
    gem 'rake', version
  end
  require 'rake/testtask'
  Rake::TestTask.class_eval {

    # Create the tasks defined by this task lib.
    def define
      desc "Run tests" + (@name==:test ? "" : " for #{@name}")
      task @name do
        # ruby "#{ruby_opts_string} #{run_code} #{file_list_string} #{option_list}"
        rails_env = ENV['RAILS_ENV']
        rubyopt = ENV['RUBYOPT']
        ENV['RAILS_ENV'] = nil
        ENV['RUBYOPT'] = nil # bundler sets this to require bundler :|
        puts "zeus test #{file_list_string}"
        ret = system "zeus test #{file_list_string}"
        ENV['RAILS_ENV'] = rails_env
        ENV['RUBYOPT'] = rubyopt
        ret
      end
      self
    end

    alias_method :_original_define, :define

    def self.inherited(klass)
      return unless klass.name == "TestTaskWithoutDescription"
      klass.class_eval {
        def self.method_added(sym)
          class_eval do
            if !@rails_hack_reversed
              @rails_hack_reversed = true
              alias_method :define, :_original_define
              def desc(*)
              end
            end
          end
        end
      }
    end
  }
end

#after_forkObject



27
28
29
30
31
# File 'lib/zeus/rails.rb', line 27

def after_fork
  reconnect_activerecord
  restart_girl_friday
  reconnect_redis
end

#bootObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/zeus/rails.rb', line 78

def boot
  _monkeypatch_rake
  $LOAD_PATH.unshift "./lib"

  require BOOT_PATH
  # config/application.rb normally requires 'rails/all'.
  # Some 'alternative' ORMs such as Mongoid give instructions to switch this require
  # out for a list of railties, not including ActiveRecord.
  # We grep config/application.rb for all requires of rails/all or railties, and require them.
  rails_components = File.read(APP_PATH + ".rb").
    scan(/^\s*require\s*['"](.*railtie.*|rails\/all)['"]/).flatten

  rails_components = ["rails/all"] if rails_components == []
  rails_components.each do |component|
    require component
  end
end

#consoleObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/zeus/rails.rb', line 145

def console
  if rails_5_1_or_higher?
    run_rails_5_1_or_higher_command('console')
  else
    require 'rails/commands/console'
    if defined?(Pry)
      # Adding Rails Console helpers to Pry.
      if (3..4).include?(::Rails::VERSION::MAJOR)
        require 'rails/console/app'
        require 'rails/console/helpers'
        TOPLEVEL_BINDING.eval('self').extend ::Rails::ConsoleMethods
      end

      Pry.start
    else
      ::Rails::Console.start(::Rails.application)
    end
  end
end

#dbconsoleObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/zeus/rails.rb', line 165

def dbconsole
  if rails_5_1_or_higher?
    run_rails_5_1_or_higher_command('dbconsole')
  else
    require 'rails/commands/dbconsole'

    meth = ::Rails::DBConsole.method(:start)

    # `Rails::DBConsole.start` has been changed to load faster in
    # https://github.com/rails/rails/commit/346bb018499cde6699fcce6c68dd7e9be45c75e1
    #
    # This will work with both versions.
    if meth.arity.zero?
      ::Rails::DBConsole.start
    else
      ::Rails::DBConsole.start(::Rails.application)
    end
  end
end

#default_bundleObject



96
97
98
99
100
# File 'lib/zeus/rails.rb', line 96

def default_bundle
  Bundler.require(:default)
  require 'zeus/pry' if defined?(Pry)
  Zeus::LoadTracking.add_feature('./Gemfile.lock')
end

#destroyObject



128
129
130
131
132
133
134
135
# File 'lib/zeus/rails.rb', line 128

def destroy
  load_rails_generators
  if rails_5_1_or_higher?
    run_rails_5_1_or_higher_command('destroy')
  else
    require 'rails/commands/destroy'
  end
end

#development_environmentObject



102
103
104
105
106
107
# File 'lib/zeus/rails.rb', line 102

def development_environment
  Bundler.require(:development)
  ::Rails.env = ENV['RAILS_ENV'] = "development"
  require APP_PATH
  ::Rails.application.require_environment!
end

#generateObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/zeus/rails.rb', line 117

def generate
  load_rails_generators

  if rails_5_1_or_higher?
    run_rails_5_1_or_higher_command('generate')
  else
    require 'rails/commands/generate'
  end

end

#prerakeObject



109
110
111
# File 'lib/zeus/rails.rb', line 109

def prerake
  require 'rake'
end

#rakeObject



113
114
115
# File 'lib/zeus/rails.rb', line 113

def rake
  Rake.application.run
end

#runnerObject



137
138
139
140
141
142
143
# File 'lib/zeus/rails.rb', line 137

def runner
  if rails_5_1_or_higher?
    run_rails_5_1_or_higher_command('runner')
  else
    require 'rails/commands/runner'
  end
end

#serverObject



185
186
187
188
189
190
191
192
193
194
# File 'lib/zeus/rails.rb', line 185

def server
  if rails_5_1_or_higher?
    run_rails_5_1_or_higher_command('server')
  else
    require 'rails/commands/server'
    server = ::Rails::Server.new
    Dir.chdir(::Rails.application.root)
    server.start
  end
end

#test(argv = ARGV) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/zeus/rails.rb', line 227

def test(argv=ARGV)
  # if there are two test frameworks and one of them is RSpec,
  # then "zeus test/rspec/testrb" without arguments runs the
  # RSpec suite by default.
  if using_rspec?(argv)
    ARGV.replace(argv)
    # if no directory is given, run the default spec directory
    argv << "spec" if argv.empty?
    if RSpec::Core::Runner.respond_to?(:invoke)
      RSpec::Core::Runner.invoke
    else
      RSpec::Core::Runner.run(argv)
    end
  else
    require 'minitest/autorun' if using_minitest?
    # Minitest and old Test::Unit
    Zeus::M.run(argv)
  end
end

#test_environmentObject



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/zeus/rails.rb', line 196

def test_environment
  Bundler.require(:test)

  ::Rails.env = ENV['RAILS_ENV'] = 'test'
  require APP_PATH

  $rails_rake_task = 'yup' # lie to skip eager loading
  ::Rails.application.require_environment!
  $rails_rake_task = nil

  $LOAD_PATH.unshift ".", "./lib", "./test", "./spec"
end

#test_helperObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/zeus/rails.rb', line 209

def test_helper
  if ENV['RAILS_TEST_HELPER']
    require ENV['RAILS_TEST_HELPER']
  else
    if File.exist?(ROOT_PATH + "/spec/rails_helper.rb")
      # RSpec >= 3.0+
      require 'rails_helper'
    elsif File.exist?(ROOT_PATH + "/spec/spec_helper.rb")
      # RSpec < 3.0
      require 'spec_helper'
    elsif File.exist?(ROOT_PATH + "/test/minitest_helper.rb")
      require 'minitest_helper'
    else
      require 'test_helper'
    end
  end
end