Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up Admin::DashboardController #313

Closed
wants to merge 27 commits into from
Closed

Cleaning up Admin::DashboardController #313

wants to merge 27 commits into from

Conversation

CGA1123
Copy link
Contributor

@CGA1123 CGA1123 commented Aug 30, 2016

I noticed that some views within the project include code that would ideally be within controllers.

@CGA1123
Copy link
Contributor Author

CGA1123 commented Aug 30, 2016

the _agent_stats.html.erb partial also has a couple statements executing queries from the view, which is not ideal.

@CGA1123 CGA1123 changed the title Cleaning up code in the view Cleaning up Admin::DashboardController Aug 30, 2016
@CGA1123 CGA1123 changed the title Cleaning up Admin::DashboardController Cleaning up Admin::DashboardController Aug 30, 2016
@scott
Copy link
Member

scott commented Aug 30, 2016

Looks related to #304. Thanks for the cleanup!

@@ -46,12 +31,28 @@ def stats
@responded_topics = Topic.where(id: responded_topic_ids)
@closed_topic_count = @topics.closed.count

@posts = Post.where('created_at >= ? AND created_at <= ?', @start_date, @end_date)
@posts = Post.where(created_at: @start_date..@end_date)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, didn't know you could write it that way

Allowed certain html attributes to be whitelisted by the sanitize
method, so that translations can use some formatting.
Improve readability imo
@CGA1123
Copy link
Contributor Author

CGA1123 commented Aug 31, 2016

For moving to code from _agent_stats.html.erb to the controller, the only idea I have so far is to create a few hashes, one for each variable that is currently declared in the view, using agent.assigned_user_id as the key, and then pull from the hash within the view.

I don't know whether this is the best solution though.

@CGA1123
Copy link
Contributor Author

CGA1123 commented Aug 31, 2016

Also, I'm not sure that the calculation for delays works properly, what happens if the second post on a topic is not from an agent?

delays = @responded_topics.map { |t| t.posts.second.created_at - t.created_at }

@scott
Copy link
Member

scott commented Aug 31, 2016

I think it would be ideal to move code from agent_stats to the model where possible because it would make it cleaner and easier to test, and I just think thats where calculations like responded_topic_count belong.

As for the delays, you may be right. Perhaps adding a scope on Post for agent would help?

@CGA1123
Copy link
Contributor Author

CGA1123 commented Aug 31, 2016

To keep consistent, would some code currently in Admin::DashboardController be better off in the model as well?

For example @reponded_topics, responded_topic_ids, and @agents

@agents_responded_topic_count = @agents.each.map { |agent| [agent.id, @responded_topics.where(assigned_user: agent).count] }.to_h
@agents_closed_topic_count = @agents.each.map { |agent| [agent.id, Topic.undeleted.where(assigned_user: agent).closed.count] }.to_h
@agents_post_count = @agents.each.map { |agent| [agent.id, @posts.where(user: agent, kind: 'reply').count] }.to_h
@agents_delays = @agents.each.map { |agent| [agent.id, median(@responded_topics.where(assigned_user: agent).map { |t| t.posts.second.created_at - t.created_at })] }.to_h
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on moving these assignments into a single iteration.

@CGA1123
Copy link
Contributor Author

CGA1123 commented Aug 31, 2016

Only problem with moving some of this code into the model is the dependence on @topics

@scott
Copy link
Member

scott commented Sep 2, 2016

What do you think about testing this? Could you write some tests?

@CGA1123
Copy link
Contributor Author

CGA1123 commented Sep 3, 2016

I'm more familiar with rspec than minitest but I can give it a shot.

@CGA1123 CGA1123 mentioned this pull request Sep 3, 2016
@scott
Copy link
Member

scott commented Sep 4, 2016

Awesome work! I am really hoping we can add some tests to validate that the stats are showing what they should. Do you have any ideas for this?

@CGA1123
Copy link
Contributor Author

CGA1123 commented Sep 4, 2016

Thanks!

Yeah I suppose it's pretty straightforward, just involves a lot of setting up for the tests.

@CGA1123
Copy link
Contributor Author

CGA1123 commented Sep 7, 2016

Can anyone point me in the right direction for testing whether the controller instance variables are being set correctly in minitest.

Maybe the equivalent for assigns['instance_variable'] in rspec?

@scott
Copy link
Member

scott commented Sep 15, 2016

You can access instance variables using assigns(:name_of_variable) Examples here: http://api.rubyonrails.org/v4.2/classes/ActionController/TestCase.html

Also, this has some conflicts after I merged #319 from @Rynaro which touched some of the same files.

Conflicts:
	app/views/admin/dashboard/_agent_stats.html.erb
	app/views/admin/dashboard/stats.html.erb
@CGA1123
Copy link
Contributor Author

CGA1123 commented Sep 15, 2016

@scott Thanks! I'll have a look and try to get those test written asap.

I fixed the conflicts.

@CGA1123
Copy link
Contributor Author

CGA1123 commented Sep 17, 2016

@scott What do you think about changing date_from_params to use params[:label] to set the start and end dates, rather than having the start_date and end_date params in the url?

It makes it easier to test, and I think it's a bit sleeker.

@CGA1123
Copy link
Contributor Author

CGA1123 commented Sep 25, 2016

@scott I'm not great with fixtures, so writing test for the DB queries made by the controller is pretty difficult for me.

However, I've added tests for other functionality.

Fixes Conflicts:
  Gemfile.lock
  app/controllers/admin/dashboard_controller.rb
  app/views/admin/dashboard/stats.html.erb
@CGA1123 CGA1123 closed this Jan 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants