sneaky-save 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MGRhZDliMTBiNWEwZDhlZThkYTIwMzdhMzNjNjQxMjYxZjM5Mzg1Mw==
5
- data.tar.gz: !binary |-
6
- YTcwMzBhNjJiOTY0ZjhiZDAyMTU4NTUzZWZlODRkYTVlOGIyZTlhNA==
2
+ SHA1:
3
+ metadata.gz: 9819547cbb741ef5983b1583f0dba3ea21d5e683
4
+ data.tar.gz: 14647c2c9aba00d24aacf85167b5982fefb43b06
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MmNiMmJlMzk2MTBjOTA1OTNhYmQwOWFlOTkyM2Y5YzhkYjQwNzVmMDY2M2Qw
10
- YmYwMTk0YWQxMTY3OGMzOTEwMTliNjk5MDAzYTRlNWY2NjliNGU2Yzk4MWY5
11
- YWUzOGZkYWMxMzQ3ZmU2OGFjOGZkNDljOWZhMGJkNGNjMjI2ZWY=
12
- data.tar.gz: !binary |-
13
- YTk3OTBmMWE1ZTdiZTA0ZGVkNDU0NGY3OTgyNTBkODU2MTRiODM5MWU1YWIy
14
- YmQzOThmNjFhY2E4ODZhNTZkM2IzYjlhMzM3OWNiMzljM2ZmNzI3OTBjMDkw
15
- ZmQxMTE2ZmUyNmVkODI2MTBkMDQxMmFlMGYwMmU5YzU4ZjdhZDk=
6
+ metadata.gz: 64abcfe769be6e0e5011c11c45664247f33ed6e7b25e6b5d292e64569b1496b2f58281de78d9035af1b31a76e76ae41dd7d91794c3557bfd513802b9e7fe6e93
7
+ data.tar.gz: 174ab52ca4496909d2aa2f3fa2cc303836f6282fab665f79c2477bf2212fd21928ee399efcdc66815b509e42d71186b57d99f02d847277926af35c3ea2464eaa
data/README.md CHANGED
@@ -17,11 +17,14 @@ gem 'sneaky-save', git: 'git://github.com/einzige/sneaky-save.git'
17
17
 
18
18
  ## Using
19
19
  ```ruby
20
- # Update
21
- @existed_record.sneaky_save
20
+ # Update. Returns true on success, false otherwise.
21
+ existing_record.sneaky_save
22
22
 
23
- # Insert
23
+ # Insert. Returns true on success, false otherwise.
24
24
  Model.new.sneaky_save
25
+
26
+ # Raise exception on failure.
27
+ record.sneaky_save!
25
28
  ```
26
29
 
27
30
  ## Running specs
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.1.0
@@ -13,14 +13,25 @@ module SneakySave
13
13
  # @return [false, true]
14
14
  def sneaky_save
15
15
  begin
16
- new_record? ? sneaky_create : sneaky_update
16
+ sneaky_create_or_update
17
17
  rescue ActiveRecord::StatementInvalid
18
18
  false
19
19
  end
20
20
  end
21
21
 
22
+ # Saves the record raising an exception if it fails.
23
+ # @return [true] if save was successful.
24
+ # @raise [ActiveRecord::StatementInvalid] if save failed.
25
+ def sneaky_save!
26
+ sneaky_create_or_update
27
+ end
28
+
22
29
  protected
23
30
 
31
+ def sneaky_create_or_update
32
+ new_record? ? sneaky_create : sneaky_update
33
+ end
34
+
24
35
  # Makes INSERT query in database without running any callbacks
25
36
  # @return [false, true]
26
37
  def sneaky_create
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{sneaky-save}
5
- s.version = '0.0.6'
5
+ s.version = '0.1.0'
6
6
 
7
7
  s.date = %q{2013-12-09}
8
8
  s.authors = ["Sergei Zinin (einzige)"]
@@ -1,67 +1,87 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SneakySave, use_connection: true do
4
- after :each do
5
- expect(subject.sneaky_save).to eq(true)
6
- end
7
-
8
4
  context 'new record' do
9
- subject { Fake.new }
5
+ subject { Fake.new(name: 'test') }
10
6
 
11
- it('returns true if everything is good') {}
7
+ describe '#sneaky_save' do
8
+ it 'returns true if everything is good' do
9
+ expect(subject.sneaky_save).to eq(true)
10
+ end
12
11
 
13
- it 'does insert of the new record' do
14
- allow(subject).to receive(:sneaky_create).once.and_return(true)
15
- end
12
+ it 'inserts the new record' do
13
+ allow(subject).to receive(:sneaky_create).once.and_return(true)
14
+ expect(subject.sneaky_save).to eq(true)
15
+ end
16
16
 
