Class: KirguduBase::ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Extended by:
Controllers::DynamicPages, Controllers::DynamicTransactions, Controllers::ObligatoryFilters
Includes:
ApplicationHelper, BreadcrumbsHelper, UrlHelper
Defined in:
app/controllers/kirgudu_base/application_controller.rb

Instance Attribute Summary

Attributes included from Controllers::DynamicPages

#kirgudu_controller_options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Controllers::DynamicPages

get_form_properties_template_set, get_kb_entry_class, get_kb_home_controller, get_kb_menu, get_kb_option, get_kb_page, get_kb_parents, get_kb_template, get_kb_view_path, kb_entry_class, kb_form_properties_template_set, kb_home_controller, kb_management_settings, kb_menu, kb_page, kb_parent_controller, kb_template, kb_views_paths_404_page, kb_views_paths_delete, kb_views_paths_edit, kb_views_paths_entry_not_found, kb_views_paths_index, kb_views_paths_management, kb_views_paths_new, kb_views_paths_show, kb_views_paths_sorting

Methods included from Controllers::DynamicTransactions

get_kb_transaction_injections, kb_transaction_injection

Methods included from Controllers::ObligatoryFilters

get_kb_obligatory_filters, kb_obligatory_filter

Methods included from ApplicationHelper

assign_portal_to_all_models, assign_portal_to_models_of_class, #build_query_string_params, #get_flash_message, #kb_auth_form_for, #kb_dynamic_form_for, #kirgudu_form_for, #print_flash_message, #print_flash_notice, #print_property, #render_html, #render_html1, #render_json, #render_json_access_denied, #render_json_error, #render_json_item, #render_json_nav, #render_json_noauth, #render_json_not_found, #render_json_ok, #render_json_page_404, #render_liquid, #render_redirect, #russian_month_labels, #security_login, #security_logout, #web_chu_form_for

Methods included from BreadcrumbsHelper

included

Methods included from UrlHelper

#url_for, #with_subdomain

Class Method Details

.get_class_hierarchyObject



256
257
258
# File 'app/controllers/kirgudu_base/application_controller.rb', line 256

def self.get_class_hierarchy
  ::ChupakabraTools::ClassHelper.get_controller_hierarchy(self)
end

.get_method_latest_result(method_name, *method_args) ⇒ Object



264
265
266
267
268
269
270
# File 'app/controllers/kirgudu_base/application_controller.rb', line 264

def self.get_method_latest_result(method_name, *method_args)
  method_result = nil
  self.get_class_hierarchy.each do |controller_class|
    method_result = controller_class.send(method_name, *method_args) if controller_class.respond_to?(method_name)
    return method_result if method_result
  end
end

.to_i18nObject

I18n String Generator Methods #####################################



137
138
139
# File 'app/controllers/kirgudu_base/application_controller.rb', line 137

def self.to_i18n
  ::ChupakabraTools::ClassHelper.controller_class_to_i18n(self)
end

.to_route_pathObject

Routes Generator Methods ########################################



149
150
151
# File 'app/controllers/kirgudu_base/application_controller.rb', line 149

def self.to_route_path
  ::ChupakabraTools::ClassHelper.controller_full_path_underscore(self)
end

.to_url_for(action, options = {}) ⇒ Object

include ActionView::Helpers include ActionDispatch::Routing include Rails.application.routes.url_helpers



165
166
167
168
169
170
171
172
173
# File 'app/controllers/kirgudu_base/application_controller.rb', line 165

def self.to_url_for(action, options = {})
  options ||= {}
  options.merge!(
    only_path: true,
    controller: self.to_route_path,
    action: action
  )
  Rails.application.routes.url_helpers.url_for(options)
end

Instance Method Details

#add_new_breadcrumb(label, path = nil) ⇒ Object



115
116
117
# File 'app/controllers/kirgudu_base/application_controller.rb', line 115

def add_new_breadcrumb(label, path = nil)
  self.breadcrumbs << {label: label, url: path}
end

#authenticate_userObject



62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/kirgudu_base/application_controller.rb', line 62

def authenticate_user
  unless @current_user
    session[:user_id] = nil
    session[:back_url] = request.url
    #params[:back_url] = request.path
    respond_to do |format|
      #format.js { render partial: "sessions/login", layout: false}
      format.html { redirect_to ::KirguduBase.authentications_controller.to_url_for(:login) }
    end
  end
end


124
125
126
# File 'app/controllers/kirgudu_base/application_controller.rb', line 124

def breadcrumbs
  @breadcrumbs ||= []
end


128
129
130
# File 'app/controllers/kirgudu_base/application_controller.rb', line 128

