Module: RedmineExtensions::ApplicationHelper

Includes:
RenderingHelper
Defined in:
app/helpers/redmine_extensions/application_helper.rb

Defined Under Namespace

Classes: EasyBoxRenderer

Instance Method Summary collapse

Methods included from RenderingHelper

#query_outputs, #render_with_fallback

Instance Method Details

#autocomplete_field(object_name, method, choices, options = {}, html_options = {}) ⇒ Object

Returns a multiselect autocomplete input tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). Additional options on the input tag can be passed as a hash with options. These options will be passed to the handling javascript as in the example shown. Available values for select are passed as choices attribute. It can be Array of values, or json path for later loading or autocomplete. Please see autocomplete_field_tag documentation for more information about options and available values format combinations. HTML options can be passed as a hash with html_options. These options will be passed to the input element.

Examples

autocomplete_field(:issue, :tag_ids, Tag.all.pluck(:name, :id), multiple: true, show_toggle_button: true)
# => <span class="easy-multiselect-tag-container"> \
      <input type="text" id="issue_tags"  /> \
      <button type="button" tabindex="-1" class="..." role="button" ...>
        <span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-s"></span><span class="ui-button-text">&nbsp;</span>
      </button>
      <a class="icon icon-add clear-link"></a> # toggle button to the multiselect tag from show_toggle_button option
      ...(wraping service tags)
        <input type="hidden" name="issue[tag_ids][]" value="#{@issue.tag_ids.first}" />
        <input type="hidden" name="issue[tag_ids][]" value="#{@issue.tag_ids.second}" />
      ...(wraping service tags end)
     </span>


312
313
314
# File 'app/helpers/redmine_extensions/application_helper.rb', line 312

def autocomplete_field(object_name, method, choices, options={}, html_options={})
  Tags::AutocompleteField.new(object_name, method, self, choices, options, html_options).render
end

#autocomplete_field_tag(name, jsonpath_or_array, selected_values, options = {}) ⇒ Object

Returns a multiselect autocomplete input tag tailored for selecting 1..N values from defined source by jsonpath_or_array. Preselected will be values in selected_values parameter, or if those are empty, and select_first_value option is set, it will select first value from source. See warning from this parameter! Additional options on the input tag can be passed as a hash with options. These options will be passed to the handling javascript. Available format for selected_values:

  • Array of values. It will search for the values in available_values for values names assigned.

  • Array of objects in format: <value send in form>, value: <label - user shown value>

Available options are:

  • multiple - tells if more than one value can be selected.

  • preload - tells if values should be preloaded all at once - in one request - if jsonpathh is a source, this parameter expect it to return all available values.

  • load_immediately - tells if values should be loaded immediatelly after page loaded, or wait for first use of the field.

    Warning! if this option is false, selected values passed in first format will be ignored till it is loaded.
      Please use second format for proper functionality.
    
  • show_toggle_button - only valid with multiple: true, preload: true options set. Shows toggle button to expand select to the multiselect tag.

  • select_first_value - if selectd_values are empty, with this option first available value will be selected.

    Available only with <tt>preload: true</tt> option.
    With <tt>load_immediately: false</tt> it will appear kinda weird for user because it will select the value after user starts to interact with input.
     please consider if this is what you want.
    
  • rootElement - Has sence only if jsonpath is used for available values. It tells if the json response has values wrapped under root element.

    For response like <tt>{projects: [[<name>, <id>], [<name2>, <id2>]]}</tt> user option <tt>rootElement: 'projects'</tt>
    


274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'app/helpers/redmine_extensions/application_helper.rb', line 274

