Guest User

Untitled

a guest
Feb 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.14 KB | None | 0 0
  1. # force const_missing to preload these classes
  2. #
  3. ActionController
  4. ActionController::Base
  5. ActiveRecord
  6. ActiveRecord::Base
  7.  
  8. # support for using helper methods in controllers/models
  9. #
  10. class Helper
  11. def Helper.list
  12. ActionView::Helpers.constants.grep(/Helper/i).map do |const|
  13. ActionView::Helpers.const_get(const)
  14. end
  15. end
  16.  
  17. attr_accessor 'controller'
  18.  
  19. def initialize controller = ( ActionController.current || ApplicationController.new ), *modules
  20. @controller = controller
  21.  
  22. modules.push nil if modules.empty?
  23.  
  24. modules.flatten.each do |mod|
  25. case mod
  26. when NilClass, :all, 'all'
  27. Helper.list.each{|helper| extend helper}
  28.  
  29. when Module
  30. extend mod
  31.  
  32. when Symbol, String
  33. mod = mod.to_s.underscore.sub(/helper$/, '')
  34. Helper.list.each do |helper|
  35. if mod == helper.name.underscore.sub(/helper$/, '')
  36. extend helper
  37. end
  38. end
  39.  
  40. when Regexp
  41. Helper.list.each do |helper|
  42. if helper.name =~ mod
  43. extend helper
  44. end
  45. end
  46.  
  47. else
  48. raise ArgumentError, mod.class.name
  49. end
  50. end
  51.  
  52. extend @controller.master_helper_module if @controller
  53. end
  54.  
  55. def session
  56. controller.session
  57. end
  58. end
  59.  
  60. module ActionController
  61. class << self
  62. attr_accessor 'current'
  63. end
  64. end
  65.  
  66. class ActionController::Base
  67. before_filter :setup_current_controller
  68. after_filter :teardown_current_controller
  69.  
  70. protected
  71. def setup_current_controller
  72. ::ActionController.current = self
  73. end
  74. def teardown_current_controller
  75. ::ActionController.current = nil
  76. end
  77.  
  78. def helper &block
  79. @helper = Helper.new(::ActionController.current || self)
  80. block ? @helper.instance_eval(&block) : @helper
  81. end
  82.  
  83. def self.helper_import *methods
  84. methods.flatten.compact.each do |method|
  85. code = <<-code
  86. def #{ method } *a, &b
  87. helper{ #{ method }(*a, &b) }
  88. end
  89. protected '#{ method }'
  90. code
  91. eval code
  92. end
  93. end
  94. end
  95.  
  96. class ActiveRecord::Base
  97. def helper &block
  98. helper = Helper.new(::ActionController.current || ::ActionController::Base.new)
  99. block ? helper.instance_eval(&block) : helper
  100. end
  101. end
  102.  
  103. # support for immediate (throw/catch) base rendering methods
  104. #
  105. class ActionController::Base
  106. protected
  107. def render! *a, &b
  108. render *a, &b unless a.empty? and b.nil?
  109. throw :render, self
  110. end
  111.  
  112. def redirect_to! *a, &b
  113. redirect_to *a, &b
  114. ensure
  115. render!
  116. end
  117.  
  118. def redirect_to_url! *a, &b
  119. redirect_to_url *a, &b
  120. ensure
  121. render!
  122. end
  123.  
  124. def not_found! options = {}
  125. options = {:inline => options} if String == options
  126. options.to_options!
  127. unless options.has_key?(:inline) or options.has_key?(:text)
  128. options[:inline] = <<-html
  129. <div style='padding:3em;color:#666;text-align:center;font-size:1.2em;'>
  130. <p>
  131. Sorry, but we could not find the page you are looking for.
  132. </p>
  133. <p style='color:#333'>
  134. #{ h helper.url_for(request.request_uri) }
  135. </p>
  136. </div>
  137. html
  138. end
  139. options[:status] ||= 404
  140. options[:layout] ||= 'application' unless options[:layout] == false
  141. render! options
  142. end
  143.  
  144. def perform_action_without_filters
  145. if self.class.action_methods.include?(action_name)
  146. catch(:render){ send(action_name) }
  147. render unless performed?
  148. elsif respond_to? :method_missing
  149. catch(:render){ send(:method_missing, action_name) }
  150. render unless performed?
  151. elsif template_exists? && template_public?
  152. render
  153. else
  154. raise ::ActionController::UnknownAction, "No action responded to #{action_name}", caller
  155. end
  156. end
  157. end
  158.  
  159. # generic error classes used for model level application errors
  160. #
  161. class ActiveRecord::Base
  162. class Error < ::ActiveRecord::ActiveRecordError; end
  163. class RecordNotFound < ::ActiveRecord::RecordNotFound; end
  164. end
  165.  
  166. # support for logging to the rails log from anywhere in the codebase
  167. #
  168. class Object
  169. def log(*a, &b)
  170. logger = RAILS_DEFAULT_LOGGER
  171. if a.empty? and b.nil?
  172. logger
  173. else
  174. logger.info(*a, &b)
  175. end
  176. end
  177. end
  178.  
  179. # support for conditional excution based on rails_env
  180. #
  181. class Object
  182. %w( test development production training ).each do |rails_env|
  183. module_eval <<-code
  184. def #{ rails_env }? &block
  185. if defined?(RAILS_ENV) and RAILS_ENV == '#{ rails_env }'
  186. return( block ? block.call : true )
  187. end
  188. return false
  189. end
  190. code
  191. end
  192.  
  193. def stage? &block
  194. if defined?(RAILS_STAGE) and RAILS_STAGE
  195. return( block ? block.call : true )
  196. end
  197. return false
  198. end
  199. end
  200.  
  201. # support for executing sql on the raw connection, getting back an array of
  202. # results
  203. #
  204. class Object
  205. def db(*a, &b)
  206. connection = ActiveRecord::Base.connection.raw_connection
  207. return connection if a.empty? and b.nil?
  208. adapter = ActiveRecord::Base.configurations[ RAILS_ENV ][ 'adapter' ].to_s
  209. case adapter
  210. when 'oci', 'oracle'
  211. cursor = connection.exec *a
  212. if b
  213. while row = cursor.fetch;
  214. b.call row
  215. end
  216. else
  217. returning [] do |rows|
  218. while row = cursor.fetch
  219. rows << row
  220. end
  221. end
  222. end
  223.  
  224. when 'mysql', 'postgresql', 'sqlite'
  225. if b
  226. connection.query(*a).each do |row|
  227. b.call row
  228. end
  229. else
  230. returning [] do |rows|
  231. connection.query(*a).each do |row|
  232. rows << row
  233. end
  234. end
  235. end
  236.  
  237. else
  238. raise ArgumentError, "#{ adapter } not implemented yet"
  239. end
  240. end
  241. end
  242.  
  243. # support for class level declarations of ActiveRecord default values for
  244. # *new* records
  245. #
  246. class ActiveRecord::Base
  247. class << self
  248. def defaults *argv, &block
  249. const_set(:Defaults, []) unless const_defined?(:Defaults)
  250. defaults = const_get(:Defaults)
  251.  
  252. options =
  253. if block
  254. argv.flatten.inject({}){|h,k| h.update k => block}
  255. else
  256. case argv.size
  257. when 2
  258. { argv.first => argv.last }
  259. else
  260. argv.inject({}){|h,x| h.update x.to_hash}
  261. end
  262. end
  263. options.to_options!
  264.  
  265. unless options.empty?
  266. options.each{|k,v| defaults << [k,v]}
  267. add_after_initialize_with_defaults! defaults
  268. end
  269.  
  270. defaults
  271. end
  272.  
  273. alias_method :default, :defaults
  274.  
  275. def add_after_initialize_with_defaults! defaults = {}
  276. return if defined?(@add_after_initialize_with_defaults)
  277.  
  278. after_initialize = instance_method(:after_initialize) rescue nil
  279.  
  280. define_method(:after_initialize) do |*args|
  281. this = self
  282. after_initialize.bind(self).call(*args) if after_initialize
  283. return unless new_record?
  284. defaults.each do |key, value|
  285. value = instance_eval(&value) if value.respond_to?(:to_proc)
  286. write_attribute key, value
  287. end
  288. end
  289.  
  290. ensure
  291. @add_after_initialize_with_defaults = true unless $!
  292. end
  293. end
  294. end
  295.  
  296. # support for indexing operator
  297. #
  298. class ActiveRecord::Base
  299. def self.[](*args, &block)
  300. find(*args, &block)
  301. end
  302. end
  303.  
  304. # support for using named routes from the console
  305. #
  306. def use_named_routes! options = {}
  307. include ActionController::UrlWriter
  308. options.to_options!
  309. options.reverse_merge! :host => 'localhost', :port => 3000
  310. default_url_options.reverse_merge!(options)
  311. end
  312.  
  313.  
  314. module Kernel
  315. private
  316. def transaction(*a, &b)
  317. ActiveRecord::Base.transaction(*a, &b)
  318. end
  319. def rollback!
  320. raise ActiveRecord::Rollback
  321. end
  322. def helper &block
  323. helper = Helper.new(::ActionController.current || ::ActionController::Base.new)
  324. block ? helper.instance_eval(&block) : helper
  325. end
  326. end
Add Comment
Please, Sign In to add comment