From ca155314fa914a535d185570b9163432ab5fa821 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 11 Dec 2010 09:37:39 -0500 Subject: [PATCH] home page caching, reset top contributors only when necessary --- app/controllers/outfits_controller.rb | 10 +- app/controllers/pets_controller.rb | 2 + app/models/item_sweeper.rb | 2 - app/models/user.rb | 5 + app/models/user_sweeper.rb | 12 ++ app/views/outfits/new.html.haml | 195 +++++++++++++------------- config/application.rb | 2 +- 7 files changed, 128 insertions(+), 100 deletions(-) create mode 100644 app/models/user_sweeper.rb diff --git a/app/controllers/outfits_controller.rb b/app/controllers/outfits_controller.rb index 7fd2ca20..8dd7b8f4 100644 --- a/app/controllers/outfits_controller.rb +++ b/app/controllers/outfits_controller.rb @@ -29,9 +29,13 @@ class OutfitsController < ApplicationController end def new - @colors = Color.all_ordered_by_name - @species = Species.all_ordered_by_name - @top_contributors = User.top_contributors.limit(3) + unless fragment_exist?(:action_suffix => 'start_from_scratch_form_content') + @colors = Color.all_ordered_by_name + @species = Species.all_ordered_by_name + end + unless fragment_exist?(:action_suffix => 'top_contributors') + @top_contributors = User.top_contributors.limit(User::PreviewTopContributorsCount) + end end def show diff --git a/app/controllers/pets_controller.rb b/app/controllers/pets_controller.rb index 9800a5ed..5dd47d3a 100644 --- a/app/controllers/pets_controller.rb +++ b/app/controllers/pets_controller.rb @@ -1,6 +1,8 @@ class PetsController < ApplicationController rescue_from Pet::PetNotFound, :with => :pet_not_found + cache_sweeper :user_sweeper + DESTINATIONS = { 'needed_items' => '?', 'root' => '#', diff --git a/app/models/item_sweeper.rb b/app/models/item_sweeper.rb index 7e9a8d62..b5a10ea6 100644 --- a/app/models/item_sweeper.rb +++ b/app/models/item_sweeper.rb @@ -1,5 +1,3 @@ -puts 'Hey! Item sweeper!' - class ItemSweeper < ActionController::Caching::Sweeper observe Item diff --git a/app/models/user.rb b/app/models/user.rb index 4a7dd4c7..d3f043db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,6 @@ class User < ActiveRecord::Base DefaultAuthServerId = 1 + PreviewTopContributorsCount = 3 has_many :contributions has_many :outfits @@ -37,4 +38,8 @@ class User < ActiveRecord::Base end user end + + def self.points_required_to_pass_top_contributor(offset) + User.top_contributors.select(:points).limit(1).offset(offset).first.points + end end diff --git a/app/models/user_sweeper.rb b/app/models/user_sweeper.rb new file mode 100644 index 00000000..f7775f67 --- /dev/null +++ b/app/models/user_sweeper.rb @@ -0,0 +1,12 @@ +class UserSweeper < ActionController::Caching::Sweeper + observe User + + def before_save(user) + if user.points_changed? + points_to_beat = User.points_required_to_pass_top_contributor(User::PreviewTopContributorsCount - 1) + if user.points >= points_to_beat + expire_fragment(:controller => 'outfits', :action => 'new', :action_suffix => 'top_contributors') + end + end + end +end diff --git a/app/views/outfits/new.html.haml b/app/views/outfits/new.html.haml index c54853ee..aac0c45b 100644 --- a/app/views/outfits/new.html.haml +++ b/app/views/outfits/new.html.haml @@ -1,104 +1,111 @@ - hide_home_link #outfit-forms - #pet-preview - = image_tag 'default_preview.png', :alt => '' - %span - %h1 Dress to Impress - %h2 Neopets wearables made easy! + - cache :action_suffix => 'outfit_forms_intro' do + #pet-preview + = image_tag 'default_preview.png', :alt => '' + %span + %h1 Dress to Impress + %h2 Neopets wearables made easy! = form_tag load_pet_path, :id => 'load-pet-to-wardrobe' do - = origin_tag root_path - = destination_tag 'wardrobe' - %fieldset - %legend Enter your pet's name - = pet_name_tag :id => 'main-pet-name' - %button{:type => "submit"} - Plan my outfit! + - cache :action_suffix => 'main_load_pet_form_content' do + = origin_tag root_path + = destination_tag 'wardrobe' + %fieldset + %legend Enter your pet's name + = pet_name_tag :id => 'main-pet-name' + %button{:type => "submit"} + Plan my outfit! = form_tag wardrobe_path, :method => 'get', :id => 'start-from-scratch' do - %fieldset - %legend Or start from scratch - = pet_attribute_select 'color', @colors, 8 - = pet_attribute_select 'species', @species - %input{:type => "submit", :value => "Go"} -%ul#sections - %li - %a{:href => "http://forum.openneo.net"} - = image_tag 'forum.png' - %h3 - %a{:href => "http://forum.openneo.net/"} Forum - %div - %h4 Join our community! - %p - Show off your designs, ask for advice, or play silly forum games - here. - %li - %a{:href => items_path} - = image_tag 'items.png' - %h3 + - cache :action_suffix => 'start_from_scratch_form_content' do + %fieldset + %legend Or start from scratch + = pet_attribute_select 'color', @colors, 8 + = pet_attribute_select 'species', @species + %input{:type => "submit", :value => "Go"} +- cache :action_suffix => 'sections_and_description' do + %ul#sections + %li + %a{:href => "http://forum.openneo.net"} + = image_tag 'forum.png' + %h3 + %a{:href => "http://forum.openneo.net/"} Forum + %div + %h4 Join our community! + %p + Show off your designs, ask for advice, or play silly forum games + here. + %li %a{:href => items_path} - Infinite Closet - %div - %h4 Looking for something? - %p - Take a look through our wearables database! - = form_tag items_path, :method => 'get' do - = text_field_tag 'q', '', :placeholder => raw('search items…'), :type => 'search' - = submit_tag 'search' - %li#blog-preview - %a{:href => "http://blog.openneo.net"} - = image_tag 'blog.png' - %h3 - %a{:href => "http://blog.openneo.net/"} OpenNeo Blog - %div - %h4 We'll keep you posted! - %p - Dress to Impress is always improving, and you can always stay in - the loop through our blog. -#description - %h2 Built by you, just for you! - %p - Dress to Impress lets you plan how you want to dress up your Neopets, - before you even go shopping! Whenever you give us a Neopet's name, we - automatically look up what it's wearing and organize the data into - our own wearables database — a community closet, if you will. - Then you can plan your outfit, mixing and matching various items, so - you can have the best-dressed Neopet in all of Neopia! - %p - To make all this possible, though, we need your help — and if - you log in at the top, we'll keep track of your - = link_to 'contributions', contributions_path - and award - = link_to 'points', top_contributors_path - so you can show off just how dedicated you really are! -#top-contributors - %h3 Top Contributors - %ol - - @top_contributors.each do |user| - %li - = link_to user.name, user_contributions_path(user) - — - = user.points - %a{:href => top_contributors_path} see more + = image_tag 'items.png' + %h3 + %a{:href => items_path} + Infinite Closet + %div + %h4 Looking for something? + %p + Take a look through our wearables database! + = form_tag items_path, :method => 'get' do + = text_field_tag 'q', '', :placeholder => raw('search items…'), :type => 'search' + = submit_tag 'search' + %li#blog-preview + %a{:href => "http://blog.openneo.net"} + = image_tag 'blog.png' + %h3 + %a{:href => "http://blog.openneo.net/"} OpenNeo Blog + %div + %h4 We'll keep you posted! + %p + Dress to Impress is always improving, and you can always stay in + the loop through our blog. + #description + %h2 Built by you, just for you! + %p + Dress to Impress lets you plan how you want to dress up your Neopets, + before you even go shopping! Whenever you give us a Neopet's name, we + automatically look up what it's wearing and organize the data into + our own wearables database — a community closet, if you will. + Then you can plan your outfit, mixing and matching various items, so + you can have the best-dressed Neopet in all of Neopia! + %p + To make all this possible, though, we need your help — and if + you log in at the top, we'll keep track of your + = link_to 'contributions', contributions_path + and award + = link_to 'points', top_contributors_path + so you can show off just how dedicated you really are! +- cache :action_suffix => 'top_contributors' do + #top-contributors + %h3 Top Contributors + %ol + - @top_contributors.each do |user| + %li + = link_to user.name, user_contributions_path(user) + — + = user.points + %a{:href => top_contributors_path} see more = form_tag load_pet_path, :id => 'how-can-i-help' do - = destination_tag 'needed_items' - = origin_tag root_path - %h2 How can I help? - %p - Enter your pet's name, and we'll tell you what items you can help us - model. Thanks so much! - = pet_name_tag - %button{:type => "submit"} - Let's model! + - cache :action_suffix => 'how_can_i_help_form_content' do + = destination_tag 'needed_items' + = origin_tag root_path + %h2 How can I help? + %p + Enter your pet's name, and we'll tell you what items you can help us + model. Thanks so much! + = pet_name_tag + %button{:type => "submit"} + Let's model! = form_tag load_pet_path, :id => 'i-found-something' do - = origin_tag root_path - %a{:href => bulk_pets_path} - add many pets - %h2 I found something! - %p - Enter the name of the pet you found, and we'll keep a copy of what - it's wearing. Thanks so much! - = pet_name_tag - %button{:type => "submit"} - I pwn! + - cache :action_suffix => 'i_found_something_form_content' do + = origin_tag root_path + %a{:href => bulk_pets_path} + add many pets + %h2 I found something! + %p + Enter the name of the pet you found, and we'll keep a copy of what + it's wearing. Thanks so much! + = pet_name_tag + %button{:type => "submit"} + I pwn! - content_for :javascripts do = include_javascript_libraries :jquery = include_javascripts :new_outfit_package diff --git a/config/application.rb b/config/application.rb index 24ea28c7..3359da28 100644 --- a/config/application.rb +++ b/config/application.rb @@ -20,7 +20,7 @@ module OpenneoImpressItems # config.plugins = [ :exception_notification, :ssl_requirement, :all ] # Activate observers that should always be running - config.active_record.observers = :item_sweeper + config.active_record.observers = :item_sweeper, :user_sweeper # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.