Skip to content

Commit

Permalink
Better cache expiration. Use stubs instead of mocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitrij Denissenko committed Sep 7, 2010
1 parent 2394308 commit 815ceb4
Show file tree
Hide file tree
Showing 69 changed files with 143 additions and 100 deletions.
3 changes: 2 additions & 1 deletion app/controllers/project_area_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def module_accessible?(project, item)
protected

def fresh_when(options = {})
options[:etag] = [User.current, Project.current, flash] + Array(options[:etag])
options[:etag] = [User.current, Project.current, flash] + Array.wrap(options[:etag])
options[:last_modified] = ([User.current, Project.current].map(&:updated_at) + Array.wrap(options[:last_modified])).compact.max
super
end

Expand Down
12 changes: 12 additions & 0 deletions config/initializers/c.extensions/array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Backport from Rails 3
class Array
def self.wrap(object)
if object.nil?
[]
elsif object.respond_to?(:to_ary)
object.to_ary
else
[object]
end
end
end
29 changes: 29 additions & 0 deletions db/migrate/20100907201050_add_timestamps_to_projects_and_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class AddTimestampsToProjectsAndUsers < ActiveRecord::Migration
def self.up
add_column :projects, :created_at, :timestamp
add_column :projects, :updated_at, :timestamp
add_column :users, :updated_at, :timestamp

rows = select_all %(
SELECT projects.id, MIN(LEAST(tickets.created_at, milestones.created_at)) AS created_at
FROM projects
INNER JOIN tickets ON tickets.project_id = projects.id
INNER JOIN milestones ON milestones.project_id = projects.id
WHERE projects.created_at IS NULL
GROUP BY projects.id
)
rows.each do |row|
update "UPDATE projects SET created_at = #{quote(row['created_at'])} WHERE id = #{row['id']}"
end

update "UPDATE projects SET created_at = #{quote(Time.now.utc.to_s(:db))} WHERE created_at IS NULL"
update "UPDATE projects SET updated_at = #{quote(Time.now.utc.to_s(:db))} WHERE updated_at IS NULL"
update "UPDATE users SET updated_at = #{quote(Time.now.utc.to_s(:db))} WHERE updated_at IS NULL"
end

def self.down
remove_column :projects, :created_at
remove_column :projects, :updated_at
remove_column :users, :updated_at
end
end
2 changes: 1 addition & 1 deletion extensions/agile_pm/spec/views/goals/edit.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/goals/edit.html.erb" do

before(:each) do
@project = mock_current_project! :name => 'Retrospectiva'
@project = stub_current_project! :name => 'Retrospectiva'
@milestone = assigns[:milestone] = stub_model(Milestone)
@goal = assigns[:goal] = stub_model(Goal, :sprint => stub_model(Sprint, :title => 'Sprint 1'))
assigns[:sprints] = [stub_model(Sprint)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/goals/index.html.erb" do

before(:each) do
@project = mock_current_project!
@project = stub_current_project!
@sprint = assigns[:current_sprint] = stub_model(Sprint, :title => 'Sprint 1')
@milestone = assigns[:milestone] = stub_model(Milestone, :sprints => [@sprint, stub_model(Sprint, :title => 'Sprint 2')])
assigns[:milestones] = [@milestone, stub_model(Milestone)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/goals/no_milestones.html.erb" do

before(:each) do
@project = mock_current_project! :name => 'Retrospectiva'
@project = stub_current_project! :name => 'Retrospectiva'
template.stub!(:permitted?).and_return(true)
template.stub!(:x_stylesheet_link_tag)
end
Expand Down
2 changes: 1 addition & 1 deletion extensions/agile_pm/spec/views/goals/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/goals/show.html.erb" do

before(:each) do
@project = mock_current_project! :name => 'Retrospectiva'
@project = stub_current_project! :name => 'Retrospectiva'
assigns[:milestone] = stub_model(Milestone)
assigns[:goal] = stub_model(Goal, :sprint => stub_model(Sprint, :title => 'Sprint 1'))

Expand Down
2 changes: 1 addition & 1 deletion extensions/agile_pm/spec/views/goals/show.js.rjs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/goals/show.js.rjs" do

before(:each) do
@project = mock_current_project! :name => 'Retrospectiva'
@project = stub_current_project! :name => 'Retrospectiva'
assigns[:milestone] = stub_model(Milestone)
assigns[:goal] = stub_model(Goal, :sprint => stub_model(Sprint, :title => 'Sprint 1'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/sprints/edit.html.erb" do

before(:each) do
@project = mock_current_project! :name => 'Retrospectiva'
@project = stub_current_project! :name => 'Retrospectiva'
@milestone = assigns[:milestone] = stub_model(Milestone)
@sprint = assigns[:sprint] = stub_model(Sprint)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/sprints/new.html.erb" do

before(:each) do
@project = mock_current_project! :name => 'Retrospectiva'
@project = stub_current_project! :name => 'Retrospectiva'
@milestone = assigns[:milestone] = stub_model(Milestone, :to_param => '37')
@sprint = assigns[:sprint] = stub_model(Sprint, :new_record? => true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/stories/backlog.html.erb" do

before(:each) do
@project = mock_current_project!
@project = stub_current_project!

@stories = [stub_model(Story), stub_model(Story), stub_model(Story)]
@stories.stub!(:active_count).and_return(1)
Expand Down
4 changes: 2 additions & 2 deletions extensions/agile_pm/spec/views/stories/comment.js.rjs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
describe "/stories/comment.js.rjs" do

before(:each) do
@user = mock_current_user! :name => 'Doesnt Matter', :email => 'test@localhost.localdomain'
@project = mock_current_project! :name => 'Retrospectiva'
@user = stub_current_user! :name => 'Doesnt Matter', :email => 'test@localhost.localdomain'
@project = stub_current_project! :name => 'Retrospectiva'
@sprint = assigns[:sprint] = stub_model(Sprint)
@milestone = assigns[:milestone] = stub_model(Milestone)

Expand Down
4 changes: 2 additions & 2 deletions extensions/agile_pm/spec/views/stories/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
describe "/stories/index.html.erb" do

before(:each) do
@project = mock_current_project!
@project = stub_current_project!

@user_a = mock_current_user!
@user_a = stub_current_user!
@user_b = stub_model(User)

@stories = assigns[:stories] = { @user_a => [ stub_model(Story), stub_model(Story) ], @user_b => stub_model(Story) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/stories/new.html.erb" do

before(:each) do
@project = mock_current_project!
@project = stub_current_project!

@sprint = assigns[:sprint] = stub_model(Sprint, :goals => [stub_model(Goal)])
@story = assigns[:story] = stub_model(Story)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "/stories/no_sprints.html.erb" do

before(:each) do
@project = mock_current_project!
@project = stub_current_project!
template.stub!(:permitted?).and_return(true)
end

Expand Down
4 changes: 2 additions & 2 deletions extensions/agile_pm/spec/views/stories/show.js.rjs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
before(:each) do
@event = stub_model(StoryComment, :created_at => 3.days.ago)

@project = mock_current_project!
@user = mock_current_user! :name => 'Doesnt Matter', :email => 'test@localhost.localdomain'
@project = stub_current_project!
@user = stub_current_user! :name => 'Doesnt Matter', :email => 'test@localhost.localdomain'
@milestone = assigns[:milestone] = stub_model(Milestone)
@sprint = assigns[:sprint] = stub_model(Sprint)
@story = assigns[:story] = stub_model(Story, :events => [@event], :title => 'My Story', :created_at => 4.days.ago)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

before do
@project = permit_access_with_current_project! :name => 'Retro', :wiki_title => 'Retro'
@user = mock_current_user! :permitted? => true, :projects => [@project]
@user = stub_current_user! :permitted? => true, :projects => [@project]

@blog_post = mock_model(BlogPost)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

before do
@project = permit_access_with_current_project! :name => 'Retro', :wiki_title => 'Retro'
@user = mock_current_user! :permitted? => true, :projects => [@project]
@user = stub_current_user! :permitted? => true, :projects => [@project]
@posts_proxy = @project.stub_association!(:blog_posts)
@posts_proxy.stub!(:posted_by).and_return(@posts_proxy)
@posts_proxy.stub!(:categorized_as).and_return(@posts_proxy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

before do
@project = permit_access_with_current_project! :name => 'Retro', :wiki_title => 'Retro'
@user = mock_current_user! :permitted? => true, :projects => [@project]
@user = stub_current_user! :permitted? => true, :projects => [@project]
@pages_proxy = @project.stub_association!(:wiki_pages)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

before do
@project = permit_access_with_current_project! :name => 'Retro', :wiki_title => 'Retro'
@user = mock_current_user! :permitted? => true, :projects => [@project]
@user = stub_current_user! :permitted? => true, :projects => [@project]
@files_proxy = @project.stub_association!(:wiki_files)
@files_proxy.stub!(:count).and_return(5)
@files_proxy.stub!(:maximum)
Expand Down
4 changes: 2 additions & 2 deletions extensions/retro_wiki/spec/helpers/format_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

describe FormatHelper do
before do
@project = mock_current_project! :existing_wiki_page_titles => ['Wiki'], :existing_revisions => []
@user = mock_current_user! :permitted? => true
@project = stub_current_project! :existing_wiki_page_titles => ['Wiki'], :existing_revisions => []
@user = stub_current_user! :permitted? => true
helper.stub!(:permitted?).and_return(true)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe AccountsController do

before do
@user = mock_current_user!
@user = stub_current_user!
@project = permit_access_with_current_project!
@new = mock_model(User)
User.stub!(:new).and_return(@new)
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe 'permission checks' do

before do
@user = mock_current_user! :public? => true, :permitted => true
@user = stub_current_user! :public? => true, :permitted => true
end

it 'should forward checks currently logged-in user' do
Expand Down Expand Up @@ -252,7 +252,7 @@ def do_get(options = {})
@tickets.stub!(:maximum)

@project = permit_access_with_current_project! :name => 'Any', :tickets => @tickets
@user = mock_current_user! :public? => false, :name => 'Agent', :email => 'agent@mail.com'
@user = stub_current_user! :public? => false, :name => 'Agent', :email => 'agent@mail.com'
end

describe 'storing' do
Expand Down Expand Up @@ -331,7 +331,7 @@ def do_get
before do
permit_access_with_current_project!
controller.stub!(:load_channels).and_return([])
@user = mock_current_user! :name => 'Doesnt Matter'
@user = stub_current_user! :name => 'Doesnt Matter'
end

def do_get
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/every_admin_area_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def action_names
end

it "should grant access to administrators" do
mock_current_user!(:admin? => true, :name => 'Admin')
stub_current_user!(:admin? => true, :name => 'Admin')
controller.class.rspec_reset
action_names.each do |action|
lambda { request_restfully(action) }.should_not raise_error(RetroAM::NoAuthorizationError)
end
end

it "should deny access to ordinary users" do
mock_current_user!(:admin? => false, :name => 'Ordinary')
stub_current_user!(:admin? => false, :name => 'Ordinary')
controller.class.rspec_reset
action_names.each do |action|
lambda { request_restfully(action) }.should raise_error(RetroAM::NoAuthorizationError)
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/project_area_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
describe ProjectAreaController do

before do
@user = mock_current_user! :admin? => true, :name => 'Agent'
@project = mock_current_project! :enabled_modules => [], :name => 'Retro'
@user = stub_current_user! :admin? => true, :name => 'Agent'
@project = stub_current_project! :enabled_modules => [], :name => 'Retro'
@item = mock(RetroAM::MenuMap::Item, :name => 'Item', :active? => true)
RetroAM.menu_items.stub!(:find).and_return(@item)
end
Expand Down Expand Up @@ -89,7 +89,7 @@ def authorize?(action_name = 'index', request_params = {})
controller_name :milestones

before do
@user = mock_current_user! :name => 'Public', :public? => true, :admin? => false, :permitted? => false
@user = stub_current_user! :name => 'Public', :public? => true, :admin? => false, :permitted? => false
@project = stub_model Project,
:name => 'Retro', :short_name => 'retro',
:enabled_modules => ['milestones']
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/projects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe ProjectsController do

before do
@user = mock_current_user! :name => 'Someone'
@user = stub_current_user! :name => 'Someone'
end

describe 'loading active projects' do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/search_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

before do
@project = permit_access_with_current_project! :name => 'Retrospectiva'
@user = mock_current_user! :has_access? => true
@user = stub_current_user! :has_access? => true
end

describe 'GET /index' do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/ticket_reports_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

before do
@project = permit_access_with_current_project! :name => 'Retrospectiva'
@user = mock_current_user! :has_access? => true
@user = stub_current_user! :has_access? => true
@reports_proxy = []
@project.stub!(:ticket_reports).and_return(@reports_proxy)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/tickets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
it_should_behave_like EveryProjectAreaController

before do
Status.stub!(:default).and_return(mock_model(Status))
Priority.stub!(:default).and_return(mock_model(Priority))
Status.stub!(:default).and_return(stub_model(Status))
Priority.stub!(:default).and_return(stub_model(Priority))
end

before do
@project = permit_access_with_current_project! :name => 'Any'
@tickets = [mock_model(Ticket)]
@tickets = [stub_model(Ticket)]
@tickets_proxy = @project.stub_association!(:tickets)
@tickets_proxy.stub!(:count)
@tickets_proxy.stub!(:maximum)
@changes_proxy = @project.stub_association!(:ticket_changes)
@user = mock_current_user! :public? => false, :name => 'Agent', :email => 'agent@mail.com'
@user = stub_current_user! :public? => false, :name => 'Agent', :email => 'agent@mail.com'
end

describe "handling GET /tickets" do
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/browse_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
before do
@project = projects(:retro)
Project.stub!(:current).and_return(@project)
@user = mock_current_user! :has_access? => true
@user = stub_current_user! :has_access? => true
@path = ['folder', 'file.rb']
@params = { :rev => 'R123', :path => @path }
helper.extend ApplicationHelper
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/format_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

describe FormatHelper do
before do
@project = mock_current_project!
@user = mock_current_user!
@project = stub_current_project!
@user = stub_current_user!
end

describe 'formatting internal links' do
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/milestones_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
describe 'ticket stats and links' do

before do
@project = mock_current_project! :to_param => 'retro'
@project = stub_current_project! :to_param => 'retro'
@milestone = mock_model Milestone,
:id => '1',
:ticket_counts => { 'open' => 10, 'in_progress' => 0, 'resolved' => 90 }.with_indifferent_access
Expand All @@ -30,7 +30,7 @@

before do
helper.stub!(:image_spacer).and_return('I')
@project = mock_current_project!
@project = stub_current_project!
@milestone = mock_model Milestone,
:progress_percentages => { 'open' => 80, 'in_progress' => 20, 'resolved' => 0 }.with_indifferent_access
end
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/navigation_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
before do
@project = projects(:retro)
Project.stub!(:current).and_return(@project)
@user = mock_current_user! :has_access? => true
@user = stub_current_user! :has_access? => true
end

describe 'linking to changesets' do
Expand Down
Loading

0 comments on commit 815ceb4

Please sign in to comment.