modularity-rails 0.15.0 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{LICENSE → MIT-LICENSE} +0 -0
- data/README.md +16 -15
- data/Rakefile +3 -14
- data/{vendor → app}/assets/javascripts/modularity/data/ajax_loader.coffee +0 -0
- data/{vendor → app}/assets/javascripts/modularity/data/cache.coffee +5 -0
- data/{vendor → app}/assets/javascripts/modularity/data/indexed_cache.coffee +8 -2
- data/{vendor → app}/assets/javascripts/modularity/data/persistence_manager.coffee +11 -0
- data/{vendor → app}/assets/javascripts/modularity/mixins/clickable.coffee +2 -2
- data/{vendor → app}/assets/javascripts/modularity/mixins/closable.coffee +0 -0
- data/{vendor/assets/javascripts/modularity.js.coffee → app/assets/javascripts/modularity/modularity.coffee} +3 -0
- data/{vendor → app}/assets/javascripts/modularity/modules/autogrow_textarea.coffee +2 -1
- data/{vendor → app}/assets/javascripts/modularity/modules/button.coffee +0 -0
- data/{vendor → app}/assets/javascripts/modularity/tools/array_tools.coffee +1 -0
- data/{vendor → app}/assets/javascripts/modularity/tools/object_tools.coffee +0 -0
- data/config/initializers/konacha.rb +6 -0
- data/lib/modularity-rails.rb +0 -1
- data/lib/modularity-rails/engine.rb +1 -0
- data/lib/modularity-rails/version.rb +1 -1
- metadata +80 -100
- data/.evergreen +0 -6
- data/.gitignore +0 -20
- data/.travis.yml +0 -8
- data/Gemfile +0 -4
- data/Guardfile +0 -8
- data/demo/.gitignore +0 -15
- data/demo/Gemfile +0 -23
- data/demo/README.rdoc +0 -261
- data/demo/Rakefile +0 -7
- data/demo/app/assets/images/rails.png +0 -0
- data/demo/app/assets/javascripts/application.coffee +0 -2
- data/demo/app/assets/stylesheets/application.css +0 -13
- data/demo/app/controllers/application_controller.rb +0 -3
- data/demo/app/helpers/application_helper.rb +0 -2
- data/demo/app/mailers/.gitkeep +0 -0
- data/demo/app/models/.gitkeep +0 -0
- data/demo/app/views/layouts/application.html.erb +0 -14
- data/demo/config.ru +0 -4
- data/demo/config/application.rb +0 -59
- data/demo/config/boot.rb +0 -6
- data/demo/config/database.yml +0 -25
- data/demo/config/environment.rb +0 -5
- data/demo/config/environments/development.rb +0 -37
- data/demo/config/environments/production.rb +0 -67
- data/demo/config/environments/test.rb +0 -37
- data/demo/config/initializers/backtrace_silencers.rb +0 -7
- data/demo/config/initializers/inflections.rb +0 -15
- data/demo/config/initializers/konacha.rb +0 -5
- data/demo/config/initializers/mime_types.rb +0 -5
- data/demo/config/initializers/secret_token.rb +0 -7
- data/demo/config/initializers/session_store.rb +0 -8
- data/demo/config/initializers/wrap_parameters.rb +0 -14
- data/demo/config/locales/en.yml +0 -5
- data/demo/config/routes.rb +0 -58
- data/demo/db/seeds.rb +0 -7
- data/demo/lib/assets/.gitkeep +0 -0
- data/demo/lib/tasks/.gitkeep +0 -0
- data/demo/log/.gitkeep +0 -0
- data/demo/public/404.html +0 -26
- data/demo/public/422.html +0 -26
- data/demo/public/500.html +0 -25
- data/demo/public/favicon.ico +0 -0
- data/demo/public/index.html +0 -241
- data/demo/public/robots.txt +0 -5
- data/demo/script/rails +0 -6
- data/demo/spec/javascripts/data/ajax_loader_spec.coffee +0 -132
- data/demo/spec/javascripts/data/cache_spec.coffee +0 -103
- data/demo/spec/javascripts/data/indexed_cache_spec.coffee +0 -51
- data/demo/spec/javascripts/data/persistence_manager_spec.coffee +0 -283
- data/demo/spec/javascripts/konacha_config.coffee +0 -2
- data/demo/spec/javascripts/mixins/closable_spec.coffee +0 -98
- data/demo/spec/javascripts/modularity_spec.coffee +0 -329
- data/demo/spec/javascripts/modules/autogrow_textarea_spec.coffee +0 -43
- data/demo/spec/javascripts/modules/button_spec.coffee +0 -39
- data/demo/spec/javascripts/modules/counter_button_spec.coffee +0 -57
- data/demo/spec/javascripts/sinon-chai.js +0 -116
- data/demo/spec/javascripts/sinon.js +0 -3555
- data/demo/spec/javascripts/spec_helper.coffee +0 -10
- data/demo/spec/javascripts/templates/autogrow_textarea.jst.ejs +0 -2
- data/demo/spec/javascripts/templates/button.jst.ejs +0 -3
- data/demo/spec/javascripts/templates/closable.jst.ejs +0 -14
- data/demo/spec/javascripts/templates/modularity.jst.ejs +0 -6
- data/demo/spec/javascripts/tools/array_tools_spec.coffee +0 -12
- data/demo/spec/javascripts/tools/object_tools_spec.coffee +0 -46
- data/demo/vendor/assets/javascripts/.gitkeep +0 -0
- data/demo/vendor/assets/stylesheets/.gitkeep +0 -0
- data/demo/vendor/plugins/.gitkeep +0 -0
- data/modularity-rails.gemspec +0 -22
- data/run_tests +0 -6
- data/vendor/assets/javascripts/modularity/modules/counter_button.coffee +0 -17
data/demo/public/robots.txt
DELETED
data/demo/script/rails
DELETED
@@ -1,6 +0,0 @@
|
|
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'
|
@@ -1,132 +0,0 @@
|
|
1
|
-
#= require spec_helper
|
2
|
-
#= require modularity/data/ajax_loader
|
3
|
-
|
4
|
-
|
5
|
-
describe 'ajax_loader', ->
|
6
|
-
|
7
|
-
ajax_loader = spy = spy_get = spy_fire = null
|
8
|
-
beforeEach ->
|
9
|
-
spy = sinon.spy()
|
10
|
-
spy_get = sinon.spy jQuery, 'get'
|
11
|
-
spy_fire = sinon.spy modularity, 'fire_global_event'
|
12
|
-
|
13
|
-
afterEach ->
|
14
|
-
spy_get.restore()
|
15
|
-
spy_fire.restore()
|
16
|
-
|
17
|
-
describe 'constructor', ->
|
18
|
-
|
19
|
-
it "stores the 'cache' settings", ->
|
20
|
-
loader = new modularity.AjaxLoader {caching: yes}
|
21
|
-
expect(loader.caching).to.be.true
|
22
|
-
|
23
|
-
it 'defaults to no caching', ->
|
24
|
-
loader = new modularity.AjaxLoader {}
|
25
|
-
expect(loader.caching).to.be.false
|
26
|
-
|
27
|
-
|
28
|
-
describe 'get', ->
|
29
|
-
url = "/users/4"
|
30
|
-
|
31
|
-
beforeEach ->
|
32
|
-
ajax_loader = new modularity.AjaxLoader {caching: yes}
|
33
|
-
|
34
|
-
describe 'first time request', ->
|
35
|
-
|
36
|
-
beforeEach ->
|
37
|
-
ajax_loader.get url, spy
|
38
|
-
|
39
|
-
it 'makes an ajax request', ->
|
40
|
-
expect(jQuery.get).to.have.been.calledOnce
|
41
|
-
|
42
|
-
it 'saves the callback for later', ->
|
43
|
-
expect(ajax_loader.cb_cache.get(url)).to.have.length 1
|
44
|
-
expect(ajax_loader.cb_cache.get(url)[0]).to.equal spy
|
45
|
-
|
46
|
-
it 'returns without calling the callback', ->
|
47
|
-
expect(spy).to.not.have.been.called
|
48
|
-
|
49
|
-
|
50
|
-
describe 'the request is already in progress', ->
|
51
|
-
|
52
|
-
existing_cb = sinon.spy()
|
53
|
-
new_cb = sinon.spy()
|
54
|
-
beforeEach ->
|
55
|
-
ajax_loader.cb_cache.add url, [existing_cb]
|
56
|
-
ajax_loader.get url, new_cb
|
57
|
-
|
58
|
-
it 'adds the callback to the callback cache', ->
|
59
|
-
expect(ajax_loader.cb_cache.get(url)).to.have.length 2
|
60
|
-
expect(ajax_loader.cb_cache.get(url)[0]).to.equal existing_cb
|
61
|
-
expect(ajax_loader.cb_cache.get(url)[1]).to.equal new_cb
|
62
|
-
|
63
|
-
it 'returns without calling the callback', ->
|
64
|
-
expect(new_cb).to.not.have.been.called
|
65
|
-
|
66
|
-
it 'does not make another ajax request', ->
|
67
|
-
expect(jQuery.get).to.not.have.been.called
|
68
|
-
|
69
|
-
it "does not fire the 'loading' event", ->
|
70
|
-
expect(modularity.fire_global_event).to.not.have.been.called
|
71
|
-
|
72
|
-
|
73
|
-
describe 'ajax request returns successfully', ->
|
74
|
-
|
75
|
-
old_jquery_get = null
|
76
|
-
beforeEach ->
|
77
|
-
jquery_callback = null
|
78
|
-
old_jquery_get = jQuery.get
|
79
|
-
jQuery.get = (url, callback) -> callback 'result'
|
80
|
-
|
81
|
-
afterEach ->
|
82
|
-
jQuery.get = old_jquery_get
|
83
|
-
|
84
|
-
describe 'with caching enabled', ->
|
85
|
-
beforeEach ->
|
86
|
-
ajax_loader.caching = true
|
87
|
-
ajax_loader.get url, spy
|
88
|
-
|
89
|
-
it 'calls the given callbacks with the server data', ->
|
90
|
-
expect(spy.calledWith('result')).to.be.true
|
91
|
-
|
92
|
-
it 'removes the callbacks from the callback cache', ->
|
93
|
-
expect(ajax_loader.cb_cache.get(url)).to.be.undefined
|
94
|
-
|
95
|
-
it 'stores the data in the data cache', ->
|
96
|
-
expect(ajax_loader.data_cache.get(url)).to.equal 'result'
|
97
|
-
|
98
|
-
it "fires the 'loading' event", ->
|
99
|
-
expect(modularity.fire_global_event.calledWith('AJAX_LOADED')).to.be.true
|
100
|
-
|
101
|
-
|
102
|
-
describe 'with caching disabled', ->
|
103
|
-
beforeEach ->
|
104
|
-
ajax_loader.caching = false
|
105
|
-
ajax_loader.get url, spy
|
106
|
-
|
107
|
-
it 'calls the given callbacks with the server data', ->
|
108
|
-
expect(spy.calledWith('result')).to.be.true
|
109
|
-
|
110
|
-
it 'removes the callbacks from the callback cache', ->
|
111
|
-
expect(ajax_loader.cb_cache.get(url)).to.be.undefined
|
112
|
-
|
113
|
-
it 'does not store the data in the data cache', ->
|
114
|
-
expect(ajax_loader.data_cache.get(url)).to.be.undefined
|
115
|
-
|
116
|
-
it "fires the 'loading' event", ->
|
117
|
-
expect(modularity.fire_global_event.calledWith('AJAX_LOADED')).to.be.true
|
118
|
-
|
119
|
-
|
120
|
-
describe 'the data has already been loaded', ->
|
121
|
-
|
122
|
-
it 'calls the callback with the cached data', ->
|
123
|
-
ajax_loader.data_cache.add url, "my data"
|
124
|
-
|
125
|
-
ajax_loader.get(url, spy)
|
126
|
-
|
127
|
-
expect(spy).to.have.been.called
|
128
|
-
expect(spy.calledWith('my data')).to.be.true
|
129
|
-
|
130
|
-
it "does not fire the 'loading' event", ->
|
131
|
-
expect(modularity.fire_global_event).to.not.have.been.called
|
132
|
-
|
@@ -1,103 +0,0 @@
|
|
1
|
-
#= require spec_helper
|
2
|
-
#= require modularity/data/cache
|
3
|
-
|
4
|
-
|
5
|
-
describe 'Cache', ->
|
6
|
-
|
7
|
-
cache = null
|
8
|
-
beforeEach ->
|
9
|
-
cache = new modularity.Cache()
|
10
|
-
|
11
|
-
|
12
|
-
describe 'constructor', ->
|
13
|
-
|
14
|
-
it 'initializes with an empty cache', ->
|
15
|
-
cache.cache.should.eql({})
|
16
|
-
|
17
|
-
|
18
|
-
describe 'add', ->
|
19
|
-
|
20
|
-
it 'stores the given data in the cache', ->
|
21
|
-
cache.add 'foo', 'bar'
|
22
|
-
cache.cache.should.eql({'foo': 'bar'})
|
23
|
-
|
24
|
-
it 'overwrites existing entries', ->
|
25
|
-
cache.cache = {'foo', 'bar'}
|
26
|
-
cache.add 'foo', 'new'
|
27
|
-
(cache.cache['foo']).should.be.equal('new')
|
28
|
-
|
29
|
-
|
30
|
-
describe 'remove', ->
|
31
|
-
|
32
|
-
it 'removes the entry with the given key from the chache', ->
|
33
|
-
cache.add 'foo', 'bar'
|
34
|
-
cache.remove 'foo'
|
35
|
-
expect(cache.get('foo')).to.be.undefined
|
36
|
-
cache.length().should.equal 0
|
37
|
-
|
38
|
-
it 'only removes the given data and leaves the rest of the cache alone', ->
|
39
|
-
cache.add 1, 'one'
|
40
|
-
cache.add 2, 'two'
|
41
|
-
cache.remove 1
|
42
|
-
expect(cache.get(1)).to.be.undefined
|
43
|
-
expect(cache.get(2)).to.equal 'two'
|
44
|
-
cache.length().should.equal 1
|
45
|
-
|
46
|
-
|
47
|
-
describe 'get', ->
|
48
|
-
|
49
|
-
it "returns undefined if the entry doesn't exist", ->
|
50
|
-
expect(cache.get('zonk')).to.be.undefined
|
51
|
-
|
52
|
-
it "returns the entry if it exists", ->
|
53
|
-
cache.add 'foo', 'bar'
|
54
|
-
cache.get('foo').should.equal('bar')
|
55
|
-
|
56
|
-
|
57
|
-
describe 'getMany', ->
|
58
|
-
|
59
|
-
result = null
|
60
|
-
beforeEach ->
|
61
|
-
cache.add('one', 1)
|
62
|
-
cache.add('two', 2)
|
63
|
-
result = cache.getMany ['one', 'three']
|
64
|
-
|
65
|
-
it "returns the values that exist in the 'found' structure", ->
|
66
|
-
result.found.should.eql({'one': 1})
|
67
|
-
|
68
|
-
it "returns the values that don't exist in the 'missing' structure", ->
|
69
|
-
result.missing.should.eql(['three'])
|
70
|
-
|
71
|
-
|
72
|
-
describe 'length', ->
|
73
|
-
|
74
|
-
it 'returns 0 for empty cache', ->
|
75
|
-
cache.length().should.equal 0
|
76
|
-
|
77
|
-
it 'returns the number of objects in the cache', ->
|
78
|
-
cache.add('foo', 'bar')
|
79
|
-
cache.add('fooz', 'baz')
|
80
|
-
cache.length().should.equal(2)
|
81
|
-
|
82
|
-
|
83
|
-
describe 'replaceAll', ->
|
84
|
-
|
85
|
-
it 'replaces the whole cache with the given data', ->
|
86
|
-
cache.add 'one', 1
|
87
|
-
cache.replaceAll {'one': 2, two: 3}
|
88
|
-
cache.length().should.equal(2)
|
89
|
-
cache.get('one').should.equal(2)
|
90
|
-
cache.get('two').should.equal(3)
|
91
|
-
|
92
|
-
describe 'with 2 parameters', ->
|
93
|
-
|
94
|
-
it 'parses the given array, and indexes on the given key', ->
|
95
|
-
ten = {id: 10, value: 'ten'}
|
96
|
-
twelve = {id: 12, value: 'twelve'}
|
97
|
-
|
98
|
-
cache.replaceAll [ten, twelve], 'id'
|
99
|
-
|
100
|
-
cache.length().should.equal 2
|
101
|
-
cache.get(10).should.equal ten
|
102
|
-
cache.get(12).should.equal twelve
|
103
|
-
|
@@ -1,51 +0,0 @@
|
|
1
|
-
#= require spec_helper
|
2
|
-
#= require modularity/data/indexed_cache
|
3
|
-
|
4
|
-
|
5
|
-
describe 'IndexedCache', ->
|
6
|
-
|
7
|
-
indexed_cache = null
|
8
|
-
entry_1 = {id: 1, value: 'one'}
|
9
|
-
entry_2 = {id: 2, value: 'two'}
|
10
|
-
beforeEach ->
|
11
|
-
indexed_cache = new modularity.IndexedCache 'id'
|
12
|
-
|
13
|
-
|
14
|
-
describe 'constructor', ->
|
15
|
-
|
16
|
-
it 'stores the given key attribute', ->
|
17
|
-
indexed_cache.key.should == 'id'
|
18
|
-
|
19
|
-
|
20
|
-
describe 'add', ->
|
21
|
-
|
22
|
-
beforeEach ->
|
23
|
-
indexed_cache.add entry_1
|
24
|
-
|
25
|
-
it 'adds the given element', ->
|
26
|
-
indexed_cache.cache.length().should.equal 1
|
27
|
-
indexed_cache.cache.cache[1].should.eql entry_1
|
28
|
-
|
29
|
-
|
30
|
-
describe 'add_all', ->
|
31
|
-
it 'adds all the given elements', ->
|
32
|
-
indexed_cache.add_all [entry_1, entry_2]
|
33
|
-
indexed_cache.length().should.equal 2
|
34
|
-
indexed_cache.cache.cache[1].should.eql entry_1
|
35
|
-
indexed_cache.cache.cache[2].should.eql entry_2
|
36
|
-
|
37
|
-
|
38
|
-
describe 'remove', ->
|
39
|
-
it 'removes the given object from the server', ->
|
40
|
-
indexed_cache.add entry_1
|
41
|
-
indexed_cache.remove entry_1
|
42
|
-
indexed_cache.length().should.equal 0
|
43
|
-
|
44
|
-
describe 'get', ->
|
45
|
-
it 'returns the element indexed by the given key', ->
|
46
|
-
indexed_cache.add entry_1
|
47
|
-
indexed_cache.get(1).should.eql entry_1
|
48
|
-
|
49
|
-
it "returns undefined if the element doesn't exist", ->
|
50
|
-
expect(indexed_cache.get(2)).to.be.undefined
|
51
|
-
|
@@ -1,283 +0,0 @@
|
|
1
|
-
#= require spec_helper
|
2
|
-
#= require modularity/data/persistence_manager
|
3
|
-
#= require jquery
|
4
|
-
|
5
|
-
|
6
|
-
describe 'PersistenceManager', ->
|
7
|
-
|
8
|
-
# Global variables for testing.
|
9
|
-
persistence_manager = null
|
10
|
-
entry_1 = {id: 1, value: 'one'}
|
11
|
-
entry_2 = {id: 2, value: 'two'}
|
12
|
-
|
13
|
-
beforeEach ->
|
14
|
-
persistence_manager = new modularity.PersistenceManager {url: '/users'}
|
15
|
-
|
16
|
-
|
17
|
-
describe 'constructor', ->
|
18
|
-
|
19
|
-
it 'initializes an empty server data cache', ->
|
20
|
-
persistence_manager.server_data.length().should.equal 0
|
21
|
-
|
22
|
-
it 'initializes an empty client data cache', ->
|
23
|
-
persistence_manager.client_data.length().should.eql 0
|
24
|
-
|
25
|
-
it 'stores the given base url', ->
|
26
|
-
persistence_manager.base_url.should.equal '/users'
|
27
|
-
|
28
|
-
it 'stores the given index key', ->
|
29
|
-
persistence_manager = new modularity.PersistenceManager {key: 'my_key', url: '/users'}
|
30
|
-
persistence_manager.key.should.equal 'my_key'
|
31
|
-
|
32
|
-
it "uses 'id' as the default if no key is given", ->
|
33
|
-
persistence_manager.key.should.equal 'id'
|
34
|
-
|
35
|
-
|
36
|
-
describe 'add_all', ->
|
37
|
-
|
38
|
-
it 'adds the given data to the server_data cache', ->
|
39
|
-
spy = sinon.spy persistence_manager.server_data, 'add_all'
|
40
|
-
data = [{id: 1}, {id: 2}]
|
41
|
-
persistence_manager.add_all data
|
42
|
-
spy.should.have.been.calledOnce
|
43
|
-
|
44
|
-
describe 'create', ->
|
45
|
-
new_obj = {value: 'foo'}
|
46
|
-
|
47
|
-
beforeEach ->
|
48
|
-
sinon.stub(jQuery, 'ajax').yieldsTo('success', entry_2)
|
49
|
-
|
50
|
-
afterEach ->
|
51
|
-
jQuery.ajax.restore()
|
52
|
-
|
53
|
-
it 'makes an ajax POST request to the CREATE url on the server', ->
|
54
|
-
persistence_manager.create new_obj, ->
|
55
|
-
jQuery.ajax.should.have.been.calledOnce
|
56
|
-
args = jQuery.ajax.args[0][0]
|
57
|
-
args.url.should.equal '/users.json'
|
58
|
-
args.type.should.equal 'POST'
|
59
|
-
|
60
|
-
it 'sends the object as payload', ->
|
61
|
-
persistence_manager.create new_obj, ->
|
62
|
-
args = jQuery.ajax.args[0][0]
|
63
|
-
args.data.should.equal new_obj
|
64
|
-
|
65
|
-
it "doesn't updates the server cache immediately because the object doesn't have an id", ->
|
66
|
-
persistence_manager.create new_obj, ->
|
67
|
-
expect(persistence_manager.server_data.get(1)).to.be.undefined
|
68
|
-
|
69
|
-
it 'updates the server cache with the server data when the call returns', (done) ->
|
70
|
-
persistence_manager.create {value: 'bar'}, (server_obj) ->
|
71
|
-
persistence_manager.server_data.get(server_obj.id).should.equal entry_2
|
72
|
-
done()
|
73
|
-
|
74
|
-
it 'calls the given callback when done', (done) ->
|
75
|
-
persistence_manager.create new_obj, ->
|
76
|
-
done()
|
77
|
-
|
78
|
-
|
79
|
-
describe 'delete', ->
|
80
|
-
|
81
|
-
beforeEach ->
|
82
|
-
sinon.stub(jQuery, 'ajax').yieldsTo('success')
|
83
|
-
|
84
|
-
afterEach ->
|
85
|
-
jQuery.ajax.restore()
|
86
|
-
|
87
|
-
it 'makes an ajax DELETE request to the item url on the server', ->
|
88
|
-
persistence_manager.delete entry_1
|
89
|
-
jQuery.ajax.should.have.been.calledOnce
|
90
|
-
args = jQuery.ajax.args[0][0]
|
91
|
-
args.url.should.equal '/users/1.json'
|
92
|
-
args.type.should.equal 'DELETE'
|
93
|
-
|
94
|
-
it 'removes the object from the client and server cache', ->
|
95
|
-
persistence_manager.server_data.add entry_1
|
96
|
-
persistence_manager.client_data.add entry_1
|
97
|
-
persistence_manager.delete entry_1
|
98
|
-
persistence_manager.server_data.length().should.equal 0
|
99
|
-
persistence_manager.client_data.length().should.equal 0
|
100
|
-
|
101
|
-
it 'calls the given callback when done', (done) ->
|
102
|
-
persistence_manager.delete entry_1, ->
|
103
|
-
done()
|
104
|
-
|
105
|
-
|
106
|
-
describe 'entry_url', ->
|
107
|
-
|
108
|
-
it 'returns the url to access a single entry', ->
|
109
|
-
persistence_manager.entry_url(entry_1).should.equal '/users/1.json'
|
110
|
-
|
111
|
-
|
112
|
-
describe 'load', ->
|
113
|
-
|
114
|
-
describe 'entry exists in client data cache', ->
|
115
|
-
it 'returns the entry from the @client_data cache if it exists there', (done) ->
|
116
|
-
persistence_manager.client_data.add entry_1
|
117
|
-
persistence_manager.load 1, (entry) ->
|
118
|
-
entry.should.equal entry_1
|
119
|
-
done()
|
120
|
-
|
121
|
-
describe 'entry exists in server data cache', ->
|
122
|
-
|
123
|
-
beforeEach ->
|
124
|
-
persistence_manager.server_data.add entry_1
|
125
|
-
|
126
|
-
it 'adds the entry to the client data cache', (done) ->
|
127
|
-
persistence_manager.load 1, ->
|
128
|
-
persistence_manager.client_data.length().should.equal 1
|
129
|
-
persistence_manager.client_data.get(1).should.eql entry_1
|
130
|
-
done()
|
131
|
-
|
132
|
-
it 'returns the entry from the client data cache', (done) ->
|
133
|
-
persistence_manager.load 1, (entry) ->
|
134
|
-
client_cache_entry = persistence_manager.client_data.get 1
|
135
|
-
entry.should.equal client_cache_entry
|
136
|
-
done()
|
137
|
-
|
138
|
-
it 'returns a different hash than the server data, so that the user can make changes to it', (done) ->
|
139
|
-
persistence_manager.load 1, (entry) ->
|
140
|
-
server_cache_entry = persistence_manager.server_data.get 1
|
141
|
-
|
142
|
-
entry.should.not.equal server_cache_entry
|
143
|
-
done()
|
144
|
-
|
145
|
-
|
146
|
-
describe "entry doesn't exist on the client at all", ->
|
147
|
-
|
148
|
-
beforeEach ->
|
149
|
-
sinon.stub(jQuery, 'ajax').yieldsTo('success', entry_1)
|
150
|
-
|
151
|
-
afterEach ->
|
152
|
-
jQuery.ajax.restore()
|
153
|
-
|
154
|
-
it "retrieves the entry from the server", (done) ->
|
155
|
-
persistence_manager.load 1, ->
|
156
|
-
jQuery.ajax.should.have.been.calledOnce
|
157
|
-
done()
|
158
|
-
|
159
|
-
|
160
|
-
it 'stores the entry in the server cache', (done) ->
|
161
|
-
persistence_manager.load 1, ->
|
162
|
-
persistence_manager.server_data.length().should.equal 1
|
163
|
-
persistence_manager.server_data.get(1).should.equal entry_1
|
164
|
-
done()
|
165
|
-
|
166
|
-
it 'stores a replica of the entry in the client cache', (done) ->
|
167
|
-
persistence_manager.load 1, ->
|
168
|
-
persistence_manager.client_data.get(1).should.not.equal entry_1
|
169
|
-
persistence_manager.client_data.get(1).should.eql entry_1
|
170
|
-
done()
|
171
|
-
|
172
|
-
it 'returns the client replica', (done) ->
|
173
|
-
persistence_manager.load 1, (entry) ->
|
174
|
-
persistence_manager.client_data.get(1).should.equal entry
|
175
|
-
persistence_manager.server_data.get(1).should.not.equal entry
|
176
|
-
done()
|
177
|
-
|
178
|
-
|
179
|
-
describe 'load_all', ->
|
180
|
-
|
181
|
-
loading_done_callback = null
|
182
|
-
beforeEach ->
|
183
|
-
sinon.stub(jQuery, 'ajax').yieldsTo('success', [entry_1, entry_2])
|
184
|
-
loading_done_callback = sinon.spy()
|
185
|
-
persistence_manager.load_all loading_done_callback, {'q': 'foo'},
|
186
|
-
|
187
|
-
afterEach ->
|
188
|
-
jQuery.ajax.restore()
|
189
|
-
|
190
|
-
it 'makes a request to the INDEX action of the server', ->
|
191
|
-
jQuery.ajax.should.have.been.calledOnce
|
192
|
-
jQuery.ajax.args[0][0].url.should.equal '/users.json'
|
193
|
-
|
194
|
-
it 'provides the given data as parameters to the request', ->
|
195
|
-
jQuery.ajax.args[0][0].data.should.eql {'q': 'foo'}
|
196
|
-
|
197
|
-
it 'fills the server cache with the response data', ->
|
198
|
-
persistence_manager.server_data.length().should.eql 2
|
199
|
-
persistence_manager.server_data.get(1).should.eql entry_1
|
200
|
-
persistence_manager.server_data.get(2).should.eql entry_2
|
201
|
-
|
202
|
-
it 'calls the given callback method when the data is available', ->
|
203
|
-
loading_done_callback.should.have.been.calledOnce
|
204
|
-
|
205
|
-
|
206
|
-
describe 'load_many', ->
|
207
|
-
|
208
|
-
describe 'all snippets in cache', ->
|
209
|
-
it 'returns an array of all requested snippets', ->
|
210
|
-
persistence_manager.add_all [{id: 1}, {id: 2}, {id: 3}]
|
211
|
-
persistence_manager.load_many [1,2], (snippets) ->
|
212
|
-
expect(snippets).to.have.length 2
|
213
|
-
expect(snippets[0].id).to.equal 1
|
214
|
-
expect(snippets[1].id).to.equal 2
|
215
|
-
|
216
|
-
|
217
|
-
describe 'some snippets are not cached', ->
|
218
|
-
|
219
|
-
it 'loads the missing snippets from the server'
|
220
|
-
|
221
|
-
|
222
|
-
describe 'save', ->
|
223
|
-
|
224
|
-
describe 'for unsaved objects', ->
|
225
|
-
it "calls the 'create' action", ->
|
226
|
-
sinon.stub persistence_manager, 'create'
|
227
|
-
persistence_manager.save {value: 'foo'}, ->
|
228
|
-
persistence_manager.create.should.have.been.calledOnce
|
229
|
-
|
230
|
-
describe 'for objects from the server', ->
|
231
|
-
it "calls the 'update' action", ->
|
232
|
-
sinon.stub persistence_manager, 'update'
|
233
|
-
persistence_manager.save entry_1, ->
|
234
|
-
persistence_manager.update.should.have.been.calledOnce
|
235
|
-
|
236
|
-
|
237
|
-
describe 'update', ->
|
238
|
-
|
239
|
-
beforeEach ->
|
240
|
-
sinon.stub(jQuery, 'ajax').yieldsTo('success', entry_2)
|
241
|
-
server_entry = {id: 1, title: 'server title', desc: 'server desc'}
|
242
|
-
persistence_manager.server_data.add server_entry
|
243
|
-
|
244
|
-
afterEach ->
|
245
|
-
jQuery.ajax.restore()
|
246
|
-
|
247
|
-
it 'makes an ajax PUT request to the UPDATE url on the server', ->
|
248
|
-
persistence_manager.update entry_1, ->
|
249
|
-
jQuery.ajax.should.have.been.calledOnce
|
250
|
-
args = jQuery.ajax.args[0][0]
|
251
|
-
args.url.should.equal '/users/1.json'
|
252
|
-
args.type.should.equal 'PUT'
|
253
|
-
|
254
|
-
it 'sends only updated colums of the object as payload', (done) ->
|
255
|
-
persistence_manager.load 1, (client_entry) ->
|
256
|
-
client_entry.title = 'client title'
|
257
|
-
|
258
|
-
persistence_manager.update client_entry
|
259
|
-
|
260
|
-
|
261
|
-
args = jQuery.ajax.args[0][0]
|
262
|
-
args.data.should.eql {id: 1, title: 'client title'}
|
263
|
-
done()
|
264
|
-
|
265
|
-
it "doesn't perform the call if nothing is changed", (done) ->
|
266
|
-
persistence_manager.load 1, (client_entry) ->
|
267
|
-
persistence_manager.update client_entry
|
268
|
-
jQuery.ajax.should.not.have.been.called
|
269
|
-
done()
|
270
|
-
|
271
|
-
it 'updates the server cache immediately with the given object', ->
|
272
|
-
persistence_manager.update entry_1, ->
|
273
|
-
persistence_manager.server_data.get(1).should.equal entry_1
|
274
|
-
|
275
|
-
it 'updates the server cache with the server object when done', (done) ->
|
276
|
-
persistence_manager.update entry_1, (server_obj) ->
|
277
|
-
persistence_manager.server_data.get(2).should.equal entry_2
|
278
|
-
done()
|
279
|
-
|
280
|
-
it 'calls the given callback when done', (done) ->
|
281
|
-
persistence_manager.update entry_1, ->
|
282
|
-
done()
|
283
|
-
|