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

Favor #public_send over #send using Rubocop's cop #2618

Merged
merged 2 commits into from
Sep 20, 2018

Conversation

sauloperez
Copy link
Contributor

@sauloperez sauloperez commented Aug 30, 2018

What? Why?

Devs keep using #send although that method does not preserve
private/protected visibility. Watching after this turned out to be quite
time-consuming while doing code review.

Currently, the Style/Send cop doesn't enforce #public_send however
(that's what we want). It simply discourages the use of #send. See
rubocop/rubocop#2081 (comment)
for details. So a new entry on the Code Conventions doc has been added
to overcome this limitation:
https://github.com/openfoodfoundation/openfoodnetwork/wiki/Code-Conventions#prefer-public_send-over-send

What should we test?

A smoke test, including:

  • Product import
  • Subscriptions
  • Uploading a new logo from Configuration > Contents
  • Inventory items (from admin/variant_overrides URL)
  • Capture (or any other modification to the payment) an order's payment (from admin/orders/:order_id/payments URL)
  • Enterprise fees

Although very little, these features are affected by this change.

Release notes

Ensure devs use #public_send over #send by enabling a Rubocop cop.

Changelog Category: Added

Devs keep using `#send` although that method does not preserve
private/protected visibility. Watching after this turned out to be quite
time-consuming while doing code review.

Currently, the Style/Send cop doesn't enforce `#public_send` however
(that's what we want). It simply discourages the use of #send. See
rubocop/rubocop#2081 (comment)
for details. So a new entry on the Code Conventions doc has been added
to overcome this limitation:
https://github.com/openfoodfoundation/openfoodnetwork/wiki/Code-Conventions#prefer-public_send-over-send
Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

Thank you!

@luisramos0
Copy link
Contributor

There are 50 warnings, this means that when a dev touches one of these files in a PR, they will have to fix it to the new convention. Correct?

@sauloperez
Copy link
Contributor Author

AFAIK that Cop doesn't have autocorrect but I can try to fix them

@sauloperez sauloperez force-pushed the enable-style-send-cop branch 4 times, most recently from c8b0d0f to e99fee1 Compare August 31, 2018 10:22
@@ -44,7 +44,7 @@ def changeable_orders_alert
private

def filtered_json(products_json)
if applicator.send(:rules).any?
if applicator.rules.any?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made this one public. If we use it from outside, it's part of applicator's public API

@@ -10,7 +10,7 @@ module Admin
# Only show payment methods that user has access to and sort by distributor name
# ! Redundant code copied from Spree::Admin::ResourceController with modifications marked
def collection
return parent.send(controller_name) if parent_data.present?
return parent.controller_name if parent_data.present?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

controller_name is a public method

Copy link
Member

Choose a reason for hiding this comment

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

The original did not call send(:controller_name). Doesn't it call a method with the same name as the controller?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

💩 yes, I went too fast on this one. Thanks for the heads-up!

@@ -15,7 +15,7 @@ def ng_form_for(name, *args, &block)
# spree.foo_path in any view rendered from non-spree-namespaced controllers.
def method_missing(method, *args, &block)
if (method.to_s.end_with?('_path') || method.to_s.end_with?('_url')) && spree.respond_to?(method)
spree.send(method, *args)
spree.public_send(method, *args)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rails' URL helpers are public

@@ -12,7 +12,7 @@ def initialize(klass, collection, attributes={}, reject_if=nil, delete_if=nil)
@collection = attributes[:collection] if attributes[:collection]

attributes.each do |name, value|
send("#{name}=", value)
public_send("#{name}=", value)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the setters of a model are meant to be public

@@ -41,7 +41,7 @@ def resume
return false unless order_cycle.orders_close_at.andand > Time.zone.now
transaction do
update_column(:canceled_at, nil)
order.send('resume') if order
order.resume if order
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these two methods are public

@@ -83,7 +83,7 @@ def relevant_address_attrs

def addresses_match?(order_address, subscription_address)
relevant_address_attrs.all? do |attr|
order_address[attr] == subscription_address.send("#{attr}_was") ||
order_address[attr] == subscription_address.public_send("#{attr}_was") ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

AR's dirty attributes API is public

@@ -42,7 +42,7 @@ def self.parse(payload, sso_secret = nil)
if BOOLS.include? k
val = ["true", "false"].include?(val) ? val == "true" : nil
end
sso.send("#{k}=", val)
sso.public_send("#{k}=", val)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

each of these K are defined as public attr_accessor in line 11

base.extend(ActiveModel::Naming)
base.extend(ActiveModel::Callbacks)
base.include(ActiveModel::Validations)
base.include(Paperclip::Glue)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

extend and include are public Module methods in Ruby 2.1.5 https://ruby-doc.org/core-2.1.5/Module.html#method-i-include

base.send :define_model_callbacks, :save, only: [:after]
base.send :define_model_callbacks, :commit, only: [:after]
base.send :define_model_callbacks, :destroy, only: [:before, :after]
base.define_model_callbacks(:save, only: [:after])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sauloperez sauloperez force-pushed the enable-style-send-cop branch 2 times, most recently from c376f70 to 7346ed4 Compare August 31, 2018 13:26
Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

What is the test pan? Do you think these changes are validated by automatic tests?

@sauloperez
Copy link
Contributor Author

Thanks for the heads-up. I think this now deserves a smoke test.

@Matt-Yorkley
Copy link
Contributor

Matt-Yorkley commented Sep 1, 2018

I think this now deserves a smoke test.

Looks like the tests are smoking already... :trollface:

@sauloperez
Copy link
Contributor Author

lol @Matt-Yorkley :trollface:

@sauloperez sauloperez force-pushed the enable-style-send-cop branch from 7346ed4 to 74bf97a Compare September 3, 2018 09:48
Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

That's a lot of changes...
Great stuff.

@sauloperez What parts of the application should be tested more carefully in staging?

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

I spotted one change that doesn't look right to me. But I might be wrong?

@@ -10,7 +10,7 @@ module Admin
# Only show payment methods that user has access to and sort by distributor name
# ! Redundant code copied from Spree::Admin::ResourceController with modifications marked
def collection
return parent.send(controller_name) if parent_data.present?
return parent.controller_name if parent_data.present?
Copy link
Member

Choose a reason for hiding this comment

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

The original did not call send(:controller_name). Doesn't it call a method with the same name as the controller?

@sauloperez sauloperez force-pushed the enable-style-send-cop branch from 74bf97a to 5a06e82 Compare September 5, 2018 10:02
@sauloperez sauloperez force-pushed the enable-style-send-cop branch from 5a06e82 to b23cb55 Compare September 5, 2018 10:05
@sauloperez
Copy link
Contributor Author

sauloperez commented Sep 5, 2018

I have extended the list of features to test. Thanks @luisramos0 !

@sauloperez
Copy link
Contributor Author

Staged on https://staging.katuma.org/

@RachL RachL self-assigned this Sep 18, 2018
@RachL
Copy link
Contributor

RachL commented Sep 20, 2018

FINALLY!
Sorry it took me so long ...
Everything looks good:
https://docs.google.com/document/d/1FLZVVyGaH6ubjeM-bpXHzeN4GxUTsTVC6fADeCO1xGk/edit

@sauloperez sauloperez merged commit 20077c9 into openfoodfoundation:master Sep 20, 2018
@sauloperez sauloperez deleted the enable-style-send-cop branch September 20, 2018 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants