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

Support scopes in relationships #1343

Merged
merged 2 commits into from
Aug 31, 2021

Conversation

oboxodo
Copy link
Contributor

@oboxodo oboxodo commented Jun 22, 2021

Rails has replaced the :conditions relationships' option with optional lambda/proc scopes for a long time already. It just makes sense for Formtastic to support them the same way it does for the :conditions option.

Given this code:

class Post < ApplicationRecord
  belongs_to :author, -> { active.order(name: :asc) }
end

class Author < ApplicationRecord
  scope :active, -> { where(active: true) }
end
- semantic_form_for(Post.new) do |f|
  = f.input :author

Without this change the autogenerated select input with the list of authors will include ALL authors because it effectively loads the list from Author.all. After this change, the list will be filled with Author.active.order(name: :asc) because the belongs_to relationship includes a scope.

This seems to bring parity to the old way of applying conditions to relationships via the :conditions option which got deprecated back in Rails 4.

Rails has relaced the `:conditions` relationships' option with optional lambda/proc `scopes` for a long time already. It just makes sense for Formtastic to support them the same way it does for the `:conditions` option.

Given this code:

```ruby
class Post < ApplicationRecord
  belongs_to :author, -> { active.order(name: :asc) }
end

class Author < ApplicationRecord
  scope :active, -> { where(active: true) }
end
```

```slim
- semantic_form_for(Post.new) do |f|
  = f.input :author
```

Without this change the autogenerated select input with the list of authors will include ALL authors because it effectivelly loads the list from `Author.all`. After this change, the list will be filled with `Author.active.order(name: :asc)` because the `belongs_to` relationship includes a scope.

This seems to bring parity to the old way of applying conditions to relationships via the `:conditions` option which got deprecated back in Rails 4.
Copy link
Contributor

@mikz mikz left a comment

Choose a reason for hiding this comment

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

I think this is great 👍

@mikz
Copy link
Contributor

mikz commented Aug 31, 2021

I'm happy to merge once I figure out why there is no Github Action build for this PR.

@mikz mikz merged commit ccce342 into formtastic:master Aug 31, 2021
@oboxodo
Copy link
Contributor Author

oboxodo commented Aug 31, 2021

Thanks! 🤘

@mikz
Copy link
Contributor

mikz commented Aug 31, 2021

Thank you! 🥇 👍

@oboxodo oboxodo deleted the support_scopes_in_relations branch August 31, 2021 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants