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


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/zeus/rails.rb', line 130

def console
  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

#dbconsoleObject


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

def dbconsole
  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

#default_bundleObject


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

def default_bundle
  Bundler.require(:default)
  Zeus::LoadTracking.add_feature('./Gemfile.lock')
end

#destroyObject


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

def destroy
  load_rails_generators
  require 'rails/commands/destroy'
end

#development_environmentObject


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

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

#generateObject


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

def generate
  load_rails_generators
  require 'rails/commands/generate'
end

#prerakeObject


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

def prerake
  require 'rake'
end

#rakeObject


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

def rake
  Rake.application.run
end

#runnerObject


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

def runner
  require 'rails/commands/runner'
end

#serverObject


163
164
165
166
167
168
# File 'lib/zeus/rails.rb', line 163

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

#test(argv = ARGV) ⇒ Object


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/zeus/rails.rb', line 201

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


170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/zeus/rails.rb', line 170

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


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

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