validb 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28235bb827e10873757a172aa4341de2125825b5
4
- data.tar.gz: 534c056bd73c20c1a720283757e569d64bfa9cec
3
+ metadata.gz: aecd94c7cb36508bb2fe17f98a8c8ff7e35fbd36
4
+ data.tar.gz: 399a316d78060de84cd0534b5c3d4286aa0d512c
5
5
  SHA512:
6
- metadata.gz: 7b58b722f5f0f96b7aea4b0f9c89e228682145c4a3876dbaf9a3bd71fb7a56d5fcc60f2b1ac624c6c6c58fcbdc6c777762644dd64812b32338db1fb5ea09f9ca
7
- data.tar.gz: 61ded8105b8e95ff8c116adf0ebb88e9ec51bc24e960d21595437b9b8b310c4d1681f6e8731ec19ec1392875dea5b5534478656fd8d5e2e07530bd8062c4a1e7
6
+ metadata.gz: 7df0136e326c3dc0fa8da5d58e5d59ba0b44c4224db734b6421d8b956affb96a2a8090ad1f5b2f39b9e1ae2b6eb0b49cc9a8b73592f177d4c84d417a2ba4e456
7
+ data.tar.gz: 9966e8375e09f154eeb6326eb3c72a84bb08c9a2448d2952af7fc01cc606931980f291601cd6199ecdc05fd94f3e3089af92a5124ea6f9b40b4cf254973bea69
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 1.0.0, released 2013-03-27
2
+ * Add rake task to generate config file
3
+ * Allow filtering of unwanted models
4
+ * Allow filtering of unwanted prefixes
5
+ * Don't check models without validation
6
+ * Don't check child models of STI
7
+
1
8
  ## 0.1.0, released 2013-03-26
2
9
  * Rake task now takes parameters differently
