elucid-merb_activerecord 1.1.0.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Generators +4 -0
- data/LICENSE +20 -0
- data/README +7 -0
- data/Rakefile +55 -0
- data/TODO +5 -0
- data/lib/active_record/merbtasks.rb +342 -0
- data/lib/generators/migration.rb +4 -0
- data/lib/generators/model.rb +8 -0
- data/lib/generators/resource_controller.rb +12 -0
- data/lib/generators/session_migration.rb +4 -0
- data/lib/generators/templates/migration/schema/migrations/%file_name%.rb +19 -0
- data/lib/generators/templates/model/app/models/%file_name%.rb +4 -0
- data/lib/generators/templates/resource_controller/app/controllers/%file_name%.rb +64 -0
- data/lib/generators/templates/resource_controller/app/views/%file_name%/edit.html.erb +3 -0
- data/lib/generators/templates/resource_controller/app/views/%file_name%/index.html.erb +3 -0
- data/lib/generators/templates/resource_controller/app/views/%file_name%/new.html.erb +3 -0
- data/lib/generators/templates/resource_controller/app/views/%file_name%/show.html.erb +3 -0
- data/lib/generators/templates/session_migration/schema/migrations/%version%_database_sessions.rb +15 -0
- data/lib/merb/orms/active_record/cleanup.rb +6 -0
- data/lib/merb/orms/active_record/connection.rb +77 -0
- data/lib/merb/orms/active_record/database.yml.sample +21 -0
- data/lib/merb/session/active_record_session.rb +65 -0
- data/lib/merb_activerecord.rb +41 -0
- data/lib/merb_activerecord/version.rb +5 -0
- metadata +142 -0
data/Generators
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Duane Johnson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
merb_active_record
|
2
|
+
=================
|
3
|
+
|
4
|
+
A plugin for the Merb framework that provides active record access
|
5
|
+
|
6
|
+
After you install the plugin, you have access to the ar_migration generator in your merb projects
|
7
|
+
example: /merb/root/script/generate ar_migration new_migration
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
# Load this library's version information
|
5
|
+
require File.expand_path('../lib/merb_activerecord/version', __FILE__)
|
6
|
+
|
7
|
+
begin
|
8
|
+
|
9
|
+
gem 'jeweler', '~> 1.4'
|
10
|
+
require 'jeweler'
|
11
|
+
|
12
|
+
Jeweler::Tasks.new do |gemspec|
|
13
|
+
|
14
|
+
gemspec.version = Merb::ActiveRecord::VERSION
|
15
|
+
|
16
|
+
gemspec.name = "elucid-merb_activerecord"
|
17
|
+
gemspec.description = "Merb plugin that provides ActiveRecord support"
|
18
|
+
gemspec.summary = "This plugin allows seamless integration of the ActiveRecord ORM with merb"
|
19
|
+
|
20
|
+
gemspec.authors = [ "Duane Johnson" ]
|
21
|
+
gemspec.email = "canadaduane@gmail.com"
|
22
|
+
gemspec.homepage = "http://github.com/merb/merb_activerecord"
|
23
|
+
|
24
|
+
gemspec.files = %w(Generators LICENSE Rakefile README TODO) + Dir['{lib,spec}/**/*']
|
25
|
+
|
26
|
+
# Runtime dependencies
|
27
|
+
gemspec.add_dependency 'merb-core', '~> 1.1.1'
|
28
|
+
gemspec.add_dependency 'activerecord', '>= 2.3.2'
|
29
|
+
|
30
|
+
# Development dependencies
|
31
|
+
gemspec.add_development_dependency 'rspec', '>= 1.2.9'
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
Jeweler::GemcutterTasks.new
|
36
|
+
|
37
|
+
rescue LoadError
|
38
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
39
|
+
end
|
40
|
+
|
41
|
+
require 'spec/rake/spectask'
|
42
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
43
|
+
spec.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
|
44
|
+
spec.libs << 'lib' << 'spec'
|
45
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
46
|
+
end
|
47
|
+
|
48
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
49
|
+
spec.libs << 'lib' << 'spec'
|
50
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
51
|
+
spec.rcov = true
|
52
|
+
end
|
53
|
+
|
54
|
+
desc 'Default: run spec examples'
|
55
|
+
task :default => 'spec'
|
data/TODO
ADDED
@@ -0,0 +1,342 @@
|
|
1
|
+
namespace :db do
|
2
|
+
|
3
|
+
task :merb_start do
|
4
|
+
Merb.start :adapter => 'runner', :environment => ENV['MERB_ENV'] || 'development', :log_file => Merb::Config[:log_file]
|
5
|
+
end
|
6
|
+
|
7
|
+
namespace :create do
|
8
|
+
desc 'Create all the local databases defined in config/database.yml'
|
9
|
+
task :all => :merb_start do
|
10
|
+
Merb::Orms::ActiveRecord.configurations.each_value do |config|
|
11
|
+
create_local_database(config)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Create the local database defined in config/database.yml for the current Merb.environment'
|
17
|
+
task :create => :merb_start do
|
18
|
+
create_local_database(Merb::Orms::ActiveRecord.configurations[Merb.environment.to_sym])
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_local_database(config)
|
22
|
+
# Only connect to local databases
|
23
|
+
if config[:host] == 'localhost' || config[:host].blank?
|
24
|
+
begin
|
25
|
+
ActiveRecord::Base.establish_connection(config)
|
26
|
+
ActiveRecord::Base.connection
|
27
|
+
rescue
|
28
|
+
case config[:adapter]
|
29
|
+
when 'mysql'
|
30
|
+
@charset = ENV['CHARSET'] || 'utf8'
|
31
|
+
@collation = ENV['COLLATION'] || 'utf8_general_ci'
|
32
|
+
begin
|
33
|
+
ActiveRecord::Base.establish_connection(config.merge({:database => nil}))
|
34
|
+
ActiveRecord::Base.connection.create_database(config[:database], {:charset => (config[:charset] || @charset), :collation => (config[:collation] || @collation)})
|
35
|
+
ActiveRecord::Base.establish_connection(config)
|
36
|
+
puts "MySQL #{config[:database]} database successfully created"
|
37
|
+
rescue
|
38
|
+
$stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config[:charset] || @charset}, collation: #{config[:collation] || @collation} (if you set the charset manually, make sure you have a matching collation)"
|
39
|
+
end
|
40
|
+
when 'postgresql'
|
41
|
+
`createdb "#{config[:database]}" -E utf8`
|
42
|
+
when 'sqlite'
|
43
|
+
`sqlite "#{config[:database]}"`
|
44
|
+
when 'sqlite3'
|
45
|
+
`sqlite3 "#{config[:database]}"`
|
46
|
+
end
|
47
|
+
else
|
48
|
+
puts "#{config[:database]} already exists"
|
49
|
+
end
|
50
|
+
else
|
51
|
+
puts "This task only creates local databases. #{config[:database]} is on a remote host."
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def drop_database(config)
|
56
|
+
begin
|
57
|
+
ActiveRecord::Base.establish_connection(config)
|
58
|
+
ActiveRecord::Base.connection
|
59
|
+
rescue
|
60
|
+
puts "could not connect to #{config[:database]}, database not dropped"
|
61
|
+
else
|
62
|
+
case config[:adapter]
|
63
|
+
when 'mysql'
|
64
|
+
ActiveRecord::Base.connection.drop_database config[:database]
|
65
|
+
when /^sqlite/
|
66
|
+
FileUtils.rm(File.join(Merb.root, config[:database]))
|
67
|
+
when 'postgresql'
|
68
|
+
ActiveRecord::Base.clear_active_connections!
|
69
|
+
`dropdb "#{config[:database]}"`
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def local_database?(config, &block)
|
75
|
+
if %w( 127.0.0.1 localhost ).include?(config[:host]) || config[:host].blank?
|
76
|
+
yield
|
77
|
+
else
|
78
|
+
puts "This task only modifies local databases. #{config[:database]} is on a remote host."
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
namespace :drop do
|
83
|
+
desc 'Drops all the local databases defined in config/database.yml'
|
84
|
+
task :all => :merb_start do
|
85
|
+
Merb::Orms::ActiveRecord.configurations.each_value do |config|
|
86
|
+
# Skip entries that don't have a database key
|
87
|
+
next unless config[:database]
|
88
|
+
# Only connect to local databases
|
89
|
+
local_database?(config) { drop_database(config) }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
desc 'Drops the database for the current environment (set MERB_ENV to target another environment)'
|
95
|
+
task :drop => :merb_start do
|
96
|
+
config = Merb::Orms::ActiveRecord.configurations[Merb.environment.to_sym]
|
97
|
+
begin
|
98
|
+
drop_database(config)
|
99
|
+
rescue Exception => e
|
100
|
+
puts "#{e.inspect} - #{config['database']} might have been already dropped"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
desc "Migrate the database through scripts in schema/migrations. Target specific version with VERSION=x"
|
105
|
+
task :migrate => :merb_start do
|
106
|
+
config = Merb::Orms::ActiveRecord.configurations[Merb.environment.to_sym]
|
107
|
+
ActiveRecord::Base.establish_connection(config)
|
108
|
+
ActiveRecord::Migrator.migrate("schema/migrations/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
109
|
+
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
110
|
+
end
|
111
|
+
|
112
|
+
namespace :migrate do
|
113
|
+
desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x'
|
114
|
+
task :redo => [ 'db:rollback', 'db:migrate' ]
|
115
|
+
|
116
|
+
desc 'Resets your database using your migrations for the current environment'
|
117
|
+
task :reset => ["db:drop", "db:create", "db:migrate"]
|
118
|
+
end
|
119
|
+
|
120
|
+
desc 'Drops and recreates the database from db/schema.rb for the current environment.'
|
121
|
+
task :reset => ['db:drop', 'db:create', 'db:schema:load']
|
122
|
+
|
123
|
+
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
|
124
|
+
task :rollback => :merb_start do
|
125
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
126
|
+
version = ActiveRecord::Migrator.current_version - step
|
127
|
+
ActiveRecord::Migrator.migrate('schema/migrations/', version)
|
128
|
+
end
|
129
|
+
|
130
|
+
desc "Raises an error if there are pending migrations"
|
131
|
+
task :abort_if_pending_migrations => :merb_start do
|
132
|
+
if defined? ActiveRecord
|
133
|
+
pending_migrations = ActiveRecord::Migrator.new(:up, 'schema/migrations').pending_migrations
|
134
|
+
|
135
|
+
if pending_migrations.any?
|
136
|
+
puts "You have #{pending_migrations.size} pending migrations:"
|
137
|
+
pending_migrations.each do |pending_migration|
|
138
|
+
puts ' %4d %s' % [pending_migration.version, pending_migration.name]
|
139
|
+
end
|
140
|
+
abort "Run `rake db:migrate` to update your database then try again."
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
desc "Retrieves the charset for the current environment's database"
|
146
|
+
task :charset => :merb_start do
|
147
|
+
config = Merb::Orms::ActiveRecord.configurations[Merb.environment.to_sym]
|
148
|
+
case config[:adapter]
|
149
|
+
when 'mysql'
|
150
|
+
ActiveRecord::Base.establish_connection(config)
|
151
|
+
puts ActiveRecord::Base.connection.charset
|
152
|
+
else
|
153
|
+
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
desc "Retrieves the collation for the current environment's database"
|
158
|
+
task :collation => :merb_start do
|
159
|
+
config = Merb::Orms::ActiveRecord.configurations[Merb.environment.to_sym]
|
160
|
+
case config[:adapter]
|
161
|
+
when 'mysql'
|
162
|
+
ActiveRecord::Base.establish_connection(config)
|
163
|
+
puts ActiveRecord::Base.connection.collation
|
164
|
+
else
|
165
|
+
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
desc "Retrieves the current schema version number"
|
170
|
+
task :version => :merb_start do
|
171
|
+
puts "Current version: #{ActiveRecord::Migrator.current_version}"
|
172
|
+
end
|
173
|
+
|
174
|
+
namespace :fixtures do
|
175
|
+
desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y"
|
176
|
+
task :load => :merb_start do
|
177
|
+
require 'active_record/fixtures'
|
178
|
+
config = Merb::Orms::ActiveRecord.configurations[Merb.environment.to_sym]
|
179
|
+
ActiveRecord::Base.establish_connection(config)
|
180
|
+
test_folder = (Merb.test_framework_generator_scope == :rspec) ? 'spec' : 'test'
|
181
|
+
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(Merb.root, test_folder, 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
|
182
|
+
Fixtures.create_fixtures(File.join(test_folder, 'fixtures'), File.basename(fixture_file, '.*'))
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
namespace :schema do
|
188
|
+
desc 'Create a schema/schema.rb file that can be portably used against any DB supported by AR'
|
189
|
+
task :dump => :merb_start do
|
190
|
+
require 'active_record/schema_dumper'
|
191
|
+
File.open(ENV['SCHEMA'] || "schema/schema.rb", "w") do |file|
|
192
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
desc "Load a schema.rb file into the database"
|
197
|
+
task :load => :merb_start do
|
198
|
+
config = Merb::Orms::ActiveRecord.configurations[Merb.environment.to_sym]
|
199
|
+
ActiveRecord::Base.establish_connection(config)
|
200
|
+
file = ENV['SCHEMA'] || "schema/schema.rb"
|
201
|
+
load(file)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
namespace :structure do
|
206
|
+
desc "Dump the database structure to a SQL file"
|
207
|
+
task :dump => :merb_start do
|
208
|
+
config = Merb::Orms::ActiveRecord.configurations[Merb.environment.to_sym]
|
209
|
+
case config[:adapter]
|
210
|
+
when "mysql", "oci", "oracle"
|
211
|
+
ActiveRecord::Base.establish_connection(config)
|
212
|
+
File.open("schema/#{Merb.environment}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
|
213
|
+
when "postgresql"
|
214
|
+
ENV['PGHOST'] = config[:host] if config[:host]
|
215
|
+
ENV['PGPORT'] = config[:port].to_s if config[:port]
|
216
|
+
ENV['PGPASSWORD'] = config[:password].to_s if config[:password]
|
217
|
+
search_path = config[:schema_search_path]
|
218
|
+
search_path = "--schema=#{search_path}" if search_path
|
219
|
+
`pg_dump -i -U "#{config[:username]}" -s -x -O -f schema/#{Merb.environment}_structure.sql #{search_path} #{config[:database]}`
|
220
|
+
raise "Error dumping database" if $?.exitstatus == 1
|
221
|
+
when "sqlite", "sqlite3"
|
222
|
+
dbfile = config[:database] || config[:dbfile]
|
223
|
+
`#{config[:adapter]} #{dbfile} .schema > schema/#{Merb.environment}_structure.sql`
|
224
|
+
when "sqlserver"
|
225
|
+
`scptxfr /s #{config[:host]} /d #{config[:database]} /I /f schema\\#{Merb.environment}_structure.sql /q /A /r`
|
226
|
+
`scptxfr /s #{config[:host]} /d #{config[:database]} /I /F schema\ /q /A /r`
|
227
|
+
when "firebird"
|
228
|
+
set_firebird_env(config)
|
229
|
+
db_string = firebird_db_string(config)
|
230
|
+
sh "isql -a #{db_string} > schema/#{Merb.environment}_structure.sql"
|
231
|
+
else
|
232
|
+
raise "Task not supported by '#{config[:adapter]}'"
|
233
|
+
end
|
234
|
+
|
235
|
+
if ActiveRecord::Base.connection.supports_migrations?
|
236
|
+
File.open("schema/#{Merb.environment}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
namespace :test do
|
242
|
+
|
243
|
+
desc "Recreate the test database from the current environment's database schema"
|
244
|
+
task :clone => %w(db:schema:dump db:test:purge) do
|
245
|
+
ActiveRecord::Base.establish_connection(Merb::Orms::ActiveRecord.configurations[:test])
|
246
|
+
ActiveRecord::Schema.verbose = false
|
247
|
+
Merb.environment = 'test'
|
248
|
+
Rake::Task["db:schema:load"].invoke
|
249
|
+
end
|
250
|
+
|
251
|
+
desc "Recreate the test databases from the development structure"
|
252
|
+
task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
|
253
|
+
config = Merb::Orms::ActiveRecord.configurations[:test]
|
254
|
+
case config[:adapter]
|
255
|
+
when "mysql"
|
256
|
+
ActiveRecord::Base.establish_connection(config)
|
257
|
+
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
|
258
|
+
IO.readlines("schema/#{Merb.environment}_structure.sql").join.split("\n\n").each do |table|
|
259
|
+
ActiveRecord::Base.connection.execute(table)
|
260
|
+
end
|
261
|
+
when "postgresql"
|
262
|
+
ENV['PGHOST'] = config[:host] if config[:host]
|
263
|
+
ENV['PGPORT'] = config[:port].to_s if config[:port]
|
264
|
+
ENV['PGPASSWORD'] = config[:password].to_s if config[:password]
|
265
|
+
`psql -U "#{config[:username]}" -f schema/#{Merb.environment}_structure.sql #{config[:database]}`
|
266
|
+
when "sqlite", "sqlite3"
|
267
|
+
dbfile = config[:database] ||config[:dbfile]
|
268
|
+
`#{config[:adapter]} #{dbfile} < schema/#{Merb.environment}_structure.sql`
|
269
|
+
when "sqlserver"
|
270
|
+
`osql -E -S #{config[:host]} -d #{config[:database]} -i schema\\#{Merb.environment}_structure.sql`
|
271
|
+
when "oci", "oracle"
|
272
|
+
ActiveRecord::Base.establish_connection(:test)
|
273
|
+
IO.readlines("schema/#{Merb.environment}_structure.sql").join.split(";\n\n").each do |ddl|
|
274
|
+
ActiveRecord::Base.connection.execute(ddl)
|
275
|
+
end
|
276
|
+
when "firebird"
|
277
|
+
set_firebird_env(config)
|
278
|
+
db_string = firebird_db_string(config)
|
279
|
+
sh "isql -i schema/#{Merb.environment}_structure.sql #{db_string}"
|
280
|
+
else
|
281
|
+
raise "Task not supported by '#{config[:adapter]}'"
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
desc "Empty the test database"
|
286
|
+
task :purge => :merb_start do
|
287
|
+
config = Merb::Orms::ActiveRecord.configurations[:test]
|
288
|
+
puts "config => #{config.inspect}"
|
289
|
+
case config[:adapter]
|
290
|
+
when "mysql"
|
291
|
+
ActiveRecord::Base.establish_connection(config)
|
292
|
+
ActiveRecord::Base.connection.recreate_database(config[:database])
|
293
|
+
when "postgresql"
|
294
|
+
ENV['PGHOST'] = config[:host] if config[:host]
|
295
|
+
ENV['PGPORT'] = config[:port].to_s if config[:port]
|
296
|
+
ENV['PGPASSWORD'] = config[:password].to_s if config[:password]
|
297
|
+
enc_option = "-E #{config[:encoding]}" if config[:encoding]
|
298
|
+
ActiveRecord::Base.clear_active_connections!
|
299
|
+
`dropdb -U "#{config[:username]}" #{config[:database]}`
|
300
|
+
`createdb #{enc_option} -U "#{config[:username]}" #{config[:database]}`
|
301
|
+
when "sqlite","sqlite3"
|
302
|
+
dbfile = config[:database] || config[:dbfile]
|
303
|
+
File.delete(dbfile) if File.exist?(dbfile)
|
304
|
+
when "sqlserver"
|
305
|
+
config ActiveRecord::Base.establish_connection(:test)
|
306
|
+
ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
|
307
|
+
ActiveRecord::Base.connection.execute(ddl)
|
308
|
+
end
|
309
|
+
when "firebird"
|
310
|
+
ActiveRecord::Base.establish_connection(:test)
|
311
|
+
ActiveRecord::Base.connection.recreate_database!
|
312
|
+
else
|
313
|
+
raise "Task not supported by '#{config[:adapter]}'"
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
desc "Prepare the test database and load the schema"
|
318
|
+
task :prepare do
|
319
|
+
if defined?(ActiveRecord::Base) && !Merb::Orms::ActiveRecord.configurations.blank?
|
320
|
+
Rake::Task[{ :sql => "db:test:clone_structure", :ruby => "db:test:clone" }[ActiveRecord::Base.schema_format]].invoke
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
namespace :sessions do
|
326
|
+
desc "Clear the sessions table"
|
327
|
+
task :clear => :merb_start do
|
328
|
+
session_table = 'session'
|
329
|
+
session_table = Inflector.pluralize(session_table) if ActiveRecord::Base.pluralize_table_names
|
330
|
+
ActiveRecord::Base.connection.execute "DELETE FROM #{session_table}"
|
331
|
+
end
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
def set_firebird_env(config)
|
336
|
+
ENV["ISC_USER"] = config["username"].to_s if config["username"]
|
337
|
+
ENV["ISC_PASSWORD"] = config["password"].to_s if config["password"]
|
338
|
+
end
|
339
|
+
|
340
|
+
def firebird_db_string(config)
|
341
|
+
FireRuby::Database.db_string_for(config.symbolize_keys)
|
342
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
Merb::Generators::ModelGenerator.template :model_activerecord, :orm => :activerecord do |t|
|
2
|
+
t.source = File.dirname(__FILE__) / "templates/model/app/models/%file_name%.rb"
|
3
|
+
t.destination = "app/models" / base_path / "#{file_name}.rb"
|
4
|
+
end
|
5
|
+
|
6
|
+
Merb::Generators::ModelGenerator.invoke :migration, :orm => :activerecord do |generator|
|
7
|
+
generator.new(destination_root, options.merge(:model => true), file_name, attributes)
|
8
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
Merb::Generators::ResourceControllerGenerator.template :controller_activerecord, :orm => :activerecord do |t|
|
2
|
+
t.source = File.dirname(__FILE__) / "templates/resource_controller/app/controllers/%file_name%.rb"
|
3
|
+
t.destination = "app/controllers" / base_path / "#{file_name}.rb"
|
4
|
+
end
|
5
|
+
|
6
|
+
[:index, :show, :edit, :new].each do |view|
|
7
|
+
Merb::Generators::ResourceControllerGenerator.template "view_#{view}_activerecord".to_sym,
|
8
|
+
:orm => :activerecord, :template_engine => :erb do |t|
|
9
|
+
t.source = File.dirname(__FILE__) / "templates/resource_controller/app/views/%file_name%/#{view}.html.erb"
|
10
|
+
t.destination = "app/views" / base_path / "#{file_name}/#{view}.html.erb"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,4 @@
|
|
1
|
+
Merb::Generators::SessionMigrationGenerator.template :session_migration_activerecord, :orm => :activerecord do |t|
|
2
|
+
t.source = File.dirname(__FILE__) / 'templates/session_migration/schema/migrations/%version%_database_sessions.rb'
|
3
|
+
t.destination = "schema/migrations/#{version}_database_sessions.rb"
|
4
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class <%= class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
<% if model -%>
|
4
|
+
create_table :<%= table_name %> do |t|
|
5
|
+
<% attributes.each do |type, name| -%>
|
6
|
+
t.<%= name %> :<%= type %>
|
7
|
+
<% end -%>
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
<% end -%>
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
<% if model -%>
|
16
|
+
drop_table :<%= table_name %>
|
17
|
+
<% end -%>
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class <%= class_name %> < Application
|
2
|
+
# provides :xml, :yaml, :js
|
3
|
+
|
4
|
+
# GET /<%= resource_path %>
|
5
|
+
def index
|
6
|
+
@<%= plural_model %> = <%= model_class_name %>.find(:all)
|
7
|
+
display @<%= plural_model %>
|
8
|
+
end
|
9
|
+
|
10
|
+
# GET /<%= resource_path %>/:id
|
11
|
+
def show
|
12
|
+
@<%= singular_model %> = <%= model_class_name %>.find_by_id(params[:id])
|
13
|
+
raise NotFound unless @<%= singular_model %>
|
14
|
+
display @<%= singular_model %>
|
15
|
+
end
|
16
|
+
|
17
|
+
# GET /<%= resource_path %>/new
|
18
|
+
def new
|
19
|
+
only_provides :html
|
20
|
+
@<%= singular_model %> = <%= model_class_name %>.new(params[:<%= singular_model %>])
|
21
|
+
render
|
22
|
+
end
|
23
|
+
|
24
|
+
# POST /<%= resource_path %>
|
25
|
+
def create
|
26
|
+
@<%= singular_model %> = <%= model_class_name %>.new(params[:<%= singular_model %>])
|
27
|
+
if @<%= singular_model %>.save
|
28
|
+
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
|
29
|
+
else
|
30
|
+
render :new
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# GET /<%= resource_path %>/:id/edit
|
35
|
+
def edit
|
36
|
+
only_provides :html
|
37
|
+
@<%= singular_model %> = <%= model_class_name %>.find_by_id(params[:id])
|
38
|
+
raise NotFound unless @<%= singular_model %>
|
39
|
+
render
|
40
|
+
end
|
41
|
+
|
42
|
+
# PUT /<%= resource_path %>/:id
|
43
|
+
def update
|
44
|
+
@<%= singular_model %> = <%= model_class_name %>.find_by_id(params[:id])
|
45
|
+
raise NotFound unless @<%= singular_model %>
|
46
|
+
if @<%= singular_model %>.update_attributes(params[:<%= singular_model %>])
|
47
|
+
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
|
48
|
+
else
|
49
|
+
raise BadRequest
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# DELETE /<%= resource_path %>/:id
|
54
|
+
def destroy
|
55
|
+
@<%= singular_model %> = <%= model_class_name %>.find_by_id(params[:id])
|
56
|
+
raise NotFound unless @<%= singular_model %>
|
57
|
+
if @<%= singular_model %>.destroy
|
58
|
+
redirect url(:<%= (modules.collect{|m| m.downcase} << plural_model).join("_") %>)
|
59
|
+
else
|
60
|
+
raise BadRequest
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
data/lib/generators/templates/session_migration/schema/migrations/%version%_database_sessions.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
class DatabaseSessions < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :sessions do |t|
|
4
|
+
t.column :session_id, :string
|
5
|
+
t.column :data, :text
|
6
|
+
t.column :created_at, :datetime
|
7
|
+
end
|
8
|
+
add_index :sessions, :session_id
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
remove_index :sessions, :session_id
|
13
|
+
drop_table :sessions
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module Merb
|
4
|
+
module Orms
|
5
|
+
module ActiveRecord
|
6
|
+
|
7
|
+
# Start a transaction.
|
8
|
+
#
|
9
|
+
# Used by Merb::Rack::Console#open_sandbox!
|
10
|
+
def self.open_sandbox!
|
11
|
+
::ActiveRecord::Base.send :increment_open_transactions
|
12
|
+
::ActiveRecord::Base.connection.begin_db_transaction
|
13
|
+
end
|
14
|
+
|
15
|
+
# Rollback a transaction.
|
16
|
+
#
|
17
|
+
# Used by Merb::Rack::Console#close_sandbox!
|
18
|
+
def self.close_sandbox!
|
19
|
+
::ActiveRecord::Base.connection.rollback_db_transaction
|
20
|
+
::ActiveRecord::Base.send :decrement_open_transactions
|
21
|
+
end
|
22
|
+
|
23
|
+
class << self
|
24
|
+
def config_file() Merb.dir_for(:config) / "database.yml" end
|
25
|
+
def sample_dest() Merb.dir_for(:config) / "database.yml.sample" end
|
26
|
+
def sample_source() File.dirname(__FILE__) / "database.yml.sample" end
|
27
|
+
|
28
|
+
def copy_sample_config
|
29
|
+
FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
|
30
|
+
end
|
31
|
+
|
32
|
+
def config
|
33
|
+
#If Merb#runs_like specifies a differnt db env, use it.
|
34
|
+
env_sym = (Merb.environment_info.nil?) ?
|
35
|
+
Merb.environment.to_sym :
|
36
|
+
Merb.environment_info[:db_env].to_sym
|
37
|
+
|
38
|
+
raise ArgumentError, "missing environment :#{Merb.environment} in config file #{config_file}" unless configurations.key?(env_sym)
|
39
|
+
@config ||= (Merb::Plugins.config[:merb_active_record] = configurations[env_sym])
|
40
|
+
end
|
41
|
+
|
42
|
+
def configurations
|
43
|
+
@configurations ||=
|
44
|
+
begin
|
45
|
+
#A proc that will recursively intern(a.k.a symbolize) the keys of the hash
|
46
|
+
intern_keys = lambda { |x|
|
47
|
+
x.inject({}) do |y, (k,v)|
|
48
|
+
y[k.to_sym || k] = v.is_a?(Hash) ? intern_keys.call(v) : v
|
49
|
+
y
|
50
|
+
end
|
51
|
+
}
|
52
|
+
intern_keys.call(Erubis.load_yaml_file(config_file))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Database connects as soon as the gem is loaded
|
57
|
+
def connect
|
58
|
+
if File.exists?(config_file)
|
59
|
+
Merb.logger.info!("Connecting to database...")
|
60
|
+
|
61
|
+
Thread.new{ loop{ sleep(60*60); ::ActiveRecord::Base.verify_active_connections! } }
|
62
|
+
|
63
|
+
::ActiveRecord::Base.logger = Merb.logger
|
64
|
+
::ActiveRecord::Base.configurations = configurations
|
65
|
+
::ActiveRecord::Base.establish_connection config
|
66
|
+
else
|
67
|
+
copy_sample_config
|
68
|
+
Merb.logger.error! "No database.yml file found in #{Merb.root}/config."
|
69
|
+
Merb.logger.error! "A sample file was created called database.yml.sample for you to copy and edit."
|
70
|
+
exit(1)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
---
|
2
|
+
# This is a sample database file for the ActiveRecord ORM
|
3
|
+
:development: &defaults
|
4
|
+
:adapter: mysql
|
5
|
+
:database: sample_development
|
6
|
+
:username: the_user
|
7
|
+
:password: secrets
|
8
|
+
:host: localhost
|
9
|
+
:socket: /tmp/mysql.sock
|
10
|
+
:encoding: utf8
|
11
|
+
|
12
|
+
:test:
|
13
|
+
<<: *defaults
|
14
|
+
:database: sample_test
|
15
|
+
|
16
|
+
:production:
|
17
|
+
<<: *defaults
|
18
|
+
:database: sample_production
|
19
|
+
|
20
|
+
:rake:
|
21
|
+
<<: *defaults
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'merb-core/dispatch/session'
|
2
|
+
require 'active_record'
|
3
|
+
require 'base64'
|
4
|
+
|
5
|
+
module Merb
|
6
|
+
|
7
|
+
# Sessions stored in ActiveRecord model.
|
8
|
+
#
|
9
|
+
# To use ActiveRecord based sessions add the following to config/init.rb:
|
10
|
+
#
|
11
|
+
# Merb::Config[:session_store] = 'activerecord'
|
12
|
+
|
13
|
+
class ActiveRecordSessionStore < ::ActiveRecord::Base
|
14
|
+
|
15
|
+
table_name = (Merb::Plugins.config[:merb_active_record][:session_table_name] || "sessions")
|
16
|
+
|
17
|
+
set_table_name table_name
|
18
|
+
|
19
|
+
serialize :data
|
20
|
+
|
21
|
+
class << self
|
22
|
+
|
23
|
+
# ==== Parameters
|
24
|
+
# session_id<String>:: ID of the session to retrieve.
|
25
|
+
#
|
26
|
+
# ==== Returns
|
27
|
+
# ContainerSession:: The session corresponding to the ID.
|
28
|
+
def retrieve_session(session_id)
|
29
|
+
if item = find_by_session_id(session_id)
|
30
|
+
item.data
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# ==== Parameters
|
35
|
+
# session_id<String>:: ID of the session to set.
|
36
|
+
# data<ContainerSession>:: The session to set.
|
37
|
+
def store_session(session_id, data)
|
38
|
+
if item = find_by_session_id(session_id)
|
39
|
+
item.update_attributes!(:data => data)
|
40
|
+
else
|
41
|
+
create(:session_id => session_id, :data => data)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# ==== Parameters
|
46
|
+
# session_id<String>:: ID of the session to delete.
|
47
|
+
def delete_session(session_id)
|
48
|
+
delete_all(["#{connection.quote_column_name('session_id')} IN (?)", session_id])
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
class ActiveRecordSession < SessionStoreContainer
|
56
|
+
|
57
|
+
# The session store type
|
58
|
+
self.session_store_type = :activerecord
|
59
|
+
|
60
|
+
# The store object is the model class itself
|
61
|
+
self.store = ActiveRecordSessionStore
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
if defined?(Merb::Plugins)
|
2
|
+
|
3
|
+
dependency "activerecord" unless defined?(ActiveRecord)
|
4
|
+
|
5
|
+
require File.join(File.dirname(__FILE__) / "merb" / "orms" / "active_record" / "cleanup" )
|
6
|
+
require File.join(File.dirname(__FILE__) / "merb" / "orms" / "active_record" / "connection")
|
7
|
+
Merb::Plugins.add_rakefiles(File.join(File.dirname(__FILE__) / "active_record" / "merbtasks"))
|
8
|
+
|
9
|
+
class Merb::Orms::ActiveRecord::Connect < Merb::BootLoader
|
10
|
+
after BeforeAppLoads
|
11
|
+
|
12
|
+
def self.run
|
13
|
+
Merb::Orms::ActiveRecord.connect
|
14
|
+
if Merb::Config.session_stores.include?(:activerecord)
|
15
|
+
Merb.logger.debug "Using ActiveRecord sessions"
|
16
|
+
require File.join(File.dirname(__FILE__) / "merb" / "session" / "active_record_session")
|
17
|
+
end
|
18
|
+
# The default identify is :id instead of :to_param so that the identify
|
19
|
+
# can be used as the default resource key
|
20
|
+
Merb::Router.root_behavior = Merb::Router.root_behavior.identify(ActiveRecord::Base => :id)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class Merb::Orms::ActiveRecord::DisconnectBeforeFork < Merb::BootLoader
|
26
|
+
after AfterAppLoads
|
27
|
+
|
28
|
+
def self.run
|
29
|
+
Merb.logger.debug "Disconnecting database connection before forking."
|
30
|
+
::ActiveRecord::Base.clear_active_connections!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
generators = File.join(File.dirname(__FILE__), 'generators')
|
36
|
+
Merb.add_generators generators / :migration
|
37
|
+
Merb.add_generators generators / :model
|
38
|
+
Merb.add_generators generators / :resource_controller
|
39
|
+
Merb.add_generators generators / :session_migration
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: elucid-merb_activerecord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 961915996
|
5
|
+
prerelease: true
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
- pre
|
11
|
+
version: 1.1.0.pre
|
12
|
+
platform: ruby
|
13
|
+
authors:
|
14
|
+
- Duane Johnson
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-10-22 00:00:00 -04:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: merb-core
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 17
|
31
|
+
segments:
|
32
|
+
- 1
|
33
|
+
- 1
|
34
|
+
- 1
|
35
|
+
version: 1.1.1
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: activerecord
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 7
|
47
|
+
segments:
|
48
|
+
- 2
|
49
|
+
- 3
|
50
|
+
- 2
|
51
|
+
version: 2.3.2
|
52
|
+
type: :runtime
|
53
|
+
version_requirements: *id002
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: rspec
|
56
|
+
prerelease: false
|
57
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 13
|
63
|
+
segments:
|
64
|
+
- 1
|
65
|
+
- 2
|
66
|
+
- 9
|
67
|
+
version: 1.2.9
|
68
|
+
type: :development
|
69
|
+
version_requirements: *id003
|
70
|
+
description: Merb plugin that provides ActiveRecord support
|
71
|
+
email: canadaduane@gmail.com
|
72
|
+
executables: []
|
73
|
+
|
74
|
+
extensions: []
|
75
|
+
|
76
|
+
extra_rdoc_files:
|
77
|
+
- LICENSE
|
78
|
+
- README
|
79
|
+
- TODO
|
80
|
+
files:
|
81
|
+
- Generators
|
82
|
+
- LICENSE
|
83
|
+
- README
|
84
|
+
- Rakefile
|
85
|
+
- TODO
|
86
|
+
- lib/active_record/merbtasks.rb
|
87
|
+
- lib/generators/migration.rb
|
88
|
+
- lib/generators/model.rb
|
89
|
+
- lib/generators/resource_controller.rb
|
90
|
+
- lib/generators/session_migration.rb
|
91
|
+
- lib/generators/templates/migration/schema/migrations/%file_name%.rb
|
92
|
+
- lib/generators/templates/model/app/models/%file_name%.rb
|
93
|
+
- lib/generators/templates/resource_controller/app/controllers/%file_name%.rb
|
94
|
+
- lib/generators/templates/resource_controller/app/views/%file_name%/edit.html.erb
|
95
|
+
- lib/generators/templates/resource_controller/app/views/%file_name%/index.html.erb
|
96
|
+
- lib/generators/templates/resource_controller/app/views/%file_name%/new.html.erb
|
97
|
+
- lib/generators/templates/resource_controller/app/views/%file_name%/show.html.erb
|
98
|
+
- lib/generators/templates/session_migration/schema/migrations/%version%_database_sessions.rb
|
99
|
+
- lib/merb/orms/active_record/cleanup.rb
|
100
|
+
- lib/merb/orms/active_record/connection.rb
|
101
|
+
- lib/merb/orms/active_record/database.yml.sample
|
102
|
+
- lib/merb/session/active_record_session.rb
|
103
|
+
- lib/merb_activerecord.rb
|
104
|
+
- lib/merb_activerecord/version.rb
|
105
|
+
has_rdoc: true
|
106
|
+
homepage: http://github.com/merb/merb_activerecord
|
107
|
+
licenses: []
|
108
|
+
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options:
|
111
|
+
- --charset=UTF-8
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
hash: 3
|
120
|
+
segments:
|
121
|
+
- 0
|
122
|
+
version: "0"
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ">"
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
hash: 25
|
129
|
+
segments:
|
130
|
+
- 1
|
131
|
+
- 3
|
132
|
+
- 1
|
133
|
+
version: 1.3.1
|
134
|
+
requirements: []
|
135
|
+
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.3.7
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: This plugin allows seamless integration of the ActiveRecord ORM with merb
|
141
|
+
test_files: []
|
142
|
+
|