Created
August 18, 2015 06:15
-
-
Save dnagir/20c97690e7dfc2825c93 to your computer and use it in GitHub Desktop.
Render a different variant in rails
This file contains 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
module ApplicationHelper | |
def with_variant(new_variant, &block) | |
old_variants = lookup_context.variants | |
begin | |
lookup_context.variants = [new_variant] | |
return block.call | |
ensure | |
lookup_context.variants = old_variants | |
end | |
end | |
end | |
# usage: with_variant(:another) { render partial: 'another/partial' } |
This file contains 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
# spec/support/variants.rb | |
# `use_variant :another` on the exapmle or within a test | |
module RailsVariantsMacros | |
extend ActiveSupport::Concern | |
def use_variant(new_variant) | |
view.lookup_context.variants = [new_variant] | |
end | |
module ClassMethods | |
def use_variant(new_variant) | |
before { use_variant(new_variant) } | |
end | |
end | |
end | |
RSpec.configure do |config| | |
config.include(RailsVariantsMacros, type: :view) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment