Class: TranslationPanel::Filter

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/translation_panel/filter.rb

Instance Method Summary collapse

Constructor Details

#initializeFilter

Returns a new instance of Filter.



5
6
7
8
# File 'lib/translation_panel/filter.rb', line 5

def initialize
  @save_url = '/translations/translations/new'
  @condition = :translation_panel?
end

Instance Method Details

#after(controller) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/translation_panel/filter.rb', line 15

def after(controller)
  if controller.send(@condition) && controller.response.status == 200
    case controller.response.content_type
    when "text/html"
      return unless page = controller.response.body
      return unless page.index("</head>")
      header_part = tag :link, :href => "/assets/translation_panel.css",
                        :media => "screen", :rel => "stylesheet", :type => "text/css"
      header_part+=  :script, "", :src => "/assets/translation_panel.js",
                        :type => "text/javascript"
      page.insert page.index("</head>"), header_part
      body_part =  :script,
                        "translationPanel = {translates: #{TranslationPanel.values.to_json}, action: '#{@save_url}', locale: '#{I18n.locale}'}",
                        {:type => "text/javascript"}, false
      page+= body_part
      controller.response.body = page
    when "text/javascript"
      return unless controller.response.body
      body_part = ";$.translator.addTranslates(#{TranslationPanel.values.to_json});"
      controller.response.body+= body_part
    end
    TranslationPanel.kill!
  end
  true
end

#before(controller) ⇒ Object



10
11
12
13
# File 'lib/translation_panel/filter.rb', line 10

def before(controller)
  TranslationPanel.show! if controller.send(@condition)
  true
end