Skip to content

Commit

Permalink
Merge pull request #63 from solidusio/safa/restore-tax-category-depre…
Browse files Browse the repository at this point in the history
…cated-cop

Restore solidus/tax_category_deprecated warning cop
  • Loading branch information
safafa authored Nov 24, 2023
2 parents a9135c4 + 747aea3 commit 91dec22
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## main (unreleased)

### New features

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

## 0.2.0 (2023-11-02)

### New features
Expand Down Expand Up @@ -36,3 +40,4 @@

- Initial release
[@MassimilianoLattanzio]: https://github.com/MassimilianoLattanzio
[@safafa]: https://github.com/safafa
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ Solidus/SpreeTDeprecated:
Enabled: true
VersionAdded: '0.1'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/22'

Solidus/TaxCategoryDeprecated:
Description: 'Checks if tax_category is being used and issue a warning'
Enabled: true
VersionAdded: '<<next>>'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/25'
22 changes: 22 additions & 0 deletions docs/cops_solidus.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,25 @@ I18n.t('bar', scope: 'spree.admin.city')
### References

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

## Solidus/TaxCategoryDeprecated

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

This cop finds .tax_category occurrences and suggest using .tax_categories instead.

### Examples

```ruby
# bad
model.tax_category = data

# good
model.tax_categories = [data]
```

### References

* [https://github.com/solidusio/rubocop-solidus/issues/25](https://github.com/solidusio/rubocop-solidus/issues/25)
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ In the following sections, you will find all available cops:
* [Solidus/SpreeIconDeprecated](cops_solidus.md#solidusspreeicondeprecated)
* [Solidus/SpreeRefundCallPerform](cops_solidus.md#solidusspreerefundcallperform)
* [Solidus/SpreeTDeprecated](cops_solidus.md#solidusspreetdeprecated)
* [Solidus/TaxCategoryDeprecated](cops_solidus.md#solidustaxcategorydeprecated)

<!-- END_COP_LIST -->
36 changes: 36 additions & 0 deletions lib/rubocop/cop/solidus/tax_category_deprecated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Solidus
# This cop finds .tax_category occurrences and suggest using .tax_categories instead.
#
# @example
#
# # bad
# model.tax_category = data
#
# # good
# model.tax_categories = [data]
#
#
#
class TaxCategoryDeprecated < Base
include TargetSolidusVersion
minimum_solidus_version 2.2

MSG = 'If you are Using Taxrate model use `.tax_categories = [data]` instead of `.tax_category = data`'

def_node_matcher :existing_tax_category?, <<~PATTERN
(send ... :tax_category)
PATTERN

def on_send(node)
return unless existing_tax_category?(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 @@ -13,3 +13,4 @@
require_relative 'solidus/spree_icon_deprecated'
require_relative 'solidus/spree_refund_call_perform'
require_relative 'solidus/spree_t_deprecated'
require_relative 'solidus/tax_category_deprecated'
17 changes: 17 additions & 0 deletions spec/rubocop/cop/solidus/tax_category_deprecated_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Solidus::TaxCategoryDeprecated, :config do
let(:config) { RuboCop::Config.new }
it 'registers an offense when using `tax_category`' do
expect_offense(<<~RUBY)
model.tax_category
^^^^^^^^^^^^^^^^^^ Solidus/TaxCategoryDeprecated: If you are Using Taxrate model use `.tax_categories = [data]` instead of `.tax_category = data`
RUBY
end

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

0 comments on commit 91dec22

Please sign in to comment.