Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
inherit_from:
- https://onehq.com/rubocop3_4.yml

AllCops:
SuggestExtensions: false
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0
3.4.4
16 changes: 13 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
62 changes: 21 additions & 41 deletions has_safe_dates.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions lib/has_safe_dates/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/has_safe_dates/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module HasSafeDates
VERSION = "2.0.3"
VERSION = "5.0.0-beta.3"
end
10 changes: 5 additions & 5 deletions spec/db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# 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"
t.date "updated_at"
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"
t.date "unsafe_date"
t.date "created_at"
t.date "updated_at"
end

end
113 changes: 56 additions & 57 deletions spec/has_safe_dates_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading