Commit e518917e097e48dd029f6c4c0cdd21cdbf355ad4

Authored by Diego de Araújo Martinez Camarinha
2 parents 973275c7 657dff5c
Exists in colab and in 2 other branches master, stable

Merge pull request #345 from mezuro/fix_home_cache

Fix home page latest content cache efectiveness
CHANGELOG.rdoc
... ... @@ -16,6 +16,7 @@ Prezento is the web interface for Mezuro.
16 16 * Fix 'Tree Metrics' and 'Hotspot Metrics' PT translations in Configuration show view
17 17 * Show the notify push url for the repository's owner (Gitlab only)
18 18 * Support for hiding repositories
  19 +* Fix home latest content caching effectiveness
19 20  
20 21 == v0.11.3 - 01/04/2016
21 22  
... ...
app/controllers/home_controller.rb
1 1 class HomeController < ApplicationController
2   - def index
3   - @latest_projects = Project.latest(5)
4   - @latest_repositories = Repository.latest(5)
5   - @latest_configurations = KalibroConfiguration.latest(5)
  2 + helper_method :latest_projects, :latest_repositories, :latest_configurations
  3 +
  4 + def latest_projects(count)
  5 + Project.latest(count)
  6 + end
  7 +
  8 + def latest_repositories(count)
  9 + Repository.latest(count)
  10 + end
  11 +
  12 + def latest_configurations(count)
  13 + KalibroConfiguration.latest(count)
6 14 end
7 15 end
... ...
app/models/kalibro_configuration.rb
... ... @@ -19,6 +19,13 @@ class KalibroConfiguration &lt; KalibroClient::Entities::Configurations::KalibroCon
19 19 self.public_or_owned_by_user
20 20 end
21 21  
  22 + def self.latest(count = 1)
  23 + all.sort { |one, another| another.id <=> one.id }.select { |kalibro_configuration|
  24 + attributes = kalibro_configuration.attributes
  25 + attributes && attributes.public
  26 + }.first(count)
  27 + end
  28 +
22 29 def attributes
23 30 @attributes ||= KalibroConfigurationAttributes.find_by(kalibro_configuration_id: self.id)
24 31 end
... ... @@ -28,8 +35,4 @@ class KalibroConfiguration &lt; KalibroClient::Entities::Configurations::KalibroCon
28 35 @attributes = nil
29 36 super
30 37 end
31   -
32   - def self.latest(count=1)
33   - all.sort { |one, another| another.id <=> one.id }.select { |kalibro_configuration| kalibro_configuration.attributes.public }.first(count)
34   - end
35 38 end
... ...
app/models/project.rb
... ... @@ -17,7 +17,10 @@ class Project &lt; KalibroClient::Entities::Processor::Project
17 17 end
18 18  
19 19 def self.latest(count = 1)
20   - all.sort { |a, b| b.id <=> a.id }.select { |project| project.attributes.public }.first(count)
  20 + all.sort { |one, another| another.id <=> one.id }.select { |project|
  21 + attributes = project.attributes
  22 + attributes && attributes.public
  23 + }.first(count)
21 24 end
22 25  
23 26 def attributes
... ...
app/views/home/index.html.erb
... ... @@ -13,7 +13,7 @@
13 13  
14 14 <ul>
15 15 <% cache action_suffix: 'latest_projects' do %>
16   - <% @latest_projects.each do |project| %>
  16 + <% latest_projects(5).each do |project| %>
17 17 <li><%= link_to(project.name, project_path(project.id)) %></li>
18 18 <% end %>
19 19 <% end %>
... ... @@ -24,7 +24,7 @@
24 24  
25 25 <ul>
26 26 <% cache action_suffix: 'latest_repositories' do %>
27   - <% @latest_repositories.each do |repository| %>
  27 + <% latest_repositories(5).each do |repository| %>
28 28 <li><%= link_to(repository.name, repository_path(repository.id)) %></li>
29 29 <% end %>
30 30 <% end %>
... ... @@ -35,7 +35,7 @@
35 35  
36 36 <ul>
37 37 <% cache action_suffix: 'latest_configurations' do %>
38   - <% @latest_configurations.each do |configuration| %>
  38 + <% latest_configurations(5).each do |configuration| %>
39 39 <li><%= link_to(configuration.name, kalibro_configuration_path(configuration.id)) %></li>
40 40 <% end %>
41 41 <% end %>
... ...
features/homepage.feature
... ... @@ -3,9 +3,11 @@ Feature: Homepage
3 3 As a regular user
4 4 I want to have in one page useful links to manage my account and session
5 5  
6   - @kalibro_processor_restart
  6 + @kalibro_configurations_restart @kalibro_processor_restart