def breadcrumbs_length
  self.breadcrumbs.length
end

#check_locale_parameterObject

def default_url_options I18n.locale end



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'app/controllers/kirgudu_base/application_controller.rb', line 524

def check_locale_parameter
  if session[:locale]
    #if params[:locale]
    if I18n.available_locales.include?(params[:locale].to_sym)
      if params[:locale] != session[:locale]
        session[:locale] = params[:locale]
        I18n.locale = params[:locale]
      else
        I18n.locale = session[:locale]
      end
    else
      I18n.locale = session[:locale]
      #redirect_to home_path, notice: :wrong_locale #"Translation \"#{params[:locale]}\" not available yet."
    end
  else
    I18n.locale = session[:locale]
    #redirect_to request.url, locale: I18n.locale
  end
  #else
  #  session[:locale] = I18n.locale
  #end
end

#class_strong_filters(list) ⇒ Object

Strong Parameters Filters Methods ########################################



300
301
302
303
304
305
306
307
308
309
310
# File 'app/controllers/kirgudu_base/application_controller.rb', line 300

def class_strong_filters(list)

  strong_filters = {}

  list.filters.each do |filter|
    #raise "Found Filter for list: #{filter}"
    strong_filters[filter.name] = params[filter.name] if params[filter.name]
  end if list.filters

  strong_filters
end

#class_strong_params(form) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/controllers/kirgudu_base/application_controller.rb', line 191

def class_strong_params(form)
  prms = []
  if form.properties
    form.properties.each_pair do |key, property|
      unless property.read_only == :true
        prms << property.name
      end
    end
  elsif form.elements
    form.elements.each do |element|
      if element.is_a?(::KiruguduBase::DynamicPages::FormProperty)
        unless element.read_only == :true
          prms << element.name
        end
      end
    end
  end

  #raise "#{self.entry_class.for_form_params}"
  #raise "#{prms.to_s}"
  params.require(self.entry_class.for_form_params).permit(prms)
end

#create_breadcrumbsObject



105
106
107
108
109
110
111
112
113
# File 'app/controllers/kirgudu_base/application_controller.rb', line 105

def create_breadcrumbs
  #if self.class.respond_to?(:dashboard_controller_class_name)
  #  if self.class.to_s == self.class.dashboard_controller_class_name.to_s
  #    add_new_breadcrumb(I18n.t("#{self.class.dashboard_controller_class_name.to_i18n}.index.title"))
  #  else
  #    add_new_breadcrumb(I18n.t("#{self.class.dashboard_controller_class_name.to_i18n}.index.title"), self.class.dashboard_controller_class_name.to_route_path)
  #  end
  #end
end

#entry_classObject



34
35
36
# File 'app/controllers/kirgudu_base/application_controller.rb', line 34

def entry_class
  self.class.get_kb_entry_class
end

#fill_obligatory_filters(filters, options = {}) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'app/controllers/kirgudu_base/application_controller.rb', line 317

def fill_obligatory_filters(filters, options = {})
  options ||= {}

  classes_to_process = self.get_class_hierarchy

  classes_to_process.each do |kontroller_klass|
    kontroller_klass.get_kb_obligatory_filters.each do |filter|
      execute_flag = true

      if filter.only && filter.only.length > 0
        execute_flag = false
        filter.only.each do |item|
          if item == self.action.to_sym
            execute_flag = true
          end
        end
      end
      if filter.skip && filter.skip.length > 0
        filter.skip.each do |item|
          if item == self.action.to_sym
            execute_flag = false
          end
        end
      end
      filters[filter.name] = self.send(filter.source_method) if execute_flag
    end if kontroller_klass.respond_to?(:get_kb_obligatory_filters)
  end
  #raise "Obligatory Filters: #{filters.to_json}"
end

#fill_parents_for_local_data(local_data, filters) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'app/controllers/kirgudu_base/application_controller.rb', line 443

def fill_parents_for_local_data(local_data, filters)
  local_data[:parents] = []
  local_data[:parent_elements] = {}
  local_data[:parent_ids] = {}

  if local_data && local_data.is_a?(Hash)
    controllers_hierarchy = ::ChupakabraTools::ClassHelper.get_controller_hierarchy(self.class)

    controllers_hierarchy.each do |ctrler|
      if ctrler.respond_to?(:get_kb_parents)
        parent_controllers = ctrler.get_kb_parents
        parent_controllers.each do |pk|
          parent_entry = pk[:controller].get_kb_entry_class.get_entry_by_id(params[pk[:id]], filters, {logger: string_logger})
          parent_hash = {
            controller: pk[:controller],
            entry: parent_entry,
            id_param: pk[:id],
            id_value: parent_entry.id
          }
          local_data[:parents] << parent_hash
        end if parent_controllers

        parent_controllers.each do |pk|
          local_data[:parent_elements][pk[:id]] = pk[:controller].get_kb_entry_class.get_entry_by_id(params[pk[:id]], filters, {logger: string_logger})
        end if parent_controllers

      end
    end
    #raise "Parent Elements: #{local_data[:parent_elements]}"
    local_data[:parent_elements].each_pair do |key, element|
      local_data[:parent_ids][key] = element.id
    end
  end
