Permalink
Browse files

Store letters for browsing in repository documents

This save a bunch of more or less expensive queries.
  • Loading branch information...
1 parent a33f4b9 commit dfdaa422f6a83ca7c4460448a61dc665dbaced51 @koraktor committed Jan 10, 2017
@@ -1,7 +1,7 @@
# This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
-# Copyright (c) 2012-2016, Sebastian Staudt
+# Copyright (c) 2012-2017, Sebastian Staudt
class ApplicationController < ActionController::Base
@@ -72,7 +72,7 @@ def sitemap
private
def main_page
- @alt_repos = Repository.only(:_id, :date, :name, :sha, :updated_at).
+ @alt_repos = Repository.only(:_id, :date, :letters, :name, :sha, :updated_at).
order_by([:name, :asc]).to_a
@repository = @alt_repos.find { |repo| repo.name == Repository::CORE }
@alt_repos -= [ @repository ]
@@ -8,7 +8,6 @@
class FormulaeController < ApplicationController
before_action :select_repository
- before_action :letters, only: [ :browse, :search ]
def browse
letter = params[:letter]
@@ -92,12 +91,6 @@ def show
private
- def letters
- @letters = ('A'..'Z').select do |letter|
- @repository.formulae.letter(letter).where(removed: false).exists?
- end
- end
-
def repository_id
@repository_id ||= params[:repository_id] || Repository::CORE
end
@@ -110,7 +103,7 @@ def select_repository
end
@repository = Repository.where(_id: /^#{repository_id}$/i).
- only(:_id, :name, :sha, :updated_at).first
+ only(:_id, :letters, :name, :sha, :updated_at).first
if @repository.nil?
raise Mongoid::Errors::DocumentNotFound.new Repository, [], repository_id
end
@@ -31,7 +31,7 @@ class Formula
has_and_belongs_to_many :deps, class_name: self.to_s, inverse_of: :revdeps, validate: false, index: true
has_and_belongs_to_many :revdeps, class_name: self.to_s, inverse_of: :deps, validate: false, index: true
- scope :letter, ->(letter) { where(name: /^#{letter.downcase}/) }
+ scope :letter, ->(letter) { where(name: /^#{letter}/) }
index( { repository_id: 1 }, { unique: false })
index( { name: 1 }, { unique: false })
@@ -1,7 +1,7 @@
# This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
-# Copyright (c) 2012-2016, Sebastian Staudt
+# Copyright (c) 2012-2017, Sebastian Staudt
class Repository
@@ -14,6 +14,7 @@ class Repository
field :_id, type: String, overwrite: true, default: ->{ name }
field :date, type: Time
field :full, type: Boolean, default: false
+ field :letters, type: Array, default: []
field :name, type: String
field :outdated, type: Boolean, default: true
field :sha, type: String
@@ -47,7 +47,7 @@
<div class="listing">
<% @alt_repos.each do |repo| %>
<div class="repo">
- <% if letter = repo.first_letter %>
+ <% if letter = repo.letters.first %>
<%= link_to repo.name, letter_repository_formulae_path(repo, letter) %>
<% else %>
<%= repo.name %>
@@ -14,16 +14,16 @@
<%= render partial: 'search', locals: { with_icon: true } %>
<ul id="letters">
- <% ('A'..'Z').each do |letter| %>
+ <% ('a'..'z').each do |letter| %>
<li>
- <% if @letters.include? letter
- letter_url = @repository.core? ? letter_formulae_path(letter.downcase) : letter_repository_formulae_path(@repository.name, letter.downcase)
- letter_class = 'current' if letter == (params[:letter] || '').upcase %>
- <%= link_to_unless_current letter, letter_url, class: letter_class do %>
- <span class="current"><%= letter %></span>
+ <% if @repository.letters.include? letter
+ letter_url = @repository.core? ? letter_formulae_path(letter) : letter_repository_formulae_path(@repository.name, letter)
+ letter_class = 'current' if letter == (params[:letter] || '').downcase %>
+ <%= link_to_unless_current letter.upcase, letter_url, class: letter_class do %>
+ <span class="current"><%= letter.upcase %></span>
<% end %>
<% else %>
- <%= letter %>
+ <%= letter.upcase %>
<% end %>
</li>
<% end %>
View
@@ -330,6 +330,10 @@ def refresh
Rails.logger.info "#{added} formulae added, #{modified} formulae modified, #{removed} formulae removed."
+ self.letters = ('a'..'z').select do |letter|
+ self.formulae.letter(letter).where(removed: false).exists?
+ end
+
last_sha
end
@@ -13,7 +13,7 @@
repo.expects(:name).returns 'Homebrew/homebrew-versions'
criteria = mock
Repository.expects(:where).with(_id: /^Homebrew\/homebrew-versions$/i).returns criteria
- criteria.expects(:only).with(:_id, :name, :sha, :updated_at).returns [ repo ]
+ criteria.expects(:only).with(:_id, :letters, :name, :sha, :updated_at).returns [ repo ]
controller.expects(:params).returns({ repository_id: 'Homebrew/homebrew-versions' })
controller.send :select_repository
@@ -35,7 +35,7 @@
repo.expects(:name).returns Repository::CORE
criteria = mock
Repository.expects(:where).with(_id: /^#{Repository::CORE}$/i).returns criteria
- criteria.expects(:only).with(:_id, :name, :sha, :updated_at).returns [ repo ]
+ criteria.expects(:only).with(:_id, :letters, :name, :sha, :updated_at).returns [ repo ]
controller.send :select_repository
@@ -51,7 +51,7 @@
repo.expects(:name).twice.returns 'Homebrew/homebrew-versions'
criteria = mock
Repository.expects(:where).with(_id: /^Homebrew\/Homebrew-versions$/i).returns criteria
- criteria.expects(:only).with(:_id, :name, :sha, :updated_at).returns [ repo ]
+ criteria.expects(:only).with(:_id, :letters, :name, :sha, :updated_at).returns [ repo ]
controller.expects(:params).returns({ repository_id: 'Homebrew/Homebrew-versions' })
controller.expects(:redirect_to).with 'http://braumeister.org/repos/Homebrew/homebrew-versions/browse'
@@ -61,7 +61,7 @@
it 'raises Mongoid::Errors::DocumentNotFound if no repository is found' do
criteria = mock
Repository.expects(:where).with(_id: /^Homebrew\/unknown$/i).returns criteria
- criteria.expects(:only).with(:_id, :name, :sha, :updated_at).returns []
+ criteria.expects(:only).with(:_id, :letters, :name, :sha, :updated_at).returns []
controller.expects(:params).returns({ repository_id: 'Homebrew/unknown' })
expect { controller.send :select_repository }.to raise_error(Mongoid::Errors::DocumentNotFound)

0 comments on commit dfdaa42

Please sign in to comment.