Rails Rail Lock Method

EDIT:

I have many answers with different approaches to solve the problem, thank you very much!

Unfortunately, none of them have worked so far.

To easily understand and reproduce the crash, I created a small Rails repository on GitHub using Rspec Suite.

One of the specifications passes (where the presenter is initialized in the view). One of the specifications does not work (where the initiator is initialized in the controller).

How to make them pass?

ORIGINAL QUESTION BELOW:

This is my host:

class UserPresenter
  def initialize(user, vc)
    @user = user
    @vc   = vc
  end

  def linkify()
    #
    # HERE IS THE PROBLEM
    #
    vc.link_to("foo") do
      yield
    end
  end
end

This is my controller:

I initialize my presenter in the controller, passing the context of the controller view with the presented model.

class UserController
  def show
    @user = User.find(#.....
    @presenter = UserPresenter.new(@user, view_context)
  end
end

In my Slim template, I call my Host to place the content in the link:

=@presenter.linkify do
  p "123"

: linkify.

, , , p 123.

: @presenter = UserPresenter.new(@user, self), .

linkify ?

+4
6

, yield, &, , .

class UserPresenter
  def initialize(user, vc)
    @user = user
    @vc   = vc
  end

  def linkify() # <-- Remove &block
    vc.link_to("foo") do
      yield
    end
  end
end

# ...
# Somewhere else, assuming you have access to @presenter which is an instance of
# UserPresenter
# ...

def show
  @presenter.linkify do
    # ...
    # do my view stuff here
    # ...
  end
end

show()

# Now, if your "View" is nothing but a block that needs to get passed in
# then you'd do this...

def show(&block)
  @presenter.linkify do
    block.call()
  end
end

# This would be used this way:

show(lambda {  
  # ...
  # View stuff here
  # ..
}) 
+2

. . . :

ApplicationHelper:

module ApplicationHelper
  def link(presenter)
    presenter.linkify(self) do
      yield
    end
  end
end

() :

def linkify(vc)
  vc.link_to("foo") do
    yield
  end
end

, vc , vc link, ( ).

:

presenter_from_view.html.slim

-@presenter = UserPresenter.new(@user, self)
=link @presenter do
  p 123

presenter_from_controller.html.slim

=link @presenter do
  p 123

, , , , . . self , link @presenter do.. ( linkify , ).

P.S.: . , , . .

+2

:

module Helpers
  def headline(&block)
    if defined?(::Rails)
      # In Rails we have to use capture!
      "<h1>#{capture(&block)}</h1>"
    else
      # If we are using Slim without a framework (Plain Tilt),
      # this works directly.
      "<h1>#{yield}</h1>"
    end
  end
end

?

def linkify(&block)
  result = capture(&block)
  vc.link_to("foo") do
    result
  end
end
+2

. UserPresenter#initialize, , , :

= @presenter.linkify(self) do
  p "123"
+1

? ...

def linkify()
  #
  # HERE IS THE PROBLEM
  #
  vc.link_to("foo") do
    yield
  end
end

vc ?

, @vc, , .

... empty() linkify() . .

. , IMO - .

+1

, , . view_context , view_context , , , .

def initialize(user, vc)
  @user = user
  @vc = vc
end

# When called from the view:
@presenter = UserPresenter.new(@user, self)
# You're passing the view directly in "as is" and renders as expected.

# However, when you pass it in from the controller:
@presenter = UserPresent.new(@user, view_context)
# you're essentially rendering the view_context in the controller, and then again
# once it renders at the end of your action. That why you're getting this:
#   "<p>123</p><a href="foo"><p>123</p></a>"

linkify, , .

+1

Source: https://habr.com/ru/post/1524032/


All Articles