Class: AdminController

Inherits:
ApplicationController show all
Defined in:
app/controllers/admin_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
# File 'app/controllers/admin_controller.rb', line 99

def create

	get_table_name

	params[:param2] = params[:hidden_one]
	
  if params[:param2].present?

col = params["#{params[:param2]}"].map { | key, value | eval key.inspect}.join(', ')
   
   val = params["#{params[:param2]}"].map { | key, value | value.inspect }.join(', ') 

records_array = []

	  	sql = "INSERT into #{params[:param2]} (#{col}) VALUES (#{val})"

 	records_array = ActiveRecord::Base.connection.execute(sql)

    	redirect_to :action => "index",:param1 => "#{params[:param2]}"

  else
    	render('new') 
  end  

end

#dashboardObject



72
73
74
75
76
# File 'app/controllers/admin_controller.rb', line 72

def dashboard

	get_table_name

end

#deleteObject



156
157
158
159
# File 'app/controllers/admin_controller.rb', line 156

def delete
  @id = params[:id]
  @name = params[:name]
end

#destroyObject



161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/admin_controller.rb', line 161

def destroy
	get_table_name

 	id = params[:id]
  name = params[:name]
 
  ActiveRecord::Base.connection.execute("DELETE from #{name} where id = '#{id}'")
	
  redirect_to :action => "index",:param1 => "#{name}"
end

#editObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/admin_controller.rb', line 125

def edit

	get_table_name
	get_status
	get_action

  @id = params[:id]
  params[:param2] = params[:name]

  @h = {}
  ActiveRecord::Base.connection.columns(params[:param2]).each { |c|	
		@h.store(c.name, c.type)
	}
end

#exportObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/controllers/admin_controller.rb', line 172

def export
	begin
		get_table_name

table_model_name = params[:param1].present? ? params[:param1].classify : @temp_table[0][:table_name].classify

if table_model_name.blank? 
	render :json => {:status_code => 401, :message => "Model name blank is not allowed" }
	return
end

table_model=nil;

begin
	table_model = table_model_name.constantize
rescue => ex
	render :json => {:status_code => 401, :message => "Model name not found or is incorrect" }
	return
end
  	
model_data = table_model.first

if !model_data
	 render :json => {:status_code => 401, :message => "Model Data not found" }
	return
end
  
model_field={}

model_field[table_model_name] = []

model_field["association"] = {}
  
model_data.attributes.each_pair do |name, value|
	model_field[table_model_name].push(name)
end
 
  ##find all relational model and fields 
  associations = table_model.reflect_on_all_associations(:has_many) 
  
  if associations.count >= 0
	  associations.each do |association|
		association_name = association.name.to_s
		
		model_field["association"][association_name]=[]
		
		table_model = association_name.classify.constantize;
		table_model.columns.each do |col|
		  model_field["association"][association_name].push(col.name)
		end
		
	  end
  end

 @model_field = model_field

 render :action => "export"

	rescue => ex
 render :json => {:status_code => 401, :message => "Model Data have problem" }
 return
	end   
end

#export_sel_fieldsObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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
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
322
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
# File 'app/controllers/admin_controller.rb', line 236