7 7 Scenario: Before signing in
8 8 Given I have a project named "GCC"
  9 + And there is a public configuration created named "Test Configuration"
  10 + And I have a public repository named "Test Repository"
9 11 Then I am at the homepage
10 12 And I should see "Home"
11 13 And I should see "Projects"
... ... @@ -19,6 +21,8 @@ Feature: Homepage
19 21 And I should see "Latest repositories"
20 22 And I should see "Latest configurations"
21 23 Then I should see "GCC" only "1" times
  24 + Then I should see "Test Configuration" only "1" times
  25 + Then I should see "Test Repository" only "1" times
22 26  
23 27 Scenario: Signed in
24 28 Given I am a regular user
... ...
features/step_definitions/kalibro_configuration_steps.rb
... ... @@ -87,6 +87,12 @@ Given(/^there is a public configuration created$/) do
87 87 FactoryGirl.create(:kalibro_configuration_attributes, kalibro_configuration_id: @public_kc.id)
88 88 end
89 89  
  90 +Given(/^there is a public configuration created named "(.*?)"$/) do |name|
  91 + @kalibro_configuration = FactoryGirl.create(:public_kalibro_configuration, name: name)
  92 + FactoryGirl.create(:kalibro_configuration_attributes, kalibro_configuration_id: @kalibro_configuration.id)
  93 +end
  94 +
  95 +
90 96 Given(/^there is a private configuration created$/) do
91 97 @private_kc = FactoryGirl.create(:another_kalibro_configuration)
92 98 FactoryGirl.create(:kalibro_configuration_attributes, :private, kalibro_configuration_id: @private_kc.id, user: FactoryGirl.create(:another_user, id: nil, email: "private@email.com"))
... ...
features/step_definitions/repository_steps.rb
... ... @@ -135,6 +135,15 @@ Given(/^I have a sample configuration with the (\w+) native metric$/) do |metric
135 135 kalibro_configuration_id: @kalibro_configuration.id})
136 136 end
137 137  
  138 +Given(/^I have a public repository named "(.*?)"$/) do |name|
  139 + @repository = FactoryGirl.create(:repository,
  140 + project_id: nil,
  141 + kalibro_configuration_id: @kalibro_configuration.id,
  142 + id: nil,
  143 + name: name)
  144 + FactoryGirl.create(:repository_attributes, {repository_id: @repository.id})
  145 +end
  146 +
138 147 When(/^I click on the sample metric's name$/) do
139 148 find_link(@metric_results.first.metric_configuration.metric.name).trigger('click')
140 149 end
... ...
spec/controllers/home_controller_spec.rb
1 1 require 'rails_helper'
2 2  
3 3 describe HomeController, :type => :controller do
4   - context 'Method' do
5   - context '#index' do
6   - before :each do
7   - Project.expects(:latest).with(5).returns([])
8   - Repository.expects(:latest).with(5).returns([])
9   - KalibroConfiguration.expects(:latest).with(5).returns([])
10   - end
11   -
  4 + context 'actions' do
  5 + context 'index' do
12 6 describe 'Rendering' do
13 7 before :each do
14 8 get :index
... ... @@ -41,4 +35,33 @@ describe HomeController, :type =&gt; :controller do
41 35 end
42 36 end
43 37 end
  38 +
  39 + context 'helpers' do
  40 + describe 'latest_repositories' do
  41 + let(:repositories) { mock }
  42 +
  43 + it 'should fetch the latest content' do
  44 + Repository.expects(:latest).with(5).returns(repositories)
  45 + expect(subject.latest_repositories(5)).to be(repositories)
  46 + end
  47 + end
  48 +
  49 + describe 'latest_projects' do
  50 + let(:projects) { mock }
  51 +
  52 + it 'should fetch the latest content' do
  53 + Project.expects(:latest).with(5).returns(projects)
  54 + expect(subject.latest_projects(5)).to be(projects)
  55 + end
  56 + end
  57 +
  58 + describe 'latest_configurations' do
  59 + let(:configurations) { mock }
  60 +
  61 + it 'should fetch the latest content' do
  62 + KalibroConfiguration.expects(:latest).with(5).returns(configurations)
  63 + expect(subject.latest_configurations(5)).to be(configurations)
  64 + end
  65 + end
  66 + end
44 67 end
... ...