Module: Backend::BaseHelper

Defined in:
app/helpers/backend/base_helper.rb

Defined Under Namespace

Classes: BasicCalendar, Menu

Instance Method Summary collapse

Instance Method Details

#add_snippets(place, _options = {}) ⇒ Object


99
100
101
102
103
104
105
# File 'app/helpers/backend/base_helper.rb', line 99

def add_snippets(place, _options = {})
  Ekylibre::Snippet.at(place).each do |s|
    snippet(s.name, { icon: :plug }.merge(s.options)) do
      render(file: s.path)
    end
  end
end

#basic_calendar(all_records, options = {}, &block) ⇒ Object

Emulate old simple_calendar API


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/backend/base_helper.rb', line 70

def basic_calendar(all_records, options = {}, &block)
  # options[:events] = all_records
  options[:param_name] ||= :started_on
  BasicCalendar.new(self, options).render do |event_on, records|
    records = all_records.select do |event|
      event_on == event.started_at.to_date
    end
    (:div) do
      (:span, event_on.day, class: 'day-number') +
        records.collect do |event|
          capture(event, &block)
        end.join.html_safe
    end
  end
end

#campaign_selector(campaign = nil, options = {}) ⇒ Object


235
236
237
238
239
240
241
242
# File 'app/helpers/backend/base_helper.rb', line 235

def campaign_selector(campaign = nil, options = {})
  unless Campaign.any?
    @current_campaign = Campaign.find_or_create_by!(harvest_year: Date.current.year)
    current_user.current_campaign = @current_campaign
  end
  campaign ||= current_campaign
  render 'backend/shared/campaign_selector', campaign: campaign, param_name: options[:param_name] || :current_campaign
end

#collectionObject


29
30
31
# File 'app/helpers/backend/base_helper.rb', line 29

def collection
  instance_variable_get('@' + controller_name)
end

#historic_of(record = nil) ⇒ Object


33
34
35
36
# File 'app/helpers/backend/base_helper.rb', line 33

def historic_of(record = nil)
  record ||= resource
  render(partial: 'backend/shared/historic', locals: { resource: resource })
end

#info(label, value, options = {}, &_block) ⇒ Object


340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'app/helpers/backend/base_helper.rb', line 340

def info(label, value, options = {}, &_block)
  css_class = "#{options.delete(:level) || :med}-info"
  options[:class] = if options[:class]
                      options[:class].to_s + ' ' + css_class
                    else
                      css_class
                    end
  options[:class] << ' important' if options.delete(:important)
  (:div, options) do
    (:span, label, class: 'title') +
      (:span, value, class: 'value')
  end
end

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


354
355
356
357
358
359
360
361
362
# File 'app/helpers/backend/base_helper.rb', line 354

def infos(options = {}, &block)
  css_class = 'big-infos'
  if options[:class]
    options[:class] += ' ' + css_class
  else
    options[:class] = css_class
  end
  (:div, options, &block)
end

#janus(*args) {|lister| ... } ⇒ Object

Yields:

  • (lister)

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'app/helpers/backend/base_helper.rb', line 277

def janus(*args, &_block)
  options = args.extract_options!
  name = args.shift || ("#{controller_path}-#{action_name}-" + caller.first.split(/\:/).second).parameterize

  lister = Ekylibre::Support::Lister.new(:face)
  yield lister
  faces = lister.faces

  return '' unless faces.any?

  faces_names = faces.map { |f| f.args.first.to_s }

  active_face = nil
  if pref = current_user.preferences.find_by(name: "interface.janus.#{name}.current_face")
    face = pref.value.to_s
    active_face = face if faces_names.include? face
  end
  active_face ||= faces_names.first

  # Adds views
  html_code = faces.map do |face|
    face_name = face.args.first.to_s
    classes = ['face']
    classes << 'active' if active_face == face_name
    (:div, id: "face-#{face_name}", data: { face: face_name }, class: classes, &face.block)
  end.join.html_safe

  # Adds toggle buttons
  if faces.count > 1
    content_for :view_toolbar do
      (:div, data: { janus: url_for(controller: '/backend/januses', action: :toggle, id: name, default: faces_names.first) }, class: 'btn-group') do
        faces.collect do |face|
          face_name = face.args.first.to_s
          classes = ['btn', 'btn-default']
          classes << 'active' if face_name == active_face
          link_to(face_name, data: { toggle: 'face' }, class: classes, title: face_name.tl) do
            (:i, '', class: "icon icon-#{face_name}") + ' '.html_safe + face_name.tl
          end
        end.join.html_safe
      end
    end
  end

  html_code
end

#lights(status, html_options = {}) ⇒ Object


