find_as_hashes 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,3 @@
1
1
  *.gem
2
2
  .bundle
3
- Gemfile.lock
4
3
  pkg/*
data/.travis.yml CHANGED
@@ -4,3 +4,8 @@ rvm:
4
4
  - ree
5
5
  - ruby-head
6
6
  - rbx-2.0
7
+ notifications:
8
+ # overriding this so that TST Media's email account doesn't get spammed
9
+ # feel free to add yourselves if you want in on this action
10
+ email:
11
+ - patrick.byrne@tstmedia.com
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ find_as_hashes (0.2.0)
5
+ activerecord
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.1.0)
11
+ activesupport (= 3.1.0)
12
+ bcrypt-ruby (~> 3.0.0)
13
+ builder (~> 3.0.0)
14
+ i18n (~> 0.6)
15
+ activerecord (3.1.0)
16
+ activemodel (= 3.1.0)
17
+ activesupport (= 3.1.0)
18
+ arel (~> 2.2.1)
19
+ tzinfo (~> 0.3.29)
20
+ activesupport (3.1.0)
21
+ multi_json (~> 1.0)
22
+ ansi (1.3.0)
23
+ arel (2.2.1)
24
+ bcrypt-ruby (3.0.1)
25
+ builder (3.0.0)
26
+ i18n (0.6.0)
27
+ multi_json (1.0.3)
28
+ rake (0.9.2)
29
+ shoulda (2.11.3)
30
+ sqlite3 (1.3.4)
31
+ turn (0.8.2)
32
+ ansi (>= 1.2.2)
33
+ tzinfo (0.3.29)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ activesupport
40
+ bundler
41
+ find_as_hashes!
42
+ rake
43
+ shoulda
44
+ sqlite3
45
+ turn
data/README.markdown CHANGED
@@ -5,10 +5,6 @@ of your ActiveRecord methods.
5
5
 
6
6
  ## Methods
7
7
 
8
- Both methods assume that you've constructed some sort of ARel-style
9
- query (e.g., regular queries like `Customer.where("balance" > 5000)` or
10
- scopes like `User.active`).
11
-
12
8
  The hashes that return are similar to the hash that returns with
13
9
  [`ActiveRecord::Base#attributes`][attrs_method], except values are not
14
10
  coerced (e.g., a tinyint column containing `1` will not be coerced into
@@ -16,10 +12,10 @@ coerced (e.g., a tinyint column containing `1` will not be coerced into
16
12
  Ruby object. Booleans will return the underlying representation and
17
13
  serialized objects will return as strings.
18
14
 
19
- * `all_as_hashes` performs the search that you've set up and returns the
20
- results as an array of hashes.
21
- * `first_as_hash` performs the search that you've set up and returns a
22
- hash of the first matching record.
15
+ * `all_as_hashes` works similarly to `all`, but returns the results as
16
+ an array of hashes.
17
+ * `first_as_hash` works similarly to `first`, but returns a hash of the
18
+ first matching record.
23
19
 
24
20
  [attrs_method]:http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-i-attributes
25
21
 
@@ -44,4 +40,7 @@ In one example, looping through 36,400 User records and accessing an
44
40
  attribute used 32% less memory and performed 17% quicker using
45
41
  `all_as_hashes` over `all`.
46
42
 
43
+ ## Tests
47
44
 
45
+ Run tests with `bundle exec rake test` (or just `rake test` if you're
46
+ daring).
@@ -1,13 +1,17 @@
1
1
  require "find_as_hashes/version"
2
+ require 'active_record'
2
3
 
3
- module ActiveRecord
4
- module FinderMethods
5
- def all_as_hashes
6
- connection.select_all(to_sql)
7
- end
4
+ module FindAsHashes
5
+ def all_as_hashes
6
+ # TODO figure out a less hacky way than where(nil) of retrieveing existing relation stack without modifying it
7
+ relation_stack = where(nil)
8
+ connection.select_all(relation_stack.joins(relation_stack.includes_values).to_sql)
9
+ end
8
10
 
9
- def first_as_hash
10
- connection.select_one(limit(1).to_sql)
11
- end
11
+ def first_as_hash
12
+ relation_stack = limit(1)
13
+ connection.select_one(relation_stack.joins(relation_stack.includes_values).to_sql)
12
14
  end
13
15
  end
16
+
17
+ ActiveRecord::Base.extend FindAsHashes
@@ -1,3 +1,3 @@
1
1
  module FindAsHashes
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -2,43 +2,97 @@ require File.dirname(__FILE__) + "/test_helper"
2
2
 
3
3
  class FindAsHashesTest < ActiveRecord::TestCase
4
4
  context "#all_as_hashes" do
5
- setup do
6
- relation = User.where(:active => true)
7
- @records = relation.all
8
- @hashes = relation.all_as_hashes
9
- end
5
+ context "on a relation" do
6
+ setup do
7
+ relation = User.where(:active => true)
8
+ @records = relation.all
9
+ @hashes = relation.all_as_hashes
10
+ end
11
+
12
+ should "return an array of attribute hashes" do
13
+ assert_equal Array, @hashes.class
14
+ assert @hashes.all? { |hash| hash.class == Hash }
15
+ end
16
+
17
+ should "return the same number of results as `all`" do
18
+ assert_equal @records.size, @hashes.size
19
+ end
10
20
 
11
- should "return an array of attribute hashes" do
12
- assert_equal Array, @hashes.class
13
- assert @hashes.all? { |hash| hash.class == Hash }
21
+ should "return the same results as `all`" do
22
+ assert_equal @records.first.name_before_type_cast, @hashes.first["name"]
23
+ assert_equal @records.first.birthday_before_type_cast, @hashes.first["birthday"]
24
+ assert_equal @records.first.active_before_type_cast, @hashes.first["active"]
25
+ end
14
26
  end
15
27
 
16
- should "return the same number of results as `all`" do
17
- assert_equal @records.size, @hashes.size
28
+ context "on a model" do
29
+ setup do
30
+ @records = User.all
31
+ @hashes = User.all_as_hashes
32
+ end
33
+
34
+ should "return the same results as `all`" do
35
+ assert_equal @records.first.name_before_type_cast, @hashes.first["name"]
36
+ assert_equal @records.first.birthday_before_type_cast, @hashes.first["birthday"]
37
+ assert_equal @records.first.active_before_type_cast, @hashes.first["active"]
38
+ end
18
39
  end
19
40
 
20
- should "return the same results as `all`" do
21
- assert_equal @records.first.name_before_type_cast, @hashes.first["name"]
22
- assert_equal @records.first.birthday_before_type_cast, @hashes.first["birthday"]
23
- assert_equal @records.first.active_before_type_cast, @hashes.first["active"]
41
+ context "using `includes`" do
42
+ setup do
43
+ end
44
+
45
+ should "use the included relation in the query" do
46
+ assert_nothing_raised do
47
+ @hashes = User.includes(:role).where("roles.admin" => true).all_as_hashes
48
+ assert Role.where(@hashes.first["role_id"]).first.admin?
49
+ end
50
+ end
24
51
  end
25
52
  end
26
53
 
27
54
  context "#first_as_hash" do
28
- setup do
29
- relation = User.where(:active => false)
30
- @record = relation.first
31
- @hash = relation.first_as_hash
55
+ context "on a relation" do
56
+ setup do
57
+ relation = User.where(:active => false)
58
+ @record = relation.first
59
+ @hash = relation.first_as_hash
60
+ end
61
+
62
+ should "return a hash of attributes" do
63
+ assert_equal Hash, @hash.class
64
+ end
65
+
66
+ should "return the same result as `first`" do
67
+ assert_equal @record.name_before_type_cast, @hash["name"]
68
+ assert_equal @record.birthday_before_type_cast, @hash["birthday"]
69
+ assert_equal @record.active_before_type_cast, @hash["active"]
70
+ end
32
71
  end
33
72
 
34
- should "return a hash of attributes" do
35
- assert_equal Hash, @hash.class
73
+ context "on a model" do
74
+ setup do
75
+ @record = User.first
76
+ @hash = User.first_as_hash
77
+ end
78
+
79
+ should "return the same result as `first`" do
80
+ assert_equal @record.name_before_type_cast, @hash["name"]
81
+ assert_equal @record.birthday_before_type_cast, @hash["birthday"]
82
+ assert_equal @record.active_before_type_cast, @hash["active"]
83
+ end
36
84
  end
37
85
 
38
- should "return the same result as `first`" do
39
- assert_equal @record.name_before_type_cast, @hash["name"]
40
- assert_equal @record.birthday_before_type_cast, @hash["birthday"]
41
- assert_equal @record.active_before_type_cast, @hash["active"]
86
+ context "using `includes`" do
87
+ setup do
88
+ end
89
+
90
+ should "use the included relation in the query" do
91
+ assert_nothing_raised do
92
+ @hash = User.includes(:role).where("roles.admin" => true).first_as_hash
93
+ assert Role.where(@hash["role_id"]).first.admin?
94
+ end
95
+ end
42
96
  end
43
97
  end
44
98
  end
@@ -0,0 +1,12 @@
1
+ admin:
2
+ name: "Admin"
3
+ admin: true
4
+ guest:
5
+ name: "Guest"
6
+ admin: false
7
+ user:
8
+ name: "User"
9
+ admin: false
10
+ staff:
11
+ name: "Corporate Staff"
12
+ admin: true
@@ -3,4 +3,5 @@ user<%= n %>:
3
3
  name: Example User <%= n %>
4
4
  birthday: <%= n.weeks.ago %>
5
5
  active: <%= n.even? %>
6
+ role_id: <%= Role.all.map(&:id)[n % 4] %>
6
7
  <% end %>
data/test/test_helper.rb CHANGED
@@ -1,10 +1,9 @@
1
+ require 'find_as_hashes'
1
2
  require 'test/unit'
2
3
  require 'turn'
3
4
  require 'shoulda'
4
- require 'active_record'
5
5
  require 'active_record/fixtures'
6
6
  require 'sqlite3'
7
- require 'find_as_hashes'
8
7
 
9
8
  ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
10
9
  ActiveRecord::Schema.define do
@@ -12,10 +11,26 @@ ActiveRecord::Schema.define do
12
11
  t.string :name
13
12
  t.date :birthday
14
13
  t.boolean :active
14
+ t.integer :role_id
15
+ end
16
+ end
17
+
18
+ ActiveRecord::Schema.define do
19
+ create_table :roles, :force => true do |t|
20
+ t.string :name
21
+ t.boolean :admin
15
22
  end
16
23
  end
17
24
 
18
25
  class User < ActiveRecord::Base
26
+ belongs_to :role
27
+ end
28
+
29
+ class Role < ActiveRecord::Base
30
+ has_many :user
19
31
  end
20
32
 
21
- Fixtures.create_fixtures "test/fixtures", :users
33
+ # Rails 3.0 has Fixtures, Rails 3.1 has ActiveRecord::Fixtures
34
+ fixtures_class = defined?(ActiveRecord::Fixtures) ? ActiveRecord::Fixtures : Fixtures
35
+ fixtures_class.create_fixtures "test/fixtures", :roles
36
+ fixtures_class.create_fixtures "test/fixtures", :users
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: find_as_hashes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-14 00:00:00.000000000Z
12
+ date: 2011-09-22 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2153703280 !ruby/object:Gem::Requirement
16
+ requirement: &2151839880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153703280
24
+ version_requirements: *2151839880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &2153702640 !ruby/object:Gem::Requirement
27
+ requirement: &2151839240 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2153702640
35
+ version_requirements: *2151839240
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &2153702080 !ruby/object:Gem::Requirement
38
+ requirement: &2151838320 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153702080
46
+ version_requirements: *2151838320
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: turn
49
- requirement: &2153701320 !ruby/object:Gem::Requirement
49
+ requirement: &2151837220 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2153701320
57
+ version_requirements: *2151837220
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: shoulda
60
- requirement: &2153700720 !ruby/object:Gem::Requirement
60
+ requirement: &2151836140 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2153700720
68
+ version_requirements: *2151836140
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activesupport
71
- requirement: &2153699760 !ruby/object:Gem::Requirement
71
+ requirement: &2151834680 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2153699760
79
+ version_requirements: *2151834680
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: sqlite3
82
- requirement: &2153699080 !ruby/object:Gem::Requirement
82
+ requirement: &2151833560 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2153699080
90
+ version_requirements: *2151833560
91
91
  description: Provides ActiveRecord methods to return results as attribute hashes rather
92
92
  than instantiated ActiveRecord objects. Useful when working with very large sets
93
93
  of results to improve performance.
@@ -100,12 +100,14 @@ files:
100
100
  - .gitignore
101
101
  - .travis.yml
102
102
  - Gemfile
103
+ - Gemfile.lock
103
104
  - README.markdown
104
105
  - Rakefile
105
106
  - find_as_hashes.gemspec
106
107
  - lib/find_as_hashes.rb
107
108
  - lib/find_as_hashes/version.rb
108
109
  - test/find_as_hashes_test.rb
110
+ - test/fixtures/roles.yml
109
111
  - test/fixtures/users.yml
110
112
  - test/test_helper.rb
111
113
  homepage: https://github.com/tstmedia/find_as_hashes
@@ -122,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
124
  version: '0'
123
125
  segments:
124
126
  - 0
125
- hash: 2685435975965919057
127
+ hash: -1576352960134467685
126
128
  required_rubygems_version: !ruby/object:Gem::Requirement
127
129
  none: false
128
130
  requirements:
@@ -131,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
133
  version: '0'
132
134
  segments:
133
135
  - 0
134
- hash: 2685435975965919057
136
+ hash: -1576352960134467685
135
137
  requirements: []
136
138
  rubyforge_project:
137
139
  rubygems_version: 1.8.6
@@ -141,5 +143,6 @@ summary: Provides ActiveRecord methods to return results as attribute hashes rat
141
143
  than instantiated ActiveRecord objects.
142
144
  test_files:
143
145
  - test/find_as_hashes_test.rb
146
+ - test/fixtures/roles.yml
144
147
  - test/fixtures/users.yml
145
148
  - test/test_helper.rb