diff --git a/app/controllers/open_conference_ware/application_controller.rb b/app/controllers/open_conference_ware/application_controller.rb index 88ba6156..605f5968 100644 --- a/app/controllers/open_conference_ware/application_controller.rb +++ b/app/controllers/open_conference_ware/application_controller.rb @@ -242,7 +242,7 @@ def log_the_session # Assign an @events variable for use by the layout when displaying available events. def assign_events - @events = Event.lookup + @events = Event.all end # Return the event and a status which describes how the event was assigned. The status can be one of the following: @@ -257,7 +257,7 @@ def get_current_event_and_assignment_status # Try finding event using params: event_id_key = controller_name == "events" ? :id : :event_id if key = params[event_id_key] - if event = Event.lookup(key) + if event = Event.find(key) return [event, :assigned_to_param] else logger.info "error, couldn't find event from key: #{key}" diff --git a/app/controllers/open_conference_ware/manage/events_controller.rb b/app/controllers/open_conference_ware/manage/events_controller.rb index 00ff7edc..99232e96 100644 --- a/app/controllers/open_conference_ware/manage/events_controller.rb +++ b/app/controllers/open_conference_ware/manage/events_controller.rb @@ -11,7 +11,7 @@ class EventsController < ApplicationController # GET /events # GET /events.xml def index - @events = Event.lookup + @events = Event.all respond_to do |format| format.html # index.html.erb @@ -108,7 +108,7 @@ def notify_speakers already_emailed = [] proposals = params[:proposal_ids].split(',') proposals.each do |proposal_id| - proposal = Proposal.lookup(proposal_id) + proposal = Proposal.find(proposal_id) rescue nil next unless proposal case params[:proposal_status] when 'accepted' diff --git a/app/controllers/open_conference_ware/proposals_controller.rb b/app/controllers/open_conference_ware/proposals_controller.rb index d770569a..3f2f7508 100644 --- a/app/controllers/open_conference_ware/proposals_controller.rb +++ b/app/controllers/open_conference_ware/proposals_controller.rb @@ -393,7 +393,7 @@ def assert_anonymous_proposals # * :invalid_proposal # * :invalid_event def get_proposal_and_assignment_status - if proposal = Proposal.lookup(params[:id].to_i) rescue nil + if proposal = Proposal.find(params[:id].to_i) rescue nil if proposal.event return [proposal, :assigned_via_param] else diff --git a/app/helpers/open_conference_ware/snippets_helper.rb b/app/helpers/open_conference_ware/snippets_helper.rb index 7045c558..e23f90dd 100644 --- a/app/helpers/open_conference_ware/snippets_helper.rb +++ b/app/helpers/open_conference_ware/snippets_helper.rb @@ -4,7 +4,7 @@ module SnippetsHelper # Return the Snippet record matching the +slug+, else raise a # ActiveRecord::RecordNotFound. def snippet_record_for(slug) - if record = Snippet.lookup(slug.to_s) + if record = Snippet.find_by_slug(slug.to_s) return record else raise ActiveRecord::RecordNotFound, "Can't find snippet: #{slug}" diff --git a/app/mixins/open_conference_ware/cache_lookups_mixin.rb b/app/mixins/open_conference_ware/cache_lookups_mixin.rb deleted file mode 100644 index 68ac5661..00000000 --- a/app/mixins/open_conference_ware/cache_lookups_mixin.rb +++ /dev/null @@ -1,128 +0,0 @@ -module OpenConferenceWare - - # = CacheLookupsMixin - # - # A mixin that provides simple caching for ActiveRecord models. It's best used - # for models that have relatively few records (less than a thousand) and are - # updated infrequently (less than once per second). - # - # == Example of usage - # - # Setup lookups for Thing: - # - # class Thing < ActiveRecord::Base - # cache_lookups_for :id, order: :created_at - # ... - # end - # - # Return array of things in the order they were created: - # things = Thing.lookup - # - # Return a particular Thing matching ID 66: - # thing = Thing.lookup(66) - module CacheLookupsMixin - def self.included(mixee) - mixee.send(:extend, ClassMethods) - mixee.class_eval do - # ActiveRecord model's attribute to use when asked to lookup a specific - # item. Defaults to :id. - cattr_accessor :lookup_key - self.lookup_key = :id - - # Hash of options passed to ActiveRecord::Base.find when populating the - # cache. This makes it possible to add :order and filter by :condition. - cattr_accessor :lookup_opts - self.lookup_opts = {} - end - end - - module ClassMethods - # Should lookups be cached? If the "perform_caching" Rails environment - # configuration setting is enabled, default to using cache. - # - # You can force caching on or off using the CACHELOOKUPS environmental - # variable, e.g. activate with: - # - # CACHELOOKUPS=1 ./script/server - def cache_lookups? - if Rails.configuration.action_controller.perform_caching - return ENV['CACHELOOKUPS'] != '0' - else - return ENV['CACHELOOKUPS'] == '1' - end - end - - # Setup the lookup caching system. - # - # Arguments: - # * key: The attribute you'll use as a key for doing lookups. - # * opts: Options to pass to ActiveRecord::Base.find. - def cache_lookups_for(key, opts={}) - self.lookup_key = key - self.lookup_opts = opts - end - - # Return silo that this class's values will be stored in. - def lookup_silo_name - return "#{self.name.gsub('::', '__')}_dict" - end - - def query_one(key) - return self.where(self.lookup_key => key).first - end - - def query_all - scope = self - scope = scope.order(self.lookup_opts[:order]) if self.lookup_opts[:order] - scope = scope.includes(*self.lookup_opts[:include]) if self.lookup_opts[:include] - return scope - end - - # Return instance from cache matching +key+. If +key+ is undefined, returns - # array of all instances. - def lookup(key=nil) - unless self.cache_lookups? - return key ? - self.query_one(key) : - self.query_all - end - - silo = self.lookup_silo_name - key_number = key.try(:[], '#'+to_s) if key.present? - ActiveRecord::Base.benchmark("Lookup: #{silo}#{key_number}") do - dict = self.fetch_object(silo){ - # FIXME Exceptions within this block are silently swallowed by something. This is bad. - dict = Hashery::Dictionary.new - for record in self.query_all - dict[record.send(self.lookup_key)] = record - end - dict - } - return key ? dict[key.kind_of?(Symbol) ? key.to_s : key] : dict.values - end - end - - def expire_cache - Rails.logger.info("Lookup, expiring: #{self.name}") - CacheWatcher.expire(/#{lookup_silo_name}_.+/) - end - - def fetch_object(silo, &block) - self.revive_associations_for(self) - return Rails.cache.fetch(silo, &block) - end - - def revive_associations_for(object) - if object.kind_of?(ActiveRecord::Base) || object.ancestors.include?(ActiveRecord::Base) - object.reflect_on_all_associations.each do |association| - name = association.class_name - unless object.constants.include?(name) - OpenConferenceWare.const_get(name) # This line forces Rails to load this class - end - end - end - end - end - - end -end diff --git a/app/models/open_conference_ware/event.rb b/app/models/open_conference_ware/event.rb index 7307ac26..4ff3752b 100644 --- a/app/models/open_conference_ware/event.rb +++ b/app/models/open_conference_ware/event.rb @@ -27,12 +27,8 @@ module OpenConferenceWare class Event < OpenConferenceWare::Base # Mixins - ### Provide cached Snippet.lookup(id) method. - include CacheLookupsMixin include SimpleSlugMixin - cache_lookups_for :slug, order: 'deadline desc', include: [:tracks, :rooms] - # Associations has_many :proposals, dependent: :destroy has_many :tracks, dependent: :destroy @@ -86,10 +82,7 @@ def current? # see if a snippet says which is current, else tries to return the event # with the latest deadline, else returns a nil. def self.current - query = lambda {|*args| self.current_by_settings || self.current_by_deadline } - return self.cache_lookups? ? - self.fetch_object('event_current', &query) : - query.call + self.current_by_settings || self.current_by_deadline end # Return current event by finding it by deadline. diff --git a/app/models/open_conference_ware/proposal.rb b/app/models/open_conference_ware/proposal.rb index f9e5ff15..69c515bb 100644 --- a/app/models/open_conference_ware/proposal.rb +++ b/app/models/open_conference_ware/proposal.rb @@ -38,9 +38,6 @@ class Proposal < OpenConferenceWare::Base # Provide ::event_tracks? and other methods for accessing SETTING include SettingsCheckersMixin - # Provide ::lookup - include CacheLookupsMixin - # Provide ::overlaps? include ScheduleOverlapsMixin @@ -57,9 +54,6 @@ class Proposal < OpenConferenceWare::Base :track_id, :track_title, :user_ids, :user_titles - - cache_lookups_for :id, order: 'submitted_at desc', include: [:event, :track, :room, :users] - # Provide #tags acts_as_taggable_on :tags diff --git a/app/models/open_conference_ware/snippet.rb b/app/models/open_conference_ware/snippet.rb index bf6f9b3a..23c6059f 100644 --- a/app/models/open_conference_ware/snippet.rb +++ b/app/models/open_conference_ware/snippet.rb @@ -15,11 +15,6 @@ module OpenConferenceWare # class Snippet < OpenConferenceWare::Base - - # Provide cached Snippet.lookup(slug) method. - include CacheLookupsMixin - cache_lookups_for :slug, order: :slug - # Load the Snippets as defined in the "spec/fixtures/snippets.yml" file and # load them into the current database, overwriting any existing records. def self.load_from_fixtures(overwrite = false) @@ -43,7 +38,7 @@ def self.reload_from_fixtures! # Returns the content for a Snippet match +slug+, else raise an ActiveRecord::RecordNotFound. def self.content_for(slug) - if record = self.lookup(slug.to_s) + if record = self.find_by_slug(slug.to_s) return record.content else raise ActiveRecord::RecordNotFound, "Couldn't find snippet: #{slug}" diff --git a/spec/controllers/open_conference_ware/proposals_controller_spec.rb b/spec/controllers/open_conference_ware/proposals_controller_spec.rb index 45f36cd3..7817c0de 100644 --- a/spec/controllers/open_conference_ware/proposals_controller_spec.rb +++ b/spec/controllers/open_conference_ware/proposals_controller_spec.rb @@ -362,7 +362,7 @@ def get_entry(proposal_symbol) it "should redirect back to proposals list if asked to display a proposal without an event" do proposal = proposals(:quentin_widgets) proposal.stub(event: nil) - Proposal.stub(find_by_id: proposal, find: proposal, lookup: proposal) + Proposal.stub(find_by_id: proposal, find: proposal) get :show, id: proposal.id @@ -449,7 +449,7 @@ def assert_show(opts={}, &block) old_session = stub_model(Proposal, status: 'confirmed', event: old_event) old_session.users << old_session_user - Proposal.stub(find: old_session, find_by_id: old_session, lookup: old_session) + Proposal.stub(find: old_session, find_by_id: old_session) Event.stub(current: current_event) get :session_show, id: old_session.id @@ -478,7 +478,7 @@ def assert_show(opts={}, &block) it "should not notify owners of acceptance if proposal confirmation controls are not visible" do event = @proposal.event event.stub(:show_proposal_confirmation_controls? => false) - Proposal.stub(lookup: @proposal) + Proposal.stub(find: @proposal) login_as(users(:quentin)) @@ -951,7 +951,7 @@ def assert_update(login=nil, inputs={}, optional_params={}, &block) describe "delete" do before do @proposal = proposals(:quentin_widgets) - Proposal.stub(:lookup).and_return(@proposal) + Proposal.stub(:find).and_return(@proposal) end def assert_delete(login=nil, &block) @@ -1238,7 +1238,7 @@ def assert_not_declined it "should return a status of :invalid_event when a proposal doesn't have a valid event" do proposal = stub_model(Proposal, state: "confirmed", event: nil) - Proposal.stub(:lookup).and_return(proposal) + Proposal.stub(:find).and_return(proposal) @controller.stub(:params).and_return({ id: 1000 }) @controller.send(:get_proposal_and_assignment_status).should == [proposal, :invalid_event] end diff --git a/spec/integration/open_conference_ware/cache_lookups_mixin_spec.rb b/spec/integration/open_conference_ware/cache_lookups_mixin_spec.rb deleted file mode 100644 index e90065fc..00000000 --- a/spec/integration/open_conference_ware/cache_lookups_mixin_spec.rb +++ /dev/null @@ -1,122 +0,0 @@ -require 'spec_helper' - -describe OpenConferenceWare::CacheLookupsMixin do - before :each do - Event.destroy_all if Event.count > 0 - User.destroy_all if User.count > 0 - end - - describe 'when enabled' do - shared_examples_for 'overrides' do - it 'should cache lookups if forced to' do - ENV.should_receive(:[]).with('CACHELOOKUPS').and_return('1') - Event.cache_lookups?.should == true - end - - it 'should not cache lookups if forced not to' do - ENV.should_receive(:[]).with('CACHELOOKUPS').and_return('0') - Event.cache_lookups?.should == false - end - end - - describe 'and environment defaults to performing caching' do - before do - Rails.configuration.action_controller.stub(perform_caching: true) - end - - it_should_behave_like 'overrides' - - it 'should cache lookups by default' do - Event.cache_lookups?.should == true - end - - end - - describe 'and environment defaults to not performing caching' do - before do - Rails.configuration.action_controller.stub(perform_caching: false) - end - - it_should_behave_like 'overrides' - - it 'should not cache lookups by default' do - Event.cache_lookups?.should == false - end - end - end - - describe 'queries' do - before :each do - Event.stub(:cache_lookups? => true) - OpenConferenceWare::CacheWatcher.expire - - @event1 = create :event - @event2 = create :event - - @events = [@event1, @event2] - @event = @event1 - - OpenConferenceWare::CacheWatcher.expire - end - - describe 'for single records' do - it 'should write to cache' do - Event.should_receive(:query_all).and_return(@events) - - Event.lookup(@event.slug).should == @event - end - - it 'should read from cache' do - Event.lookup(@event.slug).should == @event - Event.should_not_receive(:query_all) - - Event.lookup(@event.slug).should == @event - end - end - - describe 'for all records' do - it 'should write to cache' do - Event.should_receive(:query_all).and_return(@events) - - Event.lookup.should == @events - end - - it 'should read from cache' do - Event.lookup.should == @events - Event.should_not_receive(:query_all) - - Event.lookup.should == @events - end - end - - describe 'current event' do - before do - @event = create :event - end - - describe 'when caching' do - it 'should fetch from cache' do - Event.should_receive(:cache_lookups?).and_return(true) - Event.should_receive(:fetch_object).with('event_current').and_return(@event) - - Event.current.should == @event - end - end - - describe 'when not caching' do - it 'should not fetch from cache' do - Event.should_receive(:cache_lookups?).and_return(false) - Event.should_receive(:current_by_settings).and_return(@event) - - Event.current.should == @event - end - end - end - end - - describe 'silo name' do - it 'should be derived from class name' do - Event.lookup_silo_name.should == 'OpenConferenceWare__Event_dict' - end - end -end diff --git a/spec/spec_helper_customizations.rb b/spec/spec_helper_customizations.rb index a0046c34..8daeff85 100644 --- a/spec/spec_helper_customizations.rb +++ b/spec/spec_helper_customizations.rb @@ -94,7 +94,7 @@ def stub_current_event!(opts={}) assign(:events, events) end - Event.stub(:lookup).and_return do |*args| + Event.stub(:current).and_return do |*args| key = args.pop key ? event : events end diff --git a/util/transfer_schedule_items.rb b/util/transfer_schedule_items.rb index 7a431fc0..22546a4f 100644 --- a/util/transfer_schedule_items.rb +++ b/util/transfer_schedule_items.rb @@ -2,7 +2,7 @@ def transfer_schedule_items current = Event.current - past = Event.lookup('2012') + past = Event.find_by_slug('2012') Event.current.dates.each_with_index do |day, i| past_date = past.dates[i] diff --git a/util/user_favorites_contention_report.rb b/util/user_favorites_contention_report.rb index 229f4b4e..9057d1ae 100644 --- a/util/user_favorites_contention_report.rb +++ b/util/user_favorites_contention_report.rb @@ -1,5 +1,5 @@ # Prepare -event = Event.lookup '2011' +event = Event.find_by_slug '2011' proposals = event.proposals.confirmed # Setup data structures