cashier-ftbpro 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +9 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.md +195 -0
  5. data/Rakefile +11 -0
  6. data/cashier.gemspec +27 -0
  7. data/lib/cashier.rb +257 -0
  8. data/lib/cashier/adapters/cache_store.rb +66 -0
  9. data/lib/cashier/adapters/redis_store.rb +62 -0
  10. data/lib/cashier/cucumber.rb +6 -0
  11. data/lib/cashier/matchers.rb +38 -0
  12. data/lib/cashier/railtie.rb +9 -0
  13. data/lib/cashier/version.rb +3 -0
  14. data/spec/dummy/.gitignore +4 -0
  15. data/spec/dummy/Gemfile +31 -0
  16. data/spec/dummy/README +256 -0
  17. data/spec/dummy/Rakefile +7 -0
  18. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  19. data/spec/dummy/app/controllers/home_controller.rb +7 -0
  20. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  21. data/spec/dummy/app/helpers/home_helper.rb +2 -0
  22. data/spec/dummy/app/views/home/index.html.erb +1 -0
  23. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/spec/dummy/config.ru +4 -0
  25. data/spec/dummy/config/application.rb +44 -0
  26. data/spec/dummy/config/boot.rb +10 -0
  27. data/spec/dummy/config/environment.rb +5 -0
  28. data/spec/dummy/config/environments/development.rb +25 -0
  29. data/spec/dummy/config/environments/production.rb +46 -0
  30. data/spec/dummy/config/environments/test.rb +31 -0
  31. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  32. data/spec/dummy/config/initializers/inflections.rb +10 -0
  33. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  34. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  35. data/spec/dummy/config/initializers/session_store.rb +8 -0
  36. data/spec/dummy/config/locales/en.yml +5 -0
  37. data/spec/dummy/config/routes.rb +60 -0
  38. data/spec/dummy/db/seeds.rb +7 -0
  39. data/spec/dummy/lib/tasks/.gitkeep +0 -0
  40. data/spec/dummy/public/404.html +26 -0
  41. data/spec/dummy/public/422.html +26 -0
  42. data/spec/dummy/public/500.html +26 -0
  43. data/spec/dummy/public/favicon.ico +0 -0
  44. data/spec/dummy/public/images/rails.png +0 -0
  45. data/spec/dummy/public/javascripts/application.js +2 -0
  46. data/spec/dummy/public/javascripts/controls.js +965 -0
  47. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  48. data/spec/dummy/public/javascripts/effects.js +1123 -0
  49. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  50. data/spec/dummy/public/javascripts/rails.js +191 -0
  51. data/spec/dummy/public/robots.txt +5 -0
  52. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  53. data/spec/dummy/script/rails +6 -0
  54. data/spec/dummy/test/performance/browsing_test.rb +9 -0
  55. data/spec/dummy/test/test_helper.rb +13 -0
  56. data/spec/dummy/vendor/plugins/.gitkeep +0 -0
  57. data/spec/integration/rails_cache_integration_spec.rb +68 -0
  58. data/spec/integration/rails_configuration_spec.rb +9 -0
  59. data/spec/lib/cashier/adapters/cache_store_spec.rb +92 -0
  60. data/spec/lib/cashier/adapters/redis_store_spec.rb +107 -0
  61. data/spec/lib/cashier_spec.rb +154 -0
  62. data/spec/spec_helper.rb +48 -0
  63. metadata +255 -0
