Module: KirguduBase::ApplicationHelper

Included in:
ApplicationController
Defined in:
app/helpers/kirgudu_base/application_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assign_portal_to_all_models(portal) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'app/helpers/kirgudu_base/application_helper.rb', line 483

def self.assign_portal_to_all_models(portal)

	klasses = ChupakabraTools::ClassHelper.load_all_active_record_models
	models_count = 0
	records_count = 0
	klasses.each do |klass|
		unless klass.is_a?(::KirguduBase::Common::Portal)
			records_count += assign_portal_to_models_of_class(klass, portal)
			models_count += 1
		end
	end

	"Process complete.\r\nProcessed: #{models_count} models\r\nProcessed records: #{records_count}"
end

.assign_portal_to_models_of_class(klass, portal, options = {}) ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'app/helpers/kirgudu_base/application_helper.rb', line 453

def self.assign_portal_to_models_of_class(klass, portal, options = {})
	options ||= {}

	records_count = 0
	if klass && portal && portal.is_a?(::KirguduBase::Common::Portal)
		klass_instance = klass.new
		if klass_instance.respond_to?(:portal_id)
			if options[:force] == true

			elsif options[:force_all] == true
				klass.all.each do |item|
					item.portal_id = portal.id
					if item.save
						records_count += 1
					end
				end
			else
				klass.where { (portal_id == nil) | (portal_id == 0) }.each do |item|
					item.portal_id = portal.id
					if item.save
						records_count += 1
					end
				end
			end
		end
	end
	records_count
end

Instance Method Details

#build_query_string_params(prms = {}, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/helpers/kirgudu_base/application_helper.rb', line 6

def build_query_string_params(prms = {}, opts = {})

	params_new = prms ? Marshal.load(Marshal.dump(prms)) : nil

	if prms && opts
		# Creating Clone of params
		params_new = Marshal.load(Marshal.dump(prms))

		status_id = opts[:status_id]
		if status_id
			params_new[:status_id] = status_id
		end
		sort_by = opts[:sort_by]
		if sort_by
			params_new[:sort_by] = sort_by
		end
		sort_order = opts[:sort_order]
		if sort_order
			params_new[:sort_order] = sort_order
		end
		page = opts[:page]
		if page
			params_new[:page] = page
		end


		# Inverting Some Params
		invert = opts[:invert]
		if invert
			if invert.is_a?(Array)
				invert.each do |value|
					if value == :sort_order || value == 'sort_order'
						if sort_by && sort_by == prms[:sort_by]
							params_new[:sort_order] = invert_sortorder(params_new[:sort_order])
						else
							params_new[:sort_order] = 'asc'
						end
					end
				end
			end
		end

		# Removing Params from Query String
		remove = opts[:remove]
		if remove
			if remove.is_a?(Array)
				remove.each do |value|
					params_new.delete(:"#{value}")
				end
			else
				params_new.delete(:"#{remove}")
			end
		end

		params_new.delete(:controller)
		params_new.delete(:action)
		params_new.delete(:locale)
		params_new.delete(:website_id)
		params_new.delete(:remove)
	end

	params_new
end

#get_flash_message(flash_type) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'app/helpers/kirgudu_base/application_helper.rb', line 337

def get_flash_message(flash_type)
	flash_type ||= ""

	flash_type.downcase!

	message = ""

	if flash_type == "error"
		message = flash[:error]
		flash[:error] = nil
	elsif flash_type == "success"
		message = flash[:success]
		flash[:success] = nil
	elsif flash_type == "success"
		message = flash[:notice]
		flash[:notice] = nil
	elsif flash_type == "warning"
		message = flash[:warning]
		flash[:warning] = nil
	end
	message
end

#kb_auth_form_for(item, options, &proc) ⇒ Object



88
89
90
91
92
93
# File 'app/helpers/kirgudu_base/application_helper.rb', line 88

def kb_auth_form_for(item, options, &proc)
	options ||= {}
	options[:builder] = ::KirguduBase::AuthenticationFormBuilder

	form_for(item, options, &proc)
end

#kb_dynamic_form_for(item, options, &proc) ⇒ Object



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

def kb_dynamic_form_for(item, options, &proc)
	options ||= {}
	options[:builder] = ::KirguduBase::FormBuilder

	html_options = options[:html] || {}
	classes = html_options[:class]
	# if classes
	# 	classes += " form-horizontal row-fluid"
	# else
	# 	classes = "form-horizontal row-fluid"
	# end
	html_options.merge!(class: classes)

	options.merge!(html: html_options)
	form_for(item, options, &proc)
end

#kirgudu_form_for(item, form, url, &proc) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
# File 'app/helpers/kirgudu_base/application_helper.rb', line 296

def kirgudu_form_for(item, form, url, &proc)
	form_options =
		{
			builder: ::KirguduBase::FormBuilder,
			url: url,
			html: form.html_options || {},
			ajaxify: (true if form.is_ajax),
			method: (form.http_method if form.http_method)
		}
	form_for(item, form_options, &proc)
end


