Skip to content

Commit 0d501fb

Browse files
committed
Merge pull request #15 from januszm/rails_4_1
Compatibility with ActiveRecord 4.1 and Rgeo 1.1
2 parents 60cf7e0 + 79c826d commit 0d501fb

File tree

12 files changed

+99
-39
lines changed

12 files changed

+99
-39
lines changed

Gemfile

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@
3636

3737
source "http://rubygems.org"
3838

39-
gem('rgeo-activerecord', '~> 0.4.5')
40-
gem('mysql2', '>= 0.2.13')
41-
#gem('activerecord', '~> 3.0.16')
42-
#gem('activerecord', '~> 3.1.7')
43-
gem('activerecord', '~> 3.2.7')
39+
gem 'rgeo-activerecord', github: 'rgeo/rgeo-activerecord', branch: '1.0'
40+
gem 'mysql2', '>= 0.2.13'
41+
gem 'activerecord', '>= 4.0', '< 4.2'
4442

45-
group(:test) do
46-
gem('rake', '>= 0.9.2')
47-
gem('rdoc', '>= 3.12')
43+
group :test do
44+
gem 'rake', '>= 0.9.2'
45+
gem 'rdoc', '>= 3.12'
46+
gem 'pry'
4847
end

History.rdoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
=== 0.5.0 / 2015-08-06
2+
3+
* Compatibility with ActiveRecord >= 4.0, < 4.2 and with rgeo-activerecord 1.x.
4+
15
=== 0.4.3 / 2012-08-02
26

37
* The gemspec no longer includes the timestamp in the version, so that bundler can pull from github. (Reported by corneverbruggen)
@@ -14,7 +18,7 @@
1418

1519
=== 0.4.0 / 2011-08-15
1620

17-
* Various fixes for Rails 3.1 compatibility.
21+
* Various fixes for Rails 3.1 compatibility.
1822
* Now requires rgeo-activerecord 0.4.0.
1923
* INCOMPATIBLE CHANGE: simple queries (e.g. MyClass.where(:latlon => my_point)) use an objective rather than spatial equality test. Earlier versions transformed this form to use st_equals, but now if you need to test for spatial equality, you'll need to call st_equals explicitly. I'm still evaluating which direction we want to go with this in the future, but we may be stuck with the current behavior because the hack required to transform these queries to use spatial equality was egregious and broke in Rails 3.1 with no clear workaround.
2024

