| 1 | class MissingLibrary < Exception #:nodoc: all |
|---|
| 2 | end |
|---|
| 3 | begin |
|---|
| 4 | require 'active_record' |
|---|
| 5 | rescue LoadError => e |
|---|
| 6 | raise MissingLibrary, "ActiveRecord could not be loaded (is it installed?): #{e.message}" |
|---|
| 7 | end |
|---|
| 8 | |
|---|
| 9 | $AR_EXTRAS = %{ |
|---|
| 10 | Base = ActiveRecord::Base unless const_defined? :Base |
|---|
| 11 | |
|---|
| 12 | def Y; ActiveRecord::Base.verify_active_connections!; self; end |
|---|
| 13 | |
|---|
| 14 | class SchemaInfo < Base |
|---|
| 15 | end |
|---|
| 16 | |
|---|
| 17 | def self.V(n) |
|---|
| 18 | @final = [n, @final.to_i].max |
|---|
| 19 | m = (@migrations ||= []) |
|---|
| 20 | Class.new(ActiveRecord::Migration) do |
|---|
| 21 | meta_def(:version) { n } |
|---|
| 22 | meta_def(:inherited) { |k| m << k } |
|---|
| 23 | end |
|---|
| 24 | end |
|---|
| 25 | |
|---|
| 26 | def self.create_schema(opts = {}) |
|---|
| 27 | opts[:assume] ||= 0 |
|---|
| 28 | opts[:version] ||= @final |
|---|
| 29 | if @migrations |
|---|
| 30 | unless SchemaInfo.table_exists? |
|---|
| 31 | ActiveRecord::Schema.define do |
|---|
| 32 | create_table SchemaInfo.table_name do |t| |
|---|
| 33 | t.column :version, :float |
|---|
| 34 | end |
|---|
| 35 | end |
|---|
| 36 | end |
|---|
| 37 | |
|---|
| 38 | si = SchemaInfo.find(:first) || SchemaInfo.new(:version => opts[:assume]) |
|---|
| 39 | if si.version < opts[:version] |
|---|
| 40 | @migrations.each do |k| |
|---|
| 41 | k.migrate(:up) if si.version < k.version and k.version <= opts[:version] |
|---|
| 42 | k.migrate(:down) if si.version > k.version and k.version > opts[:version] |
|---|
| 43 | end |
|---|
| 44 | si.update_attributes(:version => opts[:version]) |
|---|
| 45 | end |
|---|
| 46 | end |
|---|
| 47 | end |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | module Camping |
|---|
| 51 | module Models |
|---|
| 52 | A = ActiveRecord |
|---|
| 53 | # Base is an alias for ActiveRecord::Base. The big warning I'm going to give you |
|---|
| 54 | # about this: *Base overloads table_name_prefix.* This means that if you have a |
|---|
| 55 | # model class Blog::Models::Post, it's table name will be <tt>blog_posts</tt>. |
|---|
| 56 | # |
|---|
| 57 | # ActiveRecord is not loaded if you never reference this class. The minute you |
|---|
| 58 | # use the ActiveRecord or Camping::Models::Base class, then the ActiveRecord library |
|---|
| 59 | # is loaded. |
|---|
| 60 | Base = A::Base |
|---|
| 61 | |
|---|
| 62 | # The default prefix for Camping model classes is the topmost module name lowercase |
|---|
| 63 | # and followed with an underscore. |
|---|
| 64 | # |
|---|
| 65 | # Tepee::Models::Page.table_name_prefix |
|---|
| 66 | # #=> "tepee_pages" |
|---|
| 67 | # |
|---|
| 68 | def Base.table_name_prefix |
|---|
| 69 | "#{name[/\w+/]}_".downcase.sub(/^(#{A}|camping)_/i,'') |
|---|
| 70 | end |
|---|
| 71 | module_eval $AR_EXTRAS |
|---|
| 72 | end |
|---|
| 73 | end |
|---|
| 74 | Camping::S.sub! /autoload\s*:Base\s*,\s*['"]camping\/db['"]/, "" |
|---|
| 75 | Camping::S.sub! /def\s*Y[;\s]*self[;\s]*end/, $AR_EXTRAS |
|---|
| 76 | Object.constants.map{|c|Object.const_get(c)}.each do |c| |
|---|
| 77 | c::Models.module_eval $AR_EXTRAS if c.respond_to?(:goes) |
|---|
| 78 | end |
|---|