end

#get_class_hierarchyObject

Class Hierarchy Methods ########################################



252
253
254
# File 'app/controllers/kirgudu_base/application_controller.rb', line 252

def get_class_hierarchy
  self.class.get_class_hierarchy
end

#get_class_variable(name) ⇒ Object



260
261
262
# File 'app/controllers/kirgudu_base/application_controller.rb', line 260

def get_class_variable(name)
  self.class.get_class_variable(name)
end

#get_filters_for_list(list, source) ⇒ Object

Filters #########################################



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/controllers/kirgudu_base/application_controller.rb', line 219

def get_filters_for_list(list, source)
  result = {}

  list.filters.each do |filter|
    source_value = source[filter.name]
    if [:autocomplete, :autocomplete_with_id, :text_edit, :select, :chosen_select].include?(filter.input_type)
      if source_value
        source_value.strip!
        source_value = nil if source_value.blank?
      end
    end
    result[filter.name] = source_value if source_value
  end if list.filters

  begin
    source_value = source[:page]
    result[:page] = source_value.to_i if source_value
  rescue
  end

  begin
    source_value = source[:per_page]
    result[:per_page] = source_value.to_i if source_value
  rescue
  end
  result
end

#global_values_registerObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/kirgudu_base/application_controller.rb', line 38

def global_values_register

  @current_user = get_current_user

  @settings_manager = ::KirguduBase::Settings::SettingsManager.new

  default_host_to_set = request.host
  default_host_to_set += ":#{request.port}" if  request.port && request.port != 80

  Rails.application.routes.default_url_options[:host] = default_host_to_set


  I18n.locale = :ru
  #@string_logger.info("\tHOST: #{request.host}") if @string_logger
  #@string_logger.info("\tLOCALE: #{I18n.locale}") if @string_logger
  #@current_user = get_current_user
  #@string_logger.info("\tUSER#{@current_user}") if @string_logger

  WillPaginate.per_page = 20
  #will_paginate_count = @settings_manager.get('will_paginate')
  #WillPaginate.per_page = will_paginate_count if will_paginate_count
end

#load_data_for_entry_controls(form, entry, options = {}) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'app/controllers/kirgudu_base/application_controller.rb', line 352

def load_data_for_entry_controls(form, entry, options = {})
  controls_data = {}


  form.properties.values.select { |p| !p.data_source.nil? }.each do |property|
    if property.data_source
      controls_data[property.name] = if property.data_source.type == :db || property.data_source.type == :jquery_db
                                       filters = {}
                                       parent_value = nil
                                       if property.parent
                                         parent_value = entry[property.parent]
                                         filters[property.parent] = parent_value
                                       end

                                       self.fill_obligatory_filters(filters, options)
                                       filters.merge!({
                                                        per_page: 9999999,
                                                        page: 1
                                                      })
                                       property.data_source.model.get_entries(nil, filters, options).map { |u| [u.send(property.data_source.label_property), u.send(property.data_source.value_property)] } if property.parent.nil? || parent_value
                                   elsif property.data_source.type == :i18n

                                   elsif property.data_source.type == :enum
                                     property.data_source.model.to_select
                                   elsif property.data_source.type == :array
                                     property.data_source.model.map { |i| i.is_a?(Array) ? [i[0], i[0]] : [i, i] }
                                   end
    end
  end if form.properties
  controls_data
end

#load_data_for_filters_controls(list, values_source, options = {}) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'app/controllers/kirgudu_base/application_controller.rb', line 384

def load_data_for_filters_controls(list, values_source, options = {})
  controls_data = {}

  list.filters.select { |f| !f.data_source.nil? }.each do |filter|

    controls_data[filter.name] = if filter.data_source.type == :db || filter.data_source.type == :jquery_db
                                   filters = {}
                                   if filter.parent
                                     parent_value = values_source[filter.parent]
                                     filters[filter.parent] = values_source[filter.parent] if parent_value
                                   end
                                   fill_obligatory_filters(filters, options)
                                   filters.merge!({
                                                    per_page: 9999999,
                                                    page: 1
                                                  })
                                   filter.data_source.model.get_entries(nil, filters, options).map { |u| [u.send(filter.data_source.label_property), u.send(filter.data_source.value_property)] }
                               elsif filter.data_source.type == :i18n

                               elsif filter.data_source.type == :enum
                                 filter.data_source.model.to_select
                               elsif filter.data_source.type == :array
                                 filter.data_source.model
                               end

  end if list.filters
  controls_data
