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

New cop to warn about existing_card_id usage #62

Merged
merged 5 commits into from
Nov 3, 2023
Merged
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## main (unreleased)

## 0.2.0 (2023-11-02)

### New features

* [#60](https://github.com/solidusio/rubocop-solidus/issues/60): Create new cop to check existing_card_id. ([@MassimilianoLattanzio][])

### Bug fixes

* [#60](https://github.com/solidusio/rubocop-solidus/issues/60): Fix overridden add_offense method. ([@MassimilianoLattanzio][])

## 0.1.4 (2023-08-04)

### New features
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rubocop-solidus (0.1.4)
rubocop-solidus (0.2.0)
rubocop

GEM
Expand Down
26 changes: 16 additions & 10 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,65 @@ AllCops:
Solidus/ClassEvalDecorator:
Description: 'Checks if Class.class_eval is being used in the code'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/21'

Solidus/ExistingCardIdDeprecated:
Description: 'Checks if existing_card_id is being used and suggest using wallet_payment_source_id instead'
Enabled: true
VersionAdded: '0.2'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/60'

Solidus/ReimbursementHookDeprecated:
Description: 'Checks if reimbursement_success_hooks and reimbursement_failed_hooks is being used'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/27'

Solidus/SpreeCalculatorFreeShippingDeprecated:
Description: 'Checks if Spree::Calculator::FreeShipping is being used and add deprecation message'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/29'

Solidus/SpreeCalculatorPercentPerItemDeprecated:
Description: 'Checks if Spree::Calculator::PercentPerItem is being used and add deprecation message'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/29'

Solidus/SpreeCalculatorPriceSackDeprecated:
Description: 'Checks if Spree::Calculator::PriceSack is being used and add deprecation message'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/29'

Solidus/SpreeDefaultCreditCardDeprecated:
Description: 'Checks if user.default_credit_card is used and suggest using user.wallet.default_wallet_payment_source'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/33'

Solidus/SpreeGatewayBogusDeprecated:
Description: 'Checks if SpreeGatewayBogus is being used and replaces it with Spree::PaymentMethod::BogusCreditCard'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/26'

Solidus/SpreeIconDeprecated:
Description: 'Checks if icon helper is being used and suggest `solidus_icon`'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/32'

Solidus/SpreeRefundCallPerform:
Description: 'Checks if Spree::Refund.create is being used and require calling .perform!'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/28'

Solidus/SpreeTDeprecated:
Description: 'Checks if Spree.t is being used and replaces it with I18n.t.'
Enabled: true
VersionAdded: '0.1.0'
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/22'
1 change: 1 addition & 0 deletions docs/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ In the following section you find all available cops:
#### Department [Solidus](cops_solidus.md)

* [Solidus/ClassEvalDecorator](cops_solidus.md#solidusclassevaldecorator)
* [Solidus/ExistingCardIdDeprecated](cops_solidus.md#solidusexistingcardiddeprecated)
* [Solidus/ReimbursementHookDeprecated](cops_solidus.md#solidusreimbursementhookdeprecated)
* [Solidus/SpreeCalculatorFreeShippingDeprecated](cops_solidus.md#solidusspreecalculatorfreeshippingdeprecated)
* [Solidus/SpreeCalculatorPercentPerItemDeprecated](cops_solidus.md#solidusspreecalculatorpercentperitemdeprecated)
Expand Down
48 changes: 38 additions & 10 deletions docs/cops_solidus.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | No | 0.1.0 | - | -
Enabled | Yes | No | 0.1 | - | -

Solidus suggests a decorator module instead of `class_eval` when overriding some features.
This cop finds any `class_eval` and asks to use a decorator module instead.
Expand Down Expand Up @@ -32,11 +32,39 @@ end

* [https://github.com/solidusio/rubocop-solidus/issues/21](https://github.com/solidusio/rubocop-solidus/issues/21)

## Solidus/ExistingCardIdDeprecated

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | No | 0.2 | - | 2.2

This cop finds existing_card_id occurrences and suggest using wallet_payment_source_id instead.

### Examples

```ruby
# bad
{
name: payment_method.name,
existing_card_id: payment_source.id
}

# good
{
name: payment_method.name,
wallet_payment_source_id: payment_source.wallet.wallet_payment_sources.first.id
}
```

### References

* [https://github.com/solidusio/rubocop-solidus/issues/60](https://github.com/solidusio/rubocop-solidus/issues/60)

## Solidus/ReimbursementHookDeprecated

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | No | 0.1.0 | - | 2.11
Enabled | Yes | No | 0.1 | - | 2.11

This cop finds reimbursement_success_hooks and reimbursement_failed_hooks calls and
asks to remove them and subscribe to reimbursement_reimbursed event instead.
Expand Down Expand Up @@ -66,7 +94,7 @@ reimbursement_failed_hooks.any?

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | No | 0.1.0 | - | -
Enabled | Yes | No | 0.1 | - | -

This cop finds Spree::Calculator::FreeShipping calls.
This cop is needed as they have been deprecated in future version.
Expand All @@ -88,7 +116,7 @@ Spree::Calculator::FreeShipping

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.1.0 | - | -
Enabled | Yes | Yes | 0.1 | - | -

This cop finds Spree::Calculator::PercentPerItem calls.
This cop is needed as they have been deprecated in future version.
Expand All @@ -111,7 +139,7 @@ Spree::Calculator::PercentOnLineItem

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | No | 0.1.0 | - | -
Enabled | Yes | No | 0.1 | - | -

This cop finds Spree::Calculator::PriceSack calls.
This cop is needed as they have been deprecated in future version.
Expand All @@ -133,7 +161,7 @@ Spree::Calculator::PriceSack

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.1.0 | - | 2.2
Enabled | Yes | Yes | 0.1 | - | 2.2

This cop finds user.default_credit_card suggest using user.wallet.default_wallet_payment_source.

Expand All @@ -155,7 +183,7 @@ user.wallet.default_wallet_payment_source

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.1.0 | - | 2.1
Enabled | Yes | Yes | 0.1 | - | 2.1

This cop finds Spree::Gateway::Bogus calls and replaces them with the Spree::PaymentMethod::BogusCreditCard.
This cop is needed as the Spree::Gateway::Bogus has been deprecated in future version.
Expand All @@ -182,7 +210,7 @@ Spree::PaymentMethod::BogusCreditCard.create!

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.1.0 | - | 2.3
Enabled | Yes | Yes | 0.1 | - | 2.3

This cop finds icon helper calls and suggest using solidus_icon.

Expand All @@ -204,7 +232,7 @@ helper.solidus_icon('example')

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | No | 0.1.0 | - | 2.11
Enabled | Yes | No | 0.1 | - | 2.11

This cop finds Spree::Refund.create(your: attributes) calls and
replaces them with the Spree::Refund.create(your: attributes, perform_after_create: false).perform! call.
Expand All @@ -227,7 +255,7 @@ Spree::Refund.create(your: attributes, perform_after_create: false).perform!

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.1.0 | - | -
Enabled | Yes | Yes | 0.1 | - | -

This cop finds Spree.t method calls and replaces them with the I18n,t method call.
This cop is needed as the Spree.t version has been deprecated in future version.
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/target_solidus_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def required_minimum_solidus_version
def add_offense(*args, **kwargs, &block)
return unless affected_solidus_version?

if Gem::Version.new(RUBY_VERSION) > Gem::Version.new('3')
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7')
super(*args, **kwargs, &block)
else
super(*args, &block)
Expand Down
40 changes: 40 additions & 0 deletions lib/rubocop/cop/solidus/existing_card_id_deprecated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Solidus
# This cop finds existing_card_id occurrences and suggest using wallet_payment_source_id instead.
#
# @example
#
# # bad
# {
# name: payment_method.name,
# existing_card_id: payment_source.id
# }
#
# # good
# {
# name: payment_method.name,
# wallet_payment_source_id: payment_source.wallet.wallet_payment_sources.first.id
# }
#
class ExistingCardIdDeprecated < Base
include TargetSolidusVersion
minimum_solidus_version 2.2

MSG = 'Use `wallet_payment_source_id` instead of `existing_card_id`.'

def_node_matcher :existing_card_id?, <<~PATTERN
(send ... :existing_card_id)
PATTERN

def on_send(node)
return unless existing_card_id?(node)

add_offense(node, severity: :warning)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/solidus_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative 'mixin/target_solidus_version'

require_relative 'solidus/class_eval_decorator'
require_relative 'solidus/existing_card_id_deprecated'
require_relative 'solidus/reimbursement_hook_deprecated'
require_relative 'solidus/spree_calculator_free_shipping_deprecated'
require_relative 'solidus/spree_calculator_percent_per_item_deprecated'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/solidus/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module RuboCop
module Solidus
VERSION = '0.1.4'
VERSION = '0.2.0'
end
end
9 changes: 9 additions & 0 deletions relnotes/v0.2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### New features

* [#60](https://github.com/solidusio/rubocop-solidus/issues/60): Create new cop to check existing_card_id. ([@MassimilianoLattanzio][])

### Bug fixes

* [#60](https://github.com/solidusio/rubocop-solidus/issues/60): Fix overridden add_offense method. ([@MassimilianoLattanzio][])

[@MassimilianoLattanzio]: https://github.com/MassimilianoLattanzio
16 changes: 16 additions & 0 deletions spec/rubocop/cop/solidus/existing_card_id_deprecated_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Solidus::ExistingCardIdDeprecated, :config do
it 'registers an offense when using `existing_card_id`' do
expect_offense(<<~RUBY, severity: :warning)
existing_card_id
^^^^^^^^^^^^^^^^ Use `wallet_payment_source_id` instead of `existing_card_id`.
RUBY
end

it 'does not register an offense when using `wallet_payment_source_id`' do
expect_no_offenses(<<~RUBY)
wallet_payment_source_id
RUBY
end
end