select_extra_columns 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ 2010-03-07
2
+ ==========
3
+ * First release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Kandada Boggu (KandadaBoggu.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,57 @@
1
+ select_extra_columns
2
+ ====================
3
+
4
+ Enhances the ActiveRecord finders to return join/aggregate/calculated columns along with standard table columns.
5
+
6
+ Installation
7
+ ============
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
+
10
+ ./script/plugin install git://github.com/kandadaboggu/select_extra_column.git
11
+
12
+ ### Via gem
13
+ Add the following to your application's environment.rb:
14
+ config.gem "select_extra_column", :source => "http://gemcutter.org"
15
+
16
+ Install the gem:
17
+ rake gems:install
18
+
19
+
20
+ Usage
21
+ =====
22
+
23
+ ## Getting Started
24
+
25
+ ### Enable select_extra_column in your ActiveRecord model.
26
+
27
+
28
+ class User < ActiveRecord::Base
29
+ select_extra_columns
30
+ has_many :posts
31
+ has_one :address
32
+ end
33
+
34
+ class Address < ActiveRecord::Base
35
+ belongs_to :user
36
+ end
37
+
38
+ class Post < ActiveRecord::Base
39
+ belongs_to :user
40
+ end
41
+
42
+
43
+ ### Now return the extra columns in your finders.
44
+
45
+ users = User.find(:all, :joins => :posts, :select => "users.*, count(posts.id) as post_count",
46
+ :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
+
55
+
56
+ Copyright (c) 2010 Kandada Boggu, released under the MIT license
57
+
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require File.dirname( __FILE__ ) + "/lib/select_extra_columns"
2
+
3
+ ActiveRecord::Base.send(:extend, SelectExtraColumns) unless ActiveRecord::Base.respond_to?(:select_extra_columns)
@@ -0,0 +1,45 @@
1
+ module SelectExtraColumns
2
+ def select_extra_columns
3
+ return if self.respond_to?(:find_every_with_extra_columns)
4
+ self.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def self.extended(active_record_class)
9
+ class << active_record_class
10
+ class_inheritable_array(:klasses_with_extra_columns)
11
+ alias_method_chain :find_every, :extra_columns
12
+ end
13
+ end
14
+
15
+ def find_every_with_extra_columns options
16
+ extra_columns = options.delete(:extra_columns)
17
+ return super if extra_columns.empty?
18
+ klass_with_extra_columns(extra_columns).find_every(options)
19
+ end
20
+
21
+ def validate_find_options(options)
22
+ extra_columns = options.delete(:extra_columns)
23
+ super
24
+ ensure
25
+ options[:extra_columns]= extra_columns if extra_columns
26
+ end
27
+
28
+ def klass_with_extra_columns extra_columns
29
+ # look for the class in the cache.
30
+ p "1.0"
31
+ self.klasses_with_extra_columns.select do | class_details |
32
+ return class_details[1] if class_details[0] == extra_columns
33
+ end
34
+ p "1.1"
35
+ self.columns # load the column definition
36
+ self.clone.tap do |klass|
37
+ extra_columns.each do |col_name, col_type|
38
+ # add the new column to `columns` list and `columns_hash` hash.
39
+ klass.columns << (klass.columns_hash[col_name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(col_name.to_s, nil, col_type.to_s))
40
+ end
41
+ self.klasses_with_extra_columns = [[extra_columns, klass]] # add the class to the cache
42
+ end
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: select_extra_columns
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Kandada Boggu
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-07 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Enhances the ActiveRecord finders to return join/aggregate/calculated columns along with standard table columns.
22
+ email: kandadaboggu@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - CHANGELOG.markdown
31
+ - MIT-LICENSE
32
+ - README.markdown
33
+ - init.rb
34
+ - lib/select_extra_columns.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/kandadaboggu/select_extra_columns
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.6
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Enhances the ActiveRecord finders to return join/aggregate/calculated columns along with standard table columns.
65
+ test_files: []
66
+