end

#page_for_default_redirection_after_loginObject



75
76
77
# File 'app/controllers/kirgudu_base/application_controller.rb', line 75

def 

end

#prepare_applications_dataObject



548
549
550
# File 'app/controllers/kirgudu_base/application_controller.rb', line 548

def prepare_applications_data

end

#prepare_breadcrumbsObject



120
121
122
# File 'app/controllers/kirgudu_base/application_controller.rb', line 120

def prepare_breadcrumbs
  #string_logger.info("BREADCRUMBS: #{breadcrumbs}")
end

#process_transaction_injections(entry, action, flow_position, filter_params, options = {}) ⇒ Object

Dynamic Transactions #########################################



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/controllers/kirgudu_base/application_controller.rb', line 276

def process_transaction_injections(entry, action, flow_position, filter_params, options = {})
  options ||= {}

  transaction_is_ok = true

  classes_to_process = self.get_class_hierarchy
  classes_to_process.each do |controller_klass|
    if controller_klass.respond_to?(:get_kb_transaction_injections)
      available_injections = controller_klass.get_kb_transaction_injections(action, flow_position)
      available_injections.each do |injection|
        #raise "Processing Transaction Injection"
        if self.send(injection.method, entry, filter_params, options) == false
          transaction_is_ok &= false
        end
      end if available_injections
    end
  end
  transaction_is_ok
end

#render(*args) ⇒ Object



96
97
98
99
100
101
102
# File 'app/controllers/kirgudu_base/application_controller.rb', line 96

def render *args
  self.create_breadcrumbs

  #raise "BreadCrumbs: #{self.create_breadcrumbs}"

  super
end

#set_content_typeObject



80
81
82
83
84
85
86
87
88
# File 'app/controllers/kirgudu_base/application_controller.rb', line 80

def set_content_type
  content_type = case params[:format]
                   when "json"
                     "application/json"
                   when "xml"
                     "text/xml"
                 end
  headers["Content-Type"] = content_type
end

#set_i18n_locale_from_paramsObject



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'app/controllers/kirgudu_base/application_controller.rb', line 483

def set_i18n_locale_from_params
  if params[:locale]
    if I18n.available_locales.include?(params[:locale].to_sym)
      I18n.locale = params[:locale]
      session[:locale] = params[:locale]
      cookies.permanent[:locale_set] = params[:locale]
    else
      if session[:locale]
        #params[:locale] = session[:locale]
        #I18n.locale = session[:locale]
        #locale_redirect(session[:locale])
        #redirect_to request.url  #, notice: "Translation \"#{params[:locale]}\" not available."
        #@string_logger.error flash.now[:notice]
        redirect_to url_for(locale: session[:locale])
      elsif cookies[:locale_set]
        session[:locale] = cookies[:locale_set]
        params[:locale] = cookies[:locale_set]
        I18n.locale = cookies[:locale_set]
        #locale_redirect(session[:locale])
      else
        locale = get_best_locale
        params[:locale] = locale
        session[:locale] = locale
        cookies.permanent[:locale_set] = locale
        I18n.locale = locale
        #locale_redirect(I18n.locale)
      end
    end
  elsif session[:locale]
    redirect_to url_for(locale: session[:locale])
  elsif cookies[:locale_set]
    redirect_to url_for(locale: cookies[:locale_set])
  else
    redirect_to url_for(locale: get_best_locale)
  end
end

#string_loggerObject



552
553
554
# File 'app/controllers/kirgudu_base/application_controller.rb', line 552

def string_logger
  @string_logger
end

#to_i18nObject



141
142
143
# File 'app/controllers/kirgudu_base/application_controller.rb', line 141

def to_i18n
  self.class.to_i18n
end

#to_route_pathObject



153
154
155
# File 'app/controllers/kirgudu_base/application_controller.rb', line 153

def to_route_path
  self.class.to_route_path
end

#to_url_for(action = nil, options = {}) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'app/controllers/kirgudu_base/application_controller.rb', line 175

def to_url_for(action = nil, options = {})
  self.class.to_url_for(action, options)
  #options ||= {}
  #options.merge!(
  #  controller: self.to_route_path,
  #  action: action || :index
  #)
  #Rails.application.routes.url_helpers.url_for(options)
end