acts_as_saveable 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,275 @@
1
+ shared_examples "a saver_model" do
2
+ let (:saveable_klass) { saveable.class }
3
+
4
+ it "should be saved on after a saver has saved" do
5
+ saveable.save_by :saver => saver
6
+ expect(saver.saved_on?(saveable)).to be true
7
+ expect(saver.saved_for?(saveable)).to be true
8
+ end
9
+
10
+ it "should not be saved on if a saver has not saved" do
11
+ expect(saver.saved_on?(saveable)).to be false
12
+ end
13
+
14
+ it "should be saved on after a saver has saved under scope" do
15
+ saveable.save_by :saver => saver, :save_scope => 'rank'
16
+ expect(saver.saved_on?(saveable, :save_scope => 'rank')).to be true
17
+ end
18
+
19
+ it "should not be saved on other scope after a saver has saved under one scope" do
20
+ saveable.save_by :saver => saver, :save_scope => 'rank'
21
+ expect(saver.saved_on?(saveable)).to be false
22
+ end
23
+
24
+ it "should be saved as true when a saver has saved true" do
25
+ saveable.save_by :saver => saver
26
+ expect(saver.saved_as_when_saved_on(saveable)).to be true
27
+ expect(saver.saved_as_when_saved_for(saveable)).to be true
28
+ end
29
+
30
+ it "should be saved as true when a saver has saved true under scope" do
31
+ saveable.save_by :saver => saver, :save_scope => 'rank'
32
+ expect(saver.saved_as_when_saved_for(saveable, :save_scope => 'rank')).to be true
33
+ end
34
+
35
+ it "should be saved as false when a saver has saved false" do
36
+ saveable.save_by :saver => saver, :saved => false
37
+ expect(saver.saved_as_when_saved_for(saveable)).to be false
38
+ end
39
+
40
+ it "should be saved as false when a saver has saved false under scope" do
41
+ saveable.save_by :saver => saver, :saved => false, :save_scope => 'rank'
42
+ expect(saver.saved_as_when_saved_for(saveable, :save_scope => 'rank')).to be false
43
+ end
44
+
45
+ it "should be saved as nil when a saver has never saved" do
46
+ expect(saver.saved_as_when_saving_on(saveable)).to be nil
47
+ end
48
+
49
+ it "should be saved as nil when a saver has never saved under the scope" do
50
+ saveable.save_by :saver => saver, :saved => false, :save_scope => 'rank'
51
+ expect(saver.saved_as_when_saving_on(saveable)).to be nil
52
+ end
53
+
54
+ it "should return true if saver has saved true" do
55
+ saveable.save_by :saver => saver
56
+ expect(saver.saved_up_on?(saveable)).to be true
57
+ end
58
+
59
+ it "should return false if saver has not saved true" do
60
+ saveable.save_by :saver => saver, :saved => false
61
+ expect(saver.saved_up_on?(saveable)).to be false
62
+ end
63
+
64
+ it "should return true if the saver has saved false" do
65
+ saveable.save_by :saver => saver, :saved => false
66
+ expect(saver.saved_down_on?(saveable)).to be true
67
+ end
68
+
69
+ it "should return false if the saver has not saved false" do
70
+ saveable.save_by :saver => saver, :saved => true
71
+ expect(saver.saved_down_on?(saveable)).to be false
72
+ end
73
+
74
+ it "should provide reserve functionality, saver can saved on saveable" do
75
+ saver.saved :saveable => saveable, :saved => 'bad'
76
+ expect(saver.saved_as_when_saving_on(saveable)).to be false
77
+ end
78
+
79
+ it "should allow the saver to saved up a model" do
80
+ saver.save_up_for saveable
81
+ expect(saveable.get_up_saves.first.saver).to eq(saver)
82
+ expect(saveable.saves_for.up.first.saver).to eq(saver)
83
+ end
84
+
85
+ it "should allow the saver to saved down a model" do
86
+ saver.save_down_for saveable
87
+ expect(saveable.get_down_saves.first.saver).to eq(saver)
88
+ expect(saveable.saves_for.down.first.saver).to eq(saver)
89
+ end
90
+
91
+ it "should allow the saver to unsaved a model" do
92
+ saver.save_up_for saveable
93
+ saver.unsave_for saveable
94
+ expect(saveable.find_saves_for.size).to eq(0)
95
+ expect(saveable.saves_for.count).to eq(0)
96
+ end
97
+
98
+ it "should get all of the savers saves" do
99
+ saver.save_up_for saveable
100
+ expect(saver.find_saves.size).to eq(1)
101
+ expect(saver.saves.up.count).to eq(1)
102
+ end
103
+
104
+ it "should get all of the savers up saves" do
105
+ saver.save_up_for saveable
106
+ expect(saver.find_up_saves.size).to eq(1)
107
+ expect(saver.saves.up.count).to eq(1)
108
+ end
109
+
110
+ it "should get all of the savers down saves" do
111
+ saver.save_down_for saveable
112
+ expect(saver.find_down_saves.size).to eq(1)
113
+ expect(saver.saves.down.count).to eq(1)
114
+ end
115
+
116
+ it "should get all of the saves saves for a class" do
117
+ saveable.save_by :saver => saver
118
+ saveable2.save_by :saver => saver, :saved => false
119
+ expect(saver.find_saves_for_class(saveable_klass).size).to eq(2)
120
+ expect(saver.saves.for_type(saveable_klass).count).to eq(2)
121
+ end
122
+
123
+ it "should get all of the savers up saves for a class" do
124
+ saveable.save_by :saver => saver
125
+ saveable2.save_by :saver => saver, :saved => false
126
+ expect(saver.find_up_saves_for_class(saveable_klass).size).to eq(1)
127
+ expect(saver.saves.up.for_type(saveable_klass).count).to eq(1)
128
+ end
129
+
130
+ it "should get all of the savers down saves for a class" do
131
+ saveable.save_by :saver => saver
132
+ saveable2.save_by :saver => saver, :saved => false
133
+ expect(saver.find_down_saves_for_class(saveable_klass).size).to eq(1)
134
+ expect(saver.saves.down.for_type(saveable_klass).count).to eq(1)
135
+ end
136
+
137
+ it "should be contained to instances" do
138
+ saver.saved :saveable => saveable, :saved => false
139
+ saver2.saved :saveable => saveable
140
+
141
+ expect(saver.saved_as_when_saving_on(saveable)).to be false
142
+ end
143
+
144
+ describe '#find_saved_items' do
145
+ it 'returns objects that a user has upsaved for' do
146
+ saveable.save_by :saver => saver
147
+ saveable2.save_by :saver => saver2
148
+ expect(saver.find_saved_items).to include saveable
149
+ expect(saver.find_saved_items.size).to eq(1)
150
+ end
151
+
152
+ it 'returns objects that a user has upsaved for, using scope' do
153
+ saveable.save_by :saver => saver, :save_scope => 'rank'
154
+ saveable2.save_by :saver => saver2, :save_scope => 'rank'
155
+ expect(saver.find_saved_items(:save_scope => 'rank')).to include saveable
156
+ expect(saver.find_saved_items(:save_scope => 'rank').size).to eq(1)
157
+ end
158
+
159
+ it 'returns objects that a user has downsaved for' do
160
+ saveable.save_down saver
161
+ saveable2.save_down saver2
162
+ expect(saver.find_saved_items).to include saveable
163
+ expect(saver.find_saved_items.size).to eq(1)
164
+ end
165
+
166
+ it 'returns objects that a user has downsaved for, using scope' do
167
+ saveable.save_down saver, :save_scope => 'rank'
168
+ saveable2.save_down saver2, :save_scope => 'rank'
169
+ expect(saver.find_saved_items(:save_scope => 'rank')).to include saveable
170
+ expect(saver.find_saved_items(:save_scope => 'rank').size).to eq(1)
171
+ end
172
+ end
173
+
174
+ describe '#find_up_saved_items' do
175
+ it 'returns objects that a user has upsaved for' do
176
+ saveable.save_by :saver => saver
177
+ saveable2.save_by :saver => saver2
178
+ expect(saver.find_up_saved_items).to include saveable
179
+ expect(saver.find_up_saved_items.size).to eq(1)
180
+ end
181
+
182
+ it 'returns objects that a user has upsaved for, using scope' do
183
+ saveable.save_by :saver => saver, :save_scope => 'rank'
184
+ saveable2.save_by :saver => saver2, :save_scope => 'rank'
185
+ expect(saver.find_up_saved_items(:save_scope => 'rank')).to include saveable
186
+ expect(saver.find_up_saved_items(:save_scope => 'rank').size).to eq(1)
187
+ end
188
+
189
+ it 'does not return objects that a user has downsaved for' do
190
+ saveable.save_down saver
191
+ expect(saver.find_up_saved_items.size).to eq(0)
192
+ end
193
+
194
+ it 'does not return objects that a user has downsaved for, using scope' do
195
+ saveable.save_down saver, :save_scope => 'rank'
196
+ expect(saver.find_up_saved_items(:save_scope => 'rank').size).to eq(0)
197
+ end
198
+ end
199
+
200
+ describe '#find_down_saved_items' do
201
+ it 'does not return objects that a user has upsaved for' do
202
+ saveable.save_by :saver => saver
203
+ expect(saver.find_down_saved_items.size).to eq(0)
204
+ end
205
+
206
+ it 'does not return objects that a user has upsaved for, using scope' do
207
+ saveable.save_by :saver => saver, :save_scope => 'rank'
208
+ expect(saver.find_down_saved_items(:save_scope => 'rank').size).to eq(0)
209
+ end
210
+
211
+ it 'returns objects that a user has downsaved for' do
212
+ saveable.save_down saver
213
+ saveable2.save_down saver2
214
+ expect(saver.find_down_saved_items).to include saveable
215
+ expect(saver.find_down_saved_items.size).to eq(1)
216
+ end
217
+
218
+ it 'returns objects that a user has downsaved for, using scope' do
219
+ saveable.save_down saver, :save_scope => 'rank'
220
+ saveable2.save_down saver2, :save_scope => 'rank'
221
+ expect(saver.find_down_saved_items(:save_scope => 'rank')).to include saveable
222
+ expect(saver.find_down_saved_items(:save_scope => 'rank').size).to eq(1)
223
+ end
224
+
225
+ end
226
+
227
+ describe '#get_saved' do
228
+ subject { saver.get_saved(saveable.class) }
229
+
230
+ it 'returns objects of a class that a saver has saved for' do
231
+ saveable.save_by :saver => saver
232
+ saveable2.save_down saver
233
+ expect(subject).to include saveable
234
+ expect(subject).to include saveable2
235
+ expect(subject.size).to eq(2)
236
+ end
237
+
238
+ it 'does not return objects of a class that a saver has saved for' do
239
+ saveable.save_by :saver => saver2
240
+ saveable2.save_by :saver => saver2
241
+ expect(subject.size).to eq(0)
242
+ end
243
+ end
244
+
245
+ describe '#get_up_saved' do
246
+ subject { saver.get_up_saved(saveable.class) }
247
+
248
+ it 'returns up saved items that a saver has saved for' do
249
+ saveable.save_by :saver => saver
250
+ expect(subject).to include saveable
251
+ expect(subject.size).to eq(1)
252
+ end
253
+
254
+ it 'does not return down saved items a saver has saved for' do
255
+ saveable.save_down saver
256
+ expect(subject.size).to eq(0)
257
+ end
258
+ end
259
+
260
+ describe '#get_down_saved' do
261
+ subject { saver.get_down_saved(saveable.class) }
262
+
263
+ it 'does not return up saved items that a saver has saved for' do
264
+ saveable.save_by :saver => saver
265
+ expect(subject.size).to eq(0)
266
+ end
267
+
268
+ it 'returns down saved items a saver has saved for' do
269
+ saveable.save_down saver
270
+ expect(subject).to include saveable
271
+ expect(subject.size).to eq(1)
272
+ end
273
+ end
274
+
275
+ end
@@ -0,0 +1,132 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'sqlite3'
3
+ require 'acts_as_saveable'
4
+
5
+ Dir["./spec/shared_example/**/*.rb"].sort.each {|f| require f}
6
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
7
+
8
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9
+
10
+ ActiveRecord::Schema.define(:version => 1) do
11
+ create_table :saves do |t|
12
+ t.references :saveable, :polymorphic => true
13
+ t.references :saver, :polymorphic => true
14
+
15
+ t.boolean :save_flag
16
+ t.string :save_scope
17
+ t.integer :save_weight
18
+
19
+ t.timestamps
20
+ end
21
+
22
+ add_index :saves, [:saveable_id, :saveable_type]
23
+ add_index :saves, [:saver_id, :saver_type]
24
+ add_index :saves, [:saver_id, :saver_type, :save_scope]
25
+ add_index :saves, [:saveable_id, :saveable_type, :save_scope]
26
+
27
+ create_table :savers do |t|
28
+ t.string :name
29
+ end
30
+
31
+ create_table :not_savers do |t|
32
+ t.string :name
33
+ end
34
+
35
+ create_table :saveables do |t|
36
+ t.string :name
37
+ end
38
+
39
+ create_table :saveable_savers do |t|
40
+ t.string :name
41
+ end
42
+
43
+ create_table :sti_saveables do |t|
44
+ t.string :name
45
+ t.string :type
46
+ end
47
+
48
+ create_table :sti_not_saveables do |t|
49
+ t.string :name
50
+ t.string :type
51
+ end
52
+
53
+ create_table :not_saveables do |t|
54
+ t.string :name
55
+ end
56
+
57
+ create_table :saveable_caches do |t|
58
+ t.string :name
59
+ t.integer :cached_saves_total
60
+ t.integer :cached_saves_score
61
+ t.integer :cached_saves_up
62
+ t.integer :cached_saves_down
63
+ t.integer :cached_weighted_total
64
+ t.integer :cached_weighted_score
65
+ t.float :cached_weighted_average
66
+
67
+ t.integer :cached_scoped_test_saves_total
68
+ t.integer :cached_scoped_test_saves_score
69
+ t.integer :cached_scoped_test_saves_up
70
+ t.integer :cached_scoped_test_saves_down
71
+ t.integer :cached_scoped_weighted_total
72
+ t.integer :cached_scoped_weighted_score
73
+ t.float :cached_scoped_weighted_average
74
+ end
75
+
76
+ end
77
+
78
+
79
+ class Saver < ActiveRecord::Base
80
+ acts_as_saver
81
+ end
82
+
83
+ class NotSaver < ActiveRecord::Base
84
+
85
+ end
86
+
87
+ class Saveable < ActiveRecord::Base
88
+ acts_as_saveable
89
+ validates_presence_of :name
90
+ end
91
+
92
+ class SaveableSaver < ActiveRecord::Base
93
+ acts_as_saveable
94
+ acts_as_saver
95
+ end
96
+
97
+ class StiSaveable < ActiveRecord::Base
98
+ acts_as_saveable
99
+ end
100
+
101
+ class ChildOfStiSaveable < StiSaveable
102
+ end
103
+
104
+ class StiNotSaveable < ActiveRecord::Base
105
+ validates_presence_of :name
106
+ end
107
+
108
+ class SaveableChildOfStiNotSaveable < StiNotSaveable
109
+ acts_as_saveable
110
+ end
111
+
112
+ class NotSaveable < ActiveRecord::Base
113
+ end
114
+
115
+ class SaveableCache < ActiveRecord::Base
116
+ acts_as_saveable
117
+ validates_presence_of :name
118
+ end
119
+
120
+ class ABoringClass
121
+ def self.hw
122
+ 'hello world'
123
+ end
124
+ end
125
+
126
+
127
+ def clean_database
128
+ models = [ActsAsSaveable::Save, Saver, NotSaver, Saveable, NotSaveable, SaveableCache]
129
+ models.each do |model|
130
+ ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
131
+ end
132
+ end
@@ -0,0 +1,30 @@
1
+ require 'acts_as_saveable'
2
+ require 'spec_helper'
3
+
4
+ describe ActsAsSaveable::Helpers::Words do
5
+
6
+ before :each do
7
+ @saved = ActsAsSaveable::Save.new
8
+ end
9
+
10
+ it "should know that like is a true saved" do
11
+ expect(@saved.saveable_words.that_mean_true).to include "like"
12
+ end
13
+
14
+ it "should know that bad is a false saved" do
15
+ expect(@saved.saveable_words.that_mean_false).to include "bad"
16
+ end
17
+
18
+ it "should be a saved for true when word is good" do
19
+ expect(@saved.saveable_words.meaning_of('good')).to be true
20
+ end
21
+
22
+ it "should be a saved for false when word is down" do
23
+ expect(@saved.saveable_words.meaning_of('down')).to be false
24
+ end
25
+
26
+ it "should be a saved for true when the word is unknown" do
27
+ expect(@saved.saveable_words.meaning_of('lsdhklkadhfs')).to be true
28
+ end
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_saveable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.1
5
+ platform: ruby
6
+ authors:
7
+ - Ali Ibrahim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.9
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.9
41
+ description: Rails gem to allowing records to be saveable
42
+ email:
43
+ - aliibrahim@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - README.markdown
52
+ - Rakefile
53
+ - acts_as_saveable.gemspec
54
+ - lib/acts_as_saveable.rb
55
+ - lib/acts_as_saveable/extenders/controller.rb
56
+ - lib/acts_as_saveable/extenders/saveable.rb
57
+ - lib/acts_as_saveable/extenders/saver.rb
58
+ - lib/acts_as_saveable/helpers/words.rb
59
+ - lib/acts_as_saveable/save.rb
60
+ - lib/acts_as_saveable/saveable.rb
61
+ - lib/acts_as_saveable/saver.rb
62
+ - lib/acts_as_saveable/version.rb
63
+ - lib/generators/acts_as_saveable/migration/migration_generator.rb
64
+ - lib/generators/acts_as_saveable/migration/templates/active_record/migration.rb
65
+ - spec/saveable_saver_spec.rb
66
+ - spec/saveable_spec.rb
67
+ - spec/saver_spec.rb
68
+ - spec/shared_example/saveable_model_spec.rb
69
+ - spec/shared_example/saver_model_spec.rb
70
+ - spec/spec_helper.rb
71
+ - spec/words_spec.rb
72
+ homepage: http://rubygems.org/gems/acts_as_saveable
73
+ licenses: []
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project: acts_as_saveable
91
+ rubygems_version: 2.5.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Rails gem to allowing records to be saveable
95
+ test_files:
96
+ - spec/saveable_saver_spec.rb
97
+ - spec/saveable_spec.rb
98
+ - spec/saver_spec.rb
99
+ - spec/shared_example/saveable_model_spec.rb
100
+ - spec/shared_example/saver_model_spec.rb
101
+ - spec/spec_helper.rb
102
+ - spec/words_spec.rb
103
+ has_rdoc: