activerecord-safe_initialize 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +78 -0
- data/Rakefile +9 -0
- data/activerecord-safe_initialize.gemspec +26 -0
- data/lib/active_record/safe_initialize.rb +22 -0
- data/lib/active_record/safe_initialize/version.rb +5 -0
- data/lib/activerecord-safe_initialize.rb +1 -0
- data/spec/active_record/safe_initialize_spec.rb +52 -0
- data/spec/post_spec.rb +32 -0
- data/spec/spec_helper.rb +35 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NDY2MmRmY2VlOWNiOGNiMmMxYWNiOTE1MTAxZjk5MGM3MzVlNWE3MA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTkyOWU3OWJiZTk5ZDk4NzcxYmM2MjM4ZjA3YzAyMDRmZjk3MzdmYg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MGU5ZDZmM2E3MWNmMDhlZWI2Yzg1MWRlMTYzNWI5ODNhZDI1YmVhNjM5NzUz
|
10
|
+
YjYwMzllYzk0MGQwNDA2NjRkMmYzMTUwOGM5YWVhYjAxMjFkNjJiNmQ1YjM1
|
11
|
+
M2I3ODNhY2ZjN2QxNDkzNDJkMzA0MTVkMTNkNmQwNDg4NTdiODU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NTllYmQzNmU5MmU3MGJmYzU1N2I2ODAzNmJlNWEwNTJiYmJjMjdiYTIwYjU0
|
14
|
+
Y2JkZWRiNzc2ZmFjNzhlMjI5YjhkMjM0YTc5ZjZjYmIxNDM2YWM5OTM5OWE0
|
15
|
+
OWY1MjIyNTM3ZDFhMGE3Yjk2MGVkY2QxODk2NTViYWU2ZGQ2ZDE=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Lyconic
|
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,78 @@
|
|
1
|
+
# ActiveRecord::SafeInitialize
|
2
|
+
[![Build Status](https://travis-ci.org/lyconic/activerecord-safe_initialize.svg)](https://travis-ci.org/lyconic/activerecord-safe_initialize)
|
3
|
+
|
4
|
+
Safely initialize an ActiveRecord attribute with respect to missing columns.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'activerecord-safe_initialize'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install activerecord-safe_initialize
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
If you want to select subsets of SQL tables, you have to use `after_initialize` with care:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class Post < ActiveRecord::Base
|
28
|
+
after_initialize { self.category ||= 'Default' }
|
29
|
+
end
|
30
|
+
|
31
|
+
Post.select([:title, :body, :published_at]).first # => ActiveModel::MissingAttributeError
|
32
|
+
```
|
33
|
+
|
34
|
+
To do this safely you must write:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class Post < ActiveRecord::Base
|
38
|
+
after_initialize { self.category ||= 'Default' if has_attribute?(:category) }
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
With `safe_initialize` you can write:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class Post < ActiveRecord::Base
|
46
|
+
safe_initialize :category, with: 'Default'
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
If `with` is a Symbol, `safe_initialize` will send it to `self` to get the value:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class Post < ActiveRecord::Base
|
54
|
+
safe_initialize :uuid, with: :generate_uuid
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def generate_uuid
|
59
|
+
SecureRandom.uuid
|
60
|
+
end
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
If `with` is callable, it will `instance_exec` it to get the value:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class Post < ActiveRecord::Base
|
68
|
+
safe_initialize :uuid, with: ->{ SecureRandom.uuid }
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
1. Fork it ( https://github.com/lyconic/activerecord-safe_initialize/fork )
|
75
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
76
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
77
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
78
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_record/safe_initialize/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord-safe_initialize"
|
8
|
+
spec.version = ActiveRecord::SafeInitialize::VERSION
|
9
|
+
spec.authors = ["Adam Lassek"]
|
10
|
+
spec.email = ["alassek@lyconic.com"]
|
11
|
+
spec.summary = %q{Safely initialize an ActiveRecord attribute with respect to missing columns}
|
12
|
+
spec.homepage = "https://github.com/lyconic/activerecord-safe_initialize"
|
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_dependency "activerecord", ">= 3.1"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
25
|
+
spec.add_development_dependency "sqlite3"
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_record/safe_initialize/version"
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
module SafeInitialize
|
6
|
+
def safe_initialize(attribute, options = {})
|
7
|
+
raise ArgumentError, "Missing initialization value" unless options[:with]
|
8
|
+
|
9
|
+
after_initialize do
|
10
|
+
if has_attribute?(attribute) && read_attribute(attribute).nil?
|
11
|
+
value = options[:with]
|
12
|
+
value = instance_exec(&value) if value.respond_to?(:call)
|
13
|
+
value = self.send(value) if value.is_a?(Symbol)
|
14
|
+
|
15
|
+
self.send "#{attribute}=", value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ActiveRecord::Base.extend ActiveRecord::SafeInitialize
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_record/safe_initialize'
|
@@ -0,0 +1,52 @@
|
|
1
|
+
describe ActiveRecord::SafeInitialize do
|
2
|
+
before(:all) { Post.create(title: "Test Post", body: "This is a test.", uuid: nil) }
|
3
|
+
after(:all) { Post.first.destroy }
|
4
|
+
|
5
|
+
context "with: missing" do
|
6
|
+
it "raises ArgumentError" do
|
7
|
+
expect{ Post.safe_initialize(:uuid) }.to raise_error(ArgumentError)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with: String" do
|
12
|
+
it "uses the value as the default" do
|
13
|
+
klass = Class.new(Post)
|
14
|
+
klass.safe_initialize :uuid, with: 'foo'
|
15
|
+
|
16
|
+
expect( klass.first.uuid ).to eq 'foo'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "with: Symbol" do
|
21
|
+
it "sends the symbol to self to get value" do
|
22
|
+
klass = Class.new(Post)
|
23
|
+
klass.class_eval { def init_uuid; 'bar'; end }
|
24
|
+
klass.safe_initialize :uuid, :with => :init_uuid
|
25
|
+
|
26
|
+
expect( klass.first.uuid ).to eq 'bar'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "with: callable" do
|
31
|
+
it "instance execs the callable to get the value" do
|
32
|
+
klass = Class.new(Post)
|
33
|
+
klass.class_eval { def init_uuid; 'baz'; end }
|
34
|
+
klass.safe_initialize :uuid, with: ->{ self.init_uuid }
|
35
|
+
|
36
|
+
expect( klass.first.uuid ).to eq 'baz'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "existing value" do
|
41
|
+
let(:uuid) { SecureRandom.uuid }
|
42
|
+
|
43
|
+
before(:each) { Post.first.update_attribute :uuid, uuid }
|
44
|
+
|
45
|
+
it "does nothing" do
|
46
|
+
klass = Class.new(Post)
|
47
|
+
klass.safe_initialize :uuid, with: 'foo'
|
48
|
+
|
49
|
+
expect( klass.first.uuid ).to eq uuid
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/post_spec.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
describe Post do
|
2
|
+
before(:all) { Post.create(title: "Test Post", body: "This is a test.", uuid: SecureRandom.uuid) }
|
3
|
+
after(:all) { Post.first.destroy }
|
4
|
+
|
5
|
+
context "with standard after_initialize" do
|
6
|
+
let(:klass) do
|
7
|
+
Class.new(Post).tap do |k|
|
8
|
+
k.after_initialize { self.uuid ||= SecureRandom.uuid }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "works fine when all columns are selected" do
|
13
|
+
expect{ klass.first }.to_not raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it "raises ActiveModel::MissingAttributeError when uuid is not selected" do
|
17
|
+
expect{ klass.select(:title).first }.to raise_error(ActiveModel::MissingAttributeError)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "with safe_initialize" do
|
22
|
+
let(:klass) do
|
23
|
+
Class.new(Post).tap do |k|
|
24
|
+
k.safe_initialize :uuid, with: ->{ SecureRandom.uuid }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "does not raise an error when uuid is missing" do
|
29
|
+
expect{ klass.select(:title).first }.to_not raise_error
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'active_record/safe_initialize'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
ActiveRecord::Base.configurations = {
|
5
|
+
'test' => {
|
6
|
+
'adapter' => 'sqlite3',
|
7
|
+
'database' => ':memory:',
|
8
|
+
'verbosity' => 'quiet'
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection :test
|
13
|
+
|
14
|
+
ActiveRecord::Migration.verbose = false
|
15
|
+
ActiveRecord::Schema.define(version: 20150830080017) do
|
16
|
+
|
17
|
+
create_table "posts", force: true do |t|
|
18
|
+
t.string "title"
|
19
|
+
t.text "body"
|
20
|
+
t.string "uuid"
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class Post < ActiveRecord::Base; end
|
26
|
+
|
27
|
+
RSpec.configure do |config|
|
28
|
+
config.expect_with :rspec do |expectations|
|
29
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
30
|
+
end
|
31
|
+
|
32
|
+
config.mock_with :rspec do |mocks|
|
33
|
+
mocks.verify_partial_doubles = true
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-safe_initialize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Lassek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-30 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.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
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.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.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: '3.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.3'
|
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:
|
84
|
+
email:
|
85
|
+
- alassek@lyconic.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- .travis.yml
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- activerecord-safe_initialize.gemspec
|
98
|
+
- lib/active_record/safe_initialize.rb
|
99
|
+
- lib/active_record/safe_initialize/version.rb
|
100
|
+
- lib/activerecord-safe_initialize.rb
|
101
|
+
- spec/active_record/safe_initialize_spec.rb
|
102
|
+
- spec/post_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
homepage: https://github.com/lyconic/activerecord-safe_initialize
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.5
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Safely initialize an ActiveRecord attribute with respect to missing columns
|
128
|
+
test_files:
|
129
|
+
- spec/active_record/safe_initialize_spec.rb
|
130
|
+
- spec/post_spec.rb
|
131
|
+
- spec/spec_helper.rb
|