17
- it 'stores attributes in database' do
18
- subject.name = 'test'
19
- subject.sneaky_save
20
- subject.reload
21
- expect(subject.name).to eq('test')
22
- end
17
+ it 'stores attributes in database' do
18
+ subject.name = 'test'
19
+ expect(subject.sneaky_save).to eq(true)
20
+ subject.reload
21
+ expect(subject.name).to eq('test')
22
+ end
23
+
24
+ it 'does not call any callback' do
25
+ Fake.any_instance.should_not_receive :before_save_callback
26
+ subject.sneaky_save
27
+ end
23
28
 
24
- it 'does not call any callback' do
25
- Fake.any_instance.should_not_receive :before_save_callback
29
+ it 'does not call validations' do
30
+ expect_any_instance_of(Fake).to_not receive(:valid?)
31
+ subject.sneaky_save
32
+ end
26
33
  end
27
34
 
28
- it 'does not call validations' do
29
- Fake.any_instance.should_not_receive :valid?
35
+ describe '#sneaky_save!' do
36
+ it 'raises an exception when insert fails' do
37
+ subject.name = nil
38
+ expect { subject.sneaky_save! }.to raise_error(ActiveRecord::StatementInvalid)
39
+ end
30
40
  end
31
41
  end
32
42
 
33
43
  context 'existing record' do
34
- subject { Fake.create :name => 'test' }
44
+ subject { Fake.create! :name => 'test' }
35
45
 
36
- context 'record is not changed' do
37
- it 'does nothing' do
38
- Fake.should_not_receive(:update_all)
46
+ describe '#sneaky_save' do
47
+ context 'record is not changed' do
48
+ it 'does nothing' do
49
+ Fake.should_not_receive(:update_all)
50
+ expect(subject.sneaky_save).to eq(true)
51
+ end
39
52
  end
40
- end
41
53
 
42
- context 'record is changed' do
43
- after :each do
44
- subject.name = 'new name'
45
- end
54
+ context 'record is changed' do
55
+ it 'updates attributes' do
56
+ subject.should_receive(:sneaky_update).once.and_return true
57
+ subject.name = 'new name'
58
+ expect(subject.sneaky_save).to eq(true)
59
+ end
46
60
 
47
- it 'updates attributes' do
48
- subject.should be_valid
49
- subject.should_receive(:sneaky_update).once.and_return true
50
- end
61
+ it 'stores attributes in database' do
62
+ subject.name = 'new name'
63
+ expect(subject.sneaky_save).to eq(true)
64
+ subject.reload
65
+ expect(subject.name).to eq('new name')
66
+ expect(subject.sneaky_save).to eq(true)
67
+ end
51
68
 
52
- it 'stores attributes in database' do
53
- subject.name = 'new name'
54
- expect(subject.sneaky_save).to eq(true)
55
- subject.reload
56
- expect(subject.name).to eq('new name')
57
- end
69
+ it 'does not call any callback' do
70
+ subject.should_not_receive :before_save_callback
71
+ subject.sneaky_save
72
+ end
58
73
 
59
- it 'does not call any callback' do
60
- subject.should_not_receive :before_save_callback
74
+ it 'does not call validations' do
75
+ subject.should_not_receive :valid?
76
+ subject.sneaky_save
77
+ end
61
78
  end
79
+ end
62
80
 
63
- it 'does not call validations' do
64
- subject.should_not_receive :valid?
81
+ describe '#sneaky_save!' do
82
+ it 'raises an exception when update fails' do
83
+ subject.name = nil
84
+ expect { subject.sneaky_save! }.to raise_error(ActiveRecord::StatementInvalid)
65
85
  end
66
86
  end
67
87
  end
@@ -7,12 +7,12 @@ shared_context 'use connection', use_connection: true do
7
7
 
8
8
  ActiveRecord::Schema.define do
9
9
  create_table 'fakes' do |table|
10
- table.column :name, :string
10
+ table.column :name, :string, null: false
11
11
  end
12
12
  end
13
13
 
14
14
  class Fake < ActiveRecord::Base
15
- validates_presence_of :name
15
+ validates :name, presence: true
16
16
 
17
17
  before_save :before_save_callback
18
18
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sneaky-save
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergei Zinin (einzige)
@@ -14,28 +14,28 @@ dependencies:
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: ActiveRecord extension. Allows to save record without calling callbacks
@@ -46,8 +46,8 @@ extensions: []
46
46
  extra_rdoc_files:
47
47
  - README.md
48
48
  files:
49
- - .gitignore
50
- - .travis.yml
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
51
  - Gemfile
52
52
  - README.md
53
53
  - Rakefile
@@ -68,17 +68,17 @@ require_paths:
68
68
  - lib
69
69
  required_ruby_version: !ruby/object:Gem::Requirement
70
70
  requirements:
71
- - - ! '>='
71
+ - - ">="
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  requirements:
76
- - - ! '>='
76
+ - - ">="
77
77
  - !ruby/object:Gem::Version
78
78
  version: '0'
79
79
  requirements: []
80
80
  rubyforge_project:
81
- rubygems_version: 2.1.5
81
+ rubygems_version: 2.2.2
82
82
  signing_key:
83
83
  specification_version: 4
84
84
  summary: Allows to save record without calling callbacks and validations.