root / trunk / lib / camping / db.rb

Revision 225, 2.4 kB (checked in by zimbatm, 14 months ago)

Working on Sessions and database :

  • FIX: Session table was always created
  • FIX: Sessions weren't stored correctly because of the custom
    non-autoincrement id. Using SQL injection to bypass the forced "id" :-]
    (aka. where opinionated software is not always good)
  • ENHANCEMENT: Added index for sessions's hashid for faster lookup
  • FIX: 'sqlite3_api' was loaded after the connection.
Line 
1class MissingLibrary < Exception #:nodoc: all
2end
3begin
4    require 'active_record'
5rescue LoadError => e
6    raise MissingLibrary, "ActiveRecord could not be loaded (is it installed?): #{e.message}"
7end
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
50module 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
73end
74Camping::S.sub! /autoload\s*:Base\s*,\s*['"]camping\/db['"]/, ""
75Camping::S.sub! /def\s*Y[;\s]*self[;\s]*end/, $AR_EXTRAS
76Object.constants.map{|c|Object.const_get(c)}.each do |c|
77  c::Models.module_eval $AR_EXTRAS if c.respond_to?(:goes)
78end
Note: See TracBrowser for help on using the browser.