Skip to content

move db query from view to controller and method #303

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

Merged
merged 1 commit into from
Jan 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class AdminController < BaseAdminController

def index
@networks = Network.where('protips_count_cache > 0').order('protips_count_cache desc')
end

def teams
Expand Down
6 changes: 5 additions & 1 deletion app/models/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def create_slug!

def tag_with_name!
unless self.tag_list.include? self.name
self.tag_list = (self.tag_list + [self.name, self.slug])
self.tag_list.add(self.slug)
end
end

Expand Down Expand Up @@ -240,4 +240,8 @@ def assign_members
end
end

def recent_protips_count
self.protips.where('protips.created_at > ?', 1.week.ago).count
end

end
4 changes: 2 additions & 2 deletions app/views/admin/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
h4 Pro tips created in networks in past week
section
ul.networks
-Network.where('protips_count_cache > 0').order('protips_count_cache desc').each do |network|
-@networks.each do |network|
li.network
span.name= link_to network.name, network_path(network)
span.created_at= network.protips.where('created_at > ?', 1.week.ago).count
span.created_at= network.recent_protips_count

.widget.orange
header
Expand Down
25 changes: 25 additions & 0 deletions spec/controllers/networks_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
RSpec.describe NetworksController, type: :controller do
let(:current_user) { Fabricate(:user, admin: true) }

before { controller.send :sign_in, current_user }

def valid_attributes
{
name: 'python'
}
end

def valid_session
{}
end

describe 'Create network' do
describe 'with valid attributes' do
it 'creates a network and adds to tags' do
expect do
post :create, { network: valid_attributes}, valid_session
end.to change(Tag, :count).by(1)
end
end
end
end