Skip to content

Commit

Permalink
Merge 9cd2bfa into 5968498
Browse files Browse the repository at this point in the history
  • Loading branch information
murb committed Aug 15, 2018
2 parents 5968498 + 9cd2bfa commit 94b9f5a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 32 deletions.
11 changes: 5 additions & 6 deletions act_as_time_as_boolean.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ Gem::Specification.new do |s|
s.add_dependency 'activesupport', '>= 3.2'

s.add_development_dependency 'rake'
s.add_development_dependency 'coveralls', '~> 0.6'
s.add_development_dependency 'json', '~> 1.7.7'
s.add_development_dependency 'combustion', '~> 0.5.1'
s.add_development_dependency 'rspec-rails', '~> 2.14'
s.add_development_dependency 'sqlite3', '~> 1.3.3'
s.add_development_dependency 'activerecord', '~> 4.0.4'
s.add_development_dependency 'coveralls'
s.add_development_dependency 'combustion'
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'activerecord'
end
9 changes: 6 additions & 3 deletions lib/act_as_time_as_boolean/concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ def time_as_boolean(field, options = {})
save!
end

scope field, -> {
where "#{table_name}.#{field}_at IS NOT NULL AND #{table_name}.#{field}_at <= ?", Time.current
}
unless options[:scope] == false
scope_name = options[:scope] || field
scope scope_name, -> {
where "#{table_name}.#{field}_at IS NOT NULL AND #{table_name}.#{field}_at <= ?", Time.current
}
end

if options[:opposite]
opposite = options[:opposite]
Expand Down
6 changes: 6 additions & 0 deletions spec/internal/app/models/article_with_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ArticleWithScope < ActiveRecord::Base
include ActAsTimeAsBoolean
self.table_name= 'article'

time_as_boolean :active, scope: :reactive
end
6 changes: 6 additions & 0 deletions spec/internal/app/models/article_with_scope_false.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ArticleWithScopeFalse < ActiveRecord::Base
include ActAsTimeAsBoolean
self.table_name= 'article'

time_as_boolean :active, scope: false
end
60 changes: 37 additions & 23 deletions spec/lib/act_as_time_as_boolean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Article < ActiveRecord::Base
it 'defines active= method' do
subject.should include(:active=)
end
it 'defines active! method' do
subject.should include(:active!)
end
end

describe 'with :active and opposite param' do
Expand All @@ -50,34 +53,45 @@ class Article < ActiveRecord::Base
it 'defines inactive? method' do
subject.should include(:inactive?)
end
it 'defines inactive! method' do
subject.should include(:inactive!)
end
end

describe 'scopes' do
describe 'with :active param' do
subject { Article.new }
subject { Article }

it 'define active scope' do
subject.class.methods.should include(:active)
subject.methods.should include(:active)
end
end

it 'defines active! method' do
subject.methods.should include(:active!)
describe 'with :active param and scope: false' do
subject { ArticleWithScopeFalse }

it 'define active scope' do
subject.methods.should_not include(:active)
end
end

describe 'with :active and opposite param' do
subject { ArticleWithOpposite.new }
describe 'with :active param and scope: :reactive' do
subject { ArticleWithScope }

it 'define active scope' do
subject.class.methods.should include(:active)
subject.methods.should include(:reactive)
end
end

it 'define inactive scope' do
subject.class.methods.should include(:inactive)
describe 'with :active and opposite param' do
subject { ArticleWithOpposite }

it 'define active scope' do
subject.methods.should include(:active)
end

it 'defines inactive! method' do
subject.methods.should include(:inactive!)
it 'define inactive scope' do
subject.methods.should include(:inactive)
end
end
end
Expand All @@ -89,11 +103,11 @@ class Article < ActiveRecord::Base
subject { ArticleWithOpposite.new active_at: time }

describe 'calling active' do
it { subject.active.should be_true }
it { subject.active.should be true }
end

describe 'calling active?' do
it { subject.active?.should be_true }
it { subject.active?.should be true }
end

describe 'calling active=' do
Expand Down Expand Up @@ -123,35 +137,35 @@ class Article < ActiveRecord::Base
end

describe 'calling inactive' do
it { subject.inactive.should be_false }
it { subject.inactive.should be false }
end

describe 'calling inactive?' do
it { subject.inactive?.should be_false }
it { subject.inactive?.should be false }
end

describe 'calling active!' do
before { subject.active! }

it { subject.active?.should be_true }
it { subject.active?.should be true }
end

describe 'calling inactive!' do
before { subject.inactive! }

it { subject.active?.should be_false }
it { subject.active?.should be false }
end
end

describe 'with an inactive instance' do
subject { ArticleWithOpposite.new }

describe 'calling active' do
it { subject.active.should be_false }
it { subject.active.should be false }
end

describe 'calling active?' do
it { subject.active?.should be_false }
it { subject.active?.should be false }
end

describe 'calling active=' do
Expand All @@ -173,23 +187,23 @@ class Article < ActiveRecord::Base
end

describe 'calling inactive' do
it { subject.inactive.should be_true }
it { subject.inactive.should be true }
end

describe 'calling inactive?' do
it { subject.inactive?.should be_true }
it { subject.inactive?.should be true }
end

describe 'calling active!' do
before { subject.active! }

it { subject.active?.should be_true }
it { subject.active?.should be true }
end

describe 'calling inactive!' do
before { subject.inactive! }

it { subject.active?.should be_false }
it { subject.active?.should be false }
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'bundler'

Bundler.require :default, :development

require 'coveralls'
Coveralls.wear!

Expand Down

0 comments on commit 94b9f5a

Please sign in to comment.