join_cache 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,61 @@
1
+ # join_cache
2
+
3
+ Faster ActiveRecord associations using Rails cache.
4
+
5
+ ## What?
6
+
7
+ Let's say you scaffolded your Rails app and ended up with this:
8
+
9
+ ```ruby
10
+ class Employee < ActiveRecord::Base
11
+ has_and_belongs_to_many :teams
12
+ end
13
+ ```
14
+
15
+ Retrieving the teams of one employee generates an inner join:
16
+
17
+ ```ruby
18
+ bob.teams
19
+ Team Load SELECT "teams".* FROM "teams" INNER JOIN "employees_teams" ON "teams"."id" = "employees_teams"."team_id" WHERE "employees_teams"."employee_id" = ? [["employee_id", 1]]
20
+ ```
21
+
22
+ The gem join_cache generates methods to store the team ids in cache:
23
+
24
+ ```ruby
25
+ bob.cached_team_ids
26
+ => [4, 8, 15, 16, 23, 42]
27
+
28
+ bob.cached_teams
29
+ => Team.where(id: [4, 8, 15, 16, 23, 42])
30
+ => Team Load SELECT "teams".* FROM "teams" WHERE "teams"."id" IN (4, 8, 15, 16, 23, 42)
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ In your gemfile:
36
+
37
+ ```ruby
38
+ gem 'joined_table'
39
+ ```
40
+
41
+ In your model:
42
+
43
+ ```ruby
44
+ class Employee < ActiveRecord::Base
45
+ has_and_belongs_to_many :teams
46
+ include JoinCache # make sure to add this *after* listing the associations
47
+ end
48
+ ```
49
+
50
+ ## Contributing to join_cache
51
+
52
+ * Fork the project.
53
+ * Start a feature/bugfix branch.
54
+ * Commit and push until you are happy with your contribution.
55
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
56
+
57
+ ## Copyright
58
+
59
+ Copyright (c) 2013 Kevin Bongart. See LICENSE.txt for
60
+ further details.
61
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/join_cache.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "join_cache"
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kevin Bongart"]
@@ -13,14 +13,14 @@ Gem::Specification.new do |s|
13
13
  s.email = "contact@kevinbongart.net"
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
16
- "README.rdoc"
16
+ "README.markdown"
17
17
  ]
18
18
  s.files = [
19
19
  ".document",
20
20
  "Gemfile",
21
21
  "Gemfile.lock",
22
22
  "LICENSE.txt",
23
- "README.rdoc",
23
+ "README.markdown",
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "join_cache.gemspec",
data/lib/join_cache.rb CHANGED
@@ -18,15 +18,19 @@ module JoinCache
18
18
  # => Team.where(id: [4, 8, 15, 16, 23, 42])
19
19
  #
20
20
  reflect_on_all_associations(:has_and_belongs_to_many).each do |association|
21
- singular_name = association.name.to_s.singularize # team
22
- plural_name = association.plural_name # teams
23
- cached_name = "cached_#{plural_name}" # cached_teams
24
- cached_ids_name = "cached_#{singular_name}_ids" # cached_team_ids
21
+ singular_name = association.name.to_s.singularize # team
22
+ plural_name = association.plural_name # teams
23
+ cached_name = "cached_#{plural_name}" # cached_teams
24
+ cached_ids_name = "cached_#{singular_name}_ids" # cached_team_ids
25
+ primary_key = association.class_name.foreign_key # employee_id
26
+ foreign_key = association.foreign_key # team_id
27
+ join_table = association.join_table # employees_teams
28
+ join_model = join_table.classify.pluralize.constantize # EmployeesTeams
25
29
 
26
30
  # cached_team_ids
27
31
  define_method(cached_ids_name) do
28
32
  Rails.cache.fetch("#{cache_key}/#{cached_ids_name}") do
29
- send(plural_name).pluck(self.class.name.foreign_key.to_sym)
33
+ join_model.where(primary_key => id).pluck(foreign_key.to_sym)
30
34
  end
31
35
  end
32
36
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: join_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -81,13 +81,13 @@ executables: []
81
81
  extensions: []
82
82
  extra_rdoc_files:
83
83
  - LICENSE.txt
84
- - README.rdoc
84
+ - README.markdown
85
85
  files:
86
86
  - .document
87
87
  - Gemfile
88
88
  - Gemfile.lock
89
89
  - LICENSE.txt
90
- - README.rdoc
90
+ - README.markdown
91
91
  - Rakefile
92
92
  - VERSION
93
93
  - join_cache.gemspec
@@ -109,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
109
  version: '0'
110
110
  segments:
111
111
  - 0
112
- hash: -2964477634214236973
112
+ hash: 3755351500618496333
113
113
  required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  none: false
115
115
  requirements:
data/README.rdoc DELETED
@@ -1,19 +0,0 @@
1
- = join_cache
2
-
3
- Faster ActiveRecord associations using Rails cache.
4
-
5
- == Contributing to join_cache
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
- * Fork the project.
10
- * Start a feature/bugfix branch.
11
- * Commit and push until you are happy with your contribution.
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2013 Kevin Bongart. See LICENSE.txt for
18
- further details.
19
-