I'm a junior who wants to refactor a big method in my code, which has been tested and works:  def self.authorized_status plugin_id, store_id, application_id, version, locale=nil    if plugin_id.match /^(\w|-)+$/      I18n.locale = locale || :en # I think I can move it to a concern      # for this I thought about an external method how return two values, or a hash. Any idea?      found_by = :iid      plugin = Plugin.find_by_iid(plugin_id)      if !plugin        plugin = Plugin.find_by_aid(plugin_id)        found_by = :aid      end      application_id_and_version = { application_id: application_id, version: version, status: Application::PUBLISHED_STATUS }      update_by_application_id = ApplicationUpdate.where(application_id: application_id).joins(application: :store).where(application: {store_id: store_id} )      is_store = update_by_application_id.first.application.is_store? if !update_by_application_id.first.nil?      # for the rest of the code I don't know what to do expect put validations into separate methods.       if plugin.nil? || plugin.user.nil?        { status: UNREGISTERED_PLUGIN, message: I18n.t('....') }      elsif ApplicationUpdate.where(application_id_and_version).count == 0 && !is_store        { status: UNKNOWN_APPLICATION, message: I18n.t('....') }      else        if !is_store          groups = Group.joins(users: :plugins).where(users: {plugins: {found_by => plugin_id}})          .joins(:application_updates)          .where(application_updates: application_id_and_version)        end        if !is_store && groups.empty?          { status: NOT_AUTHORIZED, message: I18n.t('....') }        else          store = Store.find store_id          authorized = (!store.preauthorized?) || is_store || plugin.authorized_for_store?(store)          if !authorized || BlacklistedPlugin.find_by_organisation_id_and_plugin_id(store.organisation_id, plugin.id)            { status: NOT_AUTHORIZED, message: I18n.t('....') }          else            { status: AUTHORIZED, message: I18n.t('....') }          end        end      end    else      { status: PLUGIN_ID_FORMAT_ERROR, message: I18n.t('....') }    end  endI saw recently to get some help a talk from Andy Pike (slide) with some nice principle like OCP or SOLID. I don't think the way he refactors the code is something I should do but maybe I'm wrong. To be honest, the method sometimes looks so weird, especially with all the ifs, that I don't know where to start.
Managing many availability status
ruby;ruby on rails