@@ -0,0 +1,191 @@
1
+ (function() {
2
+ // Technique from Juriy Zaytsev
3
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
4
+ function isEventSupported(eventName) {
5
+ var el = document.createElement('div');
6
+ eventName = 'on' + eventName;
7
+ var isSupported = (eventName in el);
8
+ if (!isSupported) {
9
+ el.setAttribute(eventName, 'return;');
10
+ isSupported = typeof el[eventName] == 'function';
11
+ }
12
+ el = null;
13
+ return isSupported;
14
+ }
15
+
16
+ function isForm(element) {
17
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
18
+ }
19
+
20
+ function isInput(element) {
21
+ if (Object.isElement(element)) {
22
+ var name = element.nodeName.toUpperCase()
23
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
24
+ }
25
+ else return false
26
+ }
27
+
28
+ var submitBubbles = isEventSupported('submit'),
29
+ changeBubbles = isEventSupported('change')
30
+
31
+ if (!submitBubbles || !changeBubbles) {
32
+ // augment the Event.Handler class to observe custom events when needed
33
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
34
+ function(init, element, eventName, selector, callback) {
35
+ init(element, eventName, selector, callback)
36
+ // is the handler being attached to an element that doesn't support this event?
37
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
38
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
39
+ // "submit" => "emulated:submit"
40
+ this.eventName = 'emulated:' + this.eventName
41
+ }
42
+ }
43
+ )
44
+ }
45
+
46
+ if (!submitBubbles) {
47
+ // discover forms on the page by observing focus events which always bubble
48
+ document.on('focusin', 'form', function(focusEvent, form) {
49
+ // special handler for the real "submit" event (one-time operation)
50
+ if (!form.retrieve('emulated:submit')) {
51
+ form.on('submit', function(submitEvent) {
52
+ var emulated = form.fire('emulated:submit', submitEvent, true)
53
+ // if custom event received preventDefault, cancel the real one too
54
+ if (emulated.returnValue === false) submitEvent.preventDefault()
55
+ })
56
+ form.store('emulated:submit', true)
57
+ }
58
+ })
59
+ }
60
+
61
+ if (!changeBubbles) {
62
+ // discover form inputs on the page
63
+ document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
64
+ // special handler for real "change" events
65
+ if (!input.retrieve('emulated:change')) {
66
+ input.on('change', function(changeEvent) {
67
+ input.fire('emulated:change', changeEvent, true)
68
+ })
69
+ input.store('emulated:change', true)
70
+ }
71
+ })
72
+ }
73
+
74
+ function handleRemote(element) {
75
+ var method, url, params;
76
+
77
+ var event = element.fire("ajax:before");
78
+ if (event.stopped) return false;
79
+
80
+ if (element.tagName.toLowerCase() === 'form') {
81
+ method = element.readAttribute('method') || 'post';
82
+ url = element.readAttribute('action');
83
+ params = element.serialize();
84
+ } else {
85
+ method = element.readAttribute('data-method') || 'get';
86
+ url = element.readAttribute('href');
87
+ params = {};
88
+ }
89
+
90
+ new Ajax.Request(url, {
91
+ method: method,
92
+ parameters: params,
93
+ evalScripts: true,
94
+
95
+ onComplete: function(request) { element.fire("ajax:complete", request); },
96
+ onSuccess: function(request) { element.fire("ajax:success", request); },
97
+ onFailure: function(request) { element.fire("ajax:failure", request); }
98
+ });
99
+
100
+ element.fire("ajax:after");
101
+ }
102
+
103
+ function handleMethod(element) {
104
+ var method = element.readAttribute('data-method'),
105
+ url = element.readAttribute('href'),
106
+ csrf_param = $$('meta[name=csrf-param]')[0],
107
+ csrf_token = $$('meta[name=csrf-token]')[0];
108
+
109
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
110
+ element.parentNode.insert(form);
111
+
112
+ if (method !== 'post') {
113
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
114
+ form.insert(field);
115
+ }
116
+
117
+ if (csrf_param) {
118
+ var param = csrf_param.readAttribute('content'),
119
+ token = csrf_token.readAttribute('content'),
120
+ field = new Element('input', { type: 'hidden', name: param, value: token });
121
+ form.insert(field);
122
+ }
123
+
124
+ form.submit();
125
+ }
126
+
127
+
128
+ document.on("click", "*[data-confirm]", function(event, element) {
129
+ var message = element.readAttribute('data-confirm');
130
+ if (!confirm(message)) event.stop();
131
+ });
132
+
133
+ document.on("click", "a[data-remote]", function(event, element) {
134
+ if (event.stopped) return;
135
+ handleRemote(element);
136
+ event.stop();
137
+ });
138
+
139
+ document.on("click", "a[data-method]", function(event, element) {
140
+ if (event.stopped) return;
141
+ handleMethod(element);
142
+ event.stop();
143
+ });
144
+
145
+ document.on("submit", function(event) {
146
+ var element = event.findElement(),
147
+ message = element.readAttribute('data-confirm');
148
+ if (message && !confirm(message)) {
149
+ event.stop();
150
+ return false;
151
+ }
152
+
153
+ var inputs = element.select("input[type=submit][data-disable-with]");
154
+ inputs.each(function(input) {
155
+ input.disabled = true;
156
+ input.writeAttribute('data-original-value', input.value);
157
+ input.value = input.readAttribute('data-disable-with');
158
+ });
159
+
160
+ var element = event.findElement("form[data-remote]");
161
+ if (element) {
162
+ handleRemote(element);
163
+ event.stop();
164
+ }
165
+ });
166
+
167
+ document.on("ajax:after", "form", function(event, element) {
168
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
169
+ inputs.each(function(input) {
170
+ input.value = input.readAttribute('data-original-value');
171
+ input.removeAttribute('data-original-value');
172
+ input.disabled = false;
173
+ });
174
+ });
175
+
176
+ Ajax.Responders.register({
177
+ onCreate: function(request) {
178
+ var csrf_meta_tag = $$('meta[name=csrf-token]')[0];
179
+
180
+ if (csrf_meta_tag) {
181
+ var header = 'X-CSRF-Token',
182
+ token = csrf_meta_tag.readAttribute('content');
183
+
184
+ if (!request.options.requestHeaders) {
185
+ request.options.requestHeaders = {};
186
+ }
187
+ request.options.requestHeaders[header] = token;
188
+ }
189
+ }
190
+ });
191
+ })();
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+ require 'rails/performance_test_help'
3
+
4
+ # Profiling results for each test method are written to tmp/performance.
5
+ class BrowsingTest < ActionDispatch::PerformanceTest
6
+ def test_homepage
7
+ get '/'
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path('../../config/environment', __FILE__)
3
+ require 'rails/test_help'
4
+
5
+ class ActiveSupport::TestCase
6
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
7
+ #
8
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
9
+ # -- they do not yet inherit this setting
10
+ fixtures :all
11
+
12
+ # Add more helper methods to be used by all tests here...
13
+ end
File without changes
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe "Rails cache integration" do
4
+ subject { Rails.cache }
5
+ let(:cashier) { Cashier }
6
+
7
+ before(:each) do
8
+ Cashier.adapter = :cache_store
9
+ end
10
+
11
+ it "should ensure that cache operations are instrumented" do
12
+ ActiveSupport::Cache::Store.instrument.should be_true
13
+ end
14
+
15
+ context "write" do
16
+ it "should write to cashier when I call Rails.cache.write with tags" do
17
+ cashier.should_receive(:store_fragment).with("foo", ["some_tag"])
18
+ subject.write("foo", "bar", :tag => ["some_tag"])
19
+ end
20
+
21
+ it "shuld not write to cashier when I call Rails.cache.write without tags" do
22
+ cashier.should_not_receive(:store_fragment)
23
+ subject.write("foo", "bar")
24
+ end
25
+
26
+ it "should not fail when I don't pass in any options" do
27
+ expect { subject.write("foo", "bar", nil) }.to_not raise_error
28
+ end
29
+ end
30
+
31
+ context "fetch" do
32
+ it "should write to cashier when I call Rails.cache.fetch with tags" do
33
+ cashier.should_receive(:store_fragment).with("foo", ["some_tag"])
34
+ subject.fetch("foo", :tag => ["some_tag"]) { "bar" }
35
+ end
36
+
37
+ it "shuld not write to cashier when I call Rails.cache.fetch without tags" do
38
+ cashier.should_not_receive(:store_fragment)
39
+ subject.fetch("foo") { "bar" }
40
+ end
41
+ end
42
+
43
+ context "read" do
44
+ it "should keep track of fragment container hierarchy" do
45
+ subject.fetch("foo1", :tag => ["some_tag", "some_other_tag"]) do
46
+ subject.fetch("foo2", :tag => ["some_inner_tag", "some_other_inner_tag"]) { "bar" }
47
+ end
48
+
49
+ cashier.get_containers(["some_inner_tag"]).should == ["some_tag", "some_other_tag"]
50
+ cashier.get_containers(["some_other_inner_tag"]).should == ["some_tag", "some_other_tag"]
51
+ end
52
+ end
53
+
54
+ context "expire" do
55
+ let(:notification_system) { ActiveSupport::Notifications }
56
+
57
+ it "should expire containers when expiring a tag" do
58
+ subject.fetch("foo3", :tag => ["outer_tag1", "outer_tag2"]) do
59
+ subject.fetch("foo4", :tag => ["inner_tag1", "inner_tag2"]) { "bar" }
60
+ end
61
+ cashier.adapter.get_fragments_for_tag("outer_tag1").should == ["foo3"]
62
+
63
+ cashier.expire("inner_tag1")
64
+ cashier.adapter.get_fragments_for_tag("outer_tag1").should == []
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Rails configuration" do
4
+ it "should be configuration through rails" do
5
+ Rails.application.config.cashier.adapter = :redis_store
6
+
7
+ Cashier.adapter.should == Cashier::Adapters::RedisStore
8
+ end
9
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cashier::Adapters::CacheStore do
4
+ subject { Cashier::Adapters::CacheStore }
5
+ let(:cache) { Rails.cache }
6
+
7
+ it "should store the fragment in a tag" do
8
+ subject.store_fragment_in_tag('fragment-key', 'dashboard')
9
+ cache.fetch('dashboard').should eql(['fragment-key'])
10
+ end
11
+
12
+ it "should store the tag in the tags array" do
13
+ subject.store_tags(["dashboard"])
14
+ cache.fetch(Cashier::CACHE_KEY).should eql(['dashboard'])
15
+ end
16
+
17
+ it "should return all of the fragments for a given tag" do
18
+ subject.store_fragment_in_tag('fragment-key', 'dashboard')
19
+ subject.store_fragment_in_tag('fragment-key-2', 'dashboard')
20
+ subject.store_fragment_in_tag('fragment-key-3', 'dashboard')
21
+
22
+ subject.get_fragments_for_tag('dashboard').length.should == 3
23
+ end
24
+
25
+ it "should delete a tag from the cache" do
26
+ subject.store_fragment_in_tag('fragment-key', 'dashboard')
27
+ Rails.cache.read('dashboard').should_not be_nil
28
+
29
+ subject.delete_tag('dashboard')
30
+ Rails.cache.read('dashboard').should be_nil
31
+ end
32
+
33
+ it "should return the list of tags" do
34
+ (1..5).each {|i| subject.store_tags(["tag-#{i}"])}
35
+ subject.tags.length.should == 5
36
+ end
37
+
38
+ it "should return the tags correctly" do
39
+ subject.store_tags(["tag-1", "tag-2", "tag-3"])
40
+ subject.tags.include?("tag-1").should be_true
41
+ end
42
+
43
+ it "should remove tags from the tags list" do
44
+ (1..5).each {|i| subject.store_tags(["tag-#{i}"])}
45
+ subject.remove_tags(["tag-1", "tag-2", "tag-3", "tag-4", "tag-5"])
46
+ subject.tags.length.should == 0
47
+ end
48
+
49
+ context "clear" do
50
+ before(:each) do
51
+ subject.store_tags(['dashboard'])
52
+ subject.store_tags(['settings'])
53
+ subject.store_tags(['email'])
54
+ end
55
+
56
+ it "should clear the cache and remove all of the tags" do
57
+ subject.should_receive(:remove_tags).with(['dashboard','settings','email'])
58
+ subject.clear
59
+ Rails.cache.read(Cashier::CACHE_KEY).should be_nil
60
+ end
61
+ end
62
+
63
+ context "keys" do
64
+ it "should return the list of keys" do
65
+ subject.store_tags(['dashboard', 'settings', 'email'])
66
+
67
+ subject.store_fragment_in_tag('key1', 'dashboard')
68
+ subject.store_fragment_in_tag('key2', 'settings')
69
+ subject.store_fragment_in_tag('key3', 'email')
70
+
71
+ subject.keys.should eql(%w(key1 key2 key3))
72
+ end
73
+ end
74
+
75
+ context "containers" do
76
+ it "should be added to tags" do
77
+ subject.add_tags_containers(['key1', 'key2'], ['container1', 'container2'])
78
+ cache.fetch('cashier-tag-containers:key1').should == ['container1', 'container2']
79
+ cache.fetch('cashier-tag-containers:key2').should == ['container1', 'container2']
80
+
81
+ subject.add_tags_containers(['key1', 'key3'], ['container3'])
82
+ cache.fetch('cashier-tag-containers:key1').should == ['container1', 'container2', 'container3']
83
+ cache.fetch('cashier-tag-containers:key3').should == ['container3']
84
+ end
85
+
86
+ it "should be returned for tags" do
87
+ subject.add_tags_containers(['key1', 'key2'], ['container1', 'container2'])
88
+ subject.get_tags_containers(['key1']).should == ['container1', 'container2']
89
+ subject.get_tags_containers(['key2']).should == ['container1', 'container2']
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cashier::Adapters::RedisStore do
4
+ subject { Cashier::Adapters::RedisStore }
5
+ let(:cache) { Rails.cache }
6
+ let(:redis) { subject.redis }
7
+
8
+ context "setting and getting the redis instance" do
9
+ it "should allow to set the redis instance" do
10
+ subject.respond_to?(:redis=).should be_true
11
+ end
12
+
13
+ it "should allow to get the redis instance" do
14
+ subject.respond_to?(:redis).should be_true
15
+ end
16
+ end
17
+
18
+ it "should return the redis instance you set" do
19
+ subject.redis = redis
20
+ subject.redis.should == redis
21
+ end
22
+
23
+ it "should store the fragment in a tag" do
24
+ subject.store_fragment_in_tag('fragment-key', 'dashboard')
25
+ redis.smembers('dashboard').should eql(['fragment-key'])
26
+ end
27
+
28
+ it "should store the tag in the tags array" do
29
+ subject.store_tags(["dashboard"])
30
+ redis.smembers(Cashier::CACHE_KEY).should eql(['dashboard'])
31
+ end
32
+
33
+ it "should return all of the fragments for a given tag" do
34
+ subject.store_fragment_in_tag('fragment-key', 'dashboard')
35
+ subject.store_fragment_in_tag('fragment-key-2', 'dashboard')
36
+ subject.store_fragment_in_tag('fragment-key-3', 'dashboard')
37
+
38
+ subject.get_fragments_for_tag('dashboard').length.should == 3
39
+ end
40
+
41
+ it "should delete a tag from the cache" do
42
+ subject.store_fragment_in_tag('fragment-key', 'dashboard')
43
+ redis.smembers('dashboard').should_not be_nil
44
+
45
+ subject.delete_tag('dashboard')
46
+ redis.exists('dashboard').should be_false
47
+ end
48
+
49
+ it "should return the list of tags" do
50
+ (1..5).each {|i| subject.store_tags(["tag-#{i}"])}
51
+ subject.tags.length.should == 5
52
+ end
53
+
54
+ it "should return the tags correctly" do
55
+ subject.store_tags(["tag-1", "tag-2", "tag-3"])
56
+ subject.tags.include?("tag-1").should be_true
57
+ end
58
+
59
+ it "should remove tags from the tags list" do
60
+ (1..5).each {|i| subject.store_tags(["tag-#{i}"])}
61
+ subject.remove_tags(["tag-1", "tag-2", "tag-3", "tag-4", "tag-5"])
62
+ subject.tags.length.should == 0
63
+ end
64
+
65
+ context "clear" do
66
+ before(:each) do
67
+ subject.store_tags(['dashboard'])
68
+ subject.store_tags(['settings'])
69
+ subject.store_tags(['email'])
70
+ end
71
+
72
+ it "should clear the cache and remove all of the tags" do
73
+ subject.clear
74
+ redis.smembers(Cashier::CACHE_KEY).should eql([])
75
+ end
76
+ end
77
+
78
+ context "keys" do
79
+ it "should return the list of keys" do
80
+ subject.store_tags(['dashboard', 'settings', 'email'])
81
+
82
+ subject.store_fragment_in_tag('key1', 'dashboard')
83
+ subject.store_fragment_in_tag('key2', 'settings')
84
+ subject.store_fragment_in_tag('key3', 'email')
85
+
86
+ subject.keys.sort.should eql(%w(key1 key2 key3))
87
+ end
88
+ end
89
+
90
+ context "containers" do
91
+ it "should be added to tags" do
92
+ subject.add_tags_containers(['key1', 'key2'], ['container1', 'container2'])
93
+ subject.add_tags_containers(['key1', 'key3'], ['container2', 'container3'])
94
+
95
+ subject.get_tags_containers(['key1']).should == ['container1', 'container2', 'container3']
96
+ subject.get_tags_containers(['key2']).should == ['container1', 'container2']
97
+ subject.get_tags_containers(['key1', 'key2']).should == ['container1', 'container2', 'container3']
98
+ end
99
+
100
+ it "should be returned for tags" do
101
+ subject.add_tags_containers(['key1', 'key2'], ['container1', 'container2'])
102
+ subject.get_tags_containers(['key1']).should == ['container1', 'container2']
103
+ subject.get_tags_containers(['key2']).should == ['container1', 'container2']
104
+ end
105
+ end
106
+
107
+ end