update_or_create 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in update_or_create.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Manish Puri
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # UpdateOrCreate
2
+
3
+
4
+ This gem gives ActiveRecord a method called as update_or_create_by which can be used along with a block to update or create by the arguments passed.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'update_or_create'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install update_or_create
19
+
20
+ ## Usage
21
+
22
+ This gem gives ActiveRecord a method called as update_or_create_by which can be used along with a block to update or create by the arguments passed.
23
+ <code>
24
+ For Eg., <br/>
25
+ irb(main):007:0> User.update_or_create_by_first_name("Manish") do |rec| <br/>
26
+ irb(main):008:1* rec.last_name='Puri'<br/>
27
+ irb(main):009:1* rec.sex= 'Male'<br/>
28
+ irb(main):010:1> end<br/><br/>
29
+
30
+ => #<User id: 13, first_name: "Manish", last_name: "Puri", sex: "Male", address: nil, created_at: "2014-02-22 18:35:32", updated_at: "2014-02-22 18:36:16">
31
+ irb(main):010:0>
32
+ </code>
33
+ You can also use User.update_or_create_by_first_name_and_last_name("Manish","Puri") and pass the block.
34
+
35
+
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( http://github.com/manishspuri/update_or_create/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+
2
+ module UpdateOrCreate
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ # add your static(class) methods here
7
+ module ClassMethods
8
+ def method_missing_with_update_or_create(method_id, *args, &block)
9
+ method_name = method_id.to_s
10
+ if method_name =~ /^update_or_create_by_(.+)$/
11
+ update_or_create($1, *args, &block)
12
+ else
13
+ method_missing_without_update_or_create(method_id, *args, &block)
14
+ end
15
+ end
16
+
17
+ alias_method_chain :method_missing, :update_or_create
18
+
19
+ def update_or_create(search, *args, &block)
20
+ parameters = search.split("_and_")
21
+ params = Hash[ parameters.zip(args) ]
22
+ obj = where(params).first || self.new(params)
23
+ yield obj
24
+ obj.save
25
+ obj
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ ActiveRecord::Base.send(:include, UpdateOrCreate)
32
+
@@ -0,0 +1 @@
1
+ .gitkeep
@@ -0,0 +1,20 @@
1
+ require 'active_record'
2
+ require 'fileutils'
3
+ require 'sqlite3'
4
+
5
+ require_relative '../lib/update_or_create.rb'
6
+
7
+ FileUtils.rm_f 'test/setup/db/test.sqlite3'
8
+
9
+ ActiveRecord::Base.establish_connection(
10
+ :adapter => 'sqlite3',
11
+ :database => './test/setup/db/test.sqlite3'
12
+ )
13
+
14
+ ActiveRecord::Migration.class_eval do
15
+ create_table :fake_users do |t|
16
+ t.string :name
17
+ t.text :address
18
+ t.string :phone
19
+ end
20
+ end
@@ -0,0 +1,41 @@
1
+ require 'minitest/autorun'
2
+ require_relative 'test_helper'
3
+
4
+
5
+ class FakeUser < ActiveRecord::Base
6
+ def self.needs_attr_accessible?
7
+ ActiveRecord::VERSION::MAJOR == 3
8
+ end
9
+
10
+ if needs_attr_accessible?
11
+ attr_accessible :name, :address,:phone
12
+ end
13
+ end
14
+
15
+ class TestDifference < MiniTest::Test
16
+
17
+ def setup
18
+ @michael = FakeUser.create!(:name=>"Michael", :address=>'Mumbai')
19
+ end
20
+
21
+ def test_update_or_create_by_name_and_address
22
+ @john =FakeUser.update_or_create_by_name_and_address("John","mumbai") do |rec|
23
+ rec.phone = '233-222-333'
24
+ end
25
+
26
+ assert_equal('John',@john.name )
27
+ assert_equal('mumbai', @john.address )
28
+
29
+ end
30
+
31
+ def test_update_or_create_by_name_for_existing_record()
32
+ @michael =FakeUser.update_or_create_by_name("Michael") do |rec|
33
+ rec.phone = '233-222-333'
34
+ end
35
+
36
+ assert_equal(@michael.phone, '233-222-333')
37
+
38
+ end
39
+
40
+
41
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "update_or_create"
7
+ spec.version = '0.0.1'
8
+ spec.authors = ["Manish Puri"]
9
+ spec.email = ["manishspuri@gmail.com"]
10
+ spec.summary = %q{This gem defines the update_or_create method for activerecord objects}
11
+ spec.description = %q{This gem defines the update_or_create method for activerecord objects}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "minitest"
23
+ spec.add_dependency 'activerecord'
24
+
25
+
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: update_or_create
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Manish Puri
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activerecord
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: This gem defines the update_or_create method for activerecord objects
79
+ email:
80
+ - manishspuri@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/update_or_create.rb
91
+ - test/setup/db/.gitkeep
92
+ - test/setup/db/test.sqlite3
93
+ - test/test_helper.rb
94
+ - test/test_update_or_create.rb
95
+ - update_or_create.gemspec
96
+ homepage: ''
97
+ licenses:
98
+ - MIT
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.23
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: This gem defines the update_or_create method for activerecord objects
121
+ test_files:
122
+ - test/setup/db/.gitkeep
123
+ - test/setup/db/test.sqlite3
124
+ - test/test_helper.rb
125
+ - test/test_update_or_create.rb