Skip to content

Make scope creation optional#6

Open
murb wants to merge 2 commits into
caedes:masterfrom
murb:make_scope_creation_optional
Open

Make scope creation optional#6
murb wants to merge 2 commits into
caedes:masterfrom
murb:make_scope_creation_optional

Conversation

@murb
Copy link
Copy Markdown

@murb murb commented Aug 15, 2018

The code

time_as_boolean :reject

was causing the following error (since Rails 5.2):

You tried to define a scope named "reject" on the model "CarMatcher", but ActiveRecord::Relation already defined an instance method with the same name.

This change allows one to work around this error, either by saying:

time_as_boolean :reject, scope: false

or alternatively:

time_as_boolean :reject, scope: :rejected

Comment thread spec/spec_helper.rb
@@ -1,3 +1,7 @@
require 'bundler'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


it 'defines inactive! method' do
subject.methods.should include(:inactive!)
it 'define inactive scope' do
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

describe 'with :active and opposite param' do
subject { ArticleWithOpposite }

it 'define active scope' do
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


it 'define inactive scope' do
subject.class.methods.should include(:inactive)
describe 'with :active and opposite param' do
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


describe 'with :active and opposite param' do
subject { ArticleWithOpposite.new }
describe 'with :active param and scope: :reactive' do
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -0,0 +1,6 @@
class ArticleWithScopeFalse < ActiveRecord::Base
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.

@@ -0,0 +1,6 @@
class ArticleWithScope < ActiveRecord::Base
include ActAsTimeAsBoolean
self.table_name= 'article'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundOperators: Surrounding space missing for operator =.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -0,0 +1,6 @@
class ArticleWithScope < ActiveRecord::Base
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.

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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [104/80]

}
unless options[:scope] == false
scope_name = options[:scope] || field
scope scope_name, -> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/Lambda: Use the lambda method for multiline lambdas.

@murb
Copy link
Copy Markdown
Author

murb commented Aug 15, 2018

ignoring @houndci-bot for now; happy to make more fixes to the code which, in all its simplicity, I find quite useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants