Module: Microservice::ApplicationHelper

Defined in:
app/helpers/microservice/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#application_nameObject



4
5
6
# File 'app/helpers/microservice/application_helper.rb', line 4

def application_name
  Microservice::Settings.application_name
end

#css_active_menu_item(identificator) ⇒ Object



92
93
94
95
96
# File 'app/helpers/microservice/application_helper.rb', line 92

def css_active_menu_item(identificator)
  css = ['item']
  css << 'active' if active_link?(identificator)
  css.join(' ')
end

#favicon_pathObject



54
55
56
# File 'app/helpers/microservice/application_helper.rb', line 54

def favicon_path
  image_path('microservice/favicon.ico')
end

#format_date(date) ⇒ Object



77
78
79
80
81
# File 'app/helpers/microservice/application_helper.rb', line 77

def format_date(date)
  return nil unless date

  ::I18n.l(date.to_date, {format: '%d.%m.%Y'})
end

#format_time(time, include_date = true) ⇒ Object



83
84
85
86
87
88
89
90
# File 'app/helpers/microservice/application_helper.rb', line 83

def format_time(time, include_date = true)
  return nil unless time
  options = {}
  options[:format] = '%H:%M'
  time = time.to_time if time.is_a?(String)
  local = (time.utc? ? time.localtime : time)
  (include_date ? "#{format_date(local)} " : '') + ::I18n.l(local, options)
end


180
181
182
183
184
185
186
187
188
# File 'app/helpers/microservice/application_helper.rb', line 180

def home_link(namespace = nil, options = {})
  options = namespace if namespace.is_a?(Hash)

  if namespace == :admin
    link_to(t(:button_administration), main_app.admin_path, {title: t(:title_administration), class: css_active_menu_item(:administration), data: {turbolinks: false}}.merge(options))
  else
    link_to(t(:button_home), main_app.root_path, {title: t(:title_home), class: css_active_menu_item(:home), data: {turbolinks: false}}.merge(options))
  end
end

#html_title(*args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/microservice/application_helper.rb', line 29

def html_title(*args)
  if args.empty?
    title = [application_name]
    title.concat(@html_title || [])
    title.reject(&:blank?).join(' - ')
  else
    @html_title ||= []
    @html_title += args
  end
end

#html_title_from_entityObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/microservice/application_helper.rb', line 58

def html_title_from_entity
  if @entity
    case action_name
    when 'show'
      html_title @entity.heading_show
    when 'new', 'create'
      html_title @entity.heading_new
    when 'edit', 'update'
      html_title @entity.heading_edit
    else
      html_title
    end
  elsif controller.respond_to?(:entity_class) && controller.entity_class
    html_title controller.entity_class.heading_index
  else
    html_title
  end
end


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/microservice/application_helper.rb', line 98

def link_to_entity(klass_or_entity, action = nil, options = {})
  entity_class = klass_or_entity.is_a?(ActiveRecord::Base) ? klass_or_entity.class : klass_or_entity
  entity = klass_or_entity.is_a?(ActiveRecord::Base) ? klass_or_entity : nil
  options, action = action, nil if action.is_a?(Hash) && action.extractable_options?

  action ||= (entity ? :show : :index)

  if options[:css_item]
    css = css_active_menu_item(entity_class)
    options[:class] ||= ''
    options[:class] << (' ' + css) if css.present?

    options[:no_icon] = true if !options.key?(:no_icon)
  end

  case action
  when :index
    # if @entity && entity_class.parent_entity_class == @entity.class && @parent_entity.nil?
    #   route_array = entity_class.class_route_array(@entity)
    # else
    #   route_array = entity_class.class_route_array(@parent_entity)
    # end

    link_to(polymorphic_path(entity_class.class_route_array(@parent_entity)), class: options[:class], title: entity_class.title_index, data: options[:data]) do
      (:i, '', class: (options[:no_icon] ? '' : 'icon ' + entity_class.css_class)) + entity_class.button_index
    end
  when :show
    link_to(polymorphic_path(entity.model_route_array), class: options[:class], title: entity.title_show, data: options[:data]) do
      (:i, '', class: (options[:no_icon] ? '' : 'info icon')) + (options[:no_label] ? '' : entity.button_show)
    end
  when :new
    link_to(new_polymorphic_path(entity_class.class_route_array(@parent_entity)), class: options[:class], title: entity_class.title_new, data: options[:data]) do
      (:i, '', class: (options[:no_icon] ? '' : 'add icon')) + entity_class.button_new
    end
  when :edit
    link_to(edit_polymorphic_path(entity.model_route_array), class: options[:class], title: entity.title_edit, data: options[:data]) do
      (:i, '', class: (options[:no_icon] ? '' : 'edit icon')) + (options[:no_label] ? '' : entity.button_edit)
    end
  when :delete
    link_to(polymorphic_path(entity.model_route_array), class: options[:class], title: entity.title_delete, data: {confirm: t(:text_are_you_sure), method: :delete}) do
      (:i, '', class: (options[:no_icon] ? '' : 'trash icon'))
    end
  end