def export_sel_fields

	begin

 model_hash = {
"PromotionCode"		=> { "PaymentTransaction" => "promotion_id"},
"Wishlist"			=> { "RequestShowing" => "attached_wishlist", "Sharing" => "wishlist_id"},
"InvestingWishlist" => { "InvestingWishlistType" => "investing_wishlist_id"},
"InvestmentType" 	=> { "InvestingWishlistType" => "investment_type_id"},
"RequestShowing" 	=> { "Sharing" => "request_id"},
"Customer"  		=> { "Wishlist" => "customer_id", "RequestShowing" => "customer_id", "Rating" => "customer_id"},
"Sharing"			=> { "Connection" => "sharing_id"},
"PropertyType" 		=> { "BuyingWishlist" => "property_type_id", "SellingWishlist" => "property_type_id", "RentingWishlist" => "property_type_id"},
"Agent"				=> { "Acceptance" => "agent_id", "AgentLanguage" => "agent_id", "AgentSpecialty" => "agent_id", "AgentDesignation" => "agent_id", "AgentZipCode" => "agent_id", "License" => "agent_id", "PaymentTransaction" => "agent_id" },
"Language"			=> { "Customer" => "preferred_language_id_1", "Customer" => "preferred_language_id_2", "AgentLanguages" => "language_id" },
"Connection"	    => { "Customer" => "customer_id" },
"User"	 		    => { "Issue" => "user_id", "Suggestion" => "user_id", "CustomerDevice" => "user_id", "CustomerLocationAlert" => "user_id", "AgentChangeInfo" => "user_id"}
 }
			 
	
table_model_name = [] , column_array =[], associated_model_array= []
as_csv1 = CSV.generate do |csv|	
	params[:ids].each do |key,value|
		key_name = key.titleize
		value.each do |i|
			column_name = key_name + "_"+i 
			column_array.push(column_name)
		end				 				
	end	
	csv <<	column_array			
	main_model = params[:model_name] ?  params[:model_name]  : "";
	
	
	params[:ids].each_with_index do |value,index|	
		if index != 0
			associated_model_array.push(value)
		end				
	end	
	
	
	main_model_ids_for_pluck = params[:ids]["#{main_model}"]
	main_model_ids_for_pluck = main_model_ids_for_pluck.collect { |x| "`" + x + "`" } 

	
	
	main_model_ids_for_pluck = main_model_ids_for_pluck.join(',')
	
		main_model_const = main_model.constantize
	
	
	row_array = []
	
	
	main_model_data = main_model_const.all.select(main_model_ids_for_pluck)
	main_model_data.each do |abc|
		s = abc.attributes.values
		s.each do |g|
			row_array.push(g)
		end
		associated_model_array.each do |ass_model_data|
			ass_model_name = ass_model_data[0]
			ass_model_ids = ass_model_data[1]
			ass_model_ids = ass_model_ids.collect { |x| "`" + x + "`" } 
			ass_model_name = ass_model_name.titleize.singularize.split(' ').join('').constantize

			 logger.info ">>>ass_model_ids>>>#{ass_model_ids}"
			
			 logger.info ">>>ass_model_idsnew>>>#{ass_model_ids}"

			main_model_id = "#{main_model.downcase}_id"
			
			
			 logger.info ">>>main_model>>>#{main_model}>>>ass_model_name>>>#{ass_model_name}"
			 logger.info ">>>model_hash>>>#{model_hash["#{main_model}"]["#{ass_model_name}"]}"
			 select_id = model_hash[main_model.to_s][ass_model_name.to_s].to_s
			 logger.info "select_id>>>#{select_id}"
			
			ass_model_table_data = ass_model_name.where("#{select_id} = #{abc.id.to_i}").select(ass_model_ids.join(','))
			if ass_model_table_data.length == 0
				tmp_d = ass_model_ids.map{|a| "" }
				tmp_d.each do |eee1|
					row_array.push(eee1)
				end
				# row_array.push(tmp_d)
			elsif ass_model_table_data.length == 1
				ass_model_table_data.each do |ass|
					ass_data = ass.attributes.values
					ass_data.each do |tmp|
						row_array.push(tmp)
						# logger.info ">>>tmp>>>#{tmp}"
					end
				end
			else
			
				
				arrayList = []
				ass_model_table_data.each do |ass|
					arrayList.push(ass.attributes.values.join(','))
					
				end
				logger.info ">>>arrayList>>>#{arrayList}"
				
				ass_model_table_data_values=[]
				(0...ass_model_table_data[0].attributes.values.length).each{|i|
					tmp1 =[]
					(0...arrayList.length).each{|j|
						tmp1.push(arrayList[j][i])
					}
				  ass_model_table_data_values.push(tmp1)
				}
				logger.info ">>>ass_model_table_data_values>>>#{ass_model_table_data_values}"
				ass_model_table_data_values.each do |eee|
					row_array.push(eee.join(','))
				end
				
					
			end
			
			
		end
		csv << row_array
		row_array = []
	
	
	end
	Rails.logger.info ">>>>>main_model_data>>>>> #{main_model_data}"
		
