Class: Zeus::Rails

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

Instance Method Summary collapse

Instance Method Details

#_monkeypatch_rakeObject



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/zeus/rails.rb', line 51

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



45
46
47
48
49
# File 'lib/zeus/rails.rb', line 45

def after_fork
  reconnect_activerecord
  restart_girl_friday
  reconnect_redis
end

#bootObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/zeus/rails.rb', line 96

def boot
  $LOAD_PATH.unshift "./lib"

  require BOOT_PATH
  _monkeypatch_rake
  # 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



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

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/zeus/rails.rb', line 183

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



114
115
116
117
118
# File 'lib/zeus/rails.rb', line 114

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

#destroyObject



146
147
148
149
150
151
152
153
# File 'lib/zeus/rails.rb', line 146

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



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

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

#generateObject



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

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



127
128
129
# File 'lib/zeus/rails.rb', line 127

def prerake
  require 'rake'
end

#rakeObject



131
132
133
# File 'lib/zeus/rails.rb', line 131

def rake
  Rake.application.run
end

#runnerObject



155
156
157
158
159
160
161
# File 'lib/zeus/rails.rb', line 155

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

#serverObject



203
204
205
206
207
208
209
210
211
212
# File 'lib/zeus/rails.rb', line 203

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/zeus/rails.rb', line 245

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



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

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



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

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