select_extra_columns 0.0.5 → 0.0.6

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 CHANGED
@@ -1,3 +1,7 @@
1
+ 2010-04-11
2
+ ==========
3
+ * Added support for sharing `extra_columns` definition across finders
4
+
1
5
  2010-04-09
2
6
  ==========
3
7
  * Changed cloning to inheritance.
data/README.markdown CHANGED
@@ -7,9 +7,11 @@ Installation
7
7
  ============
8
8
  Use either the plugin or the gem installation method depending on your preference. If you're not sure, the plugin method is simpler.
9
9
 
10
+ ## As a Plugin
11
+
10
12
  ./script/plugin install git://github.com/kandadaboggu/select_extra_column.git
11
13
 
12
- ### Via gem
14
+ ## As a Gem
13
15
  Add the following to your application's environment.rb:
14
16
  config.gem "select_extra_column", :source => "http://gemcutter.org"
15
17
 
@@ -17,12 +19,10 @@ Install the gem:
17
19
  rake gems:install
18
20
 
19
21
 
20
- Usage
21
- =====
22
-
23
- ## Getting Started
22
+ Getting Started
23
+ ===============
24
24
 
25
- ### Enable select_extra_column in your ActiveRecord model.
25
+ ## Enable select_extra_column in your ActiveRecord model.
26
26
 
27
27
 
28
28
  class User < ActiveRecord::Base
@@ -40,7 +40,7 @@ Usage
40
40
  end
41
41
 
42
42
 
43
- ### Now return the extra columns in your finders.
43
+ ## Use the extra columns in your finders.
44
44
 
45
45
  user = User.first(:joins => :address, :select => "*, addresses.street as street",
46
46
  :extra_columns => :street)
@@ -78,7 +78,7 @@ Dynamically added column fields are read only. Any value set to these fields are
78
78
 
79
79
  ### Input format for `:extra_columns`
80
80
 
81
- Option accepts String/Symbol/Array/Hash as input.
81
+ This option accepts `String`/`Symbol`/`Array`/`Hash` as input.
82
82
 
83
83
 
84
84
  #### String,Symbol format
@@ -113,7 +113,42 @@ Option accepts String/Symbol/Array/Hash as input.
113
113
  [:has_flag, :boolean]
114
114
  ]
115
115
 
116
- ### Valid data types for column fields in `:extra_columns`
116
+ ### Sharing `extra_columns` definition across finders
117
+ You can declare the extra columns in your model and use them across finders
118
+ class User < ActiveRecord::Base
119
+ select_extra_columns
120
+
121
+ extra_columns :address_info, :street, :city
122
+ extra_columns :post_info, [:post_count, :integer], :last_post_at => :datetime
123
+
124
+ has_many :posts
125
+ has_one :address
126
+ end
127
+
128
+ Now `:user_info` and `:post_info` can be used in finders.
129
+
130
+ users = User.find(:all, :joins => :posts, :select => "users.*, count(posts.id) as post_count, max(posts.created_at) as last_post_at",
131
+ :extra_columns => :post_info)
132
+
133
+ user = User.first(:joins => :address, :select => "users.*, addresses.street as street, addresses.city as city",
134
+ :extra_columns => :address_info )
135
+
136
+ ## Naming conflicts
137
+ When a symbol/string is passed as input to `:extra_columns` option, the finder uses cached `extra_columns` definition by the given name.
138
+ If no definition is found, then finder creates a new `extra_columns` definition with the input as a column.
139
+
140
+ class User < ActiveRecord::Base
141
+ select_extra_columns
142
+
143
+ extra_columns :post_count, [:post_count, :integer], :last_post_at => :datetime
144
+ end
145
+
146
+ The finder call below, `post_count` maps on to a column in the select list and a `extra_columns` definition. Finder chooses the `extra_columns` definition.
147
+ users = User.find(:all, :joins => :posts, :select => "users.*, count(posts.id) as post_count, max(posts.created_at) as last_post_at",
148
+ :extra_columns => :post_count)
149
+
150
+
151
+ ## Valid data types for column fields in `:extra_columns`
117
152
  :binary
118
153
  :boolean
119
154
  :date
@@ -28,11 +28,14 @@ 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.find{|k| k.extra_columns == extra_columns} or
32
- prepare_extra_column_klass(extra_columns)
31
+ (
32
+ [String, Symbol].include?(extra_columns.class) ?
33
+ self.klasses_with_extra_columns.find{|k| k.extra_columns_key == extra_columns.to_s} :
34
+ self.klasses_with_extra_columns.find{|k| k.extra_columns == extra_columns}
35
+ ) or extra_columns_class(extra_columns)
33
36
  end
34
37
 
35
- def prepare_extra_column_klass extra_columns
38
+ def extra_columns_class extra_columns, extra_columns_key=nil
36
39
  extra_column_definitions = prepare_extra_column_definitions(extra_columns)
37
40
  return self if extra_column_definitions.empty?
38
41
  read_only_attrs = extra_column_definitions.collect{|cd| ":#{cd.name}" }.join(",")
@@ -41,11 +44,12 @@ module SelectExtraColumns
41
44
  class ::#{klass_name} < #{self.name}
42
45
  set_table_name :#{self.table_name}
43
46
  attr_readonly #{read_only_attrs}
44
- class_inheritable_accessor :extra_columns
47
+ class_inheritable_accessor :extra_columns, :extra_columns_key
45
48
  end
46
49
  RUBY
47
50
  klass_name.constantize.tap do |klass|
48
51
  klass.extra_columns = extra_columns.is_a?(Symbol) ? extra_columns : extra_columns.clone
52
+ klass.extra_columns_key = (extra_columns_key || klass_name).to_s
49
53
  extra_column_definitions.each do |ecd|
50
54
  klass.columns << (klass.columns_hash[ecd.name] = ecd)
51
55
  end
@@ -53,6 +57,10 @@ module SelectExtraColumns
53
57
  end
54
58
  end
55
59
 
60
+ def extra_columns extra_columns_key, *args
61
+ extra_columns_class args.concat(args.extract_options!.to_a), extra_columns_key
62
+ end
63
+
56
64
  def prepare_extra_column_definitions extra_columns
57
65
  extra_columns = [extra_columns] if extra_columns.is_a?(Symbol) or extra_columns.is_a?(String)
58
66
  extra_columns = extra_columns.to_a if extra_columns.is_a?(Hash)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 5
9
- version: 0.0.5
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kandada Boggu
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-09 00:00:00 -08:00
17
+ date: 2010-03-10 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies: []
20
20