cach_em_all 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b46739a788f68c9a35ced65e0f3f043cda7547b
4
+ data.tar.gz: ea90c71242394948aa32fd8727788f9e02cb139c
5
+ SHA512:
6
+ metadata.gz: a76542174102f1af1e0d93fa94397af1df78d2048ccc074c56f170d05d9a068e122599c53afdd547c9940825cb984eaf79c7fbccaf396b6f3f41b84b3ac5ab7c
7
+ data.tar.gz: 3771c742f8890852ec00a67bb07ed060c980a82b116e35d3d627a735be22bb40dd271056ba6853e2d9ae685ba33f3705fb79b5656246b2e772304c50ca3c5fea
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Oscar Esgalha
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/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'CachEmAll'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,3 @@
1
+ require 'cach_em_all/core_ext/active_record_base_ext'
2
+ require 'cach_em_all/core_ext/active_record_relation_ext'
3
+ require 'cach_em_all/railtie' if defined? Rails
@@ -0,0 +1,14 @@
1
+ module CachEmAll
2
+ module ActiveRecordBaseExt
3
+ extend ActiveSupport::Concern
4
+ module ClassMethods
5
+ def cache_key
6
+ ar_count = count
7
+ max_updated_at = maximum(:updated_at).try(:utc).try :to_s, :number
8
+ name = self.name.tableize
9
+ prefix = 'all'
10
+ [name, [prefix, ar_count, max_updated_at].join('-')].join '/'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module CachEmAll
2
+ module ActiveRecordRelationExt
3
+ extend ActiveSupport::Concern
4
+ def cache_key
5
+ ar_count = count
6
+ max_updated_at = maximum(:updated_at).try(:utc).try :to_s, :number
7
+ name = self.name.tableize
8
+ prefix = Digest::SHA256.hexdigest to_sql.to_s
9
+ [name, [prefix, ar_count, max_updated_at].join('-')].join '/'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ module CachEmAll
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'cach_em_all' do |app|
4
+ ActiveSupport.on_load(:active_record) do
5
+ ActiveRecord::Base.send :include, CachEmAll::ActiveRecordBaseExt
6
+ ActiveRecord::Relation.send :include, CachEmAll::ActiveRecordRelationExt
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module CachEmAll
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApplicationHelper, type: :helper do
4
+
5
+ before :all do
6
+ @empty_block = Proc.new{}
7
+ end
8
+
9
+ describe '#cache' do
10
+ it "should accept an arbitrary ActiveRecord's class name" do
11
+ expect{ helper.cache(ArbitraryRecord, &@empty_block) }.not_to raise_error
12
+ end
13
+
14
+ it "should accept an arbitrary ActiveRecord's instance " do
15
+ expect{ helper.cache(ArbitraryRecord.take, &@empty_block) }.not_to raise_error
16
+ end
17
+
18
+ it "should accept an arbitrary ActiveRecord's relation" do
19
+ expect{ helper.cache(ArbitraryRecord.where(true), &@empty_block) }.not_to raise_error
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'An ActiveRecord cache_key for all models' do
4
+ # Initialize some data
5
+ before :all do
6
+ 3.times {FactoryGirl.create :arbitrary_record}
7
+ end
8
+
9
+ it 'should be the same if there was no change in the records' do
10
+ old_cache_key = ArbitraryRecord.cache_key
11
+ # Check a random number of times
12
+ (1 + rand(10)).times do
13
+ new_cache_key = ArbitraryRecord.cache_key
14
+ old_cache_key.should be_eql new_cache_key
15
+ end
16
+ end
17
+
18
+ describe 'should update when' do
19
+ it 'a new record is inserted' do
20
+ old_cache_key = ArbitraryRecord.cache_key
21
+ FactoryGirl.create :arbitrary_record
22
+ old_cache_key.should_not be_eql ArbitraryRecord.cache_key
23
+ end
24
+
25
+ it 'a record is destroyed' do
26
+ old_cache_key = ArbitraryRecord.cache_key
27
+ ArbitraryRecord.take.destroy!
28
+ old_cache_key.should_not be_eql ArbitraryRecord.cache_key
29
+ end
30
+
31
+ it 'a record is updated' do
32
+ old_cache_key = ArbitraryRecord.cache_key
33
+ record = ArbitraryRecord.take
34
+ record.name = record.name.reverse
35
+ record.save
36
+ ArbitraryRecord.set_callback :commit, :after do
37
+ old_cache_key.should_not be_eql ArbitraryRecord.cache_key
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ describe 'An ActiveRecord cache_key for a relation' do
44
+ # Initialize some data
45
+ before :all do
46
+ 3.times {FactoryGirl.create :arbitrary_record}
47
+ end
48
+
49
+ it 'should be the same if there was no change in the records' do
50
+ old_cache_key = ArbitraryRecord.where('').cache_key
51
+ # Check a random number of times
52
+ (1 + rand(10)).times do
53
+ new_cache_key = ArbitraryRecord.where('').cache_key
54
+ old_cache_key.should be_eql new_cache_key
55
+ end
56
+ end
57
+
58
+ describe 'should update when' do
59
+ it 'a new record is inserted' do
60
+ old_cache_key = ArbitraryRecord.where('').cache_key
61
+ FactoryGirl.create :arbitrary_record
62
+ old_cache_key.should_not be_eql ArbitraryRecord.where('').cache_key
63
+ end
64
+
65
+ it 'a record is destroyed' do
66
+ old_cache_key = ArbitraryRecord.where('').cache_key
67
+ ArbitraryRecord.where('').first.destroy!
68
+ old_cache_key.should_not be_eql ArbitraryRecord.where('').cache_key
69
+ end
70
+
71
+ it 'a record is updated' do
72
+ old_cache_key = ArbitraryRecord.where('').cache_key
73
+ record = ArbitraryRecord.where('').first
74
+ record.name = record.name.reverse
75
+ record.save
76
+ ArbitraryRecord.set_callback :commit, :after do
77
+ old_cache_key.should_not be_eql ArbitraryRecord.where('').cache_key
78
+ end
79
+ end
80
+
81
+ it 'the conditions are the same, but the order is different' do
82
+ old_cache_key = ArbitraryRecord.order(:id).cache_key
83
+ old_cache_key.should_not be_eql ArbitraryRecord.order(:name).cache_key
84
+ end
85
+
86
+ it 'the conditions are the same, but the limit is different' do
87
+ old_cache_key = ArbitraryRecord.order(:id).limit(10).cache_key
88
+ old_cache_key.should_not be_eql ArbitraryRecord.order(:id).limit(5).cache_key
89
+ end
90
+
91
+ it 'the conditions are the same, but the offset is different' do
92
+ old_cache_key = ArbitraryRecord.order(:id).limit(10).offset(0).cache_key
93
+ old_cache_key.should_not be_eql ArbitraryRecord.order(:id).limit(10).offset(10).cache_key
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe "An arbitrary ActiveRecord's" do
4
+ # Initialize some data
5
+ before :all do
6
+ 3.times {FactoryGirl.create :arbitrary_record}
7
+ end
8
+
9
+ it 'class should respond to cache_key' do
10
+ ArbitraryRecord.should respond_to(:cache_key)
11
+ end
12
+
13
+ it 'instance should respond to cache_key' do
14
+ ArbitraryRecord.take.should respond_to(:cache_key)
15
+ end
16
+
17
+ it 'relation should respond to cache_key' do
18
+ ArbitraryRecord.where(true).should respond_to(:cache_key)
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ ENV["RAILS_ENV"] ||= 'test'
2
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
3
+ require 'rspec/rails'
4
+ require 'rspec/autorun'
5
+ require 'factory_girl'
6
+
7
+ ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
8
+ FactoryGirl.find_definitions
9
+
10
+ RSpec.configure do |config|
11
+ config.use_transactional_fixtures = true
12
+ config.order = "random"
13
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cach_em_all
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Oscar Esgalha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl_rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ description: CachEmAll is a gem which aims to make the use of Rails 4's fragment caching
84
+ even easier.
85
+ email:
86
+ - oscaresgalha@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - lib/cach_em_all.rb
92
+ - lib/cach_em_all/core_ext/active_record_relation_ext.rb
93
+ - lib/cach_em_all/core_ext/active_record_base_ext.rb
94
+ - lib/cach_em_all/version.rb
95
+ - lib/cach_em_all/railtie.rb
96
+ - MIT-LICENSE
97
+ - Rakefile
98
+ - spec/helper_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/key_value_spec.rb
101
+ - spec/respond_to_spec.rb
102
+ homepage: http://github.com/obranchr/cach_em_all
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.5
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Fragment cache helpers for rails 4.
126
+ test_files:
127
+ - spec/helper_spec.rb
128
+ - spec/spec_helper.rb
129
+ - spec/key_value_spec.rb
130
+ - spec/respond_to_spec.rb