Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 155 lines (126 sloc) 3.701 kb
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
1 # See #ActiveRecord::Tableless
2
3 module ActiveRecord
6467087 Code revision, renaming of project
Michal Zima authored
4
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
5 # = ActiveRecord::Tableless
6467087 Code revision, renaming of project
Michal Zima authored
6 #
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
7 # Allow classes to behave like ActiveRecord models, but without an associated
8 # database table. A great way to capitalize on validations. Based on the
9 # original post at http://www.railsweenie.com/forums/2/topics/724 (which seems
10 # to have disappeared from the face of the earth).
6467087 Code revision, renaming of project
Michal Zima authored
11 #
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
12 # = Example usage
6467087 Code revision, renaming of project
Michal Zima authored
13 #
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
14 # class ContactMessage < ActiveRecord::Base
6467087 Code revision, renaming of project
Michal Zima authored
15 #
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
16 # has_no_table
6467087 Code revision, renaming of project
Michal Zima authored
17 #
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
18 # column :name, :string
19 # column :email, :string
20 # column :message, :string
6467087 Code revision, renaming of project
Michal Zima authored
21 #
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
22 # end
6467087 Code revision, renaming of project
Michal Zima authored
23 #
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
24 # msg = ContactMessage.new( params[:msg] )
25 # if msg.valid?
26 # ContactMessageSender.deliver_message( msg )
27 # redirect_to :action => :sent
28 # end
29 #
30 module Tableless
6467087 Code revision, renaming of project
Michal Zima authored
31
32 def self.included(base) #:nodoc:
33 base.send :extend, ActsMethods
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
34 end
6467087 Code revision, renaming of project
Michal Zima authored
35
3222a7a @wagenet Query string methods
wagenet authored
36 module ActsMethods #:nodoc:
6467087 Code revision, renaming of project
Michal Zima authored
37
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
38 # A model that needs to be tableless will call this method to indicate
39 # it.
40 def has_no_table
41 # keep our options handy
42 write_inheritable_attribute(
43 :tableless_options,
44 :columns => []
45 )
46 class_inheritable_reader :tableless_options
6467087 Code revision, renaming of project
Michal Zima authored
47
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
48 # extend
6467087 Code revision, renaming of project
Michal Zima authored
49 extend ActiveRecord::Tableless::SingletonMethods
50 extend ActiveRecord::Tableless::ClassMethods
51
2687b2d @wagenet Stubbed some database methods
wagenet authored
52 # include
53 include ActiveRecord::Tableless::InstanceMethods
6467087 Code revision, renaming of project
Michal Zima authored
54
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
55 # setup columns
56 end
6467087 Code revision, renaming of project
Michal Zima authored
57
ced54ee @brendon Added the ability to check if a model is tableless, and allowed for argu...
brendon authored
58 def tableless?
59 false
60 end
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
61 end
6467087 Code revision, renaming of project
Michal Zima authored
62
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
63 module SingletonMethods
6467087 Code revision, renaming of project
Michal Zima authored
64
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
65 # Return the list of columns registered for the model. Used internally by
66 # ActiveRecord
67 def columns
68 tableless_options[:columns]
69 end
6467087 Code revision, renaming of project
Michal Zima authored
70
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
71 # Register a new column.
72 def column(name, sql_type = nil, default = nil, null = true)
73 tableless_options[:columns] << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
74 end
6467087 Code revision, renaming of project
Michal Zima authored
75
5d16059 New method for bulk adding of columns
Michal Zima authored
76 # Register a set of colums with the same SQL type
77 def add_columns(sql_type, *args)
78 args.each do |col|
79 column col, sql_type
80 end
81 end
82
6467087 Code revision, renaming of project
Michal Zima authored
83 %w(find create destroy).each do |m|
84 eval %{
ced54ee @brendon Added the ability to check if a model is tableless, and allowed for argu...
brendon authored
85 def #{m}(*args)
2687b2d @wagenet Stubbed some database methods
wagenet authored
86 logger.warn "Can't #{m} a Tableless object"
87 false
88 end
89 }
90 end
6467087 Code revision, renaming of project
Michal Zima authored
91
ced54ee @brendon Added the ability to check if a model is tableless, and allowed for argu...
brendon authored
92 def tableless?
93 true
94 end
2687b2d @wagenet Stubbed some database methods
wagenet authored
95 end
6467087 Code revision, renaming of project
Michal Zima authored
96
3222a7a @wagenet Query string methods
wagenet authored
97 module ClassMethods
6467087 Code revision, renaming of project
Michal Zima authored
98
3222a7a @wagenet Query string methods
wagenet authored
99 def from_query_string(query_string)
100 unless query_string.blank?
101 params = query_string.split('&').collect do |chunk|
102 next if chunk.empty?
103 key, value = chunk.split('=', 2)
104 next if key.empty?
105 value = value.nil? ? nil : CGI.unescape(value)
106 [ CGI.unescape(key), value ]
107 end.compact.to_h
6467087 Code revision, renaming of project
Michal Zima authored
108
3222a7a @wagenet Query string methods
wagenet authored
109 new(params)
110 else
111 new
112 end
113 end
6467087 Code revision, renaming of project
Michal Zima authored
114
3222a7a @wagenet Query string methods
wagenet authored
115 end
6467087 Code revision, renaming of project
Michal Zima authored
116
2687b2d @wagenet Stubbed some database methods
wagenet authored
117 module InstanceMethods
6467087 Code revision, renaming of project
Michal Zima authored
118
3222a7a @wagenet Query string methods
wagenet authored
119 def to_query_string(prefix = nil)
120 attributes.to_a.collect{|(name,value)| escaped_var_name(name, prefix) + "=" + escape_for_url(value) if value }.compact.join("&")
121 end
6467087 Code revision, renaming of project
Michal Zima authored
122
123 %w(save destroy).each do |m|
124 eval %{
ced54ee @brendon Added the ability to check if a model is tableless, and allowed for argu...
brendon authored
125 def #{m}(*args)
2687b2d @wagenet Stubbed some database methods
wagenet authored
126 logger.warn "Can't #{m} a Tableless object"
127 false
128 end
129 }
130 end
6467087 Code revision, renaming of project
Michal Zima authored
131
3222a7a @wagenet Query string methods
wagenet authored
132 private
6467087 Code revision, renaming of project
Michal Zima authored
133
3222a7a @wagenet Query string methods
wagenet authored
134 def escaped_var_name(name, prefix = nil)
135 prefix ? "#{URI.escape(prefix)}[#{URI.escape(name)}]" : URI.escape(name)
136 end
6467087 Code revision, renaming of project
Michal Zima authored
137
3222a7a @wagenet Query string methods
wagenet authored
138 def escape_for_url(value)
139 case value
140 when true then "1"
141 when false then "0"
142 when nil then ""
143 else URI.escape(value.to_s)
144 end
145 rescue
146 ""
147 end
6467087 Code revision, renaming of project
Michal Zima authored
148
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
149 end
6467087 Code revision, renaming of project
Michal Zima authored
150
1e1aa8d Release candidate for 0.1, rough around the edges
Kenneth Kalmer authored
151 end
152 end
153
6467087 Code revision, renaming of project
Michal Zima authored
154 ActiveRecord::Base.send :include, ActiveRecord::Tableless
Something went wrong with that request. Please try again.