destroy_associates 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +1 -0
- data/destroy_associates.gemspec +27 -0
- data/lib/destroy_associates.rb +6 -0
- data/lib/destroy_associates/mixin_ar_base.rb +17 -0
- data/lib/destroy_associates/version.rb +3 -0
- data/spec/destroy_associates/mixin_ar_base_spec.rb +49 -0
- data/spec/spec_helper.rb +25 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eeacf5a5a2f58d6ab4d13defa00bae14b3dcfb7b
|
4
|
+
data.tar.gz: 5b4cb4b92395a8064ce603fdeb34820ac1ee426f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 15ac2d60e0f7058332a43ea364a6eb48ef4627c5b706d8ef0c2c25c5630cca468f95e3dd193dda0a8ec5da4c9d54063bab4b1437b9d9cf754caafbd88f13d28c
|
7
|
+
data.tar.gz: 663302d708572ca16a261b938bd4bad32f894699c4bd1d0430f25b371c99a0b13adb39d34859f8bd02f0571d59dbb36ce421b40821e11be849aa4c59350215dc
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 h-izumi
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# DestroyAssociates
|
2
|
+
|
3
|
+
Destroy association if specified attribute is blank.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'destroy_associates'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install destroy_associates
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Add "destroy_associates_if_blank" to model.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class User < ActiveRecord::Base
|
25
|
+
has_many :mails
|
26
|
+
accepts_nested_attributes_for :mails
|
27
|
+
|
28
|
+
destroy_associates_if_blank :mails, :address
|
29
|
+
end
|
30
|
+
|
31
|
+
class Mail < ActiveRecord::Base
|
32
|
+
belongs_to :user
|
33
|
+
attr_accessible :address
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
user.mails
|
39
|
+
=> [<Mail id: 1, user_id: 1, address: "alice@example.inc">]
|
40
|
+
|
41
|
+
user.mails.first.address = ""
|
42
|
+
user.save!
|
43
|
+
|
44
|
+
user.mails
|
45
|
+
=> []
|
46
|
+
```
|
47
|
+
|
48
|
+
## License
|
49
|
+
This gem is released under the MIT license.
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it ( http://github.com/h-izumi/destroy_associates/fork )
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -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
|
+
require 'destroy_associates/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "destroy_associates"
|
8
|
+
spec.version = DestroyAssociates::VERSION
|
9
|
+
spec.authors = ["h-izumi"]
|
10
|
+
spec.email = ["h-izumi@9uelle.jp"]
|
11
|
+
spec.summary = %q{Destroy associations if specified attribute is blank}
|
12
|
+
spec.description = %q{Destroy associations if specified attribute is blank}
|
13
|
+
spec.homepage = "https://github.com/h-izumi/destroy_associates"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", ">=3.2"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DestroyAssociates
|
2
|
+
module MixinARBase
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def destroy_associates_if_blank(assocs, attr)
|
9
|
+
before_save do
|
10
|
+
send(assocs).each do |assoc|
|
11
|
+
assoc.mark_for_destruction if assoc.send(attr).blank?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DestroyAssociates::MixinARBase do
|
4
|
+
create_temp_table(:users){}
|
5
|
+
create_temp_table(:mails) do |t|
|
6
|
+
t.references :user
|
7
|
+
t.string :address
|
8
|
+
end
|
9
|
+
|
10
|
+
before :all do
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
has_many :mails
|
13
|
+
accepts_nested_attributes_for :mails
|
14
|
+
destroy_associates_if_blank :mails, :address
|
15
|
+
end
|
16
|
+
|
17
|
+
class Mail < ActiveRecord::Base
|
18
|
+
belongs_to :user
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "ClassMethods" do
|
23
|
+
let(:user){ User.create! }
|
24
|
+
|
25
|
+
before do
|
26
|
+
user.mails.create!(address: "alice@sample.inc")
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".destroy_associates_if_blank" do
|
30
|
+
context "attribute value is blank" do
|
31
|
+
it "destroys associate" do
|
32
|
+
user.mails.first.address = ""
|
33
|
+
expect{
|
34
|
+
user.save!
|
35
|
+
}.to change(user.mails, :count).by(-1)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "attribute value is not blank" do
|
40
|
+
it "nothing to do" do
|
41
|
+
user.mails.first.address = "bob@sample.inc"
|
42
|
+
expect{
|
43
|
+
user.save!
|
44
|
+
}.not_to change(user.mails, :count)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
require "destroy_associates"
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.mock_framework = :rspec
|
7
|
+
config.before :all do
|
8
|
+
Dir.mkdir("tmp") unless Dir.exists?("tmp")
|
9
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "tmp/rspec.sqlite")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_temp_table(name, &block)
|
14
|
+
before :all do
|
15
|
+
migration = ActiveRecord::Migration.new
|
16
|
+
migration.verbose = false
|
17
|
+
migration.create_table name, &block
|
18
|
+
end
|
19
|
+
|
20
|
+
after :all do
|
21
|
+
migration = ActiveRecord::Migration.new
|
22
|
+
migration.verbose = false
|
23
|
+
migration.drop_table name
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: destroy_associates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- h-izumi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Destroy associations if specified attribute is blank
|
84
|
+
email:
|
85
|
+
- h-izumi@9uelle.jp
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- destroy_associates.gemspec
|
97
|
+
- lib/destroy_associates.rb
|
98
|
+
- lib/destroy_associates/mixin_ar_base.rb
|
99
|
+
- lib/destroy_associates/version.rb
|
100
|
+
- spec/destroy_associates/mixin_ar_base_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
homepage: https://github.com/h-izumi/destroy_associates
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.2.2
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Destroy associations if specified attribute is blank
|
126
|
+
test_files:
|
127
|
+
- spec/destroy_associates/mixin_ar_base_spec.rb
|
128
|
+
- spec/spec_helper.rb
|