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

Initial support for ActiveRecord enums (and other things, too) #1618

Merged
merged 12 commits into from Jul 16, 2021

Conversation

presidentbeef
Copy link
Owner

Tired of splitting this into separate PRs, so here we go!

For very simple, obvious class methods, Brakeman will now pull out the return value:

class A
  def self.cool_beans
    "nice beans!"
  end
end

A.cool_beans # => "nice beans!"

This PR also builds on that to support use of ActiveRecord enum, which essentially just defining a number of methods that return constant values or booleans.

class B < ActiveRecord::Base
  enum status: [:waiting, :running, :done]
end

B.statuses # => { waiting: 0, running: 1, done: 2 }

Additionally:

  • pluralize is slightly better now (status -> statuses instead of statuss)
  • Support for Hash#include?

Copy link

@alpaca-tc alpaca-tc 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 for great PR 🎉

I'm not a maintainer but I was a little worried 😄 🙏

arg = call.first_arg
return unless hash? arg

enum_name = arg[1].value # first key

Choose a reason for hiding this comment

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

This line raises WrongSexpError: Sexp#value called on multi-item Sexp if arg[1] is string interpolation like the followings. Unfortunately our project contains this rare case 😢

%i[a b].each do |prefix|
  enum "#{prefix}_column" => [...]
end

end

enum_method = s(:defn, enum_name, s(:args), safe_literal(call_line))
enums_method = s(:defs, s(:self), enums_name, s(:args), enum_values)

Choose a reason for hiding this comment

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

I may be wrong but I think that enum_values have to take into consideration _prefix/_suffix options.
https://api.rubyonrails.org/v5.2.3/classes/ActiveRecord/Enum.html

enum(item_type: %i[digital]) #=> define digital
enum(item_type: %i[digital], _prefix: true) #=> define item_type_digital

Repository owner locked and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants