RSpec.describe 'exposures' do let(:context) { Struct.new(:title, :assets).new('dry-view rocks!', -> input { "#{input}.jpg" }) } it 'uses exposures to build view locals' do vc = Class.new(Dry::View::Controller) do configure do |config| config.paths = SPEC_ROOT.join('fixtures/templates') config.layout = 'app' config.template = 'users' config.default_format = :html end expose :users do |input| input.fetch(:users).map { |user| user.merge(name: user[:name].upcase) } end end.new users = [ { name: 'Jane', email: 'jane@doe.org' }, { name: 'Joe', email: 'joe@doe.org' } ] expect(vc.(users: users, context: context)).to eql( 'dry-view rocks!
JANEjane@doe.org
JOEjoe@doe.org
' ) end it 'supports both blocks and instance methods as exposures' do vc = Class.new(Dry::View::Controller) do configure do |config| config.paths = SPEC_ROOT.join('fixtures/templates') config.layout = 'app' config.template = 'users' config.default_format = :html end expose :users private def users(input) input.fetch(:users).map { |user| user.merge(name: user[:name].upcase) } end end.new users = [ { name: 'Jane', email: 'jane@doe.org' }, { name: 'Joe', email: 'joe@doe.org' } ] expect(vc.(users: users, context: context)).to eql( 'dry-view rocks!
JANEjane@doe.org
JOEjoe@doe.org
' ) end it 'passes matching input data if no proc or instance method is available' do vc = Class.new(Dry::View::Controller) do configure do |config| config.paths = SPEC_ROOT.join('fixtures/templates') config.layout = 'app' config.template = 'users' config.default_format = :html end expose :users end.new users = [ { name: 'Jane', email: 'jane@doe.org' }, { name: 'Joe', email: 'joe@doe.org' } ] expect(vc.(users: users, context: context)).to eql( 'dry-view rocks!
Janejane@doe.org
Joejoe@doe.org
' ) end it 'allows exposures to depend on each other' do vc = Class.new(Dry::View::Controller) do configure do |config| config.paths = SPEC_ROOT.join('fixtures/templates') config.layout = 'app' config.template = 'users_with_count' config.default_format = :html end expose :users do |input| input.fetch(:users) end expose :users_count do |users| "#{users.length} users" end end.new users = [ {name: 'Jane', email: 'jane@doe.org'}, {name: 'Joe', email: 'joe@doe.org'} ] expect(vc.(users: users, context: context)).to eql( 'dry-view rocks!
2 users
' ) end it 'supports defining multiple exposures at once' do vc = Class.new(Dry::View::Controller) do configure do |config| config.paths = SPEC_ROOT.join('fixtures/templates') config.layout = 'app' config.template = 'users_with_count' config.default_format = :html end expose :users, :users_count private def users(input) input.fetch(:users) end def users_count(users) "#{users.length} users" end end.new users = [ {name: 'Jane', email: 'jane@doe.org'}, {name: 'Joe', email: 'joe@doe.org'} ] expect(vc.(users: users, context: context)).to eql( 'dry-view rocks!
2 users
' ) end it 'allows exposures to be hidden from the view' do vc = Class.new(Dry::View::Controller) do configure do |config| config.paths = SPEC_ROOT.join('fixtures/templates') config.layout = 'app' config.template = 'users_with_count' config.default_format = :html end private_expose :prefix do "COUNT: " end expose :users do |input| input.fetch(:users) end expose :users_count do |prefix, users| "#{prefix}#{users.length} users" end end.new users = [ {name: 'Jane', email: 'jane@doe.org'}, {name: 'Joe', email: 'joe@doe.org'} ] input = {users: users, context: context} expect(vc.(input)).to eql( 'dry-view rocks!
COUNT: 2 users
' ) expect(vc.locals(input)).to include(:users, :users_count) expect(vc.locals(input)).not_to include(:prefix) end end