3
10
  * Classes now use instances, not class methods
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Validb
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/validb.png)](https://rubygems.org/gems/validb)
4
+ [![Dependency Status](https://gemnasium.com/jgeiger/validb.png)](https://gemnasium.com/jgeiger/validb)
5
+ [![Build Status](https://travis-ci.org/jgeiger/validb.png)](https://travis-ci.org/jgeiger/validb)
6
+
3
7
  ## Description
4
8
 
5
9
  Check your database for invalid models.
@@ -22,5 +26,20 @@ Check your database for invalid models.
22
26
  ### Specific logger
23
27
  rake validb:validate logger="Validb::ConsoleLogger"
24
28
 
29
+ ### Generate config file in config/validb.json
30
+ rake validb:generate_config
31
+
32
+ #### Example validb.json file
33
+
34
+ {
35
+ "ignored_models": [
36
+ "Blog",
37
+ "Post"
38
+ ],
39
+ "ignored_prefixes": [
40
+ "Api"
41
+ ]
42
+ }
43
+
25
44
  ## License
26
45
  Released under the MIT License
@@ -3,13 +3,40 @@ namespace :validb do
3
3
  task :validate => :environment do
4
4
  models = ENV["models"] || ""
5
5
  logger = ENV["logger"] || "Validb::ConsoleLogger"
6
+ filename = ENV["config"] || default_configuration_file
6
7
 
7
8
  # force all models to load so we can find them
8
9
  Rails.application.eager_load!
9
- finder = Validb::Finder.new(models)
10
+
11
+ configuration = Validb::Configuration.new(filename)
12
+ finder = Validb::Finder.new(configuration, models)
10
13
 
11
14
  console_logger = logger.constantize.new
12
15
  checker = Validb::Checker.new(console_logger)
13
16
  checker.check(finder.models)
14
17
  end
18
+
19
+ desc "Generate config/validb.json"
20
+ task :generate_config => :environment do
21
+ if File.exists?(default_configuration_file)
22
+ puts "Configuration already exists at config/validb.json"
23
+ else
24
+ puts "Writing configuration to config/validb.json"
25
+ write_configuration_file
26
+ end
27
+ end
28
+
29
+ def default_configuration_file
30
+ @default_configuration_file ||= Rails.root.join('config', 'validb.json')
31
+ end
32
+
33
+ def default_configuration
34
+ { "ignored_models" => [], "ignored_prefixes" => [] }
35
+ end
36
+
37
+ def write_configuration_file
38
+ File.open(default_configuration_file,'w') do |file|
39
+ file.puts JSON.pretty_generate(default_configuration)
40
+ end
41
+ end
15
42
  end
data/lib/validb.rb CHANGED
@@ -5,9 +5,11 @@ require 'validb/version'
5
5
 
6
6
  module Validb
7
7
  require 'validb/railtie' if defined?(Rails)
8
+ autoload :Configuration, 'validb/configuration'
8
9
  autoload :ConsoleLogger, 'validb/console_logger'
9
10
  autoload :Checker, 'validb/checker'
10
11
  autoload :Finder, 'validb/finder'
12
+ autoload :ModelFilterer, 'validb/model_filterer'
11
13
  autoload :ModelValidator, 'validb/model_validator'
12
14
  autoload :Batcher, 'validb/batcher'
13
15
  autoload :RecordValidator, 'validb/record_validator'
@@ -0,0 +1,50 @@
1
+ module Validb
2
+ class Configuration
3
+
4
+ def initialize(filename)
5
+ @filename = filename
6
+ print_ignored
7
+ end
8
+
9
+ def ignored_models
10
+ convert_model_name_strings_to_constants
11
+ end
12
+
13
+ def ignored_prefixes
14
+ config["ignored_prefixes"]
15
+ end
16
+
17
+ private
18
+
19
+ def print_ignored
20
+ puts "Ignoring prefixes: #{ignored_prefixes.join(',')}" if ignored_prefixes.any?
21
+ puts "Ignoring models: #{config['ignored_models'].join(',')}" if ignored_models.any?
22
+ end
23
+
24
+ def config
25
+ @config ||= begin
26
+ if configuration_exists?
27
+ JSON.parse(IO.read(@filename))
28
+ else
29
+ {
30
+ "ignored_models" => [],
31
+ "ignored_prefixes" => []
32
+ }
33
+ end
34
+ end
35
+ end
36
+
37
+ def configuration_exists?
38
+ File.exists?(@filename)
39
+ end
40
+
41
+ def convert_model_name_strings_to_constants
42
+ config["ignored_models"].map do |model_string|
43
+ begin
44
+ model_string.constantize
45
+ rescue NameError
46
+ end
47
+ end.compact
48
+ end
49
+ end
50
+ end
data/lib/validb/finder.rb CHANGED
@@ -1,16 +1,21 @@
1
1
  module Validb
2
2
  class Finder
3
3
 
4
- def initialize(model_name_string)
4
+ def initialize(configuration, model_name_string)
5
+ @model_filterer = Validb::ModelFilterer.new(configuration)
5
6
  @model_name_string = model_name_string || ""
6
7
  end
7
8
 
8
9
  def models
9
- model_names.empty? ? all : all.select { |model| model_names.include?(model.name) }
10
+ @model_filterer.filter(selected_models)
10
11
  end
11
12
 
12
13
  private
13
14
 
15
+ def selected_models
16
+ model_names.empty? ? all : all.select { |model| model_names.include?(model.name) }
17
+ end
18
+
14
19
  def model_names
15
20
  @model_names ||= @model_name_string.split(",").map(&:strip)
16
21
  end
@@ -0,0 +1,43 @@
1
+ module Validb
2
+ class ModelFilterer
3
+
4
+ def initialize(configuration)
5
+ @configuration = configuration
6
+ end
7
+
8
+ def filter(models)
9
+ filtered_models = validatable_models(models)
10
+ filtered_models = non_inherited_models(filtered_models)
11
+ filtered_models = non_ignored_models(filtered_models)
12
+ non_ignored_prefix_models(filtered_models)
13
+ end
14
+
15
+ private
16
+
17
+ def ignored_models
18
+ @configuration.ignored_models
19
+ end
20
+
21
+ def ignored_prefixes
22
+ @configuration.ignored_prefixes
23
+ end
24
+
25
+ def non_ignored_prefix_models(models)
26
+ models.reject do |model|
27
+ ignored_prefixes.include?(model.name.split("::").first)
28
+ end
29
+ end
30
+
31
+ def non_ignored_models(models)
32
+ models - ignored_models
33
+ end
34
+
35
+ def non_inherited_models(models)
36
+ models.reject { |model| model != model.base_class && models.include?(model.base_class)}
37
+ end
38
+
39
+ def validatable_models(models)
40
+ models.select { |model| model.validators.any? }
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Validb
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,10 @@
1
+ {
2
+ "ignored_models": [
3
+ "Blog",
4
+ "Post",
5
+ "Fake"
6
+ ],
7
+ "ignored_prefixes": [
8
+ "Api"
9
+ ]
10
+ }
data/spec/spec_helper.rb CHANGED
@@ -19,17 +19,26 @@ ActiveRecord::Schema.define do
19
19
 
20
20
  create_table :blogs, :force => true do |t|
21
21
  t.string :title
22
- t.timestamps
23
22
  end
24
23
 
25
24
  create_table :posts, :force => true do |t|
26
25
  t.string :title
27
- t.timestamps
28
26
  end
29
27
 
30
28
  create_table :comments, :force => true do |t|
31
29
  t.string :body
32
- t.timestamps
30
+ end
31
+
32
+ create_table :users, :force => true do |t|
33
+ t.string :name
34
+ end
35
+
36
+ create_table :people, :force => true do |t|
37
+ t.string :name
38
+ end
39
+
40
+ create_table :approvals, :force => true do |t|
41
+ t.boolean :name
33
42
  end
34
43
  end
35
44
 
@@ -38,7 +47,16 @@ class Blog < ActiveRecord::Base
38
47
  end
39
48
 
40
49
  class Post < ActiveRecord::Base
50
+ validates :title, presence: true
41
51
  end
42
52
 
43
53
  class Comment < ActiveRecord::Base
54
+ validates :body, presence: true
55
+ end
56
+
57
+ class Person < ActiveRecord::Base
58
+ validates :name, presence: true
59
+ end
60
+
61
+ class User < ActiveRecord::Base
44
62
  end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Validb::Configuration do
4
+ describe "#initialize" do
5
+ it "creates a configuration" do
6
+ Validb::Configuration.any_instance.should_receive(:print_ignored)
7
+ Validb::Configuration.new("")
8
+ end
9
+ end
10
+
11
+ describe "#ignored_models" do
12
+ context "with a missing configuration file" do
13
+ it "returns an empty array" do
14
+ filename = File.expand_path(File.dirname(__FILE__) + '/../config/missing.json')
15
+ configuration = Validb::Configuration.new(filename)
16
+ configuration.ignored_models.should == []
17
+ end
18
+ end
19
+
20
+ context "with a configuration file" do
21
+ it "returns the ignored models" do
22
+ filename = File.expand_path(File.dirname(__FILE__) + '/../config/validb.json')
23
+ configuration = Validb::Configuration.new(filename)
24
+ configuration.ignored_models.should == [Blog, Post]
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "#ignored_prefixes" do
30
+ context "with a missing configuration file" do
31
+ it "returns an empty array" do
32
+ filename = File.expand_path(File.dirname(__FILE__) + '/../config/missing.json')
33
+ configuration = Validb::Configuration.new(filename)
34
+ configuration.ignored_prefixes.should == []
35
+ end
36
+ end
37
+
38
+ context "with a configuration file" do
39
+ it "returns the ignored models" do
40
+ filename = File.expand_path(File.dirname(__FILE__) + '/../config/validb.json')
41
+ configuration = Validb::Configuration.new(filename)
42
+ configuration.ignored_prefixes.should == ["Api"]
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,27 +1,38 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Validb::Finder do
4
- class NoTable < ActiveRecord::Base
4
+ class NoTableModel < ActiveRecord::Base
5
+ end
6
+
7
+ describe "#initialize" do
8
+ it "creates a model filterer" do
9
+ configuration = double('configuration')
10
+ Validb::ModelFilterer.should_receive(:new).with(configuration)
11
+ Validb::Finder.new(configuration, "")
12
+ end
5
13
  end
6
14
 
7
15
  describe "#models" do
8
16
  context "with an empty string" do
9
17
  it "returns all the models with tables" do
10
- finder = Validb::Finder.new("")
11
- finder.models.should =~ [Post, Blog, Comment]
18
+ configuration = Validb::Configuration.new("")
19
+ finder = Validb::Finder.new(configuration, "")
20
+ finder.models.should =~ [Post, Blog, Comment, Person]
12
21
  end
13
22
  end
14
23
 
15
24
  context "valid model names" do
16
25
  it "returns all the selected models with tables" do
17
- finder = Validb::Finder.new("Post, Blog")
26
+ configuration = Validb::Configuration.new("")
27
+ finder = Validb::Finder.new(configuration, "Post, Blog")
18
28
  finder.models.should =~ [Post, Blog]
19
29
  end
20
30
  end
21
31
 
22
32
  context "with invalid model names" do
23
33
  it "returns all the valid models with tables" do
24
- finder = Validb::Finder.new("Post, Blog, Fake")
34
+ configuration = Validb::Configuration.new("")
35
+ finder = Validb::Finder.new(configuration, "Post, Blog, Fake")
25
36
  finder.models.should =~ [Post, Blog]
26
37
  end
27
38
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Validb::ModelFilterer do
4
+
5
+ class Author < Person
6
+ end
7
+
8
+ module Api
9
+ class Approval < ActiveRecord::Base
10
+ self.table_name = "approvals"
11
+ end
12
+ end
13
+
14
+ describe "#models" do
15
+ it "returns all the non filtered models" do
16
+ filename = File.expand_path(File.dirname(__FILE__) + '/../config/validb.json')
17
+ configuration = Validb::Configuration.new(filename)
18
+ model_filterer = Validb::ModelFilterer.new(configuration)
19
+
20
+ models = [User, Author, Blog, Post, Api::Approval, Comment, Person]
21
+
22
+ model_filterer.filter(models).should =~ [Comment, Person]
23
+ end
24
+ end
25
+ end
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe Validb do
4
4
  it "returns the version" do
5
- Validb::VERSION.should == "0.1.0"
5
+ Validb::VERSION.should == "1.0.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joey Geiger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-26 00:00:00.000000000 Z
11
+ date: 2013-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -40,17 +40,22 @@ files:
40
40
  - lib/validb.rb
41
41
  - lib/validb/batcher.rb
42
42
  - lib/validb/checker.rb
43
+ - lib/validb/configuration.rb
43
44
  - lib/validb/console_logger.rb
44
45
  - lib/validb/finder.rb
46
+ - lib/validb/model_filterer.rb
45
47
  - lib/validb/model_validator.rb
46
48
  - lib/validb/railtie.rb
47
49
  - lib/validb/record_validator.rb
48
50
  - lib/validb/version.rb
51
+ - spec/config/validb.json
49
52
  - spec/spec_helper.rb
50
53
  - spec/validb/batcher_spec.rb
51
54
  - spec/validb/checker_spec.rb
55
+ - spec/validb/configuration_spec.rb
52
56
  - spec/validb/console_logger_spec.rb
53
57
  - spec/validb/finder_spec.rb
58
+ - spec/validb/model_filterer_spec.rb
54
59
  - spec/validb/model_validator_spec.rb
55
60
  - spec/validb/record_validator_spec.rb
56
61
  - spec/validb/version_spec.rb