Skip to content

Commit 3e1f8ad

Browse files
committed
move query from view to model, add network controller spec
1 parent 2829983 commit 3e1f8ad

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

app/controllers/admin_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class AdminController < BaseAdminController
22

33
def index
4+
@networks = Network.where('protips_count_cache > 0').order('protips_count_cache desc')
45
end
56

67
def teams

app/models/network.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def create_slug!
9696

9797
def tag_with_name!
9898
unless self.tag_list.include? self.name
99-
self.tag_list = (self.tag_list + [self.name, self.slug])
99+
self.tag_list.add(self.slug)
100100
end
101101
end
102102

@@ -240,4 +240,8 @@ def assign_members
240240
end
241241
end
242242

243+
def recent_protips_count
244+
self.protips.where('protips.created_at > ?', 1.week.ago).count
245+
end
246+
243247
end

app/views/admin/index.html.slim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@
7171
h4 Pro tips created in networks in past week
7272
section
7373
ul.networks
74-
-Network.where('protips_count_cache > 0').order('protips_count_cache desc').each do |network|
74+
-@networks.each do |network|
7575
li.network
7676
span.name= link_to network.name, network_path(network)
77-
span.created_at= network.protips.where('created_at > ?', 1.week.ago).count
77+
span.created_at= network.recent_protips_count
7878

7979
.widget.orange
8080
header
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
RSpec.describe NetworksController, type: :controller do
2+
let(:current_user) { Fabricate(:user) }
3+
4+
before { controller.send :sign_in, current_user }
5+
6+
def valid_attributes
7+
{
8+
name: 'Rust'
9+
}
10+
end
11+
12+
def valid_session
13+
{}
14+
end
15+
16+
describe 'Create network' do
17+
describe 'with valid attributes' do
18+
it 'creates a network and adds to tags' do
19+
expect do
20+
post :create, { network: valid_attributes}, valid_session
21+
end.to change(Tag, :count).by(1)
22+
end
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)