def autocomplete_field_tag(name, jsonpath_or_array, selected_values, options = {})
  options.reverse_merge!({select_first_value: false, show_toggle_button: false, load_immediately: false, preload: true, multiple: true, combo: false})
  options[:id] ||= sanitize_to_id(name)

  selected_values ||= []

  if jsonpath_or_array.is_a?(Array)
    source = jsonpath_or_array.to_json
  else
    source = "'#{jsonpath_or_array}'"
  end

  (:span, :class => 'easy-multiselect-tag-container') do
    text_field_tag('', '', (options[:html_options] || {}).merge(id: options[:id])) +
      late_javascript_tag("$('##{options[:id]}').easymultiselect({multiple: #{options[:multiple]}, rootElement: #{options[:rootElement].to_json}, inputName: '#{name}', preload: #{options[:preload]}, combo: #{options[:combo]}, source: #{source}, selected: #{selected_values.to_json}, show_toggle_button: #{options[:show_toggle_button]}, select_first_value: #{options[:select_first_value]}, load_immediately: #{options[:load_immediately]}, autocomplete_options: #{(options[:jquery_auto_complete_options]||{}).to_json} });")
  end
end

#detect_hide_elements(uniq_id, user = nil, default = true) ⇒ Object

hide elements for issues and users



33
34
35
36
# File 'app/helpers/redmine_extensions/application_helper.rb', line 33

def detect_hide_elements(uniq_id, user = nil, default = true)
  return ''.html_safe if uniq_id.blank?
  return 'style="display:none"'.html_safe if !toggle_button_expanded?(uniq_id, user, default)
end

#easy_avatar_url(user = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/helpers/redmine_extensions/application_helper.rb', line 138

def easy_avatar_url(user = nil)
  user ||= User.current
  result = if Setting.gravatar_enabled?
    options = {:ssl => (request && request.ssl?), :default => Setting.gravatar_default}
    email = nil
    if user.respond_to?(:mail)
      email = user.mail
    elsif user.to_s =~ %r{<(.+?)>}
      email = $1
    end
    email ? gravatar_url(email, options) : ''
  elsif user.easy_avatar_url.present?
    user.easy_avatar_url
  elsif user.respond_to?(:easy_avatar) && (av = user.easy_avatar).present? && (img_url = av.image.url(:small))
    get_easy_absolute_uri_for(img_url).to_s
  end
  result
end

#entity_css_icon(entity_or_entity_class) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/helpers/redmine_extensions/application_helper.rb', line 93

def entity_css_icon(entity_or_entity_class)
  return '' if entity_or_entity_class.nil?

  if entity_or_entity_class.is_a?(Class) && entity_or_entity_class.respond_to?(:css_icon)
    entity_or_entity_class.css_icon
  elsif entity_or_entity_class.is_a?(ActiveRecord::Base)
    if entity_or_entity_class.respond_to?(:css_icon)
      entity_or_entity_class.css_icon
    elsif entity_or_entity_class.class.respond_to?(:css_icon)
      entity_or_entity_class.class.css_icon
    else
      "icon icon-#{entity_or_entity_class.class.name.dasherize}"
    end
  else
    "icon icon-#{entity_or_entity_class.class.name.dasherize}"
  end
end

#get_jasmine_tagsObject



126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/redmine_extensions/application_helper.rb', line 126

def get_jasmine_tags
  tags = params[:jasmine]
  return [] if tags == 'true'
  if tags.is_a?(String)
    [tags.to_sym]
  elsif tags.is_a?(Array)
    tags.map &:to_sym
  else
    []
  end
end

#late_javascript_tag(content_or_options_with_block = nil, html_options = {}, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/helpers/redmine_extensions/application_helper.rb', line 111

def late_javascript_tag(content_or_options_with_block = nil, html_options = {}, &block)
  content =
    if block_given?
      html_options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
      capture(&block)
    else
      content_or_options_with_block
    end
  html_options.reverse_merge!({type:'application/javascript'})
  priority = html_options.delete(:priority) || 0
  content = "  EasyGem.schedule.late(function(){#{content}  }, #{priority});"

  (:script, javascript_cdata_section(content), html_options)
end

#plugin_settings_path(plugin, *attrs) ⇒ Object

——-= Hack methods =——



7
8
9
10
11
12
13
# File 'app/helpers/redmine_extensions/application_helper.rb', line 7

