3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/active_record_lite/core_ext.rb', line 3
def has_lite(options={})
if options.key?(:columns)
@_arlcolumns = options.delete(:columns)
end
class_eval <<-EOV
def self.to_lite(*args)
selects = args.presence || @_arlcolumns
scoper = unscope(:select).select(selects)
ActiveRecordLite.perform(scoper.to_sql, selects)
end
class ActiveRecordLite < ActiveRecordLite::Base
@base_class = parent
@attr_lookup = {}
columns.each do |column_name|
type = parent.type_for_attribute(column_name).type
if type.eql?(:datetime)
define_method(column_name) do
memoize_results(:"@\#{column_name}") {
read_attribute(column_name).in_time_zone
}
end
elsif type.eql?(:boolean)
define_method(column_name) do
read_attribute(column_name) == 1
end
alias_method "\#{column_name}?", column_name
elsif type.eql?(:text) && parent.serialized_attributes.key?(column_name)
klass = parent.serialized_attributes[column_name]
define_method(column_name) do
memoize_results(:"@\#{column_name}") {
klass.load(read_attribute(column_name))
}
end
else
define_method(column_name) do
read_attribute(column_name)
end
end
end
end
EOV
end
|