Class: ModelInfoController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/model_info_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_commentObject



131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/model_info_controller.rb', line 131

def add_comment
	#table_name = params[:table_name]
	#column_name = params[:column_name]
	author = params[:author]
	comment_text = params[:comment_text]
	note_id = params[:note_id]
	@c = Comment.create :author => author, :comment_text => comment_text, :note_id => note_id
	respond_to do |format|
		format.any(:xml, :html, :json) { render :json => @c.to_json}
	end
end

#add_noteObject



120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/model_info_controller.rb', line 120

def add_note
	table_name = params[:table_name]
	column_name = params[:column_name]
	author = params[:author]
	note_text = params[:note_text]
	@n = Note.create :table_name => table_name, :column_name => column_name, :author => author, :note_text => note_text
	respond_to do |format|
		format.any(:xml, :html, :json) { render :json => @n.to_json }
	end
end

#DBNotesObject



21
22
23
24
25
# File 'app/controllers/model_info_controller.rb', line 21

def DBNotes
	Rails.application.eager_load!
	@models = get_models_info
	session[:username] = nil
end

#get_author_nameObject



116
117
118
# File 'app/controllers/model_info_controller.rb', line 116

def get_author_name
	render :json => session[:username].to_json
end

#get_column_notes_countObject



160
161
162
# File 'app/controllers/model_info_controller.rb', line 160

def get_column_notes_count()
	render :json => Note.find_by_sql("select table_name, column_name, count(*) as note_count from Notes group by table_name, column_name").to_json
end

#get_column_notes_count_within_a_table(table_name) ⇒ Object

This will be called when user clicks on a particular table-name in the Accordion control



153
154
155
156
157
# File 'app/controllers/model_info_controller.rb', line 153

def get_column_notes_count_within_a_table (table_name)
	# write code that outputs some json
	#		{"col1": 10, "col2": 5, "col3": 3,......}
	render :json => Note.find_by_sql("select table_name, column_name, count(*) as note_count from Notes where table_name = ? group by table_name, column_name", table_name).to_json
end

#get_models_infoObject



9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/model_info_controller.rb', line 9

def get_models_info
	# The below line requires "cache_classes" to be "on"
	# It seems it is "on" by default in Dev , but not in Prod
	# See the following SO question & answers - http://stackoverflow.com/questions/516579/is-there-a-way-to-get-a-collection-of-all-the-models-in-your-rails-app
	Rails.application.eager_load!
	#Dir.glob(Rails.root.join('app/models/*')).each do |x| 
	#	require x 
	#end
	@models = ActiveRecord::Base.descendants
	return @models
end

#get_table_notes_countObject

This will be called on page-load via Ajax



145
146
147
148
149
# File 'app/controllers/model_info_controller.rb', line 145

def get_table_notes_count
	# write code to that outputs some json 
	# 		{"table1": 21, "table2": 2,.....}
	render :json => Note.find_by_sql("select table_name, count(*) as note_count from Notes group by table_name").to_json
end

#is_user_logged_inObject



110
111
112
113
# File 'app/controllers/model_info_controller.rb', line 110

def is_user_logged_in
	logged_in_status = !session[:username].blank?
	render :json => logged_in_status.to_json
end

#show_notes_for_columnObject

Below actions are called by Ajax calls and return JSON



99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/model_info_controller.rb', line 99

def show_notes_for_column
	@table_name = params[:table]
	@column_name = params[:column]
	@notes = Note.where(:table_name => @table_name, :column_name => @column_name)

	#respond_to do |format|
		#format.any(:xml, :html, :json) { render :json => @notes.to_json(:include => :comments) }
	#end
	render :json => @notes.to_json(:include => :comments)
end

#user_authenticateObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/model_info_controller.rb', line 81

def user_authenticate
	username = params[:username]
	password = params[:password]
	#jira_response = jira(username, password)
	#jira_response = IcentrisJira::get_user_info(username, password) # Used IcentrisJira gem (which has the same code as jira() method above)
	jira_response = IcentrisJira::getJSON("https://jira2.icentris.com/jira/rest/api/2/user?username=" + username, username, password)
	if jira_response.include? "username="
		session[:username] = jira_response.split('"displayName":')[1].split(",")[0]
		render :json => "Authentication Successful".to_json	# send back user's full name if authentication is successful
	else 
		session[:username] = nil
		render :json => "Authentication Failed".to_json # send back "Authentication Failed" if authentication has failed
	end 
end

#user_logged_inObject

This is called in before_filter for add_note() & add_comment()



55
56
57
58
59
60
61
# File 'app/controllers/model_info_controller.rb', line 55

def user_logged_in
	if session[:username].blank? 
		render :json => "not logged in".to_json
	else 
		true
	end
end