1
\$\begingroup\$

I need a little bit of refactor my initialize method because I think it will make initializer more flexible and readable.

class LogAdminData
  DEFAULT_EXCLUDED_PARAMS = %w[
    encrypted_password
    reset_password_token
    reset_password_sent_at
  ].freeze

  def initialize(admin_obj:, action_type:, old_data:, new_data:, excluded_params: %w[])
    excluded_params += DEFAULT_EXCLUDED_PARAMS
    @old_data = old_data.reject { |k, _v| excluded_params.include? k }
    @new_data = new_data.reject { |k, _v| excluded_params.include? k }
    @action_type = action_type
    @admin_email = admin_obj.email
    @admin_role = admin_obj.role
  end

  def call
    AdminPanelLog.create(
      admin_email: admin_email,
      admin_role: admin_role,
      action_type: action_type,
      new_data: new_data,
      old_data: old_data,
    )
  end

Maybe something like

  def cleanup_data(data)
    excluded_params += DEFAULT_EXCLUDED_PARAMS
    data.reject { |k, _v| excluded_params.include? k }
  end

But how to call it in initializer using old_data and new_data?

\$\endgroup\$
  • 2
    \$\begingroup\$ What does this code do, and how is it used? Is this the entire class? As it is, it looks like some essential parts of the code are missing, and your question makes little sense. \$\endgroup\$ – 200_success Sep 12 '19 at 3:24
0
\$\begingroup\$

If this is a Rails project (or you use ActiveSupport) you can replace

@old_data = old_data.reject { |k, _v| excluded_params.include? k } with @old_data = old_data.except(excluded_params)

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.