Skip to content

Fix rake task helpers polluting global namespace - #91

Closed
hibachrach wants to merge 2 commits into
graphiti-api:mainfrom
hibachrach:fix-rake-global-namespace-pollution
Closed

Fix rake task helpers polluting global namespace#91
hibachrach wants to merge 2 commits into
graphiti-api:mainfrom
hibachrach:fix-rake-global-namespace-pollution

Conversation

@hibachrach

Copy link
Copy Markdown

Before, the session method (for example) would be redefined in the
context of an RSpec request spec because the default definee in the
block within the namespace call would be main, the global object.

Additional context about default definees:
https://blog.yugui.jp/entry/846

Before, the `session` method (for example) would be redefined in the
context of an RSpec request spec because the default definee in the
block within the `namespace` call would be `main`, the global object.

Additional context about default definees:
https://blog.yugui.jp/entry/846
@jasonkarns

jasonkarns commented Jul 27, 2022

Copy link
Copy Markdown
Contributor

Another option is to define the methods in an external file/module, and only require them from within the task defs. That way they're only included when those tasks are actually run.

@hibachrach

Copy link
Copy Markdown
Author

Would you prefer that option? Willing to edit the PR if that's the case.

@jasonkarns

Copy link
Copy Markdown
Contributor

@hibachrach I'm not one of the maintainers, so I can't speak for them or the project. However, moving the module(s) into an external file that is explicitly (and only) required when needed is the approach I use and recommend on my teams. As it stands in this PR, the Graphiti::Rails::RakeHelpers modules are all being defined whether those tasks are invoked or not. (The namespace body is evaluated when rake loads the file, as that is how rake knows what tasks exist.)

What's more, then you can include Graphiti::Rails::RakeHelpers in the task body and use the methods inline (unqualified) as they were before.

@jkeen

jkeen commented Mar 18, 2025

Copy link
Copy Markdown
Collaborator

^ I agree with @jasonkarns

jkeen pushed a commit to graphiti-api/graphiti that referenced this pull request Aug 7, 2026
Ported from graphiti-api/graphiti-rails#91, following the review there: the helpers move to their own file rather than being defined inside the rake namespace.
@jkeen

jkeen commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

This is fixed in graphiti 2.0. graphiti-rails is merged into the 2.0 gem, so the rake tasks live at https://github.com/graphiti-api/graphiti/blob/beta/lib/tasks/graphiti.rake.

Your commit is now in a different repo: graphiti-api/graphiti@0a41a60. I took @jasonkarns' suggestion from this thread and moved the helpers into their own file rather than defining them inside the namespace block.

@jkeen jkeen closed this Aug 7, 2026
github-actions Bot pushed a commit to graphiti-api/graphiti that referenced this pull request Aug 7, 2026
# [2.0.0-beta.5](v2.0.0-beta.4...v2.0.0-beta.5) (2026-08-07)

### Bug Fixes

* drop the relationship guard install notice ([c204312](c204312))
* keep rake task helpers out of the global namespace ([0a41a60](0a41a60)), closes [graphiti-api/graphiti-rails#91](graphiti-api/graphiti-rails#91)

### Features

* add rspec matchers for resource relationships and attributes ([70525f7](70525f7)), closes [graphiti-api/graphiti_spec_helpers#14](graphiti-api/graphiti_spec_helpers#14)
* let the resource generator name the controller ([6ea7141](6ea7141)), closes [graphiti-api/graphiti-rails#53](graphiti-api/graphiti-rails#53)
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.

3 participants