361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
412
413
414
415
416
417
418
# File 'app/helpers/kirgudu_base/application_helper.rb', line 361

def print_flash_message

	result = ""

	message_title = ""
	message_class = ""
	message = ""


	#if flash[:notice]
	#	message_class = 'notifications-info'
	#	message_title = flash[:notice]
	#	flash[:notice] = nil
	#elsif flash[:error]
	#	message_class = 'notifications-error'
	#	message_title = flash[:error]
	#	flash[:error] = nil
	#elsif flash[:warning]
	#	message_class = 'notifications-warning'
	#	message_title = flash[:warning]
	#	flash[:warning] = nil
	#elsif flash[:success]
	#	message_class = 'notifications-success'
	#	message_title = flash[:success]
	#	flash[:success] = nil
	#end
	#
	#if flash[:message]
	#	message = content_tag('p') do
	#		flash[:message]
	#	end
	#else
	#	message = ""
	#end
	#
	#
	#unless message_title.nil? || message_title.blank?
	#	result = content_tag('div', class: "#{message_class} notifications-message") do
	#		content_tag('h3') do
	#			message_title.html_safe
	#		end + message
	#	end
	#end


	#
	#<div class="notification-warning notification-message">
	#   <h3>Wait, I must warn you!</h3>
	#<p>This is just a warning notification message.</p>
	#                                       </div>
	#
	#<div class="notification-success notification-message">
	#   <h3>Congrats, you did it!</h3>
	#<p>This is just a success notification message.</p>
	#                                     </div>

	result
end


331
332
333
334
335
# File 'app/helpers/kirgudu_base/application_helper.rb', line 331

def print_flash_notice
	flash_notice = flash[:notice]
	flash[:notice] = nil
	flash_notice.html_safe if flash_notice
end


96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/kirgudu_base/application_helper.rb', line 96

def print_property item, method, options = {}

	if item == false || item.nil?
		"{{#{method.to_s}}}" if method #prints name for MUSTACHE
	else
		if options.nil?
			item.send(method, options)
		else
			item.send(method)
		end
	end
end

#render_html(action, options = {}) ⇒ Object



236
237
238
239
240
241
242
243
# File 'app/helpers/kirgudu_base/application_helper.rb', line 236

def render_html(action, options={})
	options ||= {}
	options[:action] = action
	if params[:layout] == 'false' || params[:layout] == false
		options[:layout] = false
	end
	render options
end

#render_html1(action, options = {}) ⇒ Object



245
246
247
248
249
250
251
252
# File 'app/helpers/kirgudu_base/application_helper.rb', line 245

def render_html1(action, options={})
	options ||= {}

	if params[:layout] == 'false' || params[:layout] == false
		options[:layout] = false
	end
	render action, options
end

#render_json(data, options = {}) ⇒ Object



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
# File 'app/helpers/kirgudu_base/application_helper.rb', line 110

def render_json(data, options={})

	hash = {}.merge(options)
	hash[:status] = ::ChupakabraTools::JsonStatus.id_by_tag('ok') unless hash[:status]
	hash[:message] = "" unless hash[:message]

	if data
		if data.is_a?(Array)
			hash[:data] = data
		elsif data.is_a?(::ActiveRecord::Relation)
			collected_data = []
			data.collect { |itm| collected_data.push(itm) }
			hash[:data] = collected_data
		else
			hash[:data] = data
		end
	end

	#logger.info "JSON RESULT: #{hash.to_json}"

	if params[:callback]
		render json: hash, callback: params[:callback]
	else
		render json: hash
	end
end

#render_json_access_denied(options = {}) ⇒ Object



204
205
206
207
208
209
210
211
212
# File 'app/helpers/kirgudu_base/application_helper.rb', line 204

def render_json_access_denied(options={})
	hash = {}.merge(options)
	hash.merge!({
		            status: ::ChupakabraTools::JsonStatus.id_by_tag('access_denied')
	            })
	hash[:message] ||= I18n.t('core.messages.access_denied')

	render_json(nil, hash)
end

#render_json_error(options = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'app/helpers/kirgudu_base/application_helper.rb', line 155

def render_json_error(options={})

	hash = {}.merge(options)
	hash.merge!({
		            status: ::ChupakabraTools::JsonStatus.id_by_tag('error')
	            })
	hash[:message] ||= 'unexpected error'

	render_json(nil, hash)
end

#render_json_item(item, options = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/helpers/kirgudu_base/application_helper.rb', line 178

def render_json_item(item, options={})
	hash = {}.merge(options)

	hash[:edit_url] ||= item.edit_url if item.respond_to? :edit_url
	hash[:show_url] ||= item.total_pages if item.respond_to? :show_url

	hash.merge!({
		            status: ::ChupakabraTools::JsonStatus.id_by_tag('ok')
	            })

	hash[:message] ||= ''

	render_json(items, hash)
end

#render_json_nav(items, options = {}) ⇒ Object



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

