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

Accept a collection in fresh_when and stale? #18374

Merged
merged 1 commit into from Feb 11, 2015

Conversation

Projects
None yet
6 participants
@claudiob
Member

claudiob commented Jan 7, 2015

The methods fresh_when and stale? from ActionController::ConditionalGet accept a single record as a short form for a hash (see docs). For instance:

  def show
    @article = Article.find(params[:id])
    fresh_when(@article)
  end

is just a short form for:

  def show
    @article = Article.find(params[:id])
    fresh_when(etag: @article, last_modified: @article.created_at)
  end

This commit extends fresh_when and stale? to also accept a collection of records, so that a short form similar to the one above can be used in an index action.
After this commit, the following code:

def index
  @article = Article.all
  fresh_when(etag: @articles, last_modified: @articles.maximum(:created_at))
end

can be simply written as:

def index
  @article = Article.all
  fresh_when(@articles)
end
@egilburg

View changes

Show outdated Hide outdated actionpack/lib/action_controller/metal/conditional_get.rb
@claudiob

This comment has been minimized.

Show comment
Hide comment
@claudiob

claudiob Feb 9, 2015

Member

Good morning @rafaelfranca ☀️
Any opinion on this?

I think that's something that would help me and other developers too, since combining fresh_when and .maximum is something that I've seen in several places such as:

Member

claudiob commented Feb 9, 2015

Good morning @rafaelfranca ☀️
Any opinion on this?

I think that's something that would help me and other developers too, since combining fresh_when and .maximum is something that I've seen in several places such as:

@arthurnn

View changes

Show outdated Hide outdated actionpack/lib/action_controller/metal/conditional_get.rb
@arthurnn

This comment has been minimized.

Show comment
Hide comment
@arthurnn

arthurnn Feb 10, 2015

Member

@claudiob , wanna rebase this on latest master and try again?
thanks

Member

arthurnn commented Feb 10, 2015

@claudiob , wanna rebase this on latest master and try again?
thanks

Accept a collection in fresh_when and stale?
The methods `fresh_when` and `stale?` from ActionController::ConditionalGet
accept a single record as a short form for a hash. For instance

```ruby
  def show
    @Article = Article.find(params[:id])
    fresh_when(@Article)
  end
```

is just a short form for:

```ruby
  def show
    @Article = Article.find(params[:id])
    fresh_when(etag: @Article, last_modified: @article.created_at)
  end
```

This commit extends `fresh_when` and `stale?` to also accept a collection
of records, so that a short form similar to the one above can be used in
an `index` action. After this commit, the following code:

```ruby
def index
  @Article = Article.all
  fresh_when(etag: @articles, last_modified: @articles.maximum(:created_at))
end
```

can be simply written as:

```ruby
def index
  @Article = Article.all
  fresh_when(@articles)
end
```
@claudiob

This comment has been minimized.

Show comment
Hide comment
@claudiob

claudiob Feb 11, 2015

Member

@arthurnn Done… now we can review 🎀

Member

claudiob commented Feb 11, 2015

@arthurnn Done… now we can review 🎀

@kaspth

This comment has been minimized.

Show comment
Hide comment
@kaspth

kaspth Feb 11, 2015

Member

Looks good.

Member

kaspth commented Feb 11, 2015

Looks good.

rafaelfranca added a commit that referenced this pull request Feb 11, 2015

Merge pull request #18374 from claudiob/add-collection-to-fresh-when
Accept a collection in fresh_when and stale?

@rafaelfranca rafaelfranca merged commit e4b624a into rails:master Feb 11, 2015

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@arthurnn

This comment has been minimized.

Show comment
Hide comment
@arthurnn

arthurnn Feb 11, 2015

Member

nice!! code looks much better now!

Member

arthurnn commented Feb 11, 2015

nice!! code looks much better now!

@claudiob claudiob deleted the claudiob:add-collection-to-fresh-when branch Feb 11, 2015

# Before
def index
@article = Article.all
fresh_when(etag: @articles, last_modified: @articles.maximum(:created_at))

This comment has been minimized.

@dhh

dhh Feb 14, 2015

Member

Shouldn't this be maximum(:updated_at)?

@dhh

dhh Feb 14, 2015

Member

Shouldn't this be maximum(:updated_at)?

This comment has been minimized.

@kaspth

kaspth Feb 14, 2015

Member

Yes, it's fixed on master.

@kaspth

kaspth Feb 14, 2015

Member

Yes, it's fixed on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment