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

Add ActiveStorage getter and setter generator for has_one_attached and has_many_attached #416

Merged
merged 10 commits into from
Aug 12, 2021

Conversation

mojanjz
Copy link
Contributor

@mojanjz mojanjz commented Aug 5, 2021

Motivation

closes #376

Based on ActiveStorage documentation on has_one_attached and has_many_attached, we are missing getter/setter methods for the attachments.

Here is the source code for these getters and setters: has_one_attached and has_many_attached. The other methods are picked up by ActiveRecord scope and association generators.

Implementation

Added an ActiveStorage generator that adds the getter and setter methods for the attachments to the ActiveRecord model.

Added tests for ActiveStorage associations and scopes to active_record_associations_spec and active_record_scope_spec. I put the tests in a new describe block, but if there is a more acceptable way to do this let me know.

Tests

Added tests for the generator

@Morriar
Copy link
Collaborator

Morriar commented Aug 5, 2021

You'll have to run bin/docs and commit the results to update the doc pages 📝

Copy link
Collaborator

@Morriar Morriar left a comment

Choose a reason for hiding this comment

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

If we don't have them already, can you add tests to the related generators so we can ensure that the has_one, has_many and scope are generated properly please? 🙏

sorbet/rbi/shims/activestorage.rbi Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
sorbet/rbi/shims/activestorage.rbi Outdated Show resolved Hide resolved
@mojanjz mojanjz force-pushed the mojan-activestorage-generator branch from 24f71ee to 50941eb Compare August 9, 2021 21:02
@mojanjz mojanjz force-pushed the mojan-activestorage-generator branch 4 times, most recently from 25a9c65 to 4eeea3d Compare August 10, 2021 18:36

require "active_storage/engine"

class Dummy < Rails::Application
Copy link
Contributor Author

Choose a reason for hiding this comment

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

creating a dummy app to run all Rails initializer to load all ActiveStorage classes.

lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
@mojanjz mojanjz force-pushed the mojan-activestorage-generator branch from 203cdca to 944997e Compare August 11, 2021 16:16
@mojanjz mojanjz force-pushed the mojan-activestorage-generator branch from 944997e to 001a27c Compare August 11, 2021 16:40
)
scope.create_method(
"#{name}=",
parameters: [create_param("attachable", type: "T.untyped")],
Copy link
Contributor Author

@mojanjz mojanjz Aug 11, 2021

Choose a reason for hiding this comment

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

it seems like attachable can be any of the types mentioned here

@mojanjz mojanjz requested review from paracycle and a team August 11, 2021 16:42
@mojanjz mojanjz requested a review from Morriar August 12, 2021 15:03
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/active_storage.rb Outdated Show resolved Hide resolved
manual/generator_activestorage.md Outdated Show resolved Hide resolved
manual/generator_activestorage.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@Morriar Morriar left a comment

Choose a reason for hiding this comment

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

Amazing, thanks!

@mojanjz mojanjz merged commit 3af3da6 into main Aug 12, 2021
@shopify-shipit shopify-shipit bot temporarily deployed to production September 7, 2021 16:35 Inactive
@rafaelfranca rafaelfranca deleted the mojan-activestorage-generator branch March 29, 2023 19:16
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.

ActiveStorage dynamic proxy methods not surfaced
3 participants