Skip to content

Commit

Permalink
Created 'indices' extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
nelstrom committed Feb 9, 2009
0 parents commit a432013
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
= Indices

This extension adds indices to the database, which should speed up common queries and provide a more snappy response for page rendering.

The extension is targeted at sites running on Radiant up to version 0.6.9. These indices were added to the database in version 0.7 of Radiant, so this extension will be of no use if your site is running on version 0.7.0 or later.

== To install

Place this extension in the vendor/extensions directory of your Radiant project. Then run the Rake task:

rake [production] radiant:extensions:indices:install

== To uninstall

If you are planning on upgrading your site to run on Radiant 0.7.0 or later, it is advised that you first uninstall this extension. To do so, first run the rake task:

rake [production] radiant:extensions:indices:uninstall

Then delete this extension from your vendor extensions directory.
120 changes: 120 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# I think this is the one that should be moved to the extension Rakefile template

# In rails 1.2, plugins aren't available in the path until they're loaded.
# Check to see if the rspec plugin is installed first and require
# it if it is. If not, use the gem version.

# Determine where the RSpec plugin is by loading the boot
unless defined? RADIANT_ROOT
ENV["RAILS_ENV"] = "test"
case
when ENV["RADIANT_ENV_FILE"]
require File.dirname(ENV["RADIANT_ENV_FILE"]) + "/boot"
when File.dirname(__FILE__) =~ %r{vendor/radiant/vendor/extensions}
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../")}/config/boot"
else
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../")}/config/boot"
end
end

require 'rake'
require 'rake/rdoctask'
require 'rake/testtask'

rspec_base = File.expand_path(RADIANT_ROOT + '/vendor/plugins/rspec/lib')
$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
require 'spec/rake/spectask'
# require 'spec/translator'

# Cleanup the RADIANT_ROOT constant so specs will load the environment
Object.send(:remove_const, :RADIANT_ROOT)

extension_root = File.expand_path(File.dirname(__FILE__))

task :default => :spec
task :stats => "spec:statsetup"

desc "Run all specs in spec directory"
Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
end

namespace :spec do
desc "Run all specs in spec directory with RCov"
Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
t.rcov = true
t.rcov_opts = ['--exclude', 'spec', '--rails']
end

desc "Print Specdoc for all specs"
Spec::Rake::SpecTask.new(:doc) do |t|
t.spec_opts = ["--format", "specdoc", "--dry-run"]
t.spec_files = FileList['spec/**/*_spec.rb']
end

[:models, :controllers, :views, :helpers].each do |sub|
desc "Run the specs under spec/#{sub}"
Spec::Rake::SpecTask.new(sub) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
end
end

# Hopefully no one has written their extensions in pre-0.9 style
# desc "Translate specs from pre-0.9 to 0.9 style"
# task :translate do
# translator = ::Spec::Translator.new
# dir = RAILS_ROOT + '/spec'
# translator.translate(dir, dir)
# end

# Setup specs for stats
task :statsetup do
require 'code_statistics'
::STATS_DIRECTORIES << %w(Model\ specs spec/models)
::STATS_DIRECTORIES << %w(View\ specs spec/views)
::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers)
::STATS_DIRECTORIES << %w(Helper\ specs spec/views)
::CodeStatistics::TEST_TYPES << "Model specs"
::CodeStatistics::TEST_TYPES << "View specs"
::CodeStatistics::TEST_TYPES << "Controller specs"
::CodeStatistics::TEST_TYPES << "Helper specs"
::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
end

namespace :db do
namespace :fixtures do
desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y"
task :load => :environment do
require 'active_record/fixtures'
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'spec', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
Fixtures.create_fixtures('spec/fixtures', File.basename(fixture_file, '.*'))
end
end
end
end
end

desc 'Generate documentation for the indices extension.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'IndicesExtension'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

# For extensions that are in transition
desc 'Test the indices extension.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

# Load any custom rakefiles for extension
Dir[File.dirname(__FILE__) + '/tasks/*.rake'].sort.each { |f| require f }
19 changes: 19 additions & 0 deletions db/migrate/001_add_indices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class AddIndices < ActiveRecord::Migration
def self.up
add_index :pages, :class_name, :name => :pages_class_name
add_index :pages, :parent_id, :name => :pages_parent_id
add_index :pages, %w{slug parent_id}, :name => :pages_child_slug
add_index :pages, %w{virtual status_id}, :name => :pages_published

add_index :page_parts, %w{page_id name}, :name => :parts_by_page
end

def self.down
remove_index :page_parts, :name => :parts_by_page

remove_index :pages, :name => :pages_published
remove_index :pages, :name => :pages_child_slug
remove_index :pages, :name => :pages_parent_id
remove_index :pages, :name => :pages_class_name
end
end
5 changes: 5 additions & 0 deletions indices_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class IndicesExtension < Radiant::Extension
version "1.0"
description "Add indices to the database, for a speed boost on SQL queries."
url "http://github.com/nelstrom/radiant-indices-extension"
end
37 changes: 37 additions & 0 deletions lib/tasks/indices_extension_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace :radiant do
namespace :extensions do
namespace :indices do

desc "Runs migrations, to add indices to database."
task :install => :migrate

desc "Runs migrations, to remove indices to database."
task :uninstall => :environment do
require 'radiant/extension_migrator'
IndicesExtension.migrator.migrate(0)
end

desc "Runs the migration of the Indices extension"
task :migrate => :environment do
require 'radiant/extension_migrator'
if ENV["VERSION"]
IndicesExtension.migrator.migrate(ENV["VERSION"].to_i)
else
IndicesExtension.migrator.migrate
end
end

desc "Copies public assets of the Indices to the instance public/ directory."
task :update => :environment do
is_svn_or_dir = proc {|path| path =~ /\.svn/ || File.directory?(path) }
Dir[IndicesExtension.root + "/public/**/*"].reject(&is_svn_or_dir).each do |file|
path = file.sub(IndicesExtension.root, '')
directory = File.dirname(path)
puts "Copying #{path}..."
mkdir_p RAILS_ROOT + directory
cp file, RAILS_ROOT + path
end
end
end
end
end

0 comments on commit a432013

Please sign in to comment.