end	
	
csv_file="expost_data";	
send_data as_csv1, :type => 'text/csv; charset=utf-8; header=present', :disposition => "attachment;data=#{csv_file}.csv"	  

	rescue => ex
logger.info ">>>#{ex}"
render :json => {:status_code => 401, :message => "Export Data have problem" + ex.message }
return
	end

end

#get_actionObject



385
386
387
388
389
390
391
# File 'app/controllers/admin_controller.rb', line 385

def get_action
	@action_array  = [ 
    {:id => "change_profile", :action => "change_profile" },
    {:id => "change_company_name", :action => "change_company_name" }
   ]
	return  @action_array  
end

#get_statusObject



376
377
378
379
380
381
382
383
# File 'app/controllers/admin_controller.rb', line 376

def get_status
	@status_array  = [ 
    {:id => "pending", :status => "pending" },
    {:id => "approved", :status => "approved" },
    {:id => "reject", :status => "reject" }
   ]
	return  @status_array 
end

#get_table_column_nameObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/admin_controller.rb', line 43

def get_table_column_name

	@table = params[:param1].present? ? params[:param1] : @temp_table[0][:table_name]

	@model_name = params[:param1].present? ? params[:param1].classify.constantize : @temp_table[0][:table_name].classify.constantize
 	
 	@column_name = []
 
  ActiveRecord::Base.connection.columns(@table).each { |c|	
		@column_name << c.name
	}
end

#get_table_dataObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/admin_controller.rb', line 56

def get_table_data

	@column_data = []

	table = @table

	current_page = current_page || 1
	per_page = 10
	records_fetch_point = (current_page - 1) * per_page

	@column_data = ActiveRecord::Base.connection.execute("SELECT * FROM #{table}").to_a

	@column_data = Kaminari.paginate_array(@column_data).page(params[:page]).per(10)

end

#get_table_nameObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/admin_controller.rb', line 28

def get_table_name

	@temp_table = []

	@model_names = []

	ActiveRecord::Base.connection.tables.each do |table|      
	  @temp_table <<  {:table_name => table}
	  
	  @model_names << table.classify
	  
  end

end

#indexObject

require ‘json’ require ‘active_support’ require ‘active_support/core_ext’



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/admin_controller.rb', line 9

def index

  get_table_name #method for table_names and model_names 

  get_table_column_name # method for table_name, column_names and model_name 

  get_table_data # method for table data

  @model_data = []
  
  @model_data = @model_name.order(sort_column + " " + sort_direction).page(params[:page]).per(10)

  respond_to do |format|
   format.html
   format.json
	end

end

#newObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/admin_controller.rb', line 86

def new

	get_table_name
	get_status
	get_action

  @h = {}
  ActiveRecord::Base.connection.columns(params[:param2]).each { |c|	
		@h.store(c.name, c.type)
	}

end

#showObject



78
79
80
81
82
83
84
# File 'app/controllers/admin_controller.rb', line 78

def show
	get_table_name
 
  @id = params[:id]
  @name = params[:name]

end

#updateObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/admin_controller.rb', line 140

def update
	
	get_table_name

 	@id = params[:id]
 	params[:param2] = params[:hidden_one]

	params["#{params[:param2]}"].map { | key, value | 

	ActiveRecord::Base.connection.execute("UPDATE #{params[:param2]} SET #{key}='#{value}' WHERE id = '#{params[:id]}'")
	
	}

  redirect_to :action => "index",:param1 => "#{params[:param2]}"
end