|
1 |
| -require 'cgi' |
2 |
| -require 'active_record' |
3 |
| - |
4 |
| -require 'activerecord-tablefree/version' |
5 |
| -require 'activerecord-tablefree/cast_type' |
6 |
| -require 'activerecord-tablefree/schema_cache' |
7 |
| -require 'activerecord-tablefree/connection' |
8 |
| -require 'activerecord-tablefree/transaction' |
9 |
| - |
10 |
| -module ActiveRecord |
11 |
| - # = ActiveRecord::Tablefree |
12 |
| - # |
13 |
| - # Allow classes to behave like ActiveRecord models, but without an associated |
14 |
| - # database table. A great way to capitalize on validations. Based on the |
15 |
| - # original post at http://www.railsweenie.com/forums/2/topics/724 (which seems |
16 |
| - # to have disappeared from the face of the earth). |
17 |
| - # |
18 |
| - # = Example usage |
19 |
| - # |
20 |
| - # class ContactMessage < ActiveRecord::Base |
21 |
| - # |
22 |
| - # has_no_table |
23 |
| - # |
24 |
| - # column :name, :string |
25 |
| - # column :email, :string |
26 |
| - # column :message, :string |
27 |
| - # |
28 |
| - # end |
29 |
| - # |
30 |
| - # msg = ContactMessage.new( params[:msg] ) |
31 |
| - # if msg.valid? |
32 |
| - # ContactMessageSender.deliver_message( msg ) |
33 |
| - # redirect_to :action => :sent |
34 |
| - # end |
35 |
| - # |
36 |
| - module Tablefree |
37 |
| - |
38 |
| - class NoDatabase < StandardError; end |
39 |
| - class Unsupported < StandardError; end |
40 |
| - |
41 |
| - def self.included(base) #:nodoc: |
42 |
| - base.send :extend, ActsMethods |
43 |
| - end |
44 |
| - |
45 |
| - module ActsMethods #:nodoc: |
46 |
| - # A model that needs to be tablefree will call this method to indicate |
47 |
| - # it. |
48 |
| - def has_no_table(options = { database: :fail_fast }) |
49 |
| - raise ArgumentError, "Invalid database option '#{options[:database]}'" unless %i[fail_fast pretend_success].member? options[:database] |
50 |
| - # keep our options handy |
51 |
| - class_attribute :tablefree_options |
52 |
| - self.tablefree_options = { |
53 |
| - database: options[:database], |
54 |
| - columns_hash: {} |
55 |
| - } |
56 |
| - |
57 |
| - # extend |
58 |
| - extend ActiveRecord::Tablefree::SingletonMethods |
59 |
| - extend ActiveRecord::Tablefree::ClassMethods |
60 |
| - |
61 |
| - # include |
62 |
| - include ActiveRecord::Tablefree::InstanceMethods |
63 |
| - |
64 |
| - # setup columns |
65 |
| - include ActiveModel::AttributeAssignment |
66 |
| - include ActiveRecord::ModelSchema |
67 |
| - end |
68 |
| - |
69 |
| - def tablefree? |
70 |
| - false |
71 |
| - end |
72 |
| - end |
73 |
| - |
74 |
| - module SingletonMethods |
75 |
| - # Used internally by ActiveRecord 5. This is the special hook that makes everything else work. |
76 |
| - def load_schema! |
77 |
| - @columns_hash = tablefree_options[:columns_hash].except(*ignored_columns) |
78 |
| - @columns_hash.each do |name, column| |
79 |
| - define_attribute( |
80 |
| - name, |
81 |
| - connection.lookup_cast_type_from_column(column), |
82 |
| - default: column.default, |
83 |
| - user_provided_default: false |
84 |
| - ) |
85 |
| - end |
86 |
| - end |
87 |
| - |
88 |
| - # Register a new column. |
89 |
| - def column(name, sql_type = nil, default = nil, null = true) |
90 |
| - cast_type = "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize.new |
91 |
| - tablefree_options[:columns_hash][name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, cast_type, sql_type.to_s, null) |
92 |
| - end |
93 |
| - |
94 |
| - # Register a set of columns with the same SQL type |
95 |
| - def add_columns(sql_type, *args) |
96 |
| - args.each do |col| |
97 |
| - column col, sql_type |
98 |
| - end |
99 |
| - end |
100 |
| - |
101 |
| - def destroy(*_args) |
102 |
| - case tablefree_options[:database] |
103 |
| - when :pretend_success |
104 |
| - new |
105 |
| - when :fail_fast |
106 |
| - raise NoDatabase, "Can't #destroy on Tablefree class" |
107 |
| - end |
108 |
| - end |
109 |
| - |
110 |
| - def destroy_all(*_args) |
111 |
| - case tablefree_options[:database] |
112 |
| - when :pretend_success |
113 |
| - [] |
114 |
| - when :fail_fast |
115 |
| - raise NoDatabase, "Can't #destroy_all on Tablefree class" |
116 |
| - end |
117 |
| - end |
118 |
| - |
119 |
| - case ActiveRecord::VERSION::MAJOR |
120 |
| - when 5 |
121 |
| - def find_by_sql(*_args) |
122 |
| - case tablefree_options[:database] |
123 |
| - when :pretend_success |
124 |
| - [] |
125 |
| - when :fail_fast |
126 |
| - raise NoDatabase, "Can't #find_by_sql on Tablefree class" |
127 |
| - end |
128 |
| - end |
129 |
| - else |
130 |
| - raise Unsupported, 'Unsupported ActiveRecord version' |
131 |
| - end |
132 |
| - |
133 |
| - def transaction |
134 |
| - # case tablefree_options[:database] |
135 |
| - # when :pretend_success |
136 |
| - @_current_transaction_records ||= [] |
137 |
| - yield |
138 |
| - # when :fail_fast |
139 |
| - # raise NoDatabase.new("Can't #transaction on Tablefree class") |
140 |
| - # end |
141 |
| - end |
142 |
| - |
143 |
| - def tablefree? |
144 |
| - true |
145 |
| - end |
146 |
| - |
147 |
| - def table_exists? |
148 |
| - false |
149 |
| - end |
150 |
| - end |
151 |
| - |
152 |
| - module ClassMethods |
153 |
| - def from_query_string(query_string) |
154 |
| - if query_string.blank? |
155 |
| - new |
156 |
| - else |
157 |
| - params = query_string.split('&').collect do |chunk| |
158 |
| - next if chunk.empty? |
159 |
| - key, value = chunk.split('=', 2) |
160 |
| - next if key.empty? |
161 |
| - value = value.nil? ? nil : CGI.unescape(value) |
162 |
| - [CGI.unescape(key), value] |
163 |
| - end.compact.to_h |
164 |
| - |
165 |
| - new(params) |
166 |
| - end |
167 |
| - end |
168 |
| - |
169 |
| - def connection |
170 |
| - @_connection ||= ActiveRecord::Tablefree::Connection.new |
171 |
| - end |
172 |
| - end |
173 |
| - |
174 |
| - module InstanceMethods |
175 |
| - def to_query_string(prefix = nil) |
176 |
| - attributes.to_a.collect { |(name, value)| escaped_var_name(name, prefix) + '=' + escape_for_url(value) if value }.compact.join('&') |
177 |
| - end |
178 |
| - |
179 |
| - def quote_value(_value, _column = nil) |
180 |
| - '' |
181 |
| - end |
182 |
| - |
183 |
| - %w[create create_record _create_record update update_record _update_record].each do |method_name| |
184 |
| - define_method(method_name) do |*_args| |
185 |
| - case self.class.tablefree_options[:database] |
186 |
| - when :pretend_success |
187 |
| - true |
188 |
| - when :fail_fast |
189 |
| - raise NoDatabase, "Can't ##{method_name} a Tablefree object" |
190 |
| - end |
191 |
| - end |
192 |
| - end |
193 |
| - |
194 |
| - def destroy |
195 |
| - case self.class.tablefree_options[:database] |
196 |
| - when :pretend_success |
197 |
| - @destroyed = true |
198 |
| - freeze |
199 |
| - when :fail_fast |
200 |
| - raise NoDatabase, "Can't #destroy a Tablefree object" |
201 |
| - end |
202 |
| - end |
203 |
| - |
204 |
| - def reload(*_args) |
205 |
| - case self.class.tablefree_options[:database] |
206 |
| - when :pretend_success |
207 |
| - self |
208 |
| - when :fail_fast |
209 |
| - raise NoDatabase, "Can't #reload a Tablefree object" |
210 |
| - end |
211 |
| - end |
212 |
| - |
213 |
| - def add_to_transaction; end |
214 |
| - |
215 |
| - private |
216 |
| - |
217 |
| - def escaped_var_name(name, prefix = nil) |
218 |
| - prefix ? "#{CGI.escape(prefix)}[#{CGI.escape(name)}]" : CGI.escape(name) |
219 |
| - end |
220 |
| - |
221 |
| - def escape_for_url(value) |
222 |
| - case value |
223 |
| - when true then '1' |
224 |
| - when false then '0' |
225 |
| - when nil then '' |
226 |
| - else CGI.escape(value.to_s) |
227 |
| - end |
228 |
| - rescue |
229 |
| - '' |
230 |
| - end |
231 |
| - end |
232 |
| - end |
233 |
| -end |
234 |
| - |
235 |
| -ActiveRecord::Base.send(:include, ActiveRecord::Tablefree) |
| 1 | +# legacy automatic require support |
| 2 | +require 'activerecord/tablefree' |
0 commit comments