returning 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile*.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in returning.gemspec
4
+ gemspec
5
+
6
+ gem 'activerecord', '~> 3.0'
7
+ gem 'rake'
8
+ gem 'pg'
data/Gemfile3.1 ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in returning.gemspec
4
+ gemspec
5
+
6
+ gem 'activerecord', '~> 3.1.rc6'
7
+ gem 'rake'
8
+ gem 'pg'
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ Hi there,
2
+
3
+ Returning
4
+ =========
5
+
6
+ It's a simple implementation of the +returning+ for ActiveRecord 3.0 and 3.1.
7
+
8
+ How to use
9
+ ----------
10
+
11
+ Add it to your Gemfile
12
+
13
+ gem 'returning'
14
+
15
+ And then
16
+
17
+ # This sends UPDATE objects SET field = 42 WHERE id = 42 RETURNING generation
18
+ object.save(:returning => 'generation') # => #<0xblah Object generation=2>
19
+ # This sends DELETE FROM objects WHERE id = 42 RETURNING generation
20
+ object.destroy(:returning => 'generation') # => #<0xblah Object generation=3>
21
+
22
+ It returns readonly object with fields that are specified in the returning option.
23
+
24
+ Cheers
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,45 @@
1
+ module Returning
2
+ module ActiveRecord
3
+ module Adapter
4
+ def to_sql(arel)
5
+ if arel.respond_to?(:ast)
6
+ visitor.accept(arel.ast)
7
+ else
8
+ arel
9
+ end
10
+ end
11
+
12
+ def returning?
13
+ !!@_returning
14
+ end
15
+
16
+ def update(sql, name = nil, binds = [])
17
+ if @_returning
18
+ records = @_returning[1].find_by_sql("#{to_sql(sql)} RETURNING #{@_returning[0]}", binds)
19
+ records.each { |r| r.readonly! }
20
+ records
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def delete(arel, name = nil, binds = [])
27
+ if @_returning
28
+ records = @_returning[1].find_by_sql("#{to_sql(arel)} RETURNING #{@_returning[0]}", binds)
29
+ records.each { |r| r.readonly! }
30
+ records
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+
37
+ def returning(columns, klass)
38
+ old_returning, @_returning = @_returning, [columns, klass]
39
+ yield
40
+ ensure
41
+ @_returning = old_returning
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,49 @@
1
+ require 'active_support/concern'
2
+
3
+ module Returning
4
+ module ActiveRecord
5
+ module Returning
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ if method(:find_by_sql).arity == 1
10
+ class_eval do
11
+ def self.find_by_sql(sql, binds = [])
12
+ connection.select_all(sanitize_sql(sql), "#{name} Load").collect! { |record| instantiate(record) }
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ def save(options = {})
19
+ if r = options[:returning]
20
+ connection.returning(r, self.class) do
21
+ super()
22
+ end
23
+ else
24
+ super()
25
+ end
26
+ end
27
+
28
+ def create_or_update
29
+ if connection.returning? && !new_record?
30
+ fields = update
31
+ # if no attributes were changed return self
32
+ fields == 0 ? self : fields[0]
33
+ else
34
+ super
35
+ end
36
+ end
37
+
38
+ def destroy(options = {})
39
+ if r = options[:returning]
40
+ connection.returning(r, self.class) do
41
+ super()
42
+ end
43
+ else
44
+ super()
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Returning
2
+ VERSION = "0.0.1"
3
+ end
data/lib/returning.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "returning/version"
2
+ require 'returning/active_record/returning'
3
+ require 'returning/active_record/adapter'
4
+
5
+ require 'active_record'
6
+ require 'active_record/connection_adapters/postgresql_adapter'
7
+
8
+ module Returning
9
+ # Your code goes here...
10
+ end
11
+
12
+
13
+ ActiveRecord::Base.send(:include, Returning::ActiveRecord::Returning)
14
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, Returning::ActiveRecord::Adapter)
data/returning.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "returning/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "returning"
7
+ s.version = Returning::VERSION
8
+ s.authors = ["Eugene Pimenov"]
9
+ s.email = ["eugene@soocial.com"]
10
+ s.homepage = "http://github.com/libc/returning"
11
+ s.summary = %q{Add PostgreSQL RETURNING thingie to ActiveRecord}
12
+ s.description = %q{Simple implementation of RETURNING for ActiveRecord}
13
+
14
+ s.rubyforge_project = "returning"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'activerecord', '>= 3.0'
22
+ s.add_development_dependency 'rspec', '~> 2.0'
23
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Returning do
4
+ before do
5
+ Post.create :name => "hello", :author => "josh"
6
+ end
7
+
8
+ describe '#save' do
9
+ let(:post) { Post.first }
10
+ context 'when called with :returning => name' do
11
+ subject do
12
+ post.name = 'hello world'
13
+ post.save(:returning => 'name')
14
+ end
15
+
16
+ its('name') { should == 'hello world' }
17
+ it { should be_readonly }
18
+ end
19
+
20
+ context 'when nothing changed' do
21
+ it 'returns self' do
22
+ post.save(:returning => 'name').should eql(post)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#destroy' do
28
+ it 'returns the column passed to returning' do
29
+ post = Post.first
30
+ post.name = 'hello world'
31
+ post.destroy(:returning => 'name').name.should == 'hello world'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ Bundler.require
5
+
6
+ Dir['spec/support/**/*.rb'].each { |f| require f }
7
+
8
+ RSpec.configure do
9
+ end
@@ -0,0 +1,36 @@
1
+ require 'active_record'
2
+
3
+ config = {:adapter => 'postgresql', 'database' => 'returning_test', :host => 'localhost', :encoding => 'unicode'}
4
+ begin
5
+ ActiveRecord::Base.establish_connection config
6
+ ActiveRecord::Base.connection
7
+ rescue
8
+ begin
9
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
10
+ ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => 'utf8'))
11
+ ActiveRecord::Base.establish_connection(config)
12
+ rescue => e
13
+ STDERR.puts "**** Couldn't create database returning_test"
14
+ STDERR.puts "#{e.class}: #{e.message}"
15
+ STDERR.puts e.backtrace.join("\n")
16
+ exit 1
17
+ end
18
+ end
19
+
20
+
21
+ class Migration < ActiveRecord::Migration
22
+ def self.up
23
+ create_table :posts, :force => true do |t|
24
+ t.string :name, :author
25
+ end
26
+ end
27
+
28
+ def self.down
29
+ drop_table :posts
30
+ end
31
+ end
32
+
33
+ Migration.up
34
+
35
+ class Post < ActiveRecord::Base
36
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: returning
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Eugene Pimenov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-22 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 7
27
+ segments:
28
+ - 3
29
+ - 0
30
+ version: "3.0"
31
+ name: activerecord
32
+ prerelease: false
33
+ version_requirements: *id001
34
+ type: :runtime
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 2
44
+ - 0
45
+ version: "2.0"
46
+ name: rspec
47
+ prerelease: false
48
+ version_requirements: *id002
49
+ type: :development
50
+ description: Simple implementation of RETURNING for ActiveRecord
51
+ email:
52
+ - eugene@soocial.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - Gemfile3.1
63
+ - README.md
64
+ - Rakefile
65
+ - lib/returning.rb
66
+ - lib/returning/active_record/adapter.rb
67
+ - lib/returning/active_record/returning.rb
68
+ - lib/returning/version.rb
69
+ - returning.gemspec
70
+ - spec/returning_spec.rb
71
+ - spec/spec_helper.rb
72
+ - spec/support/ar.rb
73
+ homepage: http://github.com/libc/returning
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project: returning
102
+ rubygems_version: 1.8.5
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Add PostgreSQL RETURNING thingie to ActiveRecord
106
+ test_files:
107
+ - spec/returning_spec.rb
108
+ - spec/spec_helper.rb
109
+ - spec/support/ar.rb