Skip to content

Commit

Permalink
Merge pull request #44 from plicjo/postgres_upgrade_to_rails_5
Browse files Browse the repository at this point in the history
Update for Rails 5
  • Loading branch information
boazy authored Nov 16, 2016
2 parents b3e8be2 + 8537632 commit a8215d9
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
---
sudo: false
rvm:
- 2.1.5
- 2.3.1
gemfile:
- gemfiles/activerecord-4.2/Gemfile.mysql2
- gemfiles/activerecord-4.2/Gemfile.postgresql
- gemfiles/activerecord-4.2/Gemfile.sqlite3
- gemfiles/activerecord-5.0/Gemfile.mysql2
- gemfiles/activerecord-5.0/Gemfile.postgresql
- gemfiles/activerecord-5.0/Gemfile.sqlite3
env: POSTGRESQL_DB_USER=postgres MYSQL_DB_USER=travis
addons:
postgresql: '9.4'
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ As of version 1.2.0, SchemaValidations supports and is tested on:

<!-- SCHEMA_DEV: MATRIX - begin -->
<!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->
* ruby **2.1.5** with activerecord **4.2**, using **mysql2**, **postgresql** or **sqlite3**
* ruby **2.3.1** with activerecord **4.2**, using **mysql2**, **postgresql** or **sqlite3**
* ruby **2.3.1** with activerecord **5.0**, using **mysql2**, **postgresql** or **sqlite3**

<!-- SCHEMA_DEV: MATRIX - end -->

Expand Down
3 changes: 3 additions & 0 deletions gemfiles/activerecord-5.0/Gemfile.base
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eval File.read File.expand_path('../../Gemfile.base', __FILE__)

gem "activerecord", "~> 5.0.0"
10 changes: 10 additions & 0 deletions gemfiles/activerecord-5.0/Gemfile.mysql2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "mysql2"
end

platform :jruby do
gem 'activerecord-jdbcmysql-adapter'
end
10 changes: 10 additions & 0 deletions gemfiles/activerecord-5.0/Gemfile.postgresql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "pg"
end

platform :jruby do
gem 'activerecord-jdbcpostgresql-adapter'
end
10 changes: 10 additions & 0 deletions gemfiles/activerecord-5.0/Gemfile.sqlite3
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "sqlite3"
end

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>=1.3.0.beta2'
end
22 changes: 11 additions & 11 deletions lib/schema_validations/active_record/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def load_column_validations #:nodoc:
when respond_to?(:defined_enums) && defined_enums.has_key?(column.name) then :enum
when column.type == :integer then :integer
when column.type == :decimal then :decimal
when column.number? then :numeric
when column.text? then :text
when column.type == :float then :numeric
when column.type == :text || column.type == :string then :text
when column.type == :boolean then :boolean
end

Expand Down Expand Up @@ -133,15 +133,15 @@ def load_column_validations #:nodoc:
end

def load_integer_column_validations(name, column) # :nodoc:
options = { :allow_nil => true, :only_integer => true }

if range = column.cast_type.try(:range)
options[:greater_than_or_equal_to] = range.begin
if range.exclude_end?
options[:less_than] = range.end
else
options[:less_than_or_equal_to] = range.end
end
integer_range = ::ActiveRecord::Type::Integer.new.range
# The Ruby Range object does not support excluding the beginning of a Range,
# so we always include :greater_than_or_equal_to
options = { :allow_nil => true, :only_integer => true, greater_than_or_equal_to: integer_range.begin }

if integer_range.exclude_end?
options[:less_than] = integer_range.end
else
options[:less_than_or_equal_to] = integer_range.end
end

validate_logged :validates_numericality_of, name, options
Expand Down
3 changes: 2 additions & 1 deletion schema_dev.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ruby:
- 2.1.5
- 2.3.1
activerecord:
- 4.2
- 5.0
db:
- mysql2
- postgresql
Expand Down
4 changes: 2 additions & 2 deletions schema_validations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_dependency("schema_plus_columns")
s.add_dependency("activerecord", "~> 4.2", ">= 4.2.1")
s.add_dependency("activerecord", ">= 4.2.1", "< 5.1")
s.add_dependency("valuable")

s.add_development_dependency("schema_dev", "~> 3.6")
s.add_development_dependency("rake")
s.add_development_dependency("rdoc")
Expand Down
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
config.around(:each) do |example|
DatabaseCleaner.clean
remove_all_models

class ActiveRecord::InternalMetadata
def self.create_table
end

def self.[]=(first, second)
end
end

ActiveRecord::Migration.suppress_messages do
example.run
end
Expand Down
8 changes: 7 additions & 1 deletion spec/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,18 @@ class ArticleReview < ActiveRecord::Base
expect(Article.new(votes: -2147483649).error_on(:votes).size).to eq(1)
end

it "can include the end range" do
allow_any_instance_of(::ActiveRecord::Type::Integer).to receive(:range).and_return(-2147483648..2147483648)

expect(Article.new(votes: 2147483648).error_on(:votes).size).to eq(0)
end

it "should validate the range of decimal precision with scale" do
expect(Article.new(max10: 10).error_on(:max10).size).to eq(1)
expect(Article.new(max10: 5).error_on(:max10).size).to eq(0)
expect(Article.new(max10: -10).error_on(:max10).size).to eq(1)
end

it "should validate the range of decimal precision without scale" do
expect(Article.new(max100: 100).error_on(:max100).size).to eq(1)
expect(Article.new(max100: 50).error_on(:max100).size).to eq(0)
Expand Down

0 comments on commit a8215d9

Please sign in to comment.