From 3bc52afd8444ed18d29150e6e124dc0cdf2e2c12 Mon Sep 17 00:00:00 2001 From: Tyler Callahan Date: Wed, 14 Dec 2022 15:33:57 -0800 Subject: [PATCH] Add option to skip generating scope This adds a `:scopes` option to `boolean_timestamps` that allows the user to skip generating the scope. This may be desirable in particular when the generated scope conflicts with an existing Rails method. For example, `boolean_timestamps :loaded_at` attempts to create a scope `:loaded` and raises an error because the method `:loaded` is already defined. Adding the `scopes: false` option avoids the error. Fixes #7 --- README.md | 36 +++++++++++++++++++++++---------- lib/boolean_timestamps.rb | 18 +++++++++-------- spec/boolean_timestamps_spec.rb | 7 +++++++ 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index a61df79..129c2df 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,31 @@ class Post < ActiveRecord::Base end ``` +### Option: Generated scope + +By default, this adds a scope for each timestamp column: + +```ruby +Post.published +#=> [#] + +Post.published(false) +#=> [#] +``` + +To skip defining the scope, set the `:scopes` option to `false` when calling `boolean_timestamps`. + +```ruby +class Post < ActiveRecord::Base + boolean_timestamps :published_at, scopes: false +end +``` +```ruby +Post.respond_to?(:published) +#=> false +``` + + ## Example ```ruby @@ -34,17 +59,6 @@ puts post.published_at #=> nil ``` -This also adds a scope for each timestamps column - -```ruby -Post.published -#=> [#] - -Post.published(false) -#=> [#] -``` - - ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. diff --git a/lib/boolean_timestamps.rb b/lib/boolean_timestamps.rb index 740f57f..981194e 100644 --- a/lib/boolean_timestamps.rb +++ b/lib/boolean_timestamps.rb @@ -6,7 +6,7 @@ module BooleanTimestamps extend ActiveSupport::Concern class_methods do - def boolean_timestamps(*attributes) + def boolean_timestamps(*attributes, scopes: true) attributes.each do |timestamp_attribute| boolean_attribute = timestamp_attribute.to_s.gsub(/_at\z/, '') define_method boolean_attribute do @@ -22,13 +22,15 @@ def boolean_timestamps(*attributes) send("#{timestamp_attribute}=", timestamp) end end - scope boolean_attribute, lambda { |value = true| - if value - where.not(timestamp_attribute => nil) - else - where(timestamp_attribute => nil) - end - } + if scopes + scope boolean_attribute, lambda { |value = true| + if value + where.not(timestamp_attribute => nil) + else + where(timestamp_attribute => nil) + end + } + end end end end diff --git a/spec/boolean_timestamps_spec.rb b/spec/boolean_timestamps_spec.rb index 86ffb50..45ecf7c 100644 --- a/spec/boolean_timestamps_spec.rb +++ b/spec/boolean_timestamps_spec.rb @@ -5,6 +5,10 @@ class User < ActiveRecord::Base boolean_timestamps :activated_at end + class UserWithoutScopes < ActiveRecord::Base + boolean_timestamps :activated_at, scopes: false + end + class War < ActiveRecord::Base boolean_timestamps :massive_attack_at end @@ -43,5 +47,8 @@ class War < ActiveRecord::Base expect(User.activated(true).to_sql).to include('activated_at" IS NOT NULL') expect(User.activated(false).to_sql).to include('activated_at" IS NULL') end + it 'does not add a scope when scopes option is false' do + expect(UserWithoutScopes.respond_to?(:activated)).to be false + end end end