diff --git a/.circleci/config.yml b/.circleci/config.yml index d09efa2..5bec898 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -25,8 +25,8 @@ jobs: - restore_cache: keys: - - has_safe_dates-dependencies-v1-{{ checksum "has_safe_dates.gemspec" }} - - has_safe_dates-dependencies-v1- + - has_safe_dates-dependencies-v1-{{ checksum "has_safe_dates.gemspec" }} + - has_safe_dates-dependencies-v1- - run: name: Install dependencies @@ -65,8 +65,8 @@ jobs: - restore_cache: keys: - - has_safe_dates-dependencies-v1-{{ checksum "has_safe_dates.gemspec" }} - - has_safe_dates-dependencies-v1- + - has_safe_dates-dependencies-v1-{{ checksum "has_safe_dates.gemspec" }} + - has_safe_dates-dependencies-v1- - run: name: Install dependencies diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..8d3cbe8 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,5 @@ +inherit_from: + - https://onehq.com/rubocop3_4.yml + +AllCops: + SuggestExtensions: false \ No newline at end of file diff --git a/.ruby-version b/.ruby-version index 4a36342..f989260 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.0.0 +3.4.4 diff --git a/Gemfile b/Gemfile index 5a96427..7425f42 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,17 @@ # frozen_string_literal: true -source "http://rubygems.org" - -gemspec +source "https://rubygems.org" +gem "base64" +gem "benchmark" +gem "bigdecimal" gem "byebug" +gem "logger" +gem "mutex_m" +gem "ostruct" +gem "rubocop", "~> 1.81" +gem "rubocop-performance", "~> 1.26" +gem "rubocop-rails", "~> 2.34" +gem "sqlite3", "~> 1.4" + +gemspec diff --git a/has_safe_dates.gemspec b/has_safe_dates.gemspec index dce531a..d571d9d 100644 --- a/has_safe_dates.gemspec +++ b/has_safe_dates.gemspec @@ -7,48 +7,28 @@ require "has_safe_dates/version" # Describe your gem and declare its dependencies: Gem::Specification.new do |s| - s.name = "has_safe_dates" - s.version = HasSafeDates::VERSION - s.authors = ["kylejginavan"] - s.date = "2012-02-24" - s.description = "Uses Chronic to parse incoming dates and does not raise errors on invalid multi parameter settings" - s.email = "kylejginavan@gmail.com" + s.name = "has_safe_dates" + s.version = HasSafeDates::VERSION + s.authors = ["kylejginavan"] + s.date = "2012-02-24" + s.description = "Uses Chronic to parse incoming dates and does not raise errors on invalid multi parameter settings" + s.email = "kylejginavan@gmail.com" + s.required_ruby_version = ">= 3.4" - s.extra_rdoc_files = [ - "MIT-LICENSE.txt", - "README.md" - ] - s.files = `git ls-files`.split("\n") - s.homepage = "http://github.com/kylejginavan/has_safe_dates" - s.require_paths = ["lib"] - s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") - s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } - s.summary = "Chronic based date setting for ActiveRecord models" + s.extra_rdoc_files = ["MIT-LICENSE.txt", "README.md"] + s.files = `git ls-files`.split("\n") + s.homepage = "http://github.com/kylejginavan/has_safe_dates" + s.require_paths = ["lib"] + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) } + s.summary = "Chronic based date setting for ActiveRecord models" + s.licenses = ["HQ"] - if s.respond_to? :specification_version then - s.specification_version = 3 + s.add_dependency "chronic", "~> 0.10" - if Gem::Version.new(Gem::VERSION) >= Gem::Version.new("1.2.0") then - s.add_development_dependency("activerecord", ["~> 6.0"]) - s.add_development_dependency("bundler", ["~> 2.0"]) - s.add_dependency("chronic") - s.add_development_dependency("database_cleaner") - s.add_development_dependency("rdoc", ["~> 3.12"]) - s.add_development_dependency("rspec", [">= 0", "< 3"]) - s.add_development_dependency("rspec_junit_formatter", ["~> 0.3", ">= 0.3.0"]) - s.add_development_dependency("sqlite3") - else - s.add_dependency("activerecord", [">= 3.1.0"]) - s.add_dependency("bundler", ["~> 2.0"]) - s.add_dependency("chronic") - s.add_dependency("rdoc", ["~> 3.12"]) - s.add_dependency("rspec", [">= 0"]) - end - else - s.add_dependency("activerecord", [">= 3.1.0"]) - s.add_dependency("bundler", ["~> 2.0"]) - s.add_dependency("chronic") - s.add_dependency("rdoc", ["~> 3.12"]) - s.add_dependency("rspec", [">= 0"]) - end + s.add_development_dependency "activerecord", "~> 7.2" + s.add_development_dependency "database_cleaner", "~> 2.1" + s.add_development_dependency "rdoc", "~> 7.0" + s.add_development_dependency "rspec", "~> 3.13" + s.add_development_dependency "rspec_junit_formatter", "~> 0.6", ">= 0.3.0" end diff --git a/lib/has_safe_dates/core_ext.rb b/lib/has_safe_dates/core_ext.rb index 3feb84b..1d4b174 100644 --- a/lib/has_safe_dates/core_ext.rb +++ b/lib/has_safe_dates/core_ext.rb @@ -72,11 +72,11 @@ def execute_callstack_for_multiparameter_attributes(callstack) end end else - super(callstack) # has_safe_dates is not enabled for the current field, so invoke the super method + super(callstack) # has_safe_dates is not enabled for the current field, so invoke the super method end end end end -::ActiveRecord::Base.send :include, ::HasSafeDates::CoreExt -::ActiveRecord::Base.send :prepend, ::HasSafeDates::DateTimeExt +::ActiveRecord::Base.include ::HasSafeDates::CoreExt +::ActiveRecord::Base.prepend ::HasSafeDates::DateTimeExt diff --git a/lib/has_safe_dates/version.rb b/lib/has_safe_dates/version.rb index 9c5a43a..634deb8 100644 --- a/lib/has_safe_dates/version.rb +++ b/lib/has_safe_dates/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module HasSafeDates - VERSION = "2.0.3" + VERSION = "5.0.0-beta.3" end diff --git a/spec/db/schema.rb b/spec/db/schema.rb index 2491183..778e5a6 100644 --- a/spec/db/schema.rb +++ b/spec/db/schema.rb @@ -1,8 +1,9 @@ -# require File.join(File.dirname(__FILE__), 'fixtures/document') +# frozen_string_literal: true -ActiveRecord::Schema.define(:version => 0) do +# require File.join(File.dirname(__FILE__), 'fixtures/document') - create_table "comments", :force => true do |t| +ActiveRecord::Schema.define(version: 0) do + create_table "comments", force: true do |t| t.text "body" t.date "approved_at" t.date "created_at" @@ -10,7 +11,7 @@ t.datetime "published_at" end - create_table "posts", :force => true do |t| + create_table "posts", force: true do |t| t.string "title" t.date "published_date" t.date "safe_date" @@ -18,5 +19,4 @@ t.date "created_at" t.date "updated_at" end - end diff --git a/spec/has_safe_dates_spec.rb b/spec/has_safe_dates_spec.rb index 9ec6d1b..cbcb88d 100644 --- a/spec/has_safe_dates_spec.rb +++ b/spec/has_safe_dates_spec.rb @@ -1,132 +1,131 @@ -require 'spec_helper' +# frozen_string_literal: true + +require "spec_helper" class TestUser < ActiveRecord::Base; end class Post < ActiveRecord::Base - has_safe_dates :published_date, :safe_date, :error_message => 'is not a real date' + has_safe_dates :published_date, :safe_date, error_message: "is not a real date" end class Comment < ActiveRecord::Base - has_safe_dates :approved_at, :published_at, :error_message => 'is not a real date' + has_safe_dates :approved_at, :published_at, error_message: "is not a real date" end -describe "HasSafeDates" do - - before(:each) do +RSpec.describe "HasSafeDates" do + before do @post = Post.new @comment = Comment.new end describe "class method" do - it "should work" do - expect { - TestUser.send(:has_safe_dates, :published_date) - }.to_not raise_error + expect { TestUser.send(:has_safe_dates, :published_date) }.not_to raise_error end it "should raise an error if no fields are passed in" do - expect { - TestUser.send(:has_safe_dates) - }.to raise_error(ArgumentError, 'Must define the fields you want to be converted to safe dates with "has_safe_dates :my_field_name_date, :my_other_field_name_date"') + expect { TestUser.send(:has_safe_dates) }. + to raise_error(ArgumentError, + 'Must define the fields you want to be converted to safe dates with "has_safe_dates :my_field_name_date, :my_other_field_name_date"') end - it "does not touch an field that it has not been told to make safe" do - @post.update_attribute(:unsafe_date, '1st of December 2012') + it "does not touch a field that it has not been told to make safe" do + @post.update_attribute(:unsafe_date, "1st of December 2012") @post.reload - @post.unsafe_date.should be_nil + + expect(@post.unsafe_date).to be_nil end end describe "safe date parsing" do - - ['2012-12-1', '1st of December 2012', 'first of dec 2012', '1 Dec 2012'].each do |date| + ["2012-12-1", "1st of December 2012", "first of dec 2012", "1 Dec 2012"].each do |date| it "allows you to set the date as '#{date}'" do @post.update_attribute(:safe_date, date) @comment.update_attribute(:approved_at, date) @post.reload @comment.reload - @post.safe_date.should == Date.new(2012, 12, 1) - @comment.approved_at.should == Date.new(2012, 12, 1) + + expect(@post.safe_date).to eq(Date.new(2012, 12, 1)) + expect(@comment.approved_at).to eq(Date.new(2012, 12, 1)) end end - [' ', '', nil].each do |date| + [" ", "", nil].each do |date| it "sets the field to nil if given the blank value #{date.inspect}" do @post.safe_date = date @post.valid? - @post.safe_date.should == nil - @post.errors.should be_blank + + expect(@post.safe_date).to be_nil + expect(@post.errors).to be_empty end end - ['random', 'does not compute'].each do |date| + ["random", "does not compute"].each do |date| it "sets the field to nil and sets a validation error if given the value #{date.inspect}" do @post.safe_date = date @post.valid? - @post.safe_date.should == nil - @post.errors.should_not be_blank - @post.errors[:safe_date].should == ['is not a real date'] + + expect(@post.safe_date).to be_nil + expect(@post.errors[:safe_date]).to eq(["is not a real date"]) end end - end describe "multiparameter parsing" do + it "doesn't blow up when given incorrect values" do + invalid_post_attributes = { "published_date(1i)" => "2001", "published_date(2i)" => "12", "published_date(3i)" => "abc" } + invalid_comment_attributes = { "approved_at(1i)" => "2001", "approved_at(2i)" => "15", "approved_at(3i)" => "17" } - it "doesn't blow up when given an incorrect values" do - invalid_post_attributes = {'published_date(1i)' => "2001", 'published_date(2i)' => "12", 'published_date(3i)' => "abc"} - invalid_comment_attributes = {'approved_at(1i)' => "2001", 'approved_at(2i)' => "15", 'approved_at(3i)' => "17"} - expect { + expect do @post.update(invalid_post_attributes) @comment.update(invalid_comment_attributes) - }.to_not raise_error + end.not_to raise_error end it "does not interfere with a date column that it has not been told to make safe" do - invalid_attributes = {'unsafe_date(1i)' => "2001", 'unsafe_date(2i)' => "12", 'unsafe_date(3i)' => "abc"} - expect { - @post.update(invalid_attributes) - }.to raise_error(ActiveRecord::MultiparameterAssignmentErrors) + invalid_attributes = { "unsafe_date(1i)" => "2001", "unsafe_date(2i)" => "12", "unsafe_date(3i)" => "abc" } + + expect { @post.update(invalid_attributes) }.to raise_error(ActiveRecord::MultiparameterAssignmentErrors) end it "adds an error when Chronic returns nil" do - invalid_post_attributes = {'published_date(1i)' => "2014", 'published_date(2i)' => "12", 'published_date(3i)' => "abc"} + invalid_post_attributes = { "published_date(1i)" => "2014", "published_date(2i)" => "12", "published_date(3i)" => "abc" } @post.update(invalid_post_attributes) - @post.errors[:published_date].should == ['is not a real date'] + + expect(@post.errors[:published_date]).to eq(["is not a real date"]) end context "with time" do it "works" do - invalid_comment_attributes = {'published_at(1i)' => "2014", 'published_at(2i)' => "12", 'published_at(3i)' => "22", 'published_at(4i)' => "12", 'published_at(5i)' => "34"} - expect { - @comment.update(invalid_comment_attributes) - }.to_not raise_error - expect(@comment.published_at.utc.strftime("%FT%T%:z")).to eq(Time.new(2014, 12, 22, 12, 34).utc.strftime("%FT%T%:z")) + attrs = { "published_at(1i)" => "2014", "published_at(2i)" => "12", "published_at(3i)" => "22", "published_at(4i)" => "12", "published_at(5i)" => "34" } + + expect { @comment.update(attrs) }.not_to raise_error + expect(@comment.published_at.utc.strftime("%FT%T%:z")). + to eq(Time.new(2014, 12, 22, 12, 34).utc.strftime("%FT%T%:z")) end - it "doesn't blow up when given an incorrect values" do - invalid_post_attributes = {'published_date(1i)' => "2001", 'published_date(2i)' => "12", 'published_date(3i)' => "17", 'published_date(4i)' => "12", 'published_date(5i)' => "34"} - invalid_comment_attributes = {'published_at(1i)' => "2001", 'published_at(2i)' => "15", 'published_at(3i)' => "17", 'published_at(4i)' => "12", 'published_at(5i)' => "34"} - expect { + it "doesn't blow up when given incorrect values" do + invalid_post_attributes = { "published_date(1i)" => "2001", "published_date(2i)" => "12", "published_date(3i)" => "17", "published_date(4i)" => "12", "published_date(5i)" => "34" } + invalid_comment_attributes = { "published_at(1i)" => "2001", "published_at(2i)" => "15", "published_at(3i)" => "17", "published_at(4i)" => "12", "published_at(5i)" => "34" } + + expect do @post.update(invalid_post_attributes) @comment.update(invalid_comment_attributes) - }.to_not raise_error + end.not_to raise_error end it "does not interfere with a date column that it has not been told to make safe" do - invalid_attributes = {'unsafe_date(1i)' => "2011", 'unsafe_date(2i)' => "9", 'unsafe_date(3i)' => "83", 'unsafe_date(4i)' => "12", 'unsafe_date(5i)' => "34"} - expect { - @post.update(invalid_attributes) - }.to raise_error(ActiveRecord::MultiparameterAssignmentErrors) + invalid_attributes = { "unsafe_date(1i)" => "2011", "unsafe_date(2i)" => "9", "unsafe_date(3i)" => "83", "unsafe_date(4i)" => "12", "unsafe_date(5i)" => "34" } + + expect { @post.update(invalid_attributes) }.to raise_error(ActiveRecord::MultiparameterAssignmentErrors) end it "adds an error when Chronic returns nil" do - invalid_comment_attributes = {'published_at(1i)' => "2009", 'published_at(2i)' => "12", 'published_at(3i)' => "90", 'published_at(4i)' => "12", 'published_at(5i)' => "34"} - @comment.update(invalid_comment_attributes) - @comment.errors[:published_at].should == ['is not a real date'] + attrs = { "published_at(1i)" => "2009", "published_at(2i)" => "12", "published_at(3i)" => "90", "published_at(4i)" => "12", "published_at(5i)" => "34" } + @comment.update(attrs) + + expect(@comment.errors[:published_at]).to eq(["is not a real date"]) end end end - end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c312067..c541961 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,6 @@ +# frozen_string_literal: true + +require "logger" require "active_support" require "active_record" require "database_cleaner" @@ -5,32 +8,25 @@ ENV["debug"] = "test" unless ENV["debug"] -# Establish DB Connection -config = YAML::load(IO.read(File.join(File.dirname(__FILE__), "db", "database.yml"))) -ActiveRecord::Base.configurations = {"test" => config[ENV["DB"] || "sqlite3"]} -ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["test"]) +config_path = File.join(__dir__, "db", "database.yml") +config = YAML.load_file(config_path) + +db_key = ENV["DB"] || "sqlite3" +db_config = config.fetch(db_key) -# Load Test Schema into the Database -load(File.dirname(__FILE__) + "/db/schema.rb") +ActiveRecord::Base.establish_connection(db_config) -# Load in our code -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" +load File.join(__dir__, "db", "schema.rb") +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) require "has_safe_dates" RSpec.configure do |config| - config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) end - config.before(:each) do - DatabaseCleaner.start - end - - config.after(:each) do - DatabaseCleaner.clean - end - + config.before(:each) { DatabaseCleaner.start } + config.after(:each) { DatabaseCleaner.clean } end