Guest User

Untitled

a guest
Apr 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. # support for using helper methods in controllers/models
  2. #
  3. class Helper
  4. ActionView::Helpers.constants.grep(/Helper/i).each do |const|
  5. include ActionView::Helpers.const_get(const)
  6. end
  7.  
  8. attr_accessor 'controller'
  9.  
  10. def initialize controller = nil
  11. @controller = controller
  12. extend @controller.master_helper_module if @controller
  13. end
  14.  
  15. def session
  16. controller.session
  17. end
  18. end
  19.  
  20. class ApplicationController < ActionController::Base
  21. cattr_accessor 'current'
  22. before_filter{|controller| ApplicationController.current = controller}
  23.  
  24. protected
  25. def helper &block
  26. @helper = Helper.new(ApplicationController.current || self)
  27. block ? @helper.instance_eval(&block) : @helper
  28. end
  29.  
  30. def ApplicationController.helper_import *methods
  31. methods.flatten.compact.each do |method|
  32. code = <<-code
  33. def #{ method } *a, &b
  34. helper{ #{ method }(*a, &b) }
  35. end
  36. protected '#{ method }'
  37. code
  38. eval code
  39. end
  40. end
  41. end
  42.  
  43. class ActiveRecord::Base
  44. def helper &block
  45. helper = Helper.new(ApplicationController.current || ApplicationController.new)
  46. block ? helper.instance_eval(&block) : helper
  47. end
  48. end
  49.  
  50. # support for immediate (throw/catch) base rendering methods
  51. #
  52. class ApplicationController < ActionController::Base
  53. protected
  54. def render! *a, &b
  55. render *a, &b unless a.empty? and b.nil?
  56. throw :render, self
  57. end
  58.  
  59. def redirect_to! *a, &b
  60. redirect_to *a, &b
  61. ensure
  62. render!
  63. end
  64.  
  65. def redirect_to_url! *a, &b
  66. redirect_to_url *a, &b
  67. ensure
  68. render!
  69. end
  70.  
  71. def not_found! text = 'Not Found'
  72. render! :text => text, :status => 404
  73. end
  74.  
  75. def perform_action_without_filters
  76. if self.class.action_methods.include?(action_name)
  77. catch(:render){ send(action_name) }
  78. render unless performed?
  79. elsif respond_to? :method_missing
  80. catch(:render){ send(:method_missing, action_name) }
  81. render unless performed?
  82. elsif template_exists? && template_public?
  83. render
  84. else
  85. raise ::ActionController::UnknownAction, "No action responded to #{action_name}", caller
  86. end
  87. end
  88. end
  89.  
  90. # generic error classes used for model level application errors
  91. #
  92. class ActiveRecord::Base
  93. class Error < ::ActiveRecord::ActiveRecordError; end
  94. class RecordNotFound < ::ActiveRecord::RecordNotFound; end
  95. end
  96.  
  97. class Object
  98. # support for logging to the rails log from anywhere in the codebase
  99. #
  100. def log(*a, &b)
  101. logger = RAILS_DEFAULT_LOGGER
  102. if a.empty? and b.nil?
  103. logger
  104. else
  105. logger.info(*a, &b)
  106. end
  107. end
  108.  
  109. # support for executing sql on the raw connection, getting back an array of
  110. # results
  111. #
  112. def db(*a, &b)
  113. connection = ActiveRecord::Base.connection.raw_connection
  114. return connection if a.empty? and b.nil?
  115. adapter = ActiveRecord::Base.configurations[ RAILS_ENV ][ 'adapter' ].to_s
  116. case adapter
  117. when 'oci'
  118. cursor = connection.exec *a
  119. if b
  120. while row = cursor.fetch;
  121. b.call row
  122. end
  123. else
  124. returning [] do |rows|
  125. while row = cursor.fetch
  126. rows << row
  127. end
  128. end
  129. end
  130.  
  131. when 'mysql'
  132. if b
  133. connection.query(*a).each do |row|
  134. b.call row
  135. end
  136. else
  137. returning [] do |rows|
  138. connection.query(*a).each do |row|
  139. rows << row
  140. end
  141. end
  142. end
  143.  
  144. else
  145. raise ArgumentError, "#{ adapter } not implemented yet"
  146. end
  147. end
  148. end
Add Comment
Please, Sign In to add comment