Module: HappyPlace::Controller
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/happy_place/controller.rb
Instance Method Summary collapse
- #clean_script(class_method, arg) ⇒ Object
-
#js(js_class: nil, method: nil, partial: nil) ⇒ Object
instance methods to go on every controller go here.
Instance Method Details
#clean_script(class_method, arg) ⇒ Object
46 47 48 |
# File 'lib/happy_place/controller.rb', line 46 def clean_script(class_method, arg) "<script type='application/javascript'>jQuery(document).ready(function($) {" + render_to_string(js: class_method + arg) + "});</script>" end |
#js(js_class: nil, method: nil, partial: nil) ⇒ Object
instance methods to go on every controller go here
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/happy_place/controller.rb', line 16 def js(js_class: nil, method: nil, partial: nil) return unless [:js, :html].include?(request.format.to_sym) js_class ||= self.class.name.gsub("::", ".") method ||= action_name if partial.present? appendable = (render_to_string partial: partial).gsub("\n", "") arg = "('#{appendable}');" else arg = "()" end class_method = [js_class, method].join(".") if request.format.to_sym == :js render js: class_method + arg elsif request.format.to_sym == :html render response_body = response.body before_body_end_index = response_body.rindex('</body>') if before_body_end_index.present? before_body = response_body[0, before_body_end_index].html_safe after_body = response_body[before_body_end_index..-1].html_safe response.body = before_body + clean_script(class_method, arg).html_safe + after_body end end end |