Skip to content
This repository was archived by the owner on May 21, 2024. It is now read-only.

Commit 33d377d

Browse files
committed
initial checkin to git
0 parents  commit 33d377d

33 files changed

+1098
-0
lines changed

CHANGELOG

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
0.1.0 - Released Dec 27, 2006
2+
* Initial release
3+
4+
0.5.0 - Released Dec 29, 2006
5+
* Added support for PostgreSQL (Michael Schuerig)
6+
* Fixed the schema dumper
7+
8+
0.5.1 - Released Jan 10, 2007
9+
* Patch by Clifford T. Matthews to use String.dump to dump out the view select statement
10+
11+
0.6.0 - Released May 9, 2007
12+
* Added support for SQL Server (Seth Ladd)-
13+
* Added support for using views to map non-friendly database field names to AR-friendly names (Nathan Vack)
14+
* Added support for Oracle (Alistair Davidson)
15+
16+
0.6.1 - Released June 6, 2007
17+
* Added test for union support
18+
* Updated tests to include new table to support union test

CONTRIB

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The following people have kindly contributed to improve Rails SQL Views:
2+
3+
Anthony Eden
4+
Michael Schuerig
5+
Clifford T. Matthews
6+
Seth Ladd
7+
8+
So if you see them in the halls of your local Ruby or Rails gatherings please remember to say Hi and Thanks. ;-)

README

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
== Rails SQL Views
2+
3+
Library which adds SQL Views to Rails. Adds create_view and drop_view to the ActiveRecord::ConnectionAdapters::AbstractAdapter (which makes them available to migrations) and adds support for dumping views in the ActiveRecord::SchemaDumper.
4+
5+
== Installation
6+
7+
To install:
8+
9+
gem install rails_sql_views
10+
11+
Then add the following to your Rails config/environment.rb:
12+
13+
require_gem 'rails_sql_views'
14+
require 'rails_sql_views'
15+
16+
== Usage
17+
18+
You can then use create_view and drop_view in your migrations. For example:
19+
20+
class CreatePersonView < ActiveRecord::Migration
21+
def self.up
22+
create_view :v_people, "select * from people" do |t|
23+
t.column :id
24+
t.column :name
25+
t.column :social_security
26+
end
27+
end
28+
29+
def self.down
30+
drop_view :v_people
31+
end
32+
end
33+
34+
This extension also adds support for views in the ActiveRecord::SchemaDumper class.
35+
36+
The following drivers are supported:
37+
38+
MySQL
39+
PostgreSQL (Native and Pure Ruby)
40+
Oracle
41+
SQL Server
42+
43+
== Known Issues
44+
45+
* Drivers not mentioned above are not supported.
46+
47+
If you find any issues please send an email to [email protected] .
48+
49+
== Contributing
50+
51+
If you would like to implement view support for other adapters then please drop me an email. Better yet, write up the adapter modifications and send them to me. :-)

Rakefile

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
require 'rake'
2+
require 'rake/testtask'
3+
require 'rake/rdoctask'
4+
require 'rake/packagetask'
5+
require 'rake/gempackagetask'
6+
require 'rake/contrib/rubyforgepublisher'
7+
8+
require File.join(File.dirname(__FILE__), 'lib/rails_sql_views', 'version')
9+
10+
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
11+
PKG_NAME = 'rails_sql_views'
12+
PKG_VERSION = RailsSqlViews::VERSION::STRING + PKG_BUILD
13+
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
14+
PKG_DESTINATION = ENV["PKG_DESTINATION"] || "../#{PKG_NAME}"
15+
16+
RELEASE_NAME = "REL #{PKG_VERSION}"
17+
18+
RUBY_FORGE_PROJECT = "activewarehouse"
19+
RUBY_FORGE_USER = "aeden"
20+
21+
desc 'Default: run unit tests.'
22+
task :default => :test
23+
24+
desc 'Test the library.'
25+
Rake::TestTask.new(:test) do |t|
26+
t.libs << 'lib'
27+
t.pattern = 'test/**/*_test.rb'
28+
t.verbose = true
29+
end
30+
31+
namespace :rcov do
32+
desc 'Measures test coverage'
33+
task :test do
34+
rm_f 'coverage.data'
35+
mkdir 'coverage' unless File.exist?('coverage')
36+
rcov = "rcov --aggregate coverage.data --text-summary -Ilib"
37+
system("#{rcov} test/*_test.rb test/**/*_test.rb")
38+
system("open coverage/index.html") if PLATFORM['darwin']
39+
end
40+
end
41+
42+
desc 'Generate documentation library.'
43+
Rake::RDocTask.new(:rdoc) do |rdoc|
44+
rdoc.rdoc_dir = 'rdoc'
45+
rdoc.title = 'Rails SQL Views'
46+
rdoc.options << '--line-numbers' << '--inline-source'
47+
rdoc.rdoc_files.include('README')
48+
rdoc.rdoc_files.include('lib/**/*.rb')
49+
end
50+
51+
PKG_FILES = FileList[
52+
'CHANGELOG',
53+
'README',
54+
'Rakefile',
55+
'bin/**/*',
56+
'lib/**/*',
57+
] - [ 'test' ]
58+
59+
spec = Gem::Specification.new do |s|
60+
s.name = 'rails_sql_views'
61+
s.version = PKG_VERSION
62+
s.summary = "Adds SQL Views to Rails."
63+
s.description = <<-EOF
64+
Library which adds SQL Views to Rails.
65+
EOF
66+
67+
s.add_dependency('activerecord', '>= 1.14.4')
68+
s.add_dependency('rake', '>= 0.7.1')
69+
70+
s.rdoc_options << '--exclude' << '.'
71+
s.has_rdoc = false
72+
73+
s.files = PKG_FILES.to_a.delete_if {|f| f.include?('.svn')}
74+
s.require_path = 'lib'
75+
76+
s.author = "Anthony Eden"
77+
s.email = "[email protected]"
78+
s.homepage = "http://activewarehouse.rubyforge.org/rails_sql_views"
79+
s.rubyforge_project = "activewarehouse"
80+
end
81+
82+
Rake::GemPackageTask.new(spec) do |pkg|
83+
pkg.gem_spec = spec
84+
pkg.need_tar = true
85+
pkg.need_zip = true
86+
end
87+
88+
desc "Generate code statistics"
89+
task :lines do
90+
lines, codelines, total_lines, total_codelines = 0, 0, 0, 0
91+
92+
for file_name in FileList["lib/**/*.rb"]
93+
next if file_name =~ /vendor/
94+
f = File.open(file_name)
95+
96+
while line = f.gets
97+
lines += 1
98+
next if line =~ /^\s*$/
99+
next if line =~ /^\s*#/
100+
codelines += 1
101+
end
102+
puts "L: #{sprintf("%4d", lines)}, LOC #{sprintf("%4d", codelines)} | #{file_name}"
103+
104+
total_lines += lines
105+
total_codelines += codelines
106+
107+
lines, codelines = 0, 0
108+
end
109+
110+
puts "Total: Lines #{total_lines}, LOC #{total_codelines}"
111+
end
112+
113+
desc "Publish the release files to RubyForge."
114+
task :release => [ :package ] do
115+
`rubyforge login`
116+
117+
for ext in %w( gem tgz zip )
118+
release_command = "rubyforge add_release activewarehouse #{PKG_NAME} 'REL #{PKG_VERSION}' pkg/#{PKG_NAME}-#{PKG_VERSION}.#{ext}"
119+
puts release_command
120+
system(release_command)
121+
end
122+
end
123+
124+
desc "Publish the API documentation"
125+
task :pdoc => [:rdoc] do
126+
Rake::SshDirPublisher.new("[email protected]", "/var/www/gforge-projects/activewarehouse/rails_sql_views/rdoc", "rdoc").upload
127+
end