def render_json_nav(items, options={})
	hash = {}.merge(options)

	hash.merge!({
		            status: ::ChupakabraTools::JsonStatus.id_by_tag('ok')
		            #total_pages: (items.total_pages if items.respond_to? :total_pages)
	            })


	hash[:page] ||= items.current_page if items.respond_to? :current_page
	hash[:nav_url] ||= items.klass.nav_url(scope) if items.respond_to?(:klass) && items.klass.respond_to?(:nav_url_template)

	hash[:message] ||= ''

	render_json(items, hash)
end

#render_json_noauth(options = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
# File 'app/helpers/kirgudu_base/application_helper.rb', line 194

def render_json_noauth(options={})
	hash = {}.merge(options)
	hash.merge!({
		            status: ::ChupakabraTools::JsonStatus.id_by_tag('need_authentication')
	            })
	hash[:message] ||= I18n.t('common.messages.not_authenticated')

	render_json(nil, hash)
end

#render_json_not_found(options = {}) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'app/helpers/kirgudu_base/application_helper.rb', line 215

def render_json_not_found(options={})
	hash = {}.merge(options)
	hash.merge!({
		            status: ::ChupakabraTools::JsonStatus.id_by_tag('not_found')
	            })
	hash[:message] ||= I18n.t('core.messages.record_not_found')

	render_json(nil, hash)
end

#render_json_ok(items, options = {}) ⇒ Object



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

def render_json_ok(items, options={})

	hash = {}.merge(options)
	hash.merge!({
		            status: ::ChupakabraTools::JsonStatus.id_by_tag('ok')
	            })
	hash[:message] ||= ''

	render_json(items, hash)
end

#render_json_page_404(options = {}) ⇒ Object



226
227
228
229
230
231
232
233
234
# File 'app/helpers/kirgudu_base/application_helper.rb', line 226

def render_json_page_404(options = {})
	hash = {}.merge(options)
	hash.merge!({
		            status: ::ChupakabraTools::JsonStatus.id_by_tag('page_404')
	            })
	hash[:message] ||= I18n.t('core.messages.page_404')

	render_json(nil, hash)
end

#render_liquid(name, options = {}) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'app/helpers/kirgudu_base/application_helper.rb', line 265

def render_liquid(name, options={})
	# source: ts, db, cache
	# interface: cabinet, web, admin
	# layout:


	options ||= {}
	options.stringify_keys!

	#if options["source"] = "file_system"
	#
	#elsif options["source"] = "db"
	#
	#elsif options["source"] = "cache"
	#
	#end
	#
	#if options["layout"]
	#  # rendering layout
	#end

	cur_controller = controller_path
	cur_action = action_name


	@liquid_template = ::Liquid::Template.parse(File.open("#{Rails.root}/app/views/#{cur_controller}/#{name}.html.liquid"))
	@liquid_template.render(options)

end

#render_redirect(url, options = {}) ⇒ Object



255
256
257
258
259
260
261
262
# File 'app/helpers/kirgudu_base/application_helper.rb', line 255

def render_redirect(url, options={})
	if params[:layout] == 'false' || params[:layout] == false
		cookies[:redirect_url] = {:value => url, :expires => 1.minute.from_now}
		render text: "", status: :no_content
	else
		redirect_to url, options
	end
end

#russian_month_labels(month_name) ⇒ Object

склонение месяцев



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'app/helpers/kirgudu_base/application_helper.rb', line 432

def russian_month_labels(month_name)

	last_to_symbols = month_name[-2, 2]

	if last_to_symbols == "рь"
		month_name = month_name[0..-3] + "ря"
	elsif last_to_symbols == "ль"
		month_name = month_name[0..-3] + "ля"
	elsif last_to_symbols =="нь"
		month_name = month_name[0..-3] + "ля"
	elsif last_to_symbols == "рт"
		month_name = month_name[0..-3] + "рта"
	elsif last_to_symbols == "ай"
		month_name = month_name[0..-3] + "ая"
	elsif last_to_symbols == "ст"
		month_name = month_name[0..-3] + "ста"
	end
	return month_name
end

#security_loginObject



420
421
422
# File 'app/helpers/kirgudu_base/application_helper.rb', line 420

def 

end

#security_logout(session) ⇒ Object



424
425
426
427
428
# File 'app/helpers/kirgudu_base/application_helper.rb', line 424

def security_logout(session)
	session[:user_id] = nil
	session[:user_expire] = 1.day.ago
	session[:remember_me] = false
end

#web_chu_form_for(item, options, &proc) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'app/helpers/kirgudu_base/application_helper.rb', line 309

def web_chu_form_for(item, options, &proc)
	options ||= {}
	options[:builder] = WebShops::ChuFormBuilder

	html_options = options[:html] || {}
	classes = html_options[:class]
	#if classes
	#	classes += " form-horizontal row-fluid"
	#else
	#	classes = "form-horizontal row-fluid"
	#end
	html_options.merge!(class: classes)

	ajaxify = html_options[:ajaxify]

	unless ajaxify
		html_options.merge!(ajaxify: true)
	end
	options.merge!(html: html_options)
	form_for(item, options, &proc)
end