select_extra_columns 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.markdown CHANGED
@@ -1,3 +1,7 @@
1
+ 2010-03-08
2
+ ==========
3
+ * Added support for multiple input types for extra_columns
4
+
1
5
  2010-03-08
2
6
  ==========
3
7
  * Fixed the bugs
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 # returns the post count
48
-
49
- users = User.find(:all, :joins => :address, :select => "users.*, addresses.street as street, addresses.city as city",
50
- :extra_columns => {:street => :string, :city => :string } )
51
- users.first.street # returns the street
52
- users.first.city # returns the city
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
 
@@ -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.empty?
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.select do | class_details |
32
- return class_details[1] if class_details[0] == extra_columns
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 klass, cols, cols_hash, extra_columns
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
- extra_columns.each do |col_name, col_type|
54
- # add the new column to `columns` list and `columns_hash` hash.
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 = [[extra_columns, klass]] # add the class to the cache
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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kandada Boggu