end


143
144
145
146
147
148
149
# File 'app/helpers/microservice/application_helper.rb', line 143

def link_to_function(name, function = nil, html_options = {}, &block)
  if block_given?
    (:a, {href: '#', onclick: "#{name}; return false;"}.merge(function || {}), &block)
  else
    (:a, name, {href: '#', onclick: "#{function}; return false;"}.merge(html_options))
  end
end


190
191
192
193
194
195
# File 'app/helpers/microservice/application_helper.rb', line 190

def (options = {})
  css = ['item']
  css << 'active' if controller_name == 'UsersController'

  link_to(t(:button_log_in), main_app.new_user_session_path, {title: t(:title_log_in), class: css.join(' '), data: {turbolinks: false}}.merge(options))
end


209
210
211
212
213
# File 'app/helpers/microservice/application_helper.rb', line 209

def logout_link(options = {})
  css = ['item']

  link_to(t(:button_log_out), main_app.destroy_user_session_path, {title: t(:title_log_out), class: css.join(' '), method: :delete, data: {turbolinks: false}}.merge(options))
end

#markdown(text) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/helpers/microservice/application_helper.rb', line 151

def markdown(text)
  options = {
    filter_html: true,
    hard_wrap: true,
    link_attributes: {rel: 'nofollow', target: '_blank'},
    space_after_headers: true,
    fenced_code_blocks: true
  }

  extensions = {
    autolink: true,
    superscript: true,
    disable_indented_code_blocks: true
  }

  renderer = Redcarpet::Render::HTML.new(options)
  markdown = Redcarpet::Markdown.new(renderer, extensions)

  markdown.render(text.to_s).html_safe
end

#render_flash_messagesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/microservice/application_helper.rb', line 8

def render_flash_messages
  s = ''
  flash.each do |k, v|
    case k
    when 'alert'
      message_css = 'warning'
      icon_css = 'warning'
    else
      message_css = 'success'
      icon_css = 'info'
    end
    s << "<div class=\"ui icon #{message_css} message\">"
    s << '<i class="close icon"></i>'
    s << "<i class=\"#{icon_css} icon\"></i>"
    s << '<div class=\"content\"></div>'
    s << "<div class=\"header\">#{v}</div>"
    s << '</div>'
  end
  s.html_safe
end

#show_top_menu_new?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'app/helpers/microservice/application_helper.rb', line 172

def show_top_menu_new?
  controller.respond_to?(:entity_class) && controller.entity_class && !controller.entity_class.hide_top_menu_new
end

#show_top_menu_search?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'app/helpers/microservice/application_helper.rb', line 176

def show_top_menu_search?
  controller.respond_to?(:entity_class) && controller.entity_class && !controller.entity_class.hide_top_menu_search
end


203
204
205
206
207
# File 'app/helpers/microservice/application_helper.rb', line 203

def (options = {})
  css = ['item']

  link_to(t(:button_sign_up), 'https://www.easysoftware.com', {title: t(:title_sign_up), class: css.join(' '), data: {turbolinks: false}}.merge(options))
end


197
198
199
200
201
# File 'app/helpers/microservice/application_helper.rb', line 197

def (options = {})
  css = ['item']

  link_to(t(:button_log_in), main_app.user_sso_omniauth_authorize_path, {title: t(:title_log_in), class: css.join(' '), data: {turbolinks: false}}.merge(options))
end

#title(*args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/microservice/application_helper.rb', line 40

def title(*args)
  strings = args.map do |arg|
    if arg.is_a?(Array) && arg.size >= 2
      link_to(*arg)
    else
      h(arg.to_s)
    end
  end

  html_title(args.reverse.map {|s| (s.is_a?(Array) ? s.first : s).to_s})

  ('h1', strings.join(' &#187; ').html_safe, class: 'ui header')
end