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

Safa/restore discounted amount deprecated cop #66

Merged
merged 3 commits into from
Nov 24, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### New features

* [#25](https://github.com/solidusio/rubocop-solidus/issues/25): Add solidus/tax_category_deprecated warning cop. ([@safafa][])
* [#31](https://github.com/solidusio/rubocop-solidus/issues/31): Add discounted amount deprecated warning cop. ([@safafa][])

### Bug fixes

* [#53](https://github.com/solidusio/rubocop-solidus/pull/53): Change spree_default_credit_card_deperecated cop severity to warning. ([@safafa][])
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Solidus/ClassEvalDecorator:
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/21'

Solidus/DiscountedAmountDeprecated:
Description: 'Checks if .discounted_amount is being used and suggest using .total_before_tax instead.'
Enabled: true
VersionAdded: '<<next>>'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/31'

Solidus/ExistingCardIdDeprecated:
Description: 'Checks if existing_card_id is being used and suggest using wallet_payment_source_id instead'
Enabled: true
Expand Down
22 changes: 22 additions & 0 deletions docs/cops_solidus.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ end

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

## Solidus/DiscountedAmountDeprecated

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | No | <<next>> | - | 2.4

This cop finds .discounted_amount occurrences and suggest using .total_before_tax instead.

### Examples

```ruby
# bad
line_item.discounted_amount

# good
line_item.total_before_tax
```

### References

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

## Solidus/ExistingCardIdDeprecated

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ In the following sections, you will find all available cops:
#### Department [Solidus](cops_solidus.md)

* [Solidus/ClassEvalDecorator](cops_solidus.md#solidusclassevaldecorator)
* [Solidus/DiscountedAmountDeprecated](cops_solidus.md#solidusdiscountedamountdeprecated)
* [Solidus/ExistingCardIdDeprecated](cops_solidus.md#solidusexistingcardiddeprecated)
* [Solidus/ReimbursementHookDeprecated](cops_solidus.md#solidusreimbursementhookdeprecated)
* [Solidus/SpreeCalculatorFreeShippingDeprecated](cops_solidus.md#solidusspreecalculatorfreeshippingdeprecated)
Expand Down
35 changes: 35 additions & 0 deletions lib/rubocop/cop/solidus/discounted_amount_deprecated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Solidus
# This cop finds .discounted_amount occurrences and suggest using .total_before_tax instead.
#
# @example
#
# # bad
# line_item.discounted_amount
#
# # good
# line_item.total_before_tax
#
#
class DiscountedAmountDeprecated < Base
include TargetSolidusVersion
minimum_solidus_version 2.4

MSG = 'Use `.total_before_tax` instead of `.discounted_amount`.'

def_node_matcher :bad_method?, <<~PATTERN
(send ... :discounted_amount)
PATTERN

def on_send(node)
return unless bad_method?(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/discounted_amount_deprecated'
require_relative 'solidus/existing_card_id_deprecated'
require_relative 'solidus/reimbursement_hook_deprecated'
require_relative 'solidus/spree_calculator_free_shipping_deprecated'
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/solidus/discounted_amount_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::DiscountedAmountDeprecated, :config do
it 'registers an offense when using .discounted_amount' do
expect_offense(<<~RUBY)
line_item.discounted_amount
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `.total_before_tax` instead of `.discounted_amount`.
RUBY
end

it 'does not register an offense when using .total_before_tax' do
expect_no_offenses(<<~RUBY)
line_item.total_before_tax
RUBY
end
end