244
245
246
247
248
249
250
251
252
253
254
255
# File 'app/helpers/backend/base_helper.rb', line 244

def lights(status, html_options = {})
  if html_options.key?(:class)
    html_options[:class] << " lights lights-#{status}"
  else
    html_options[:class] = "lights lights-#{status}"
  end
  (:span, html_options) do
    (:span, nil, class: 'go') +
      (:span, nil, class: 'caution') +
      (:span, nil, class: 'stop')
  end
end

#main_campaign_selectorObject


229
230
231
232
233
# File 'app/helpers/backend/base_helper.rb', line 229

def main_campaign_selector
  content_for(:heading_toolbar) do
    campaign_selector
  end
end

#main_list(*args) ⇒ Object


272
273
274
275
# File 'app/helpers/backend/base_helper.rb', line 272

def main_list(*args)
  options = args.extract_options!
  list *args, options.deep_merge(content_for: { settings: :meta_toolbar, pagination: :meta_toolbar, actions: :main_toolbar })
end

#main_state_bar(resource, options = {}) ⇒ Object


264
265
266
# File 'app/helpers/backend/base_helper.rb', line 264

def main_state_bar(resource, options = {})
  content_for(:main_statebar, state_bar(resource, options))
end

#main_state_bar_tagObject


268
269
270
# File 'app/helpers/backend/base_helper.rb', line 268

def main_state_bar_tag
  content_for(:main_statebar) if content_for?(:main_statebar)
end

It's the menu generated for the current user Therefore: No current user => No menu


48
49
50
# File 'app/helpers/backend/base_helper.rb', line 48

def menus
  Ekylibre.menu
end

#movements_chart(resource) ⇒ Object

chart for product movements


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/helpers/backend/base_helper.rb', line 208

def movements_chart(resource)
  movements = resource.movements.reorder(:started_at)
  series = []
  now = (Time.zone.now + 7.days)
  window = 1.day
  min = (resource.born_at ? resource.born_at : now - window) - 7.days
  min = now - window if (now - min) < window
  if movements.any?
    data = []
    data += movements.inject({}) do |hash, pair|
      hash[pair.started_at.to_usec] = pair.population.to_d
      hash
    end.collect { |k, v| [k, v.to_s.to_f] }
    # current population
    data << [now.to_usec, resource.population.to_d.to_s.to_f]
    series << { name: resource.name, data: data, step: 'left' }
  end
  return no_data if series.empty?
  line_highcharts(series, legend: {}, y_axis: { title: { text: :indicator.tl } }, x_axis: { type: 'datetime', title: { enabled: true, text: :months.tl }, min: min.to_usec })
end

42
43
44
# File 'app/helpers/backend/base_helper.rb', line 42

def navigation_tag
  render(partial: 'layouts/navigation')
end

#part_authorized?(part) ⇒ Boolean

Returns:

  • (Boolean)

86
87
88
89
90
91
92
93
# File 'app/helpers/backend/base_helper.rb', line 86

def part_authorized?(part)
  part.children.each do |group|
    group.children.each do |item|
      return true if authorized?(item.default_page.to_hash)
    end
  end
  false
end

#resourceObject


21
22
23
# File 'app/helpers/backend/base_helper.rb', line 21

def resource
  instance_variable_get('@' + controller_name.singularize)
end

#resource_info(name, options = {}, &block) ⇒ Object


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'app/helpers/backend/base_helper.rb', line 323

def resource_info(name, options = {}, &block)
  value = resource.send(name)
  return nil if value.blank? && !options[:force]
  nomenclature = options.delete(:nomenclature)
  if nomenclature.is_a?(TrueClass)
    value = Nomen.find(name.to_s.pluralize, value)
  elsif nomenclature.is_a?(Symbol)
    value = Nomen.find(nomenclature, value)
  end
  label = options.delete(:label) || resource_model.human_attribute_name(name)
  if block_given?
    info(label, capture(value, &block), options)
  else
    info(label, value.respond_to?(:l) ? value.l : value, options)
  end
end

#resource_modelObject


25
26
27
# File 'app/helpers/backend/base_helper.rb', line 25

def resource_model
  controller_name.classify.constantize
end

#root_modelsObject


38
39
40
# File 'app/helpers/backend/base_helper.rb', line 38

def root_models
  Ekylibre::Schema.models.collect { |a| [Ekylibre::Record.human_name(a.to_s.singularize), a.to_s.singularize] }.sort { |a, b| a[0].ascii <=> b[0].ascii }
end

#side_menu(*args) {|menu| ... } ⇒ Object

