active_record_upsert 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e82a70f09e1da45939fb0dba461762e5b7b2986
4
- data.tar.gz: 736288186198273924c7f0caee5bb94e0a05ce52
3
+ metadata.gz: 7882a124e75ba90b73063ee0972dbe78478a56f0
4
+ data.tar.gz: e2de676c14dc4ef582756549ddbcc357da6395e6
5
5
  SHA512:
6
- metadata.gz: edfd93a34947411bb9a390203bced749a2a95ca131e05ab1e0c31242f98197a4dfdcb92e9b9746538367377faf977dee1f452c6f7236ad88706aefc368e683ae
7
- data.tar.gz: 452a0b7898226ba626657d11d426b6e1cec5a805e5978b67b5785cb17868f5509dc61c443699f7d25fd8b594804d896f08e577e88a13a76b880541f63bdb5753
6
+ metadata.gz: 2ea854f8b213733904066171fac163e2524d9584a967510f93bf58797457d3227a72495e9b71789b40ec9d8a8cd5652d6cbda1374ccdd6641835cbb8b04f1bda
7
+ data.tar.gz: aceeeee29b6d44f2a91b5e5b91527994efe04ceec42931ea8d95031ab23f00902f71a8e973fe3b6fe220cdf8afd9a720044684f5976bf048638fefabb6dfa69d
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
+ --require spec_helper
1
2
  --format documentation
2
3
  --color
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # ActiveRecordUpsert
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_record_upsert`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Real upsert for PostgreSQL 9.5+ and ActiveRecord. Uses ON CONFLICT DO UPDATE.
6
4
 
7
5
  ## Installation
8
6
 
@@ -21,16 +19,26 @@ Or install it yourself as:
21
19
  $ gem install active_record_upsert
22
20
 
23
21
  ## Usage
22
+ Just use `ActiveRecord.upsert` or `ActiveRecord#upsert`.
23
+ *ActiveRecordUpsert* respects timestamps.
24
+
25
+ ```
26
+ class MyRecord < ActiveRecord::Base
27
+ end
24
28
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
29
+ MyRecord.create(name: 'foo', wisdom: 1)
30
+ => #<MyRecord id: 1, name: "foo", created_at: "2016-02-20 14:15:55", updated_at: "2016-02-20 14:15:55", wisdom: 1>
28
31
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+ MyRecord.upsert(id: 1, wisdom: 3)
33
+ => #<MyRecord id: 2, name: "foo", created_at: "2016-02-20 14:15:55", updated_at: "2016-02-20 14:18:15", wisdom: 3>
30
34
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+ r = MyRecord.new(id: 1)
36
+ r.name = 'bar'
37
+ r.upsert
38
+ => #<MyRecord id: 2, name: "bar", created_at: "2016-02-20 14:17:50", updated_at: "2016-02-20 14:18:49", wisdom: 3>
39
+ ```
32
40
 
33
41
  ## Contributing
34
42
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record_upsert.
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jesjos/active_record_upsert.
36
44
 
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Jesper Josefsson"]
10
10
  spec.email = ["jesper.josefsson@gmail.com"]
11
11
 
12
- spec.summary = %q{PG Upsert for AR}
13
-
12
+ spec.summary = %q{Real PostgreSQL upserts using ON CONFLICT for ActiveRecord}
13
+
14
14
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
15
  spec.bindir = "exe"
16
16
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
@@ -27,9 +27,9 @@ Gem::Specification.new do |spec|
27
27
  spec.add_runtime_dependency 'pg', '> 0'
28
28
  end
29
29
 
30
-
31
-
32
30
  spec.add_development_dependency "bundler", "~> 1.11"
33
31
  spec.add_development_dependency "rake", "~> 10.0"
34
32
  spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "pry", "> 0"
34
+ spec.add_development_dependency "database_cleaner", "~> 1.5.1"
35
35
  end
data/bin/console CHANGED
@@ -4,6 +4,8 @@ require "bundler/setup"
4
4
  require 'active_record'
5
5
  require 'active_record/connection_adapters/postgresql_adapter'
6
6
  require "active_record_upsert"
7
+ require File.join(__dir__, '../spec/setup')
8
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
7
9
 
8
10
  # You can add fixtures and/or initialization code here to make experimenting
9
11
  # with your gem easier. You can also use a different console, if you like.
@@ -1,11 +1,12 @@
1
1
  module ActiveRecordUpsert
2
2
  module ActiveRecord
3
3
  module PersistenceExtensions
4
- def upsert(*args)
4
+
5
+ def upsert
5
6
  raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
6
7
  values = run_callbacks(:save) {
7
8
  run_callbacks(:create) {
8
- _upsert_record(*args)
9
+ _upsert_record
9
10
  }
10
11
  }
11
12
  assign_attributes(values.first.to_h)
@@ -14,7 +15,7 @@ module ActiveRecordUpsert
14
15
  false
15
16
  end
16
17
 
17
- def _upsert_record(attribute_names = self.attribute_names)
18
+ def _upsert_record(attribute_names = changed)
18
19
  attributes_values = arel_attributes_with_values_for_create(attribute_names)
19
20
  values = self.class.unscoped.upsert attributes_values
20
21
  @new_record = false
@@ -24,7 +25,7 @@ module ActiveRecordUpsert
24
25
  module ClassMethods
25
26
  def upsert(attributes, &block)
26
27
  if attributes.is_a?(Array)
27
- attributes.collect { |attr| upsert(attr, &block) }
28
+ attributes.collect { |hash| upsert(hash, &block) }
28
29
  else
29
30
  new(attributes, &block).upsert
30
31
  end
@@ -32,7 +33,6 @@ module ActiveRecordUpsert
32
33
  end
33
34
  end
34
35
 
35
- puts 'JOJOJOJOJOJO'
36
36
  ::ActiveRecord::Base.prepend(PersistenceExtensions)
37
37
  ::ActiveRecord::Base.extend(PersistenceExtensions::ClassMethods)
38
38
  end
@@ -1,7 +1,7 @@
1
1
  module ActiveRecordUpsert
2
2
  module ActiveRecord
3
3
  module TimestampExtensions
4
- def _upsert_record
4
+ def _upsert_record(*args)
5
5
  if self.record_timestamps
6
6
  current_time = current_time_from_proper_timezone
7
7
 
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordUpsert
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_upsert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesper Josefsson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-18 00:00:00.000000000 Z
11
+ date: 2016-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -94,6 +94,34 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: database_cleaner
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.5.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.5.1
97
125
  description:
98
126
  email:
99
127
  - jesper.josefsson@gmail.com
@@ -152,5 +180,5 @@ rubyforge_project:
152
180
  rubygems_version: 2.5.1
153
181
  signing_key:
154
182
  specification_version: 4
155
- summary: PG Upsert for AR
183
+ summary: Real PostgreSQL upserts using ON CONFLICT for ActiveRecord
156
184
  test_files: []