select_extra_columns 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.markdown +4 -0
- data/README.markdown +66 -8
- data/lib/select_extra_columns.rb +28 -16
- metadata +2 -2
data/CHANGELOG.markdown
CHANGED
data/README.markdown
CHANGED
@@ -42,16 +42,74 @@ Usage
|
|
42
42
|
|
43
43
|
### Now return the extra columns in your finders.
|
44
44
|
|
45
|
+
user = User.first(:joins => :address, :select => "*, addresses.street as street",
|
46
|
+
:extra_columns => :street)
|
47
|
+
user.city # returns the street
|
48
|
+
|
45
49
|
users = User.find(:all, :joins => :posts, :select => "users.*, count(posts.id) as post_count",
|
46
50
|
:extra_columns => {:post_count => :integer} )
|
47
|
-
users.first.post_count
|
48
|
-
|
49
|
-
|
50
|
-
:extra_columns =>
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
users.first.post_count # returns the post count
|
52
|
+
|
53
|
+
user = User.first(:joins => :address, :select => "users.*, addresses.street as street, addresses.city as city",
|
54
|
+
:extra_columns => [:street, :city] )
|
55
|
+
user.street # returns the street
|
56
|
+
user.city # returns the city
|
57
|
+
|
58
|
+
users = User.all(:joins => :address, :select => "*, addresses.active as active, addresses.city as city",
|
59
|
+
:extra_columns => [:city, [:active, :boolean]]
|
60
|
+
users.first.street # returns the street
|
61
|
+
users.first.active # returns true/false
|
62
|
+
|
63
|
+
### Input format for `:extra_options`
|
64
|
+
|
65
|
+
Option accepts String/Symbol/Array/Hash as input.
|
66
|
+
|
67
|
+
Example:
|
68
|
+
|
69
|
+
#String,Symbol format
|
70
|
+
:extra_columns => :first_name # Single string field: `first_name`
|
71
|
+
|
72
|
+
:extra_columns => "first_name" # Single string field: `first_name`
|
73
|
+
|
74
|
+
# Hash format
|
75
|
+
:extra_columns => { # Two string fields and a boolean field
|
76
|
+
:first_name => :string,
|
77
|
+
:last_name => :string,
|
78
|
+
:has_flag => :boolean
|
79
|
+
}
|
80
|
+
|
81
|
+
:extra_columns => { # Two string fields and a boolean field
|
82
|
+
"first_name" => :string,
|
83
|
+
"last_name" => :string,
|
84
|
+
"has_flag" => :boolean
|
85
|
+
}
|
86
|
+
|
87
|
+
# Array format
|
88
|
+
:extra_columns => [ # Two string fields and a boolean field
|
89
|
+
[:first_name, :string],
|
90
|
+
[:last_name, :string],
|
91
|
+
[:has_flag, :boolean]
|
92
|
+
]
|
93
|
+
|
94
|
+
:extra_columns => [:first_name, :last_name] # Two string fields
|
95
|
+
|
96
|
+
:extra_columns => [ # Two string fields and a boolean field
|
97
|
+
:first_name, :last_name, # type is inferred as string
|
98
|
+
[:has_flag, :boolean]
|
99
|
+
]
|
100
|
+
|
101
|
+
### Valid data types of fields in `:extra_columns`
|
102
|
+
:binary
|
103
|
+
:boolean
|
104
|
+
:date
|
105
|
+
:datetime
|
106
|
+
:decimal
|
107
|
+
:float
|
108
|
+
:integer
|
109
|
+
:string
|
110
|
+
:text
|
111
|
+
:time
|
112
|
+
:timestamp
|
55
113
|
|
56
114
|
Copyright (c) 2010 Kandada Boggu, released under the MIT license
|
57
115
|
|
data/lib/select_extra_columns.rb
CHANGED
@@ -15,7 +15,7 @@ module SelectExtraColumns
|
|
15
15
|
|
16
16
|
def find_every_with_extra_columns options
|
17
17
|
extra_columns = options.delete(:extra_columns)
|
18
|
-
return find_every_without_extra_columns options if extra_columns.
|
18
|
+
return find_every_without_extra_columns options if extra_columns.nil?
|
19
19
|
klass_with_extra_columns(extra_columns).send(:find_every, options)
|
20
20
|
end
|
21
21
|
|
@@ -28,17 +28,15 @@ module SelectExtraColumns
|
|
28
28
|
|
29
29
|
def klass_with_extra_columns extra_columns
|
30
30
|
# look for the class in the cache.
|
31
|
-
self.klasses_with_extra_columns.
|
32
|
-
|
33
|
-
end
|
34
|
-
# load the column definition
|
35
|
-
cols, cols_hash = self.columns, self.columns_hash
|
36
|
-
self.clone.tap do |klass|
|
37
|
-
prepare_extra_column_klass(klass, cols, cols_hash, extra_columns)
|
38
|
-
end
|
31
|
+
self.klasses_with_extra_columns.find{|k| k.extra_columns == extra_columns} or
|
32
|
+
prepare_extra_column_klass(extra_columns)
|
39
33
|
end
|
40
34
|
|
41
|
-
def prepare_extra_column_klass
|
35
|
+
def prepare_extra_column_klass extra_columns
|
36
|
+
extra_column_definitions = prepare_extra_column_definitions(extra_columns)
|
37
|
+
return self if extra_column_definitions.empty?
|
38
|
+
cols, cols_hash = self.columns, self.columns_hash
|
39
|
+
self.clone.tap do |klass|
|
42
40
|
class << klass
|
43
41
|
attr_accessor :extra_columns
|
44
42
|
# over ride readonly_attributes to include the extra_columns
|
@@ -47,14 +45,28 @@ module SelectExtraColumns
|
|
47
45
|
end
|
48
46
|
end
|
49
47
|
#Make new copy of @columns, and @columns_hash and @extra_columns variables
|
50
|
-
klass.instance_variable_set("@columns", cols.clone)
|
48
|
+
klass.instance_variable_set("@columns", cols.clone)
|
51
49
|
klass.instance_variable_set("@columns_hash", cols_hash.clone)
|
52
|
-
klass.extra_columns = extra_columns.clone
|
53
|
-
|
54
|
-
|
55
|
-
klass.columns << (klass.columns_hash[col_name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(col_name.to_s, nil, col_type.to_s))
|
50
|
+
klass.extra_columns = extra_columns.is_a?(Symbol) ? extra_columns : extra_columns.clone
|
51
|
+
extra_column_definitions.each do |ecd|
|
52
|
+
klass.columns << (klass.columns_hash[ecd.name] = ecd)
|
56
53
|
end
|
57
|
-
self.klasses_with_extra_columns = [
|
54
|
+
self.klasses_with_extra_columns = [klass] # add the class to the cache
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def prepare_extra_column_definitions extra_columns
|
59
|
+
extra_columns = [extra_columns] if extra_columns.is_a?(Symbol) or extra_columns.is_a?(String)
|
60
|
+
extra_columns = extra_columns.to_a if extra_columns.is_a?(Hash)
|
61
|
+
return [] unless extra_columns.is_a?(Array)
|
62
|
+
[].tap do |result|
|
63
|
+
extra_columns.each do |col_detail|
|
64
|
+
col_detail = [col_detail] if col_detail.is_a?(Symbol) or col_detail.is_a?(String)
|
65
|
+
next unless col_detail.is_a?(Array)
|
66
|
+
col_name, col_type = col_detail[0].to_s, (col_detail[1] || "string").to_s
|
67
|
+
result << ActiveRecord::ConnectionAdapters::Column.new(col_name, nil, col_type)
|
68
|
+
end
|
69
|
+
end
|
58
70
|
end
|
59
71
|
end
|
60
72
|
end
|