Yields:

  • (menu)

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
# File 'app/helpers/backend/base_helper.rb', line 107

def side_menu(*args, &_block)
  return '' unless block_given?
  main_options = (args[-1].is_a?(Hash) ? args.delete_at(-1) : {})
  menu = Menu.new
  yield menu

  main_name = args[0].to_s.to_sym
  main_options[:icon] ||= main_name.to_s.parameterize.tr('_', '-')

  html = ''.html_safe
  for name, url, options in menu.items
    li_options = {}
    li_options[:class] = 'active' if options.delete(:active)

    kontroller = (url.is_a?(Hash) ? url[:controller] : nil) || controller_name
    options[:title] ||= ::I18n.t("actions.#{kontroller}.#{name}".to_sym, { default: ["labels.#{name}".to_sym] }.merge(options.delete(:i18n) || {}))
    if icon = options.delete(:icon)
      item[:title] = (:i, '', class: 'icon-' + icon.to_s) + ' '.html_safe + h(item[:title])
    end
    url[:action] ||= name if name != :back && url.is_a?(Hash)
    html << (:li, link_to(options[:title], url, options), li_options) if authorized?(url)
  end

  unless html.blank?
    html = (:ul, html)
    snippet(main_name, main_options) { html }
  end

  nil
end

#side_tagObject


95
96
97
# File 'app/helpers/backend/base_helper.rb', line 95

def side_tag
  render(partial: 'layouts/side')
end

#snippet(name, options = {}, &block) ⇒ Object


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/helpers/backend/base_helper.rb', line 150

def snippet(name, options = {}, &block)
  collapsed = current_user.preference("interface.snippets.#{name}.collapsed", false, :boolean).value
  collapsed = false if collapsed && options[:title].is_a?(FalseClass)

  options[:class] ||= ''
  options[:icon] ||= name
  options[:class] << " snippet-#{options[:icon]}"
  options[:class] << ' active' if options[:active]

  html = ''
  html << "<div id='#{name}' class='snippet#{' ' + options[:class].to_s if options[:class]}#{' collapsed' if collapsed}'>"

  unless options[:title].is_a?(FalseClass)
    html << "<a href='#{url_for(controller: '/backend/snippets', action: :toggle, id: name)}' class='snippet-title' data-toggle-snippet='true'>"
    html << "<i class='collapser'></i>"
    html << '<h3><i></i>' + (options[:title] || "snippets.#{name}".t(default: ["labels.#{name}".to_sym])) + '</h3>'
    html << '</a>'
  end

  html << "<div class='snippet-content'" + (collapsed ? ' style="display: none"' : '') + '>'
  begin
    html << capture(&block)
  rescue Exception => e
    html << (:small, "#{e.class.name}: #{e.message}")
  end
  html << '</div>'

  html << '</div>'
  content_for(:aside, html.html_safe)
  nil
end

#state_bar(resource, _options = {}) ⇒ Object


257
258
259
260
261
262
# File 'app/helpers/backend/base_helper.rb', line 257

def state_bar(resource, _options = {})
  machine = resource.class.state_machine
  state = resource.state
  state = machine.state(state.to_sym) unless state.is_a?(StateMachine::State) || state.nil?
  render 'state_bar', states: machine.states, current_state: state, resource: resource
end

#variable_readings(resource) ⇒ Object

chart for variables readings


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/helpers/backend/base_helper.rb', line 183

def variable_readings(resource)
  indicators = resource.variable_indicators.delete_if { |i| ![:measure, :decimal].include?(i.datatype) }
  series = []
  now = (Time.zone.now + 7.days)
  window = 1.day
  min = (resource.born_at ? resource.born_at : now - window)
  min = now - window if (now - min) < window
  indicators.each do |indicator| # [:population, :nitrogen_concentration].collect{|i| Nomen::Indicator[i] }
    items = ProductReading.where(indicator_name: indicator.name, product: resource).where('? < read_at AND read_at < ?', min, now).order(:read_at)
    next unless items.any?
    data = []
    data << [min.to_usec, resource.get(indicator, at: min).to_d.to_s.to_f]
    data += items.inject({}) do |hash, pair|
      hash[pair.read_at.to_usec] = pair.value.to_d
      hash
    end.collect { |k, v| [k, v.to_s.to_f] }
    data << [now.to_usec, resource.get(indicator, at: now).to_d.to_s.to_f]
    series << { name: indicator.human_name, data: data, step: 'left' }
  end
  return no_data if series.empty?

  line_highcharts(series, legend: {}, y_axis: { title: { text: :indicator.tl } }, x_axis: { type: 'datetime', title: { enabled: true, text: :months.tl }, min: min.to_usec })
end