Max
Max

Reputation: 71

How to properly include ActionView::Helpers in the class outside of ActionController::Base?

I want to create custom class that will generate notifications for users. My question is: how to generate link to resource in notification text via link_to Rails helper?

What I alreay tried:

Followed instructions from this and similar answers like "How to use link_to in Model": included ActionView::Helpers::UrlHelper and Rails.application.routes.url_helpers into Maker class and had follwing error, that appears in the create_text method when I try to use link_to:

EmployeesControllerTest#test_company_vacancy_daily:
NameError: undefined local variable or method `controller' for #<Notifies::Maker:0x000000069ae218>
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionview-5.0.0.1/lib/action_view/routing_url_for.rb:132:in `optimize_routes_generation?'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionpack-5.0.0.1/lib/action_dispatch/routing/route_set.rb:192:in `optimize_routes_generation?'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionpack-5.0.0.1/lib/action_dispatch/routing/route_set.rb:172:in `call'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionpack-5.0.0.1/lib/action_dispatch/routing/route_set.rb:295:in `block (2 levels) in define_url_helper'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionpack-5.0.0.1/lib/action_dispatch/routing/polymorphic_routes.rb:262:in `handle_model_call'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionview-5.0.0.1/lib/action_view/routing_url_for.rb:116:in `url_for'
    /usr/local/rvm/gems/ruby-2.2.5/gems/actionview-5.0.0.1/lib/action_view/helpers/url_helper.rb:196:in `link_to'
    /app/lib/notifies.rb:32:in `block in create_text'
    /app/lib/notifies.rb:32:in `map!'
    /app/lib/notifies.rb:32:in `create_text'
    /app/lib/notifies.rb:38:in `run'
    /app/test/controllers/notifications_test.rb:167:in `block (2 levels) in <class:EmployeesControllerTest>'
    /usr/local/rvm/gems/ruby-2.2.5/gems/activesupport-5.0.0.1/lib/active_support/testing/assertions.rb:71:in `assert_difference'
    /app/test/controllers/notifications_test.rb:166:in `block in <class:EmployeesControllerTest>'

And my code is:

module Notifies
  class Maker

    include ActionView::Helpers::UrlHelper
    include Rails.application.routes.url_helpers

    def initialize(model, kind)
      @model = model
      @kind = kind

      case @model.class.to_s
      when 'Company'
        @to = @model.admin
      when 'Employee'
        @to = @model
      end
    end

    def build_list
      present = Time.now.utc
      past = present - 24.hours

      popularities = Popularity.where(to: @model.entity.id).select do |e|
        e.updated_at.utc.between?(past, present)
      end

      popularities.map! { |p| p.from_entity.turn }
    end

    def create_text(popularities = build_list)
      text = 'Those users interested in your contacts: '
      popularities.map! { |p| link_to p.full_name, p }
      text + popularities.join(', ')
    end

    def run
      popularities = build_list
      text = create_text(popularities)
      @to.notify(kind: @kind, text: text) if popularities.any?
    end
  end
end

Upvotes: 1

Views: 1603

Answers (2)

Max
Max

Reputation: 71

I figured out how to do this. The error was caused by incorrect url_for invocation, and it should be called with parameter only_path, like so:

def create_text(popularities = build_list)
  text = 'Those users interested in your contacts: '
  popularities.map! do |reciever|
    link_to reciever.full_name, url_for([reciever, only_path: true])
  end
  text + popularities.join(', ')
end

Or as alternative a default_host parameter should be set in the enviropment configuration file.

Upvotes: 2

jvillian
jvillian

Reputation: 20253

I'd recommend using a variation of the Presenter Pattern (which lets you call methods like link_to in a Plain Old Ruby Object). Ryan Bates has a good RailsCast on the topic. Here's roughly how I'd do it...

First, I'd create a PresenterBase class (I keep a presenters folder in my app directory):

  app/presenters/presenter_base.rb

  class PresenterBase

    class << self
      def present(controller)     new(controller).present                 end
    end

      def initialize(controller)  @controller = controller                end

    private

      def controller()            @controller                             end
      def view_context()          controller.view_context                 end

      def get_controller_variable(sym)
        controller.instance_variable_get("@#{sym}")
      end

      def method_missing(*args, &block)
        view_context.send(*args, &block)
      end

  end

Notice that I pass in the controller, and then access the view_context via controller.view_context. Then, I override method_missing to send unknown methods (like link_to) to the view_context (which has the link_to method).

Then your Notifies::Maker might look something like this:

  class Notifies::Maker < PresenterBase

    class << self
      def run(controller) new(controller).run end
    end

      def run
        to_notify.notify(kind: kind, text: text) if popularities.any?
      end

    private

      def kind()            @kind ||= get_controller_variable(:kind)      end
      def model()           @model ||= get_controller_variable(:model)    end
      def past_time()       present_time - 24.hours                       end
      def popularities()    @popularities ||= get_popularities            end
      def present_time()    @present_time ||= Time.now.utc                end

      def to_notify() 
        case model.class.to_s
        when 'Company'
          model.admin
        when 'Employee'
          model
        end
      end

      def get_popularities
        Popularity.where(to: model.entity.id).select do |e|
          e.updated_at.utc.between?(past_time, present_time)
        end
      end

      def text
        'Those users interested in your contacts: ' << links
      end

      def links
        popularities.
          map!{ |p| p.from_entity.turn }.
          map!{ |p| link_to p.full_name, p }.
          join(', ')
      end

  end

Finally, your controller might look something like this:

  class FooController < ApplicationController

    def foo_method
      @model = some_method_to_set_model
      @kind = some_method_to_set_kind
      Notifies::Maker.run(self)
    end

  end

I didn't test, so it could be buggy. But, perhaps it will help.

Upvotes: 1

Related Questions