README.rdoc

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
The MySQL2 Spatial \ActiveRecord Adapter is an \ActiveRecord connection
44
adapter based on the standard mysql2 adapter. It extends the standard
55
adapter to provide support for spatial columns and indexes in MySQL,
6-
using the {RGeo}[http://github.com/dazuma/rgeo] library to represent
6+
using the {RGeo}[http://github.com/rgeo/rgeo] library to represent
77
spatial data in Ruby. Like the standard mysql2 adapter, this adapter
88
requires the mysql2 gem.
99

1010
== What This Adapter Provides
1111

1212
=== Spatial Migrations
1313

14-
First, this adapter extends the migration syntax to support creating
14+
First, this adapter extends the migration syntax to support creating
1515
spatial columns and indexes. To create a spatial column, use the
1616
<tt>:geometry</tt> type, or any of the OGC spatial types such as
1717
<tt>:point</tt> or <tt>:line_string</tt>. To create a spatial index, set
1818
the <tt>:spatial</tt> option to true. Remember that, on some versions of
1919
MySQL, only the MyISAM engine supports spatial indexes, and the indexed
2020
column may need to be NOT NULL.
2121

22-
Examples:
22+
Examples (require update):
2323

2424
create_table :my_spatial_table, :options => 'ENGINE=MyISAM' do |t|
2525
t.column :latlon, :point, :null => false
@@ -55,13 +55,13 @@ the "rgeo-activerecord" gem.
5555
Examples, given the spatial table defined above:
5656

5757
class MySpatialTable < ActiveRecord::Base
58-
58+
5959
# By default, use the GEOS implementation for spatial columns.
6060
self.rgeo_factory_generator = RGeo::Geos.method(:factory)
61-
61+
6262
# But use a geographic implementation for the :latlon column.
6363
set_rgeo_factory_for_column(:latlon, RGeo::Geographic.spherical_factory)
64-
64+
6565
end
6666

6767
Now you can interact with the data using the RGeo types:
@@ -98,13 +98,13 @@ write more complex queries in SQL.
9898

9999
This adapter has the following requirements:
100100

101-
* Ruby 1.8.7 or later. Ruby 1.9.2 or later preferred.
101+
* Ruby 1.9.3 or later. Ruby 2.0.0 or later preferred.
102102
* MySQL server 5.0 or later required for spatial extensions.
103-
* \ActiveRecord 3.0.3 or later. Earlier versions will not work.
104-
Should be compatible with Rails versions through 3.2.x.
103+
* \ActiveRecord 4.0.0 or later. Earlier versions will not work.
104+
Should be compatible with Rails versions through 4.0.x-4.1.x.
105105
* mysql2 gem 0.2.13 or later.
106106
* rgeo gem 0.3.15 or later.
107-
* rgeo-activerecord gem 0.4.5 or later.
107+
* rgeo-activerecord gem 1.x.
108108

109109
Install this adapter as a gem:
110110

@@ -149,18 +149,16 @@ Rails as of Rails 3.0.3, so we hope it will get rectified at some point.
149149

150150
=== Development and support
151151

152-
Documentation is available at http://dazuma.github.com/activerecord-mysql2spatial-adapter/rdoc
152+
Documentation is available at http://rgeo.github.com/activerecord-mysql2spatial-adapter/rdoc
153153

154-
Source code is hosted on Github at http://github.com/dazuma/activerecord-mysql2spatial-adapter
154+
Source code is hosted on Github at http://github.com/rgeo/activerecord-mysql2spatial-adapter
155155

156156
Contributions are welcome. Fork the project on Github.
157157

158-
Report bugs on Github issues at http://github.org/dazuma/activerecord-mysql2spatial-adapter/issues
158+
Report bugs on Github issues at http://github.org/rgeo/activerecord-mysql2spatial-adapter/issues
159159

160160
Support available on the rgeo-users google group at http://groups.google.com/group/rgeo-users
161161

162-
Contact the author at dazuma at gmail dot com.
163-
164162
=== Acknowledgments
165163

166164
The Mysql2Spatial Adapter and its supporting libraries (including RGeo)

Rakefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ RAKEFILE_CONFIG = {} unless defined?(::RAKEFILE_CONFIG)
4444
# Gemspec
4545

4646
require 'rubygems'
47+
require 'pry'
4748
gemspec_ = eval(::File.read(::Dir.glob('*.gemspec').first))
4849
release_gemspec_ = eval(::File.read(::Dir.glob('*.gemspec').first))
4950
release_gemspec_.version = gemspec_.version.to_s.sub(/\.nonrelease$/, '')
@@ -68,6 +69,8 @@ platform_suffix_ =
6869
'mri18'
6970
elsif ::RUBY_VERSION =~ /^1\.9\..*$/
7071
'mri19'
72+
elsif ::RUBY_VERSION =~ /^2\..*$/
73+
'mri2'
7174
else
7275
raise "Unknown version of Matz Ruby Interpreter (#{::RUBY_VERSION})"
7376
end

Version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.4.3
1+
0.5.0

activerecord-mysql2spatial-adapter.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@
4242
s_.email = 'dazuma@gmail.com'
4343
s_.homepage = "http://dazuma.github.com/activerecord-mysql2spatial-adapter"
4444
s_.rubyforge_project = 'virtuoso'
45-
s_.required_ruby_version = '>= 1.8.7'
45+
s_.required_ruby_version = '>= 1.9.3'
4646
s_.files = ::Dir.glob("lib/**/*.rb") +
4747
::Dir.glob("test/**/*.rb") +
4848
::Dir.glob("*.rdoc") +
4949
['Version']
5050
s_.extra_rdoc_files = ::Dir.glob("*.rdoc")
5151
s_.test_files = ::Dir.glob("test/**/tc_*.rb")
5252
s_.platform = ::Gem::Platform::RUBY
53-
s_.add_dependency('rgeo-activerecord', '~> 0.4.5')
53+
s_.add_dependency('rgeo-activerecord', '~> 1.0')
5454
s_.add_dependency('mysql2', '>= 0.2.13')
5555
end

lib/active_record/connection_adapters/mysql2spatial_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module Mysql2SpatialAdapter
8484

8585

8686
require 'active_record/connection_adapters/mysql2spatial_adapter/version.rb'
87+
require 'active_record/connection_adapters/mysql2spatial_adapter/column_methods.rb' # check if this works with Rails < 4.x
8788
require 'active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb'
8889
require 'active_record/connection_adapters/mysql2spatial_adapter/spatial_column.rb'
8990
require 'active_record/connection_adapters/mysql2spatial_adapter/arel_tosql.rb'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module ActiveRecord
2+
module ConnectionAdapters
3+
module Mysql2SpatialAdapter
4+
module ColumnMethods
5+
def spatial(name, options = {})
6+
raise "You must set a type. For example: 't.spatial limit: { type: 'point' }'" unless options[:limit][:type]
7+
column(name, options[:limit][:type], options)
8+
end
9+
10+
def geography(name, options = {})
11+
column(name, :geography, options)
12+
end
13+
14+
def geometry(name, options = {})
15+
column(name, :geometry, options)
16+
end
17+
18+
def geometry_collection(name, options = {})
19+
column(name, :geometry_collection, options)
20+
end
21+
22+
def line_string(name, options = {})
23+
column(name, :line_string, options)
24+
end
25+
26+
def multi_line_string(name, options = {})
27+
column(name, :multi_line_string, options)
28+
end
29+
30+
def multi_point(name, options = {})
31+
column(name, :multi_point, options)
32+
end
33+
34+
def multi_polygon(name, options = {})
35+
column(name, :multi_polygon, options)
36+
end
37+
38+
def point(name, options = {})
39+
column(name, :point, options)
40+
end
41+
end
42+
43+
ConnectionAdapters::TableDefinition.send(:include, ColumnMethods)
44+
end
45+
end
46+
end

lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module Mysql2SpatialAdapter
4646
class MainAdapter < ConnectionAdapters::Mysql2Adapter
4747

4848

49-
NATIVE_DATABASE_TYPES = Mysql2Adapter::NATIVE_DATABASE_TYPES.merge(:spatial => {:name => "geometry"})
49+
NATIVE_DATABASE_TYPES = Mysql2Adapter::NATIVE_DATABASE_TYPES.merge(spatial: { name: 'geometry' })
5050

5151

5252
def initialize(*args_)
@@ -120,7 +120,7 @@ def columns(table_name_, name_=nil)
120120
columns_
121121
end
122122

123-
123+
# Returns an array of indexes for the given table.
124124
def indexes(table_name_, name_=nil)
125125
indexes_ = []
126126
current_index_ = nil
@@ -129,11 +129,20 @@ def indexes(table_name_, name_=nil)
129129
if current_index_ != row_[:Key_name]
130130
next if row_[:Key_name] == 'PRIMARY' # skip the primary key
131131
current_index_ = row_[:Key_name]
132-
indexes_ << ::RGeo::ActiveRecord::SpatialIndexDefinition.new(row_[:Table], row_[:Key_name], row_[:Non_unique] == 0, [], [], row_[:Index_type] == 'SPATIAL')
132+
mysql_index_type = row_[:Index_type].downcase.to_sym
133+
index_type = INDEX_TYPES.include?(mysql_index_type) ? mysql_index_type : nil
134+
index_using = INDEX_USINGS.include?(mysql_index_type) ? mysql_index_type : nil
135+
options = [row_[:Table], row_[:Key_name], row_[:Non_unique].to_i == 0, [], [], nil, nil, index_type, index_using]
136+
indexes_ << if mysql_index_type == :spatial
137+
options.push(true)
138+
::RGeo::ActiveRecord::SpatialIndexDefinition.new(*options)
139+
else
140+
IndexDefinition.new(*options)
141+
end
133142
end
134143
last_index_ = indexes_.last
135144
last_index_.columns << row_[:Column_name]
136-
last_index_.lengths << row_[:Sub_part] unless last_index_.spatial
145+
last_index_.lengths << row_[:Sub_part] unless mysql_index_type == :spatial
137146
end
138147
indexes_
139148
end

lib/active_record/connection_adapters/mysql2spatial_adapter/spatial_column.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def initialize(factory_settings_, table_name_, name_, default_, sql_type_=nil, n
6060
super(name_, default_,sql_type_, null_)
6161
@geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name(sql_type_)
6262
if type == :spatial
63-
@limit = {:type => @geometric_type.type_name.underscore}
63+
@limit = { type: @geometric_type.type_name.underscore }
6464
end
6565
FACTORY_SETTINGS_CACHE[factory_settings_.object_id] = factory_settings_
6666
end

0 commit comments

Comments
 (0)