Commit 78ebbb225eea809d0db196e68699ef6eb0af5fbc

Authored by AntonioTerceiro
1 parent 4bb9f71a

ActionItem265: we are going to need some functions in the database to georeferencing plugins


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1642 3f533792-8f58-4932-b0fe-aaf55b0a4547
config/environment.rb
... ... @@ -91,6 +91,9 @@ Tag.hierarchical = true
91 91 # several local libraries
92 92 require 'noosfero'
93 93  
  94 +# sqlite extensions to able to test/develop georeference with sqlite
  95 +require 'sqlite_extension'
  96 +
94 97 # locally-developed modules
95 98 require 'acts_as_filesystem'
96 99 require 'acts_as_searchable'
... ...
lib/sqlite_extension.rb 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i
  2 +
  3 + database = ActiveRecord::Base.connection.raw_connection
  4 +
  5 + database.create_function('sin', 1, :numeric) do |func, value|
  6 + func.set_result(Math.sin(value))
  7 + end
  8 +
  9 + database.create_function('cos', 1, :numeric) do |func, value|
  10 + func.set_result(Math.cos(value))
  11 + end
  12 +
  13 + database.create_function('pow', 2, :numeric) do |func, base, exponent|
  14 + func.set_result(base.to_f ** exponent.to_f)
  15 + end
  16 +
  17 +end
... ...
test/unit/sqlite_extension_test.rb 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +# if this test is run without SQLite (e.g. with mysql or postgres), the tests
  4 +# will just pass. The idea is to test our local extensions to SQLite.
  5 +class SQliteExtensionTest < Test::Unit::TestCase
  6 +
  7 + should 'have sine function' do
  8 + assert_in_delta 0.0, ActiveRecord::Base.connection.execute('select sin(3.14159265358979) as sin').first['sin'], 0.0001
  9 + end
  10 +
  11 + should 'have cosine function' do
  12 + assert_in_delta -1.0, ActiveRecord::Base.connection.execute('select cos(3.14159265358979) as cos').first['cos'], 0.0001
  13 + end
  14 +
  15 + should 'have power function' do
  16 + assert_in_delta 8.0, ActiveRecord::Base.connection.execute('select pow(2.0, 3.0) as result').first['result'], 0.0001
  17 + end
  18 +
  19 +end
... ...