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



445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'app/helpers/kirgudu_base/application_helper.rb', line 445

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



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'app/helpers/kirgudu_base/application_helper.rb', line 415

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



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/helpers/kirgudu_base/application_helper.rb', line 299

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



258
259
260
261
262
263
264
265
266
267
268
# File 'app/helpers/kirgudu_base/application_helper.rb', line 258

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


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
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
# File 'app/helpers/kirgudu_base/application_helper.rb', line 323

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


293
294
295
296
297
# File 'app/helpers/kirgudu_base/application_helper.rb', line 293

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
# 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



198
199
200
201
202
203
204
205
# File 'app/helpers/kirgudu_base/application_helper.rb', line 198

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



207
208
209
210
211
212
213
214
# File 'app/helpers/kirgudu_base/application_helper.rb', line 207

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



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

def render_json(data, options={})
	hash = {
		status: ::ChupakabraTools::ApiStatus.id_by_tag('ok'),
		message: "ok"
	}.merge(options || {})

	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



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

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


	render_json(nil, hash)
end

#render_json_error(options = {}) ⇒ Object



136
137
138
139
140
141
142
143
# File 'app/helpers/kirgudu_base/application_helper.rb', line 136

def render_json_error(options={})

	hash = {
		message: "unexpected error"
	}.merge(options || {})
	.megre({status: ::ChupakabraTools::ApiStatus.id_by_tag('error')})
	render_json(nil, hash)
end

#render_json_need_authentication(options = {}) ⇒ Object



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

def render_json_need_authentication(options={})
	hash = {
		message: I18n.t('common.messages.not_authenticated')
	}.merge(options || {})
	.merge({
		       status: ::ChupakabraTools::ApiStatus.id_by_tag('need_authentication')
	       })
	render_json(nil, hash)
end

#render_json_not_found(options = {}) ⇒ Object



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

def render_json_not_found(options={})
	hash = {
		message: I18n.t('core.messages.record_not_found')
	}.merge(options || {})
	.merge({
		       status: ::ChupakabraTools::ApiStatus.id_by_tag('record_not_found')
	       })
	render_json(nil, hash)
end

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



145
146
147
148
149
150
151
152
# File 'app/helpers/kirgudu_base/application_helper.rb', line 145

def render_json_ok(items, options={})

	hash = {
		message: ""
	}.merge(options || {}).merge({status: ::ChupakabraTools::ApiStatus.id_by_tag('ok')})

	render_json(items, hash)
end

#render_json_page_404(options = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'app/helpers/kirgudu_base/application_helper.rb', line 188

def render_json_page_404(options = {})
	hash = {
		message: I18n.t('core.messages.page_404')
	}.merge(options || {})
	.merge({
		       status: ::ChupakabraTools::ApiStatus.id_by_tag('page_404')
	       })
	render_json(nil, hash)
end

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



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'app/helpers/kirgudu_base/application_helper.rb', line 227

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



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

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

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



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'app/helpers/kirgudu_base/application_helper.rb', line 394

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



382
383
384
# File 'app/helpers/kirgudu_base/application_helper.rb', line 382

def 

end

#security_logout(session) ⇒ Object



386
387
388
389
390
# File 'app/helpers/kirgudu_base/application_helper.rb', line 386

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



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

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