Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to skip generating scope #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_at: Fri, 04 Sep 2020 18:16:38 UTC +00:00>]

Post.published(false)
#=> [#<Post published_at: nil>]
```

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
Expand All @@ -34,17 +59,6 @@ puts post.published_at
#=> nil
```

This also adds a scope for each timestamps column

```ruby
Post.published
#=> [#<Post published_at: Fri, 04 Sep 2020 18:16:38 UTC +00:00>]

Post.published(false)
#=> [#<Post published_at: nil>]
```


## 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.
Expand Down
18 changes: 10 additions & 8 deletions lib/boolean_timestamps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/boolean_timestamps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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