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

Clean view context when controller changes #799

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/draper/view_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ def self.controller
RequestStore.store[:current_controller]
end

# Sets the current controller.
# Sets the current controller. Clears view context when we are setting
# different controller.
def self.controller=(controller)
clear! if RequestStore.store[:current_controller] != controller
RequestStore.store[:current_controller] = controller
end

Expand Down
28 changes: 28 additions & 0 deletions spec/draper/view_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ module Draper
ViewContext.controller = :stored_controller
expect(store[:current_controller]).to be :stored_controller
end

it "cleans context when controller changes" do
store = {
current_controller: :stored_controller,
current_view_context: :stored_view_context
}

allow(RequestStore).to receive_messages store: store

ViewContext.controller = :other_stored_controller

expect(store).to include(current_controller: :other_stored_controller)
expect(store).not_to include(:current_view_context)
end

it "doesn't clean context when controller is the same" do
store = {
current_controller: :stored_controller,
current_view_context: :stored_view_context
}

allow(RequestStore).to receive_messages store: store

ViewContext.controller = :stored_controller

expect(store).to include(current_controller: :stored_controller)
expect(store).to include(current_view_context: :stored_view_context)
end
end

describe ".current" do
Expand Down