simple_cacheable 1.4.1 → 1.5.0

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.
@@ -1,5 +1,13 @@
1
1
  class Account < ActiveRecord::Base
2
+ include Cacheable
2
3
  belongs_to :user
3
4
 
4
5
  belongs_to :group
6
+
7
+ belongs_to :account_location, class_name: Location,
8
+ foreign_key: :account_location_id
9
+
10
+ model_cache do
11
+ with_association :user, :account_location
12
+ end
5
13
  end
@@ -1,9 +1,15 @@
1
1
  class Post < ActiveRecord::Base
2
2
  include Cacheable
3
+ extend FriendlyId
3
4
 
4
5
  belongs_to :location
5
6
  belongs_to :user
6
7
 
8
+ friendly_modules = [:slugged]
9
+ friendly_modules << :finders if Cacheable.rails4?
10
+
11
+ friendly_id :title, use: friendly_modules
12
+
7
13
  has_many :comments, :as => :commentable
8
14
  has_many :images, :as => :viewable
9
15
 
@@ -13,9 +19,12 @@ class Post < ActiveRecord::Base
13
19
  with_key
14
20
  with_attribute :user_id
15
21
  with_association :user, :comments, :images, :tags
16
- with_class_method :default_post, :retrieve_with_user_id, :retrieve_with_both
22
+ with_class_method :retrieve_with_user_id, :retrieve_with_both, :default_post,
23
+ :where_options_are
17
24
  end
18
25
 
26
+ before_validation :create_slug
27
+
19
28
  def self.default_post
20
29
  Post.first
21
30
  end
@@ -28,4 +37,15 @@ class Post < ActiveRecord::Base
28
37
  Post.find(post_id) == Post.find_by_user_id(user_id)
29
38
  end
30
39
 
40
+ def self.where_options_are(options={})
41
+ Post.where(options).first
42
+ end
43
+
44
+ def create_slug
45
+ self.slug = title
46
+ end
47
+
48
+ def to_param
49
+ slug
50
+ end
31
51
  end
@@ -10,9 +10,10 @@ class User < ActiveRecord::Base
10
10
  model_cache do
11
11
  with_key
12
12
  with_attribute :login
13
- with_method :last_post
13
+ with_method :last_post, :bad_iv_name!, :bad_iv_name?, :admin?, :hash_with_class_key
14
14
  with_association :posts, :account, :images, :group
15
- with_class_method :default_name
15
+ with_class_method :default_name, :user_with_id, :user_with_email,
16
+ :users_with_ids, :users_with_ids_in, :user_with_attributes
16
17
  end
17
18
 
18
19
  def last_post
@@ -23,4 +24,39 @@ class User < ActiveRecord::Base
23
24
  "flyerhzm"
24
25
  end
25
26
 
27
+ def bad_iv_name!
28
+ 42
29
+ end
30
+
31
+ def bad_iv_name?
32
+ 44
33
+ end
34
+
35
+ def admin?
36
+ false
37
+ end
38
+
39
+ def hash_with_class_key
40
+ {:foo => "Bar", :class => "batman"}
41
+ end
42
+
43
+ # accepts a number
44
+ def self.user_with_id(id)
45
+ User.find(id)
46
+ end
47
+
48
+ # accepts a string
49
+ def self.user_with_email(email)
50
+ User.find_by_email(email)
51
+ end
52
+
53
+ # accepts an array
54
+ def self.users_with_ids(ids)
55
+ User.find(ids)
56
+ end
57
+
58
+ # accepts a range
59
+ def self.users_with_ids_in(range)
60
+ User.select { |u| range.include?(u.id) }
61
+ end
26
62
  end
@@ -3,10 +3,13 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
3
 
4
4
  require 'rails'
5
5
  require 'active_record'
6
+ require 'support/ar_patches'
7
+ require 'support/coder_macro'
6
8
  require 'rspec'
7
9
  require 'mocha/api'
8
10
  require 'memcached'
9
11
  require 'cacheable'
12
+ require 'friendly_id'
10
13
 
11
14
  # It needs this order otherwise cacheable throws
12
15
  # errors when looking for reflection classes
@@ -39,6 +42,8 @@ RSpec.configure do |config|
39
42
  config.filter_run focus: true
40
43
  config.run_all_when_everything_filtered = true
41
44
 
45
+ config.include CoderMacro
46
+
42
47
  config.before :all do
43
48
  ::ActiveRecord::Schema.define(:version => 1) do
44
49
  create_table :users do |t|
@@ -49,12 +54,14 @@ RSpec.configure do |config|
49
54
  create_table :accounts do |t|
50
55
  t.integer :user_id
51
56
  t.integer :group_id
57
+ t.integer :account_location_id
52
58
  end
53
59
 
54
60
  create_table :posts do |t|
55
61
  t.integer :user_id
56
62
  t.string :title
57
63
  t.integer :location_id
64
+ t.string :slug
58
65
  end
59
66
 
60
67
  create_table :comments do |t|
@@ -86,6 +93,10 @@ RSpec.configure do |config|
86
93
  end
87
94
  end
88
95
 
96
+ config.before :each do
97
+ Rails.cache.clear
98
+ end
99
+
89
100
  end
90
101
 
91
102
  config.after :all do
@@ -94,3 +105,5 @@ RSpec.configure do |config|
94
105
  end
95
106
  end
96
107
  end
