-
Notifications
You must be signed in to change notification settings - Fork 0
Rolemodel::ResourceFor::ControllerExtension #205
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Rolemodel | ||
| module ResourceFor | ||
| module ControllerExtension | ||
| extend ActiveSupport::Concern | ||
|
|
||
| private | ||
|
|
||
| def resource_for(type_symbol) | ||
| params[type_symbol].safe_constantize.find(params[params[type_symbol].foreign_key]) | ||
| end | ||
|
OutlawAndy marked this conversation as resolved.
|
||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'action_controller' | ||
| require 'action_dispatch/testing/integration' | ||
|
|
||
| # The engine hands `resource_for` to controllers through this initializer during app boot. | ||
| # Running it here gives us the same wiring without booting an app inside the generator specs' process. | ||
| Rolemodel::Engine.initializers.detect { it.name == 'rolemodel.action_controller' }.run | ||
|
|
||
| # Stand-ins for ActiveRecord models. `resource_for` only needs a constant that responds to `find`, | ||
| # so these keep the spec database-free while still exercising the real router & params stack. | ||
| class SpecResource | ||
| attr_reader :id | ||
|
|
||
| def initialize(id) | ||
| @id = id | ||
| end | ||
|
|
||
| def self.find(id) = new(id) | ||
| end | ||
|
|
||
| class Estimate < SpecResource; end | ||
| class Widget < SpecResource; end | ||
|
|
||
| module Reporting | ||
| class Widget < SpecResource; end | ||
| end | ||
|
|
||
| # Note the absence of an `include` — the engine adds `resource_for` to every controller. | ||
| class CommentsController < ActionController::Base | ||
| def index | ||
| commentable = resource_for(:commentable_type) | ||
|
|
||
| render plain: "#{commentable.class.name}##{commentable.id}" | ||
| end | ||
| end | ||
|
|
||
| class ReportsController < ActionController::Base | ||
| def index | ||
| reportable = resource_for(:reportable_type) | ||
|
|
||
| render plain: "#{reportable.class.name}##{reportable.id}" | ||
| end | ||
| end | ||
|
|
||
| RSpec.describe Rolemodel::ResourceFor::ControllerExtension, type: :request do | ||
| let(:routes) do | ||
| ActionDispatch::Routing::RouteSet.new.tap do |route_set| | ||
| route_set.draw do | ||
| concern :commentable do | ||
| resources :comments, only: %i[index], commentable_type: parent_resource.name.classify | ||
| end | ||
|
|
||
| shallow do | ||
| resources :accounts do | ||
| resources :estimates, concerns: :commentable do | ||
| resources :widgets, concerns: :commentable do | ||
| # An explicit, namespaced type — its id param comes from the demodulized name | ||
| resources :reports, only: %i[index], reportable_type: 'Reporting::Widget' | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| let(:session) { ActionDispatch::Integration::Session.new(routes) } | ||
|
|
||
| def get_body(path, **params) | ||
| session.get(path, **params) | ||
| session.response.body | ||
| end | ||
|
|
||
| it 'loads the parent named by the route default' do | ||
| expect(get_body('/estimates/1/comments')).to eq('Estimate#1') | ||
| end | ||
|
|
||
| it 'loads a different parent for the same controller' do | ||
| expect(get_body('/widgets/9/comments')).to eq('Widget#9') | ||
| end | ||
|
|
||
| it 'prefers the route default over a request param of the same name' do | ||
| expect(get_body('/estimates/1/comments', params: { commentable_type: 'Widget' })).to eq('Estimate#1') | ||
| end | ||
|
|
||
| it 'derives the id param from the demodulized class name' do | ||
| expect(get_body('/widgets/9/reports')).to eq('Reporting::Widget#9') | ||
| end | ||
|
|
||
| it 'is available to every controller as a private method' do | ||
| expect(ActionController::Base.private_method_defined?(:resource_for)).to be(true) | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.