Class: RedmineExtensions::QueryOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/redmine_extensions/query_output.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_presenter, outputs = nil) ⇒ QueryOutput

Returns a new instance of QueryOutput.



76
77
78
79
# File 'lib/redmine_extensions/query_output.rb', line 76

def initialize(query_presenter, outputs=nil)
  @query = query_presenter
  @outputs = outputs || query_presenter.outputs
end

Instance Attribute Details

#queryObject

Returns the value of attribute query.



4
5
6
# File 'lib/redmine_extensions/query_output.rb', line 4

def query
  @query
end

Class Method Details

.available_for?(query) ⇒ Boolean

Take query and decide if it is possible to render it by this output

Returns:

  • (Boolean)


72
73
74
# File 'lib/redmine_extensions/query_output.rb', line 72

def self.available_for?(query)
  query.is_a? Query
end

.available_output_klasses_for(query) ⇒ Object



57
58
59
# File 'lib/redmine_extensions/query_output.rb', line 57

def self.available_output_klasses_for(query)
  filter_registered_for(query).values
end

.available_outputs_for(query) ⇒ Object



53
54
55
# File 'lib/redmine_extensions/query_output.rb', line 53

def self.available_outputs_for(query)
  filter_registered_for(query).keys
end

.filter_registered_for(query, whitelist = []) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/redmine_extensions/query_output.rb', line 39

def self.filter_registered_for(query, whitelist = [])
  whitelist += (registered_whitelists[query.type] || [])
  res = registered_outputs
  res = res.slice(*whitelist.map(&:to_sym)) if whitelist.any?
  res = res.select do |_name, output|
    output.available_for?(query)
  end
  res.merge(registered_per_query[query.type] || {})
end

.keyObject



66
67
68
# File 'lib/redmine_extensions/query_output.rb', line 66

def self.key
  self.name.split('::').last.sub(/Output$/, '').underscore
end

.output_available?(query, output) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/redmine_extensions/query_output.rb', line 49

def self.output_available?(query, output)
  self.filter_registered_for(query, [output]).any?
end

.output_klass_for(output, query = nil) ⇒ Object



61
62
63
64
# File 'lib/redmine_extensions/query_output.rb', line 61

def self.output_klass_for(output, query=nil)
  filtered = query.nil? ? registered_outputs : filter_registered_for(query)
  filtered[output.to_sym]
end

.register_output(klass, options = {}) ⇒ Object



19
20
21
22
# File 'lib/redmine_extensions/query_output.rb', line 19

def self.register_output(klass, options={})
  register_as = (options[:as] || klass.key).to_sym
  registered_outputs[register_as] = klass
end

.register_output_for_query(klass, query_class_names, **options) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/redmine_extensions/query_output.rb', line 24

def self.register_output_for_query(klass, query_class_names, **options)
  register_as ||= (options[:as] || klass.key).to_sym
  Array.wrap(query_class_names).each do |query_class_name|
    registered_per_query[query_class_name] ||= {}
    registered_per_query[query_class_name][register_as] = klass
  end
end

.registered_outputsObject



7
8
9
# File 'lib/redmine_extensions/query_output.rb', line 7

def self.registered_outputs
  @@registered_outputs ||= {}
end

.registered_per_queryObject



11
12
13
# File 'lib/redmine_extensions/query_output.rb', line 11

def self.registered_per_query
  @@registered_per_query ||= {}
end

.registered_whitelistsObject



15
16
17
# File 'lib/redmine_extensions/query_output.rb', line 15

def self.registered_whitelists
  @@registered_whitelists ||= {}
end

.whitelist_outputs_for_query(query_class_names, outputs) ⇒ Object



32
33
34
35
36
37
# File 'lib/redmine_extensions/query_output.rb', line 32

def self.whitelist_outputs_for_query(query_class_names, outputs)
  Array.wrap(query_class_names).each do |query_class_name|
    registered_whitelists[query_class_name] ||= []
    registered_whitelists[query_class_name].concat( Array.wrap(outputs).map(&:to_s) ).uniq!
  end
end

Instance Method Details

#data_partialObject



81
82
83
# File 'lib/redmine_extensions/query_output.rb', line 81

def data_partial
  'easy_queries/easy_query_'+key
end

#edit_formObject



143
144
145
# File 'lib/redmine_extensions/query_output.rb', line 143

def edit_form
  'easy_queries/form_'+key+'_settings'
end

#enabled?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/redmine_extensions/query_output.rb', line 101

def enabled?
  @outputs.output_enabled?(key)
end

#hObject



147
148
149
# File 'lib/redmine_extensions/query_output.rb', line 147

def h
  @outputs.view_context
end

#headerObject



109
110
111
112
# File 'lib/redmine_extensions/query_output.rb', line 109

def header
  content = options["#{key}_header".to_sym]
  h.(:h3, content.html_safe) unless content.blank?
end

#keyObject



93
94
95
# File 'lib/redmine_extensions/query_output.rb', line 93

def key
  self.class.key
end

#labelObject



97
98
99
# File 'lib/redmine_extensions/query_output.rb', line 97

def label
  h.l('label_easy_query_output.'+key, default: key.humanize)
end

#orderObject



85
86
87
# File 'lib/redmine_extensions/query_output.rb', line 85

def order
  10
end

#render_dataObject



89
90
91
# File 'lib/redmine_extensions/query_output.rb', line 89

def render_data
  h.render partial: data_partial, locals: variables
end

#render_edit(action = 'edit') ⇒ Object



133
134
135
136
137
# File 'lib/redmine_extensions/query_output.rb', line 133

def render_edit(action='edit')
  h.(:fieldset, class: "easy-query-filters-field #{key}_settings", style: ('display: none;' unless enabled? )) do
    h.(:legend, label) + render_edit_form(action)
  end
end

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/redmine_extensions/query_output.rb', line 114

def render_edit_box(style=:check_box, options={})
  box_id = "#{query.modul_uniq_id}output_#{key}"

  options[:class] = "#{options[:class]} #{query.modul_uniq_id}output_switch #{query.modul_uniq_id}content_switch"
  options[:enabled] = enabled? unless options.key?(:enabled)

  r = ''
  case style
  when :hidden_field
    r << h.hidden_field_tag(query.block_name.blank? ? 'outputs[]' : "#{query.block_name}[outputs][]", key, id: box_id, class: options[:class])
  when :check_box, :radio_button
    r << h.send("#{style}_tag" , query.block_name.blank? ? 'outputs[]' : "#{query.block_name}[outputs][]", key, options[:enabled], id: box_id, class: options[:class])
    r << h.label_tag(box_id, h.l('label_my_page_issue_output.' + key), :class => 'inline')
  else
    raise 'Style of edit box is not allowed'
  end
  r.html_safe
end

#render_edit_form(action = 'edit') ⇒ Object



139
140
141
# File 'lib/redmine_extensions/query_output.rb', line 139

def render_edit_form(action='edit')
  h.render(self.edit_form, options.reverse_merge(query: query, modul_uniq_id: query.modul_uniq_id, block_name: query.block_name, action: action, page_module: query.page_module))
end

#variablesObject



105
106
107
# File 'lib/redmine_extensions/query_output.rb', line 105

def variables
  options.reverse_merge(query: @query, output: self)
end