From fbfd7b244d3a27e7f1d0f3bd6e1c5e74d21d7bbc Mon Sep 17 00:00:00 2001 From: Josh Nussbaum Date: Thu, 6 Jan 2011 06:19:57 -0500 Subject: [PATCH] initial commit --- .gitignore | 7 ++++ LICENSE | 23 +++++++++++ README.md | 30 ++++++++++++++ Rakefile | 31 ++++++++++++++ config/routes.rb | 3 ++ .../install_generator.rb | 13 ++++++ lib/generators/templates/config/sitemap.rb | 31 ++++++++++++++ lib/spree_sitemap_generator.rb | 37 +++++++++++++++++ lib/spree_sitemap_generator/spree_defaults.rb | 41 +++++++++++++++++++ lib/spree_sitemap_generator_hooks.rb | 3 ++ lib/tasks/spree_sitemap_generator.rake | 5 +++ spec/spec_helper.rb | 31 ++++++++++++++ spree_sitemap_generator.gemspec | 22 ++++++++++ 13 files changed, 277 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Rakefile create mode 100644 config/routes.rb create mode 100644 lib/generators/spree_sitemap_generator/install_generator.rb create mode 100644 lib/generators/templates/config/sitemap.rb create mode 100644 lib/spree_sitemap_generator.rb create mode 100644 lib/spree_sitemap_generator/spree_defaults.rb create mode 100644 lib/spree_sitemap_generator_hooks.rb create mode 100644 lib/tasks/spree_sitemap_generator.rake create mode 100644 spec/spec_helper.rb create mode 100644 spree_sitemap_generator.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2965ef6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +\#* +*~ +.#* +.DS_Store +tmp +*.swp +spec/test_app diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5800638 --- /dev/null +++ b/LICENSE @@ -0,0 +1,23 @@ +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of the Rails Dog LLC nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..de0733b --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +Spree Sitemap Generator +===================== + +Spree sitemap generator is a sitemap generator based on the sitemap_generator gem http://github.com/kjvarga/sitemap_generator. It adheres to the Sitemap 0.9 protocol specification. + +Installation +======= + + gem install spree-sitemap-generator + +Example goes here. + +Setup +====== + + echo "public/sitemap*" >> .gitignore + +Features +===== +- Notifies search engine of new sitemaps (Google, Yahoo, Ask, Bing) +- Compresses sitemaps with gzip +- Provides basic sitemap of a Spree site +- Allows you to easily add additional sitemaps for pages you add to your site + + +Special Thanks +===== + + +Copyright (c) 2010 [name of extension creator], released under the New BSD License diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..0048b67 --- /dev/null +++ b/Rakefile @@ -0,0 +1,31 @@ +require File.expand_path('../../config/application', __FILE__) + +require 'rubygems' +require 'rake' +require 'rake/testtask' +require 'rake/packagetask' +require 'rake/gempackagetask' + +spec = eval(File.read('spree_sitemap_generator.gemspec')) + +Rake::GemPackageTask.new(spec) do |p| + p.gem_spec = spec +end + +desc "Release to gemcutter" +task :release => :package do + require 'rake/gemcutter' + Rake::Gemcutter::Tasks.new(spec).define + Rake::Task['gem:push'].invoke +end + +desc "Default Task" +task :default => [ :spec ] + +require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new + +# require 'cucumber/rake/task' +# Cucumber::Rake::Task.new do |t| +# t.cucumber_opts = %w{--format pretty} +# end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..1c3c3b6 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,3 @@ +Rails.application.routes.draw do + # Add your extension routes here +end diff --git a/lib/generators/spree_sitemap_generator/install_generator.rb b/lib/generators/spree_sitemap_generator/install_generator.rb new file mode 100644 index 0000000..2433893 --- /dev/null +++ b/lib/generators/spree_sitemap_generator/install_generator.rb @@ -0,0 +1,13 @@ +module SpreeSitemapGenerator + module Generators + class InstallGenerator < Rails::Generators::Base + source_root File.expand_path("../../templates", __FILE__) + + desc "Configures your Rails application for use with spree_sitemap_generator" + def copy_config + directory "config" + end + + end + end +end diff --git a/lib/generators/templates/config/sitemap.rb b/lib/generators/templates/config/sitemap.rb new file mode 100644 index 0000000..e4cc3ab --- /dev/null +++ b/lib/generators/templates/config/sitemap.rb @@ -0,0 +1,31 @@ +SitemapGenerator::Sitemap.add_links do |sitemap| + # Put links creation logic here. + # + # The root path '/' and sitemap index file are added automatically. + # Links are added to the Sitemap in the order they are specified. + # + # Usage: sitemap.add(path, options={}) + # (default options are used if you don't specify) + # + # Defaults: :priority => 0.5, :changefreq => 'weekly', + # :lastmod => Time.now, :host => default_host + # + # + # Examples: + # + # Add '/articles' + # + # sitemap.add articles_path, :priority => 0.7, :changefreq => 'daily' + # + # Add individual articles: + # + # Article.find_each do |article| + # sitemap.add article_path(article), :lastmod => article.updated_at + # end + sitemap.add_login + sitemap.add_signup + sitemap.add_account + sitemap.add_password_reset + sitemap.add_taxons + sitemap.add_products +end diff --git a/lib/spree_sitemap_generator.rb b/lib/spree_sitemap_generator.rb new file mode 100644 index 0000000..63da9ee --- /dev/null +++ b/lib/spree_sitemap_generator.rb @@ -0,0 +1,37 @@ +require 'spree_core' +require 'spree_sitemap_generator_hooks' +require 'sitemap_generator' + +module SpreeSitemapGenerator + class Engine < Rails::Engine + + config.autoload_paths += %W(#{config.root}/lib) + + def self.activate + + ActiveRecord::Relation.class_eval do + def last_updated + last_update = order('updated_at DESC').first + last_update.try(:updated_at) + end + end + + ActiveRecord::Base.class_eval do + def self.last_updated + scoped.last_updated + end + end + + SitemapGenerator::Sitemap.default_host = "http://#{Spree::Config[:site_url]}" + + require 'spree_sitemap_generator/spree_defaults' + SitemapGenerator::LinkSet.send :include, SpreeSitemapGenerator::SpreeDefaults + + Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c| + Rails.env.production? ? require(c) : load(c) + end + end + + config.to_prepare &method(:activate).to_proc + end +end diff --git a/lib/spree_sitemap_generator/spree_defaults.rb b/lib/spree_sitemap_generator/spree_defaults.rb new file mode 100644 index 0000000..1107ac5 --- /dev/null +++ b/lib/spree_sitemap_generator/spree_defaults.rb @@ -0,0 +1,41 @@ +module SpreeSitemapGenerator::SpreeDefaults + def default_url_options + {:host => URI.parse(SitemapGenerator::Sitemap.default_host).host} + end + include ::Rails.application.routes.url_helpers + + def add_login(options={}) + add(login_path, options) + end + + def add_signup(options={}) + add(signup_path, options) + end + + def add_account(options={}) + add(account_path, options) + end + + def add_password_reset(options={}) + add(new_password_reset_path, options) + end + + def add_products(options={}) + active_products = Product.active + + add(products_path, options.merge(:lastmod => active_products.last_updated)) + active_products.each do |product| + add(product_path(product), options.merge(:lastmod => product.updated_at)) + end + end + + def add_taxons(options={}) + Taxon.roots.each {|taxon| add_taxon(taxon, options) } + end + + def add_taxon(taxon, options={}) + add(nested_taxons_path(taxon.permalink), options.merge(:lastmod => taxon.products.last_updated)) + taxon.children.each {|child| add_taxon(child, options) } + end +end + diff --git a/lib/spree_sitemap_generator_hooks.rb b/lib/spree_sitemap_generator_hooks.rb new file mode 100644 index 0000000..b831815 --- /dev/null +++ b/lib/spree_sitemap_generator_hooks.rb @@ -0,0 +1,3 @@ +class SpreeSitemapGeneratorHooks < Spree::ThemeSupport::HookListener + # custom hooks go here +end \ No newline at end of file diff --git a/lib/tasks/spree_sitemap_generator.rake b/lib/tasks/spree_sitemap_generator.rake new file mode 100644 index 0000000..ac62c85 --- /dev/null +++ b/lib/tasks/spree_sitemap_generator.rake @@ -0,0 +1,5 @@ +# add custom rake tasks here +desc 'test it' +task :foo do + puts 1 +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..353fdcc --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,31 @@ +# This file is copied to ~/spec when you run 'ruby script/generate rspec' +# from the project root directory. +ENV["RAILS_ENV"] ||= 'test' +require File.expand_path("../../../config/environment", __FILE__) +require 'rspec/rails' +require 'fabrication' + +# Requires supporting files with custom matchers and macros, etc, +# in ./support/ and its subdirectories. +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} + +RSpec.configure do |config| + # == Mock Framework + # + # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: + # + # config.mock_with :mocha + # config.mock_with :flexmock + # config.mock_with :rr + config.mock_with :rspec + + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + #config.include Devise::TestHelpers, :type => :controller + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, comment the following line or assign false + # instead of true. + config.use_transactional_fixtures = true +end + +@configuration ||= AppConfiguration.find_or_create_by_name("Default configuration") diff --git a/spree_sitemap_generator.gemspec b/spree_sitemap_generator.gemspec new file mode 100644 index 0000000..b1cdb08 --- /dev/null +++ b/spree_sitemap_generator.gemspec @@ -0,0 +1,22 @@ +Gem::Specification.new do |s| + s.platform = Gem::Platform::RUBY + s.name = 'spree_sitemap_generator' + s.version = '3.0.2' + s.summary = 'Add gem summary here' + #s.description = 'Add (optional) gem description here' + s.required_ruby_version = '>= 1.8.7' + + # s.author = 'David Heinemeier Hansson' + # s.email = 'david@loudthinking.com' + # s.homepage = 'http://www.rubyonrails.org' + # s.rubyforge_project = 'actionmailer' + + s.files = Dir['CHANGELOG', 'README.md', 'LICENSE', 'lib/**/*', 'app/**/*'] + s.require_path = 'lib' + s.requirements << 'none' + + s.has_rdoc = true + + s.add_dependency('spree_core', '>= 0.30.1') + s.add_dependency('sitemap_generator', '>= 1.3.4') +end