108
+
109
+
@@ -0,0 +1,51 @@
1
+ # Monkey patch to raise exceptions from after_commit
2
+ # https://github.com/rails/rails/pull/11123
3
+
4
+ module ActiveRecord
5
+ module ConnectionAdapters
6
+ module DatabaseStatements
7
+ def within_new_transaction(options = {})
8
+ transaction = begin_transaction(options)
9
+ yield
10
+ rescue Exception => error
11
+ rollback_transaction if transaction
12
+ raise
13
+ ensure
14
+ begin
15
+ commit_transaction unless error
16
+ rescue Exception
17
+ rollback_transaction unless transaction.state.complete?
18
+ raise
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ module ActiveRecord
26
+ module ConnectionAdapters
27
+ # Not patched but need to be here
28
+ class Transaction; end
29
+ class ClosedTransaction < Transaction; end
30
+
31
+ class TransactionState
32
+ def complete?
33
+ committed? || rolledback?
34
+ end
35
+ end
36
+
37
+ class OpenTransaction < Transaction
38
+ def commit_records
39
+ @state.set_state(:committed)
40
+ records.uniq.each do |record|
41
+ begin
42
+ record.committed!
43
+ rescue => e
44
+ record.logger.error(e) if record.respond_to?(:logger) && record.logger
45
+ raise
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,12 @@
1
+ module CoderMacro
2
+
3
+ # Helper to give correct equivalent of what's in cache
4
+ def coder(object)
5
+ if object.is_a?(Array)
6
+ object.map {|obj| Cacheable.send(:coder_from_record, obj) }
7
+ else
8
+ Cacheable.send(:coder_from_record, object)
9
+ end
10
+ end
11
+
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_cacheable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-30 00:00:00.000000000 Z
12
+ date: 2014-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - '>='
19
19
  - !ruby/object:Gem::Version
20
- version: 3.0.0
20
+ version: '3'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - '>='
26
26
  - !ruby/object:Gem::Version
27
- version: 3.0.0
27
+ version: '3'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rspec
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -53,6 +53,20 @@ dependencies:
53
53
  - - '='
54
54
  - !ruby/object:Gem::Version
55
55
  version: 0.10.5
56
+ - !ruby/object:Gem::Dependency
57
+ name: friendly_id
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
56
70
  description: a simple cache implementation based on activerecord
57
71
  email:
58
72
  - flyerhzm@gmail.com
@@ -66,16 +80,22 @@ files:
66
80
  - .ruby-gemset
67
81
  - .ruby-version
68
82
  - .travis.yml
83
+ - Appraisals
69
84
  - ChangeLog
70
85
  - Gemfile
71
86
  - Gemfile.lock
72
87
  - README.md
73
88
  - Rakefile
74
89
  - cacheable.gemspec
90
+ - gemfiles/3.2.gemfile
91
+ - gemfiles/3.2.gemfile.lock
92
+ - gemfiles/4.0.gemfile
93
+ - gemfiles/4.0.gemfile.lock
75
94
  - lib/cacheable.rb
76
95
  - lib/cacheable/caches.rb
77
96
  - lib/cacheable/expiry.rb
78
97
  - lib/cacheable/keys.rb
98
+ - lib/cacheable/model_fetch.rb
79
99
  - lib/cacheable/types/association_cache.rb
80
100
  - lib/cacheable/types/attribute_cache.rb
81
101
  - lib/cacheable/types/class_method_cache.rb
@@ -83,6 +103,7 @@ files:
83
103
  - lib/cacheable/types/method_cache.rb
84
104
  - lib/cacheable/version.rb
85
105
  - spec/cacheable/expiry_cache_spec.rb
106
+ - spec/cacheable/model_fetch_spec.rb
86
107
  - spec/cacheable/types/association_cache_spec.rb
87
108
  - spec/cacheable/types/attribute_cache_spec.rb
88
109
  - spec/cacheable/types/class_method_cache_spec.rb
@@ -99,8 +120,11 @@ files:
99
120
  - spec/models/tag.rb
100
121
  - spec/models/user.rb
101
122
  - spec/spec_helper.rb
102
- homepage: https://github.com/flyerhzm/simple-cacheable
103
- licenses: []
123
+ - spec/support/ar_patches.rb
124
+ - spec/support/coder_macro.rb
125
+ homepage: https://github.com/flyerhzm/simple_cacheable
126
+ licenses:
127
+ - MIT
104
128
  metadata: {}
105
129
  post_install_message:
106
130
  rdoc_options: []
@@ -118,12 +142,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
142
  version: '0'
119
143
  requirements: []
120
144
  rubyforge_project:
121
- rubygems_version: 2.0.3
145
+ rubygems_version: 2.1.11
122
146
  signing_key:
123
147
  specification_version: 4
124
148
  summary: a simple cache implementation based on activerecord
125
149
  test_files:
126
150
  - spec/cacheable/expiry_cache_spec.rb
151
+ - spec/cacheable/model_fetch_spec.rb
127
152
  - spec/cacheable/types/association_cache_spec.rb
128
153
  - spec/cacheable/types/attribute_cache_spec.rb
129
154
  - spec/cacheable/types/class_method_cache_spec.rb
@@ -140,3 +165,5 @@ test_files:
140
165
  - spec/models/tag.rb
141
166
  - spec/models/user.rb
142
167
  - spec/spec_helper.rb
168
+ - spec/support/ar_patches.rb
169
+ - spec/support/coder_macro.rb