record_mold 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e33b3dc7e7534ff38f53af6f66d4a27896a8582c64614e38372cf34c86eb3e7
4
+ data.tar.gz: 1068849b2453055583392fce2f28ce12da8830e3bce153a4777cd1ba9a7a3f93
5
+ SHA512:
6
+ metadata.gz: 6068bac170f860b37b8274c1d957b6ebe5ba5c1287a4debd36c3148739d17e6d764df5d997adf2277843ad25cd57594962b1d2ab9a8ebb2d5f219bc41971500c
7
+ data.tar.gz: 38d5fb8be90935146b8a6c8dbfdb335678f5994c9e39412b469925acc7686b94fe002f45176c909bc6140f81dcb681d51d99dadcbd08928b077f297a5ada22fd
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .bundle/*
2
+ log/*
3
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in record_mold.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,151 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ record_mold (0.1.0)
5
+ activerecord
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.2.3)
11
+ actionpack (= 5.2.3)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailer (5.2.3)
15
+ actionpack (= 5.2.3)
16
+ actionview (= 5.2.3)
17
+ activejob (= 5.2.3)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.2.3)
21
+ actionview (= 5.2.3)
22
+ activesupport (= 5.2.3)
23
+ rack (~> 2.0)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.2.3)
28
+ activesupport (= 5.2.3)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.2.3)
34
+ activesupport (= 5.2.3)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.2.3)
37
+ activesupport (= 5.2.3)
38
+ activerecord (5.2.3)
39
+ activemodel (= 5.2.3)
40
+ activesupport (= 5.2.3)
41
+ arel (>= 9.0)
42
+ activestorage (5.2.3)
43
+ actionpack (= 5.2.3)
44
+ activerecord (= 5.2.3)
45
+ marcel (~> 0.3.1)
46
+ activesupport (5.2.3)
47
+ concurrent-ruby (~> 1.0, >= 1.0.2)
48
+ i18n (>= 0.7, < 2)
49
+ minitest (~> 5.1)
50
+ tzinfo (~> 1.1)
51
+ arel (9.0.0)
52
+ builder (3.2.3)
53
+ concurrent-ruby (1.1.5)
54
+ crass (1.0.4)
55
+ diff-lcs (1.3)
56
+ erubi (1.8.0)
57
+ globalid (0.4.2)
58
+ activesupport (>= 4.2.0)
59
+ i18n (1.6.0)
60
+ concurrent-ruby (~> 1.0)
61
+ loofah (2.2.3)
62
+ crass (~> 1.0.2)
63
+ nokogiri (>= 1.5.9)
64
+ mail (2.7.1)
65
+ mini_mime (>= 0.1.1)
66
+ marcel (0.3.3)
67
+ mimemagic (~> 0.3.2)
68
+ method_source (0.9.2)
69
+ mimemagic (0.3.3)
70
+ mini_mime (1.0.2)
71
+ mini_portile2 (2.4.0)
72
+ minitest (5.11.3)
73
+ nio4r (2.4.0)
74
+ nokogiri (1.10.3)
75
+ mini_portile2 (~> 2.4.0)
76
+ pg (1.1.4)
77
+ rack (2.0.7)
78
+ rack-test (1.1.0)
79
+ rack (>= 1.0, < 3)
80
+ rails (5.2.3)
81
+ actioncable (= 5.2.3)
82
+ actionmailer (= 5.2.3)
83
+ actionpack (= 5.2.3)
84
+ actionview (= 5.2.3)
85
+ activejob (= 5.2.3)
86
+ activemodel (= 5.2.3)
87
+ activerecord (= 5.2.3)
88
+ activestorage (= 5.2.3)
89
+ activesupport (= 5.2.3)
90
+ bundler (>= 1.3.0)
91
+ railties (= 5.2.3)
92
+ sprockets-rails (>= 2.0.0)
93
+ rails-dom-testing (2.0.3)
94
+ activesupport (>= 4.2.0)
95
+ nokogiri (>= 1.6)
96
+ rails-html-sanitizer (1.0.4)
97
+ loofah (~> 2.2, >= 2.2.2)
98
+ railties (5.2.3)
99
+ actionpack (= 5.2.3)
100
+ activesupport (= 5.2.3)
101
+ method_source
102
+ rake (>= 0.8.7)
103
+ thor (>= 0.19.0, < 2.0)
104
+ rake (12.3.2)
105
+ rspec-core (3.8.2)
106
+ rspec-support (~> 3.8.0)
107
+ rspec-expectations (3.8.4)
108
+ diff-lcs (>= 1.2.0, < 2.0)
109
+ rspec-support (~> 3.8.0)
110
+ rspec-mocks (3.8.1)
111
+ diff-lcs (>= 1.2.0, < 2.0)
112
+ rspec-support (~> 3.8.0)
113
+ rspec-rails (3.8.2)
114
+ actionpack (>= 3.0)
115
+ activesupport (>= 3.0)
116
+ railties (>= 3.0)
117
+ rspec-core (~> 3.8.0)
118
+ rspec-expectations (~> 3.8.0)
119
+ rspec-mocks (~> 3.8.0)
120
+ rspec-support (~> 3.8.0)
121
+ rspec-support (3.8.2)
122
+ sprockets (3.7.2)
123
+ concurrent-ruby (~> 1.0)
124
+ rack (> 1, < 3)
125
+ sprockets-rails (3.2.1)
126
+ actionpack (>= 4.0)
127
+ activesupport (>= 4.0)
128
+ sprockets (>= 3.0.0)
129
+ sqlite3 (1.4.1)
130
+ thor (0.20.3)
131
+ thread_safe (0.3.6)
132
+ tzinfo (1.2.5)
133
+ thread_safe (~> 0.1)
134
+ websocket-driver (0.7.1)
135
+ websocket-extensions (>= 0.1.0)
136
+ websocket-extensions (0.1.4)
137
+
138
+ PLATFORMS
139
+ ruby
140
+
141
+ DEPENDENCIES
142
+ bundler (~> 1.3)
143
+ pg
144
+ rails
145
+ rake
146
+ record_mold!
147
+ rspec-rails
148
+ sqlite3
149
+
150
+ BUNDLED WITH
151
+ 1.17.2
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # RecordMold
2
+ This gem automatically creates ActiveRecord validations derived by database schema.
3
+
4
+ ## Usage
5
+ ```
6
+ class User < ActiveRecord
7
+ include RecordMold
8
+ end
9
+ ```
10
+
11
+ ## Installation
12
+ ```
13
+ # in your Gemfile
14
+ gem 'record_mold'
15
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ module RecordMold
2
+ VERSION = Gem.loaded_specs['record_mold'].version.to_s
3
+ end
@@ -0,0 +1,35 @@
1
+ module RecordMold
2
+ def self.included(model)
3
+ exception_columns = model.send(:timestamp_attributes_for_create) + model.send(:timestamp_attributes_for_update)
4
+ model.columns.each do |field|
5
+ next if field.name == model.primary_key
6
+ next if exception_columns.include?(field.name)
7
+
8
+ allow_nil = field.null
9
+ case field.type
10
+ when :boolean
11
+ model.send(:validates_inclusion_of, field.name.to_sym, in: [true, false], allow_nil: allow_nil)
12
+ when :integer, :float
13
+ options = { only_integer: field.type == :integer, allow_nil: allow_nil }
14
+ if field.limit && field.type == :integer
15
+ unsigned = field.sql_type.include?('unsigned')
16
+ options[:less_than] = unsigned ? 2 ** (8 * field.limit) : 2 ** (8 * field.limit) / 2
17
+ options[:greater_than] = unsigned ? -(2 ** (8 * field.limit)) - 1 : -(2 ** (8 * field.limit) / 2) - 1
18
+ end
19
+ model.send(:validates_numericality_of, field.name.to_sym, **options)
20
+ when :string, :text
21
+ if field.limit
22
+ maximum = field.limit
23
+ model.send(:validates_length_of, field.name.to_sym, maximum: maximum, allow_nil: allow_nil)
24
+ end
25
+ else
26
+ model.send(:validates_presence_of, field.name.to_sym) unless allow_nil
27
+ end
28
+ end
29
+
30
+ ActiveRecord::Base.connection.indexes(model.table_name).each do |key|
31
+ col_syms = key.columns.map(&:to_sym)
32
+ model.send(:validates_uniqueness_of, col_syms.first.to_sym, scope: col_syms[1..-1]) if key.unique
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "record_mold"
8
+ spec.version = '0.1.0'
9
+ spec.authors = ["Kenji Gonnokami"]
10
+ spec.email = ["amatsukikuu@gmail.com"]
11
+ spec.description = 'Provide validations from ActiveRecord schema information.'
12
+ spec.summary = 'A module that provides validations from ActiveRecord schema information.'
13
+ spec.homepage = 'https://github.com/AmatsukiKu/record_mold'
14
+ spec.license = "WTFPL"
15
+
16
+ spec.files = `git ls-files`.split($/)
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 'railties'
22
+ spec.add_dependency 'activerecord'
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'rails'
26
+ spec.add_development_dependency 'rspec-rails'
27
+ spec.add_development_dependency 'sqlite3'
28
+ spec.add_development_dependency 'pg'
29
+ end
30
+
@@ -0,0 +1,10 @@
1
+ version: '3'
2
+ services:
3
+ db:
4
+ image: postgres:9.6.9
5
+ environment:
6
+ POSTGRES_PASSWORD: password
7
+ ports:
8
+ - 5432:5432
9
+ volumes:
10
+ - ./init:/docker-entrypoint-initdb.d
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RecordMold do
4
+ before do
5
+ class User
6
+ include RecordMold
7
+ end
8
+ end
9
+
10
+ describe 'Class included this module' do
11
+ it 'includes validations from database schema' do
12
+ case ENV['TEST_DATABASE_ADAPTER']
13
+ when 'postgresql'
14
+ expect(User.validators.map { |validator| { class: validator.class, attributes: validator.attributes, options: validator.options } }).to eq(
15
+ [
16
+ {
17
+ class: ActiveRecord::Validations::LengthValidator,
18
+ attributes: [:name],
19
+ options: { maximum: 30, allow_nil: false }
20
+ },
21
+ {
22
+ class: ActiveModel::Validations::NumericalityValidator,
23
+ attributes: [:age],
24
+ options: { only_integer: true, greater_than: -2147483649, less_than: 2147483648, allow_nil: false }
25
+ },
26
+ {
27
+ class: ActiveModel::Validations::NumericalityValidator,
28
+ attributes: [:height],
29
+ options: { only_integer: true, greater_than: -2147483649, less_than: 2147483648, allow_nil: true }
30
+ },
31
+ {
32
+ class: ActiveModel::Validations::InclusionValidator,
33
+ attributes: [:left],
34
+ options: { in: [true, false], allow_nil: false }
35
+ },
36
+ {
37
+ class: ActiveRecord::Validations::UniquenessValidator,
38
+ attributes: [:my_number],
39
+ options: { case_sensitive: true, scope: [] }
40
+ }
41
+ ]
42
+ )
43
+ else
44
+ expect(User.validators.map { |validator| { class: validator.class, attributes: validator.attributes, options: validator.options } }).to eq(
45
+ [
46
+ {
47
+ class: ActiveRecord::Validations::LengthValidator,
48
+ attributes: [:name],
49
+ options: { maximum: 30, allow_nil: false }
50
+ },
51
+ {
52
+ class: ActiveModel::Validations::NumericalityValidator,
53
+ attributes: [:age],
54
+ options: { only_integer: true, allow_nil: false }
55
+ },
56
+ {
57
+ class: ActiveModel::Validations::NumericalityValidator,
58
+ attributes: [:height],
59
+ options: { only_integer: true, allow_nil: true }
60
+ },
61
+ {
62
+ class: ActiveModel::Validations::InclusionValidator,
63
+ attributes: [:left],
64
+ options: { in: [true, false], allow_nil: false }
65
+ },
66
+ {
67
+ class: ActiveRecord::Validations::UniquenessValidator,
68
+ attributes: [:my_number],
69
+ options: { case_sensitive: true, scope: [] }
70
+ }
71
+ ]
72
+ )
73
+ end
74
+ end
75
+ end
76
+
77
+ describe 'Instance of class included this module' do
78
+ let(:user) { User.new(name: 'Bob', age: 18, left: false, my_number: '00000001') }
79
+
80
+ context 'user\'s data is valid' do
81
+ it 'is valid' do
82
+ expect(user.valid?).to eq(true)
83
+ end
84
+ end
85
+
86
+ context 'user\'s age is nil' do
87
+ it 'is not valid' do
88
+ user[:age] = nil
89
+ expect(user.valid?).to eq(false)
90
+ end
91
+ end
92
+ end
93
+ end
data/spec/fake_app.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ case ENV['TEST_DATABASE_ADAPTER']
4
+ when 'postgresql'
5
+ ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'testdb', user: 'postgres', password: 'password', port: 5432, host: '127.0.0.1')
6
+ else
7
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
8
+ end
9
+
10
+ module RecordMoldTestApp
11
+ Application = Class.new(Rails::Application) do
12
+ config.eager_load = false
13
+ config.active_support.deprecation = :log
14
+ end.initialize!
15
+ end
16
+
17
+ # models
18
+ class User < ActiveRecord::Base
19
+ end
20
+
21
+ # migrations
22
+ class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
23
+ def self.up
24
+ create_table(:users) do |t|
25
+ t.string :name, limit: 30, null: false
26
+ t.text :introduction
27
+ t.integer :age, null: false
28
+ t.integer :height
29
+ t.boolean :left, null: false
30
+ t.string :my_number, null: false
31
+ t.index :my_number, unique: true
32
+ t.datetime "created_at", null: false
33
+ t.datetime "updated_at", null: false
34
+ end
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ CREATE DATABASE testdb;
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rails'
5
+ require 'active_record'
6
+ require 'record_mold'
7
+ require 'fake_app'
8
+ CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'users'
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: record_mold
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kenji Gonnokami
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-11 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
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: rails
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: rspec-rails
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
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pg
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Provide validations from ActiveRecord schema information.
112
+ email:
113
+ - amatsukikuu@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - README.md
122
+ - Rakefile
123
+ - lib/record_mold.rb
124
+ - lib/record_mold/version.rb
125
+ - record_mold.gemspec
126
+ - spec/docker-compose.yml
127
+ - spec/examples/validations_spec.rb
128
+ - spec/fake_app.rb
129
+ - spec/init/init.sql
130
+ - spec/spec_helper.rb
131
+ homepage: https://github.com/AmatsukiKu/record_mold
132
+ licenses:
133
+ - WTFPL
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubygems_version: 3.0.3
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: A module that provides validations from ActiveRecord schema information.
154
+ test_files:
155
+ - spec/docker-compose.yml
156
+ - spec/examples/validations_spec.rb
157
+ - spec/fake_app.rb
158
+ - spec/init/init.sql
159
+ - spec/spec_helper.rb