init.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'rails_sql_views'

lib/core_ext/module.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This is required for 1.1.6 support
2+
class Module
3+
def alias_method_chain(target, feature)
4+
# Strip out punctuation on predicates or bang methods since
5+
# e.g. target?_without_feature is not a valid method name.
6+
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
7+
yield(aliased_target, punctuation) if block_given?
8+
alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target
9+
alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
10+
end
11+
end

lib/rails_sql_views.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#--
2+
# Copyright (c) 2006 Anthony Eden
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining
5+
# a copy of this software and associated documentation files (the
6+
# "Software"), to deal in the Software without restriction, including
7+
# without limitation the rights to use, copy, modify, merge, publish,
8+
# distribute, sublicense, and/or sell copies of the Software, and to
9+
# permit persons to whom the Software is furnished to do so, subject to
10+
# the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be
13+
# included in all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
#++
23+
24+
$:.unshift(File.dirname(__FILE__))
25+
26+
require 'rubygems'
27+
unless Kernel.respond_to?(:gem)
28+
Kernel.send :alias_method, :gem, :require_gem
29+
end
30+
31+
unless defined?(ActiveRecord)
32+
begin
33+
$:.unshift(File.dirname(__FILE__) + "/../../activerecord/lib")
34+
require 'active_record'
35+
rescue LoadError
36+
gem 'activerecord'
37+
end
38+
end
39+
40+
require 'core_ext/module'
41+
42+
require 'rails_sql_views/connection_adapters/abstract/schema_definitions'
43+
require 'rails_sql_views/connection_adapters/abstract/schema_statements'
44+
require 'rails_sql_views/connection_adapters/mysql_adapter'
45+
require 'rails_sql_views/connection_adapters/postgresql_adapter'
46+
require 'rails_sql_views/connection_adapters/sqlserver_adapter'
47+
require 'rails_sql_views/schema_dumper'
48+
49+
class ActiveRecord::ConnectionAdapters::AbstractAdapter
50+
include RailsSqlViews::ConnectionAdapters::SchemaStatements
51+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module RailsSqlViews
2+
module ConnectionAdapters #:nodoc:
3+
# Abstract definition of a View
4+
class ViewDefinition
5+
attr_accessor :columns, :select_query
6+
7+
def initialize(base, select_query)
8+
@columns = []
9+
@base = base
10+
@select_query = select_query
11+
end
12+
13+
def column(name)
14+
column = name.to_s
15+
@columns << column unless @columns.include? column
16+
self
17+
end
18+
19+
def to_sql
20+
@columns * ', '
21+
end
22+
23+
end
24+
25+
class MappingDefinition
26+
27+
# Generates a hash of the form :old_column => :new_column
28+
# Initially, it'll map column names to themselves.
29+
# use map_column to modify the list.
30+
def initialize(columns)
31+
@columns = columns
32+
@map = Hash.new()
33+
columns.each do |c|
34+
@map[c] = c
35+
end
36+
37+
end
38+
39+
# Create a mapping from an old column name to a new one.
40+
# If the new name is nil, specify that the old column shouldn't
41+
# appear in this new view.
42+
def map_column(old_name, new_name)
43+
unless @map.include?(old_name)
44+
raise ActiveRecord::ActiveRecordError, "column #{old_name} not found, can't be mapped"
45+
end
46+
if new_name.nil?
47+
@map.delete old_name
48+
@columns.delete old_name
49+
else
50+
@map[old_name] = new_name
51+
end
52+
end
53+
54+
def select_cols
55+
@columns
56+
end
57+
58+
def view_cols
59+
@columns.map { |c| @map[c] }
60+
end
61+
end
62+
end
63+
end

0 commit comments

Comments
 (0)