activerecord-auto_defaults 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 activerecord-auto_defaults.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Simon COURTOIS
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,59 @@
1
+ # Activerecord::AutoDefaults
2
+
3
+ Sets the default values of nil attributes based on the database schema.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'activerecord-auto_defaults'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install activerecord-auto_defaults
18
+
19
+ ## Usage
20
+
21
+ Given the following code in your application schema:
22
+
23
+ ``` ruby
24
+ create_table "articles", :force => true do |t|
25
+ t.string "title", :limit => 100, :default => "", :null => false
26
+ t.text "body", :limit => 3000, :default => "", :null => false
27
+
28
+ t.timestamps
29
+ end
30
+ ```
31
+
32
+ It may occur that you receive `nil` values for given attributes. Since they are
33
+ flagged as `NOT NULL` and have de default value, it should be used to fill the
34
+ `nil` attributes.
35
+
36
+ That's what AutoDefaults is for.
37
+
38
+ ``` ruby
39
+ Article.create(title: nil, body: nil)
40
+ +----+-------+------+
41
+ | id | title | body |
42
+ +----+-------+------+
43
+ | 1 | | |
44
+ +----+-------+------+
45
+ ```
46
+
47
+ You will no longer get a SQL error telling you that a field should not be `NULL`
48
+ and as a null value.
49
+
50
+ This avoids duplicating the schema instructions in a `set_defaults` callback to
51
+ set this values before validation.
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/active_record/auto_defaults/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Simon COURTOIS"]
6
+ gem.email = ["scourtois@cubyx.fr"]
7
+ gem.description = %q{Sets the default values of nil attributes based on the database schema.}
8
+ gem.summary = %q{Sets the default values of nil attributes based on the database schema.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "activerecord-auto_defaults"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ActiveRecord::AutoDefaults::VERSION
17
+
18
+ gem.add_dependency 'activerecord', '~> 3.0'
19
+ gem.add_dependency 'activesupport', '~> 3.0'
20
+ gem.add_dependency 'railties', '~> 3.0'
21
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_support/concern'
2
+ require 'active_record'
3
+ require 'active_record/auto_defaults/version'
4
+
5
+ module ActiveRecord
6
+ module AutoDefaults
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ before_validation :set_default_values_from_schema
11
+ end
12
+
13
+ def set_default_values_from_schema
14
+ columns_hash = self.class.columns_hash
15
+
16
+ self.attributes.keys.each do |attr|
17
+ next unless self.read_attribute(attr).nil?
18
+ next unless column_desc = columns_hash[attr]
19
+
20
+ unless column_desc.null || column_desc.default.nil?
21
+ self.send(:"#{attr}=", column_desc.default)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module AutoDefaults
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'active_record/auto_defaults'
@@ -0,0 +1,10 @@
1
+ module AutoDefaults
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ def create_initializer_file
5
+ create_file 'config/initializers/activerecord-auto_defaults.rb',
6
+ "ActiveRecord::Base.send(:include, ActiveRecord::AutoDefaults)\n"
7
+ end
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-auto_defaults
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Simon COURTOIS
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70129304558120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70129304558120
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70129304556920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70129304556920
36
+ - !ruby/object:Gem::Dependency
37
+ name: railties
38
+ requirement: &70129304554880 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70129304554880
47
+ description: Sets the default values of nil attributes based on the database schema.
48
+ email:
49
+ - scourtois@cubyx.fr
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - activerecord-auto_defaults.gemspec
60
+ - lib/active_record/auto_defaults.rb
61
+ - lib/active_record/auto_defaults/version.rb
62
+ - lib/activerecord-auto_defaults.rb
63
+ - lib/generators/auto_defaults/install_generator.rb
64
+ homepage: ''
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.15
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Sets the default values of nil attributes based on the database schema.
88
+ test_files: []
89
+ has_rdoc: