Class: RedmineExtensions::EasyQueryHelpers::Outputs

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redmine_extensions/easy_query_helpers/outputs.rb

Overview

—– OUTPUTS HELPER CLASS —-

Instance Method Summary collapse

Constructor Details

#initialize(presenter, view_context = nil) ⇒ Outputs

Returns a new instance of Outputs.



7
8
9
10
11
12
13
14
15
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 7

def initialize(presenter, view_context = nil)
  if presenter.is_a?(RedmineExtensions::BasePresenter)
    @presenter = presenter
    @query = @presenter.model
  else
    @presenter = @query = presenter
  end
  @view_context = view_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 87

def method_missing(name, *args)
  if name.to_s.ends_with?('?')
    output_enabled?(name.to_s[0..-2])
  else
    super
  end
end

Instance Method Details

#available_output_namesObject



44
45
46
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 44

def available_output_names
  @available_output_names ||= RedmineExtensions::QueryOutput.available_outputs_for( @query )
end

#available_outputsObject



48
49
50
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 48

def available_outputs
  @available_outputs ||= RedmineExtensions::QueryOutput.available_output_klasses_for( @query ).map{|klass| klass.new(@presenter, self) }
end

#each(style = :enabled, &block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 25

def each(style = :enabled, &block)
  if style == :enabled
    outputs.each(&block)
  else
    available_outputs.each(&block)
  end
end

#enabled_outputsObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 33

def enabled_outputs
  available = available_output_names.map(&:to_s)
  res = if available.one?
    available
  else
    Array(@query.outputs).map(&:to_s) & available
  end
  res << 'list' if res.empty?
  res
end

#hObject



83
84
85
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 83

def h
  view_context
end

#output_enabled?(output) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 52

def output_enabled?(output)
  enabled_outputs.include?(output.to_s)
end

#outputsObject



21
22
23
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 21

def outputs
  @outputs ||= enabled_outputs.map{|o| RedmineExtensions::QueryOutput.output_klass_for(o, @query).new(@presenter, self) }.sort_by{|a| a.order}
end

#render_dataObject



75
76
77
78
79
80
81
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 75

def render_data
  if outputs.any?
    outputs.map{ |output| output.render_data }.join.html_safe
  else
    view_context.l(:label_no_output)
  end
end

#render_editObject



71
72
73
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 71

def render_edit
  outputs.map{ |output| output.render_edit }.join.html_safe
end

#render_edit_selects(style = :check_box, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 56

def render_edit_selects(style=:check_box, options={})
  options.delete(:enabled)
  if available_outputs.count == 1
    available_outputs.first.render_edit_box(:hidden_field, options)
  else
    h.(:p) do
      s = h.(:label, h.l(:label_easy_query_outputs))
      available_outputs.each do |o|
        s << o.render_edit_box(style, options.dup)
      end
      s
    end
  end
end

#view_contextObject



17
18
19
# File 'lib/redmine_extensions/easy_query_helpers/outputs.rb', line 17

def view_context
  @view_context || @presenter.h
end