Skip to content

Conversation

@seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Jan 5, 2025

This commit introduces three additional callbacks for the sake of parity
with other lifecycle methods:

  • before_reload - evaluated before re-fetching the resource's data
    with a GET request

    class User < ActiveResource::Base
      before_reload -> { throw :abort }, unless: :id?
    end
  • after_reload - evaluated after re-fetching the resource's data with
    a GET request

    class User < ActiveResource::Base
      after_reload -> { Rails.logger.info("Congratulations, the callback has run!") }
    end
  • around_reload - evaluated around re-fetching the resource's data
    with a GET request

    class User < ActiveResource::Base
      around_reload :log_reloading
    
      schema do
        attribute :email, :string
      end
    
      def log_reloading
        Rails.logger.info("Reloading user with email: #{email}")
        yield
        Rails.logger.info("User reloaded with email: #{email}")
      end
    end

@rafaelfranca
Copy link
Member

rafaelfranca commented Sep 10, 2025

You forgot to explain why those callbacks are useful. Can you please provide a use case?

This commit introduces three additional callbacks for the sake of parity
with other lifecycle methods:

* `before_reload` - evaluated before re-fetching the resource's data
  with a `GET` request

  ```ruby
  class User < ActiveResource::Base
    before_reload -> { throw :abort }, unless: :id?
  end
  ```

* `after_reload` - evaluated after re-fetching the resource's data with
  a `GET` request

  ```ruby
  class User < ActiveResource::Base
    after_reload -> { Rails.logger.info("Congratulations, the callback has run!") }
  end
  ```

* `around_reload` - evaluated around re-fetching the resource's data
  with a `GET` request

  ```ruby
  class User < ActiveResource::Base
    around_reload :log_reloading

    schema do
      attribute :email, :string
    end

    def log_reloading
      Rails.logger.info("Reloading user with email: #{email}")
      yield
      Rails.logger.info("User reloaded with email: #{email}")
    end
  end
  ```
@rafaelfranca rafaelfranca merged commit d488fe1 into rails:main Sep 11, 2025
19 checks passed
@seanpdoyle seanpdoyle deleted the reload_callbacks branch September 11, 2025 21:46
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.

2 participants