Skip to content

Commit

Permalink
Added the rest
Browse files Browse the repository at this point in the history
  • Loading branch information
littlefyr committed Aug 12, 2008
0 parents commit f07a289
Show file tree
Hide file tree
Showing 34 changed files with 2,036 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README
@@ -0,0 +1,10 @@
= Location

== Introduction

Location is a plugin that will allow you to create relatively robust list of locations (stores, churches, etc.). This uses the Directory extension by Loren Johnson as a starting point but makes a number of different decisions. First it uses Geokit (http://geokit.rubyforge.org) to handle the heavy lifting. Second, the JavaScript is completely rewritten to use version 2 of the Google Maps API and to make some additional.

== Geokit Modifications

* To get the Geokit libraries to load, I had to modify the requires to use the full path. /geokit/mappable.rb now has require File.dirname(__FILE__) + '/defaults'
* All references to the configuration values have been changed to use Radiant::Config
120 changes: 120 additions & 0 deletions Rakefile
@@ -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 location extension.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'LocationExtension'
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 location 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 }
Binary file added app/.DS_Store
Binary file not shown.
Binary file added app/controllers/.DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions app/controllers/admin/location_controller.rb
@@ -0,0 +1,3 @@
class Admin::LocationController < Admin::AbstractModelController
model_class Location
end
Binary file added app/helpers/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions app/helpers/admin/location_helper.rb
@@ -0,0 +1,2 @@
module Admin::LocationHelper
end
16 changes: 16 additions & 0 deletions app/models/location.rb
@@ -0,0 +1,16 @@
class Location < ActiveRecord::Base
# Associations
belongs_to :created_by, :class_name => 'User'
belongs_to :updated_by, :class_name => 'User'

acts_as_mappable :default_units => :kms
before_validation :geocode_address
private
def geocode_address
unless self.manual_geocode
geo=GeoKit::Geocoders::MultiGeocoder.geocode self.full_address
errors.add(:full_address, "Could not Geocode address") if !geo.success
self.lat, self.lng = geo.lat,geo.lng if geo.success
end
end
end
174 changes: 174 additions & 0 deletions app/models/location_page.rb
@@ -0,0 +1,174 @@
class LocationPage < Page
include Radiant::Taggable
include GeoKit::Geocoders
description %{
TODO: Describe the Location page type
}


desc %{
The root location element.
Retrieves locations based on the supplied parameters. Permitted parameters are:
* *origin* -- The address from which to start searching
* *lat* & *lng* -- The coordinates of the origin from which to start searching. These override the *origign* attribute
* *distance* -- Find all the locations within the supplied distance
* *units* -- The units to use for distance calculations (km or miles)
* *count* -- The number of results to show
* *offset* -- The number of the first result (for pagination)
Attributes:
* *group* -- When supplied, it will show those locations with a matching group value
}
tag "location" do |tag|
#TODO Clone the options rather than modifying the instance so that the tag
# can be reused on the page
unless tag.attr["group"].blank?
@options[:conditions] = {:group => tag.attr[group]}
end

tag.locals.locations = Location.find(:all, @options)
tag.expand
end

desc %{
loops through each of the locations
}
tag "location:each" do |tag|
st = tag.attr.include?('offset') ? tag.attr['offset'].to_i : 0
en = tag.attr.include?('length') ? tag.attr['length'].to_i - st : -1
each_location tag, st, en
end
tag "location:first" do |tag|
st = 0
en = tag.attr.include?('length') ? tag.attr['length'].to_i : 1
each_location tag, st, en
end
tag "location:last" do |tag|
st = tag.attr.include?('length') ? - tag.attr['length'].to_i : -2
en = -1
each_location tag, st, en
end
tag "location:count" do |tag|
tag.locals.locations.length
end
tag "location:each:has_next?" do |tag|
(tag.locals.locations.length - tag.locals.location_index) > 1
end
tag "location:each:index" do |tag|
tag.locals.location_index
end

tag "location:if_distance" do |tag|
if tag.locals.location.respond_to?("distance")
tag.expand
end
end
tag "location:unless_distance" do |tag|
if !tag.locals.location.respond_to?("distance")
tag.expand
end
end
tag "location:distance" do |tag|
tag.locals.location.distance
end

[:full_address, :group, :website_url, :name, :lat, :lng].each do |method|
desc %{ returns the #{method} attribute of the Location}
tag("#{method.to_s}") do |tag|
tag.locals.location.send(method)
end
end

desc %{Allows you to use page tags (such as <r:slug>, <r:title>, etc.) for the page associated with the location.}
tag "location:page" do |tag|
if tag.locals.location.page?
tag.locals.page = Page.find_by_path tag.locals.location.page
tag.expand
end
end
tag "location:unless_page" do |tag|
unless Page.find_by_path tag.locals.location.page
tag.expand
end
end
tag "location:if_page" do |tag|
return unless tag.locals.location.page?
if Page.find_by_path tag.locals.location.page
tag.expand
end
end
tag "location:map" do |tag|
#content = '<script type="text/javascript" src="http://www.google.com/jsapi?key=#{Radiant::Config['geokit.geocoders.google']}"></script>'

end

def process(request,response)
# Parameters
# origin = Address string (if lat and lng are defined they are used preferentially)
# lat = latitude of origin
# lng = longitude of origin
# distance = distance to search within
# units = the units to apply to the distance (km or miles)
# count = the number of results to return
# offset = The result number to start with

@origin = request.parameters["origin"]
@lat = request.parameters["lat"]
@lng = request.parameters["lng"]
@distance = request.parameters["distance"]
@units = request.parameters["units"]
@count = request.parameters["count"]
@offset = request.parameters["offset"]

if @lat.blank? || @lng.blank?
unless @origin.blank?
geocode = MultiGeocoder.geocode(@origin)
logger.debug("geocode: #{geocode}")
@origin_geo = geocode if geocode.success
else
@origin_geo = nil;
end
else
@origin_geo = [@lat.to_f, @lng.to_f]
end

@options = {}
unless @origin_geo.nil?
@options[:origin] = @origin_geo
@options[:order] = "distance"
end
unless @distance.blank?
@options[:within] = @distance
end
unless @units.blank?
@options[:units] = @units
end
unless @count.blank?
@options[:limit] = @count
end
unless @offset.blank?
@options[:offset] = @offset
end

super request, response
end
def cache?
false
end

private
def each_location(tag, first=0, last=-1)
rng = Range.new first, last
offset = ((first < 0) ? tag.locals.location.length : 0) + first
result = []

tag.locals.locations[rng].each_with_index do |item, idx|
tag.locals.location_index = offset + idx
tag.locals.location = item
result << tag.expand
end
result
end
end
Binary file added app/views/.DS_Store
Binary file not shown.
Binary file added app/views/admin/.DS_Store
Binary file not shown.
Binary file added app/views/admin/location/.DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions app/views/admin/location/_location.html.haml
@@ -0,0 +1,7 @@
%tr.node.level-1
%td.page
= link_to image("location", :class => "icon", :alt => 'page-icon', :title => '') + '<span class="title">' + h(location.name) +'</span>', location_edit_url(location)
%small.info
= h location.full_address
%td.remove
%td.remove= link_to image('remove', :alt => 'remove location'), location_remove_url(:id => location)

0 comments on commit f07a289

Please sign in to comment.