def plugin_settings_path(plugin, *attrs)
  if plugin.is_a?(Redmine::Plugin) && (plugin.settings[:only_easy] || plugin.settings[:easy_settings])
    edit_easy_setting_path(plugin, *attrs)
  else
    super
  end
end

#present(model, options = {}, &block) ⇒ Object

——-= Rendering and presenting methods =——-



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/redmine_extensions/application_helper.rb', line 17

def present(model, options={}, &block)
  if model.is_a?(RedmineExtensions::BasePresenter)
    presenter = model.update_options(options.merge(view_context: self))
  else
    presenter = RedmineExtensions::BasePresenter.present(model, self, options)
  end
  if block_given?
    yield presenter
  else
    presenter
  end
end

#query_for_entity(entity_class) ⇒ Object



47
48
49
50
51
52
# File 'app/helpers/redmine_extensions/application_helper.rb', line 47

def query_for_entity(entity_class)
  entity_class_name = entity_class.name
  query_class = "Easy#{entity_class_name}Query".constantize rescue nil
  return query_class if query_class && query_class < EasyQuery
  query_class ||= "#{entity_class_name}Query".constantize rescue nil
end

#render_entity_assignments(entity, target_entity, options = {}, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/redmine_extensions/application_helper.rb', line 54

def render_entity_assignments(entity, target_entity, options = {}, &block)
  options ||= {}
  collection_name = options.delete(:collection_name) || target_entity.name.pluralize.underscore
  query_class = query_for_entity(target_entity)

  return '' if !query_class || !entity.respond_to?(collection_name)

  project = options.delete(:project)

  query = query_class.new(:name => 'c_query')
  query.project = project
  query.set_entity_scope(entity, collection_name)
  query.column_names = options[:query_column_names] unless options[:query_column_names].blank?

  entities = query.entities

  entities_count = entities.size
  options[:entities_count] = entities_count
  options[:module_name] ||= "entity_#{entity.class.name.underscore}_#{entity.id}_#{collection_name}"
  options[:heading] ||= l("label_#{query.entity.name.underscore}_plural", :default => 'Heading')

  if options[:context_menus_path].nil?
    options[:context_menus_path] = [
      "context_menu_#{collection_name}_path".to_sym,
      "context_menus_#{collection_name}_path".to_sym,
      "#{collection_name}_context_menu_path".to_sym
    ].detect do |m|
      m if respond_to?(m)
    end
  end

  query.output = options[:display_style] || (entities_count > 3 ? 'list' : 'tiles')

  render(:partial => 'easy_entity_assignments/assignments_container', :locals => {
    :entity => entity,
    :query => query, :project => project,
    :entities => entities, :entities_count => entities_count, :options => options})
end

#render_module_easy_box(id, heading, options = {}, &block) ⇒ Object

Options

  • class: Hash or String - This option can be used to add custom CSS classes. It can be String or Hash.

    class: {heading: 'heading-additional-css', container: 'container-additional-css'}
    
  • heading_tag: name of HTML element of module heading - By default its its h3

** Aliases for this options are: wrapping_heading_element, header_tag

  • toggle: false - This disable toggle function (collapsible and remember)

** Aliases for this options are: collapsible, no_expander

  • remember: false - This disable remember function of toggle container

** Aliases for this options are: ajax_call



167
168
169
170
171
172
173
174
175
176
# File 'app/helpers/redmine_extensions/application_helper.rb', line 167

def render_module_easy_box(id, heading, options = {}, &block) # with fallback to old
  options[:toggle] = true unless options.key?(:toggle)
  options[:remember] = options.delete(:ajax_call) if options.key?(:ajax_call)
  options[:collapsible] = !options.delete(:no_expander) if options.key?(:no_expander)

  renderer = EasyBoxRenderer.new(self, id, heading, options)
  renderer.content = capture {yield renderer}

  renderer.render
end

#url_to_entity(entity, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'app/helpers/redmine_extensions/application_helper.rb', line 38

def url_to_entity(entity, options={})
  m = "url_to_#{entity.class.name.underscore}".to_sym
  if respond_to?(m)
    send(m, entity, options)
  else
    nil
  end
end